【问题标题】:unable to interface beaglebonegreen with mcp3008 using SPI无法使用 SPI 将 beaglebonegreen 与 mcp3008 连接
【发布时间】:2023-04-06 22:45:01
【问题描述】:

我无法将 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 pin 84 (PIN84) pin 85 (PIN85) pin 86 (PIN86) pin 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 个计数,而对于 0 输入电压,我期望计数为 0,依此类推。 有人可以帮我吗?你能告诉我哪里做错了吗?在 beaglebone 上使用 SPI 时我还需要考虑哪些其他事项?任何形式的帮助都将不胜感激!

【问题讨论】:

    标签: c embedded-linux beagleboard


    【解决方案1】:

    按照其他论坛的建议解决了它是硬件相关的事情。我必须在 vcc 和 gnd 之间放置 0.1uf 电容器,模拟地和数字地是共同的。

    【讨论】:

      猜你喜欢
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多