feat(spi):update spi

This commit is contained in:
2026-07-06 20:06:44 +08:00
parent a699f03d72
commit b4131a9a40
2 changed files with 25 additions and 11 deletions

View File

@@ -75,8 +75,10 @@ void my_spi_server_thread_func(void* arg) {
uint8_t buf[FRAME_LEN];
uint8_t last_buf[FRAME_LEN] = {0};
int fd, ret;
unsigned int frame_count = 0;
int data_changed = 0;
/* 注册退出信号 */
signal(SIGINT, sigint_handler);
@@ -137,17 +139,29 @@ void my_spi_server_thread_func(void* arg) {
/* 6. 循环接收数据 */
while (keep_running) {
memset(buf, 0, FRAME_LEN);
ret = read(fd, buf, FRAME_LEN);
if (ret < 0) {
perror("read");
break;
} else if (ret == 0) {
/* 非阻塞模式可能返回0但这里用了阻塞模式一般不会出现 */
//continue;
}
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)NULL,
.rx_buf = (unsigned long)buf,
.len = FRAME_LEN,
.speed_hz = SPI_SPEED,
.bits_per_word = SPI_BITS,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 0) {
perror("spi transfer");
break;
}
ret = FRAME_LEN; /* transfer 成功则返回 FRAME_LEN */
frame_count++;
/* 判断数据是否有变化 */
data_changed = (memcmp(last_buf, buf, FRAME_LEN) != 0);
if (!data_changed)
continue;
memcpy(last_buf, buf, FRAME_LEN);
printf("\n========== Frame #%u (%d bytes) ==========\n", frame_count, ret);
printf("HEX : ");
for (int i = 0; i < ret; i++)