43 lines
963 B
C
43 lines
963 B
C
#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;
|
|
} |