无法使用SPI连接beaglebonegreen与mcp3008

时间:2019-01-19 09:22:27

标签: c embedded-linux beagleboard

我无法将mcp 3008与beaglebone green接口。尝试将通信设置为模式0(CPOL = 0 CPHA = 0),dts的内置频率设置为1Mhz。 我尝试过的事情 1.启用设备树BB-SPI0-MCP3008-00A0.dts
   在执行    ls- al / dev / spidev1。*    我有     crw-rw ---- 1个root spi 153,0 Oct 7 16:40 /dev/spidev1.1

2。执行    cat /sys/kernel/debug/pinctrl/44e10800.pinmux/pingroups

组:pinmux_bb_spi0_pins引脚84(PIN84)引脚85(PIN85)引脚86(PIN86)引脚87(PIN87)

我的参考代码如下

  #include <stdint.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <getopt.h>
    #include <fcntl.h>
    #include <sys/ioctl.h>
    #include <linux/types.h>
    #include <linux/spi/spidev.h>
    #define ARRAY_SIZE(array) sizeof(array) / sizeof(array[0])

    static const char *DEVICE = "/dev/spidev1.1"; 
    static uint8_t MODE = SPI_MODE_0; 
    static uint8_t BITS = 8; 
    static uint32_t CLOCK = 1000000; 
    static uint16_t DELAY = 5;

    /* * Ensure all settings are correct for the ADC */ 
    static int prepare(int fd) 
    {
      if (ioctl(fd, SPI_IOC_WR_MODE, &MODE) == -1) 
     {
       perror("Can't set MODE"); return -1; 
     }

    if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &BITS) == -1) 
   {
     perror("Can't set number of BITS"); 
     return -1;
   }

    if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &CLOCK) == -1)
     { 
        perror("Can't set write CLOCK"); 
        return -1; 
     }

     if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &CLOCK) == -1) 
    { 
      perror("Can't set read CLOCK"); return -1; 
    }

    return 0; 
    }

    /* * (SGL/DIF = 0, D2=D1=D0=0) */ 
    uint8_t control_bits_differential(uint8_t channel)
     { 
        return (channel & 7) << 4;
      }

     /* * (SGL/DIF = 1, D2=D1=D0=0) */ 
     uint8_t control_bits(uint8_t channel)
     { 
       return 0x8 | control_bits_differential(channel);
     }

    /* * Given a prep'd descriptor, and an ADC channel, fetch the * raw ADC 
     value for the given channel. */ 
     int readadc(int fd, uint8_t channel)
    { 
      uint8_t tx[] = {1, control_bits(channel), 0
    }; 
    uint8_t rx[3]={0,0,0};

    struct spi_ioc_transfer tr = { 
    .tx_buf = (unsigned long)tx, 
    .rx_buf = (unsigned long)rx, 
    .len = ARRAY_SIZE(tx),
    .delay_usecs = DELAY,
    .speed_hz = CLOCK, 
    .bits_per_word = BITS, };

     if (ioctl(fd, SPI_IOC_MESSAGE(1), &tr) == 1) 
     { 
        perror("IO Error"); 
        abort(); 
      }

      return ((rx[1] << 8) & 0x300) | (rx[2] & 0xFF);
      }

      int main(int argc, char **argv) 
     {

       int fd = open(DEVICE, O_RDWR);
       if (fd <= 0)
      { 
         printf("Device %s not found\n", DEVICE);
         return -1; 
       }

      if (prepare(fd) == -1)
       { return -1; }

      uint8_t i,radc=0;
      for(i = 0;i < 8;i++) 
      { printf("Channel %d: %d\n", i + 1, readadc(fd, i)); }

      close(fd);

      return 0; 
     }

执行此代码后,对于任何施加的电压,我都会得到1023的计数,对于零输入电压,我希望得到023的计数,依此类推。 有人可以帮我吗?您能告诉我我做错了什么吗?在beaglebone上使用SPI时我还需要考虑什么其他事项?任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

根据其他论坛的建议解决了它,这是与硬件有关的事情。我必须在vcc和gnd之间放置0.1uf电容器,而模拟地和数字地很常见。