【问题标题】:MAX31865 SPI communication failedMAX31865 SPI通讯失败
【发布时间】:2016-04-25 13:27:32
【问题描述】:

我与 MAX31865 的通信出现问题。我写了一个简单的 linux 应用程序,但是当我运行它时,程序永远不会结束。 MAX31865没有任何回应。

这是我的代码(仅用于 conf):

static void transfer(int fd)
{
    int ret;
    uint8_t tx[] ={0x80};

    uint8_t rx[ARRAY_SIZE(tx)];
    struct spi_ioc_transfer tr = {
        .tx_buf = (unsigned long)tx,
        .rx_buf = (unsigned long)rx,
        .len = ARRAY_SIZE(tx),
        .delay_usecs = delay,
        .speed_hz = 0,
        .bits_per_word = 0,
    };

    ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
    if (ret == 1)
        pabort("can't send spi message");

}

int main(int argc, char *argv[])
{
    int ret = 0;
    int fd;

    parse_opts(argc, argv);

    fd = open(device, O_RDWR);
    if (fd < 0)
        pabort("can't open device");

    ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
    if (ret == -1)
        pabort("can't set spi mode");

    ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
    if (ret == -1)
        pabort("can't get spi mode");

    ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
    if (ret == -1)
        pabort("can't set bits per word");

    ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
    if (ret == -1)
        pabort("can't get bits per word");
    printf("%d",bits);

    ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
    if (ret == -1)
        pabort("can't set max speed hz");

    ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
    if (ret == -1)
        pabort("can't get max speed hz");

    printf("spi mode: %d\n", mode);
    printf("bits per word: %d\n", bits);

    transfer(fd);
    printf("max speed: %d Hz\n", speed);

    transfer(fd);

    close(fd);

    return ret;
}

如果有任何帮助,我将不胜感激

【问题讨论】:

  • 为了便于我们人类阅读和理解:1) 一致地缩进。在每个左大括号“{”后缩进。在每个右大括号 '}' 之前取消缩进。永远不要使用制表符进行缩进。建议为每个缩进级别使用 4 个空格,因为它足够宽,即使使用可变宽度字体也可以看到。
  • 您在连接外部硬件时遇到问题。您拥有修复它所需的一切。我们有一些源代码和“程序永无止境”。我没有任何回应”。当您使用调试器单步执行程序时,您甚至没有告诉我们该程序走了多远。我们甚至不知道你的芯片是否有电。我们无法为您提供帮助,您应该在发布之前就知道这一点:(
  • 插入一些printf进行调试,以便您确切知道哪个调用停止了
  • 这是ioctl() 手册页的摘录通常,成功时返回零。一些 ioctl() 请求使用返回值作为输出参数,并在成功时返回一个非负值。出错时返回 -1,并适当设置 errno。 除其他外,它表示成功时不一定返回零。当从main() 返回并希望指示成功时,必须返回 0。因此,ret 可能不包含 0,因此不应在 return 运算符中使用。
  • 在 ioctl() 函数之前一切正常。我检查了示波器上的mosi,一切正常,但程序无法继续

标签: c linux spi


【解决方案1】:

这是我的完整代码。我已经注意到问题出在哪里了

#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>


static void pabort(const char *s)
{
    perror(s);
    abort();
}

static const char *device = "/dev/spidev0.0";
static uint8_t mode = 3;
static uint8_t bits = 8;
static uint32_t speed = 1000000;
static uint16_t delay;

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))


static void transfer(int fd)
{
    int ret;
    uint8_t tx[] ={0x80};

    uint8_t rx[1];
    struct spi_ioc_transfer tr = {
        .tx_buf = (unsigned long)tx,
        .rx_buf = (unsigned long)rx,
        .len = ARRAY_SIZE(tx),
        .delay_usecs = delay,
        .speed_hz = 0,
        .bits_per_word = 0,
    };

    ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); //->> this ioctl function makes problems
    if (ret == 1)
         pabort("can't send spi message");

 }

int main(int argc, char *argv[])
{
    int ret = 0;
    int fd;

    fd = open(device, O_RDWR);
    if (fd < 0)
        pabort("can't open device");

    ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
    if (ret == -1)
        pabort("can't set spi mode");

    ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
    if (ret == -1)
        pabort("can't get spi mode");

    ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
    if (ret == -1)
    pabort("can't set bits per word");

    ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
    if (ret == -1)
        pabort("can't get bits per word");
    printf("%d",bits);

    ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
    if (ret == -1)
        pabort("can't set max speed hz");

    ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
    if (ret == -1)
        pabort("can't get max speed hz");

    printf("spi mode: %d\n", mode);
    printf("bits per word: %d\n", bits);  //---> This printf works fine

    transfer(fd);   
    printf("max speed: %d Hz\n", speed);  //--> This printf doesn't work

    close(fd);
}

【讨论】:

  • 如果您不打算在 IO 请求结构中使用它们,为什么还要读取速度、位数等?
  • 这是测试程序。我想测试它是如何工作的。这是没有必要的。问题出在 transfer() 函数中
猜你喜欢
  • 1970-01-01
  • 2019-04-06
  • 2022-01-20
  • 2021-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 1970-01-01
相关资源
最近更新 更多