2016-04-25 21 views
0

Ich habe ein Problem mit der Kommunikation mit MAX31865. Ich habe eine einfache Linux-Anwendung geschrieben, aber wenn ich es ausführe, endet das Programm nie. Ich habe keine Antwort von MAX31865.MAX31865 SPI Kommunikation fehlgeschlagen

hier ist mein Code (nur 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; 
} 

Ich werde für jede Hilfe

+0

für die einfache Lesbarkeit und Verständnis von uns Menschen: 1) Einzug konsequent. Nach jeder öffnenden Klammer '{' einrücken. Un-Einzug vor jeder schließenden Klammer '}'. Verwenden Sie niemals Tabs zum Einrücken. Schlagen Sie vor, für jede Einzugsebene 4 Leerzeichen zu verwenden, da diese so breit sind, dass sie auch mit Schriftarten mit variabler Breite angezeigt werden können. – user3629249

+0

Sie haben Probleme mit der externen Hardware. DU hast alles, was du brauchst, um es zu reparieren. Wir haben ein bisschen Quellcode und "Programm endet nie". Ich habe keine Antwort. Sie haben uns nicht einmal gesagt, wie weit das Programm gekommen ist, als Sie es mit Ihrem Debugger durchgegangen sind. Wir wissen nicht einmal, ob Sie Strom auf dem Chip haben. Wir können Ihnen nicht helfen, und Sie sollten gewusst haben, bevor Sie gepostet :( –

+0

Fügen Sie einige 'printf' für debug, so dass Sie genau wissen, welcher Anruf, der – 4386427

Antwort

0

Hier ist mein vollständiger Code dankbar. Ich habe bemerkt, wo das Problem ist

#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

Warum lesen Sie die Geschwindigkeit, Bits usw., wenn Sie nicht in der IO-Anfrage-Struktur verwenden werden? –

+0

Es ist Testprogramm. Ich möchte testen, wie es funktioniert. Es ist nicht notwendig. Das Problem liegt in der Funktion transfer() – kmbm