Hi @pengyong_zhang
I tried to run the test from the terminal:
pidev_test -D /dev/spidev0.0 -s 500000 -b 8 -H -p "\xBA\x00" -v
spi mode: 0x1
bits per word: 8
max speed: 500000 Hz (500 kHz)
TX | BA 00 __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ |..|
RX | 00 00 __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ |..|
Also, I tried to run this C code :
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <string.h>
#include <errno.h>
int main() {
int fd = open("/dev/spidev0.0", O_RDWR);
if (fd < 0) {
perror("open");
return 1;
}
uint8_t mode = SPI_MODE_1;
uint8_t bits = 8;
uint32_t speed = 500000;
if (ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0 ||
ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0 ||
ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) {
perror("SPI setup");
close(fd);
return 1;
}
uint8_t tx[2] = { 0xBA, 0x00 }; // Example: read reg 0x1D
uint8_t rx[2] = { 0 };
struct spi_ioc_transfer tr = {
.tx_buf = (__u64)(uintptr_t)tx,
.rx_buf = (__u64)(uintptr_t)rx,
.len = 2,
.speed_hz = speed,
.bits_per_word = bits,
.cs_change = 0,
};
int ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1) {
perror("SPI_IOC_MESSAGE");
close(fd);
return 1;
}
printf("TX: 0x%02X 0x%02X\n", tx[0], tx[1]);
printf("RX: 0x%02X 0x%02X\n", rx[0], rx[1]);
printf("Received Device ID: 0x%02X\n", rx[1]);
close(fd);
return 0;
}
In both, I get the same result. In the following snapshot, you can see the logic analyzer:
