SPI RX trouble on TI Sitara AM3358



  • I've been working on an app in Qt and I've run into some trouble. My program needs to use SPI to communicate with a 22 bit ADC (Microchip MCP3551) attached to a load cell. I'm running the program on a TI Sitara AM3358 processor.

    I know my system is connected to the ADC chip correctly, I can get values back from the chip in linux by using the hexdump command. I've also verified the connections using a logic analyser.

    Where I'm running into trouble is communicating to the system inside of Qt. Earlier I had done some tests using a loopback and the program responds correctly to that. When I stop using the loopback and attach my pins to the ADC chip my logic analyser displays the values that I'm expecting but my Qt program does not give me usable information on my SPI port.

    To be honest something is probably set up incorrectly, but I'm not sure where to begin looking. Relevant code snippets are below.

    Thanks for any assistance.

    @
    #ifdef SPIDEBUG
    //declare device to use
    static const char* device = "/dev/spidev1.0";
    //declare variables
    int fd;
    int ret;
    BYTE mode = 0;
    BYTE bits = 8;
    DWORD speed = 5000000;
    BYTE tx[255] = "639";
    BYTE rx[255];

    //open the SPI device
    fd = open(device, O_RDWR);
    
    //verify we opened the device properly
    if (fd < 0)
        qDebug() << "can't open device";
    
    
     //spi mode
    
    ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
    if (ret == -1)
        qDebug() << "can't set spi mode";
    
    ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
    if (ret == -1)
        qDebug() << "can't get spi mode";
    
    
     //bits per word
    
    ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
    if (ret == -1)
        qDebug() << "can't set bits per word";
    
    ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
    if (ret == -1)
        qDebug() << "can't get bits per word";
    
    
     //max speed hz
    
    ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
    if (ret == -1)
        qDebug() << "can't set max speed hz";
    
    ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
    if (ret == -1)
        qDebug() << "can't get max speed hz";
    
    //declare new instance of SPI Utilities
    SPIUtils *x = new SPIUtils;
    //send values over tx, receive data back in rx
    x->SPItransfer(fd, tx, 3, rx);
    
    qDebug() << rx;
    
    //close file
    ::close(fd);
    

    #endif

    void SPIUtils::SPItransfer(int filedescriptor, BYTE *tx, BYTE Length, BYTE *rx)
    {
    //declare variables
    int ret;
    spi_ioc_transfer tr;

    //set up SPI transfer structure
    //set up tx buffer
    tr.tx_buf = (unsigned long) tx;
    //set up rx buffer
    tr.rx_buf = (unsigned long) rx;
    //set up length of message to send
    tr.len = Length;
    //set inter-character delay
    tr.delay_usecs = 0;
    //set max speed
    tr.speed_hz = 5000000;
    //set number of bits per word
    tr.bits_per_word = 8;
    //set up chip select
    tr.cs_change = 0;
    //not sure what this is
    tr.pad = 0;
    
    //set up SPI to send message
    ret = ioctl(filedescriptor, SPI_IOC_MESSAGE(1), &tr);
    //check result from SPI port setup
    if (ret < 1)
    {
        qDebug() << filedescriptor;
        qDebug() << ret;
        qDebug() << errno;
        qDebug() << "can't send spi message";
    }
    
    return;
    

    }

    @


  • Lifetime Qt Champion

    Hi,

    Where/when are you calling the code in the SPIDEBUG ifdef ?



  • Hi,

    Its declared at the beginning of the file. I think I've figured the issue out. Turns out I'm an idiot. If you're going to use an array, you need to reference the array properly. Once I got my array issue sorted out, the rest fell into place.

    Thanks anyways.


  • Lifetime Qt Champion

    Don't worry, missing an array initialization happened to all of use.

    However, your ifdefed code should rather be integrated into your class. Floating like that in your file it should not even compile.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.