SPI通信与覆盆子pi

时间:2017-10-30 10:52:58

标签: c raspberry-pi

我正在尝试使用树莓派1 B来控制一些数据采集。通信由SPI执行,我正在使用此代码。这是由githuber提供的,我试图解决这个问题,因为2天但我找不到解决方案......

我将MISO和MOSI连接在一起以测试RX和TX功能但我收到任何东西......有没有人已经有同样的问题?

非常感谢!

我在报价中收到的内容......

  

root @ raspberrypi:/ usr / local / src / metrologyboard#。/ spi send hello   世界,spidev0.0 rx_buffer =

 #include <stdio.h>
    #include <stdint.h>
    #include <fcntl.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <sys/ioctl.h>
    #include <string.h>

    #include <linux/spi/spidev.h>

    typedef struct {
        uint8_t mode;
        uint8_t bits_per_word;
        uint32_t speed;
        uint16_t delay;
    } spi_config_t;

    int spi_open(char *device, spi_config_t config);
    int spi_close(int fd);
    int spi_xfer(int fd, uint8_t *tx_buffer, uint8_t tx_len, uint8_t *rx_buffer, uint8_t rx_len);
    int spi_read(int fd, uint8_t *rx_buffer, uint8_t rx_len);
    int spi_write(int fd, uint8_t *tx_buffer, uint8_t tx_len);

    spi_config_t spi_config;
    uint8_t tx_buffer[32];
    uint8_t rx_buffer[32];

    void main( void)
    {
      int spifd;
      spi_config.mode=3;
      spi_config.speed=1000000;
      spi_config.delay=0;
      spi_config.bits_per_word=8;

      spifd=spi_open("/dev/spidev0.0",spi_config);

      memset(tx_buffer,0,32);
      memset(rx_buffer,0,32);
      sprintf(tx_buffer,"hello world");
      printf("sending %s, to spidev0.0 in full duplex \n ",tx_buffer); 
      //spi_xfer(spifd,tx_buffer,strlen(tx_buffer),rx_buffer,strlen(tx_buffer));
    spi_write(spifd,tx_buffer,strlen(tx_buffer));
    spi_read(spifd,rx_buffer,strlen(tx_buffer));
      printf("rx_buffer=%s\n",rx_buffer);
      spi_close(spifd); 
    }

    int spi_open(char *device, spi_config_t config) {
        int fd;

        /* Open block device */
        fd = open(device, O_RDWR);

        if (fd < 0) {
            return fd;
        }

        /* Set SPI_POL and SPI_PHA */
        if (ioctl(fd, SPI_IOC_WR_MODE, &config.mode) < 0) {
            return -1;
        }

        if (ioctl(fd, SPI_IOC_RD_MODE, &config.mode) < 0) {
            return -1;
        }

        /* Set bits per word*/
        if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &config.bits_per_word) < 0) {
            return -1;
        }
        if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &config.bits_per_word) < 0) {
            return -1;
        }

        /* Set SPI speed*/
        if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &config.speed) < 0) {
            return -1;
        }
        if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &config.speed) < 0) {
            return -1;
        }

        /* Return file descriptor */
        return fd;
    }

    int spi_close(int fd) {
        return close(fd);
    }

    int spi_xfer(int fd, uint8_t *tx_buffer, uint8_t tx_len, uint8_t *rx_buffer, uint8_t rx_len){
        struct spi_ioc_transfer spi_message[2];
        memset(spi_message, 0, sizeof(spi_message));

        spi_message[0].tx_buf = (unsigned long)tx_buffer;
        spi_message[0].len = tx_len;
        spi_message[1].rx_buf = (unsigned long)rx_buffer;
        spi_message[1].len = rx_len;

        return ioctl(fd, SPI_IOC_MESSAGE(2), spi_message);
    }

    int spi_read(int fd, uint8_t *rx_buffer, uint8_t rx_len){
        struct spi_ioc_transfer spi_message[1];
        memset(spi_message, 0, sizeof(spi_message));

        spi_message[0].rx_buf = (unsigned long)rx_buffer;
        spi_message[0].len = rx_len;


        return ioctl(fd, SPI_IOC_MESSAGE(1), spi_message);
    }

    int spi_write(int fd, uint8_t *tx_buffer, uint8_t tx_len){
        struct spi_ioc_transfer spi_message[1];
        memset(spi_message, 0, sizeof(spi_message));

        spi_message[0].tx_buf = (unsigned long)tx_buffer;
        spi_message[0].len = tx_len;

        return ioctl(fd, SPI_IOC_MESSAGE(1), spi_message);
    }

0 个答案:

没有答案