开启spi
sudo raspi-config
 
选择Interfacing options,选择spi打开
 
lsmod
 
可以看到spi_bcm2835
 
短接MISO和MOSI
编写回环代码spitest.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
/* dac_test /dev/spidevB.D <val> */
int main(int argc, char **argv)
{
        int fd;
        unsigned int val;
        struct spi_ioc_transfer        xfer[1];
        unsigned char tx_buf[2];
        unsigned char rx_buf[2];
        int        status;
        unsigned long speed = 50000; // SPI速度
        unsigned long spimode = SPI_MODE_0;
        if(argc != 4) {
                printf("Usage: %s /dev/spidevB.D <val>\n", argv[0]);
                return 0;
        }
        fd = open(argv[1], O_RDWR);
        if (fd < 0) {
                printf("can not open %s\n", argv[1]);
                return 1;
        }
        // 设置SPI模式
        if (ioctl(fd, SPI_IOC_WR_MODE, &spimode) < 0) {
            perror("设置SPI模式失败");
            return 1;
        }
        // 设置SPI速度
        if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) {
            perror("设置SPI速度失败");
            return 1;
        }
        
        tx_buf[0] = (unsigned long)strtoul(argv[2],NULL,0);
        tx_buf[1] = strtoul(argv[3],NULL,0);
        memset(xfer, 0, sizeof xfer);
        xfer[0].len = 2;
        xfer[0].tx_buf = (unsigned long) tx_buf;
        xfer[0].rx_buf = (unsigned long) rx_buf;
        status = ioctl(fd, SPI_IOC_MESSAGE(1), xfer);//告诉内核我们要发送1个SPI消息。这部分中的1表示我们要发送的消息数量。
        if (status < 0) {
                printf("SPI_IOC_MESSAGE\n");
                return -1;
        }
        /* print result */
        printf("recived0 %X\n",rx_buf[0]);
        printf("recived1 %X\n",rx_buf[1]);
        return 0;
}
 
版本2
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#define SPI_DEVICE "/dev/spidev0.0" // SPI设备路径
int main(int argc, char *argv[]) {
    int spi_fd;
    unsigned char txData[2] = {0x00, 0x00}; // 要发送的两个字节数据
    unsigned char rxData[2] = {0x00, 0x00}; // 接收的两个字节数据
    struct spi_ioc_transfer spi;
    unsigned long speed = 50000; // SPI速度
    unsigned long spimode = SPI_MODE_0;
    
    int opt;
    while ((opt = getopt(argc, argv, "a:b:")) != -1) {
        switch (opt) {
            case 'a':
                txData[0] = (unsigned long)strtoul(optarg, NULL, 0);
                break;
            case 'b':
                txData[1] = (unsigned long)strtoul(optarg, NULL, 0);
                break;
            default:
                fprintf(stderr, "Usage: %s -a <byte1> -b <byte2>\n", argv[0]);
                exit(EXIT_FAILURE);
        }
    }
    // 打开SPI设备
    if ((spi_fd = open(SPI_DEVICE, O_RDWR)) < 0) {
        perror("打开SPI设备失败");
        return 1;
    }
    // 设置SPI模式
    if (ioctl(spi_fd, SPI_IOC_WR_MODE, &spimode) < 0) {
        perror("设置SPI模式失败");
        return 1;
    }
    // 设置SPI速度
    if (ioctl(spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) {
        perror("设置SPI速度失败");
        return 1;
    }
    memset(&spi, 0, sizeof(spi));
    spi.tx_buf = (unsigned long)txData;
    spi.rx_buf = (unsigned long)rxData;
    spi.len = 2; // 设置传输长度为2字节
    spi.speed_hz = speed;
    spi.bits_per_word = 8;
    // 发送并接收数据
    if (ioctl(spi_fd, SPI_IOC_MESSAGE(1), &spi) < 0) {
        perror("SPI发送接收失败");
    } else {
        printf("Received: 0x%X 0x%X\n", rxData[0], rxData[1]);
    }
    close(spi_fd);
    return 0;
}
 






![[PICO VR眼镜]眼动追踪串流Unity开发与使用方法,眼动追踪打包报错问题解决(Eye Tracking/手势跟踪)](https://i-blog.csdnimg.cn/direct/63442aacb4c548288c3041b3de670fe9.png)













