This commit is contained in:
Passthem
2025-04-11 15:40:18 +08:00
commit 7e455c591a
10 changed files with 296 additions and 0 deletions

43
spi_utils.c Normal file
View File

@ -0,0 +1,43 @@
#include "spi_utils.h"
int init_device(const char* device,
uint8_t spi_mode,
uint8_t spi_bits,
uint32_t spi_speed) {
int fd;
if ((fd = open(device, O_RDWR)) < 0) {
perror("SPI 设备打开失败");
close(fd);
return -1;
}
ioctl(fd, SPI_IOC_WR_MODE, &spi_mode);
ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &spi_bits);
ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi_speed);
return fd;
}
int send_buffer(int fd,
uint8_t* buffer,
const int data_size,
uint32_t spi_speed,
uint8_t spi_bits) {
int ret;
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)buffer,
.len = data_size,
.speed_hz = spi_speed,
.bits_per_word = spi_bits,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 0) {
perror("SPI 信息发送失败");
}
return ret;
}