2016-04-25 224 views
0

我在與MAX31865通信時遇到問題。我寫了一個簡單的linux應用程序,但是當我運行它時,程序永遠不會結束。我沒有任何MAX31865的迴應。MAX31865 SPI通信失敗

這裏是我的代碼(僅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; 
} 

我會任何幫助

+0

爲了便於我們讀取和理解人類:1)一致地縮進。在每個大括號'{'後縮進。在每個大括號'}'之前取消縮進。從不使用製表符來縮進。建議每個縮進級別使用4個空格,因爲即使使用可變寬度的字體,該空間也足夠寬。 – user3629249

+0

您遇到與外部硬件連接的問題。你有所有需要修復它。我們有一些源代碼和'程序永遠不會結束。我沒有任何迴應'。您甚至沒有告訴我們當您使用調試器完成該程序時程序到底有多遠。我們甚至不知道你是否擁有芯片的電源。我們不能幫你,你應該知道,在你發佈之前:( –

+0

插入一些'printf'進行調試,以便你確切地知道哪個通話停止 – 4386427

回答

0

這裏是我完整的代碼表示感謝。我注意到問題出在哪裏

#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); 
} 
+0

爲什麼要讀取速度,位等,如果你不打算在IO請求結構中使用它們? –

+0

這是測試程序。我想測試它是如何工作的。這不是必要的。問題出在transfer()函數中 – kmbm

相關問題