Getting incorrect SPI data received

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

I am a bit confused regarding use of the flags when sending SPI data and seem to get back wrong data from my device

I have a SPIM component set up and started and can see activity on CS, Miso, Mosi and Sck but get wrong data back.

Is my code below correct to send a command byte (read command and register ID) and then read back the expected bytes?

Header length is number of command bytes and header buffer is buffer passed to routine containing command bytes.

readlenth is number of bytes to read and they are passed back in readBuffer.

int readfromspi(uint16 headerLength, const uint8 *headerBuffer, uint32 readlength, uint8 *readBuffer)

{

CS_Write(0);

    for(i=0; i<headerLength; i++)

    {

    SPIM_WriteTxData(headerBuffer);

        while(!(SPIM_ReadTxStatus() & SPIM_STS_SPI_DONE)){};

      readBuffer[0] = SPIM_ReadRxData() ; // Dummy read as we write the header

    }

    for(i=0; i<(int)readlength; i++)

    {

        SPIM_WriteTxData(0x00);  //dummy write

        while(!(SPIM_ReadTxStatus() & SPIM_STS_SPI_DONE)){};

      readBuffer = SPIM_ReadRxData() ; // SPI_receive_data

    }

    CS_Write(1);

  

    return 0;

}

0 Likes
1 Solution

Hi,

Can you try the following and let us know if it works?

Use the SPIM_GetRxBufferSize() API to read the Rx buffer size and then, read the data using SPIM_ReadRxData()?

This is to ensure that you are reading the data, only after the Rx FIFO has received the data and not after Tx FIFO is flushed out. There may be a delay between the two, depending on your interface.

Thanks,

Shanmathi

View solution in original post

0 Likes
4 Replies
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

Cant' find how to edit my post so will add further info here.

I looked again at the waveforms and the data being send and received at the hardware level is correct.

The main issue is the data I get in "readBuffer" is incorrect

0 Likes

Hi,

Can you try the following and let us know if it works?

Use the SPIM_GetRxBufferSize() API to read the Rx buffer size and then, read the data using SPIM_ReadRxData()?

This is to ensure that you are reading the data, only after the Rx FIFO has received the data and not after Tx FIFO is flushed out. There may be a delay between the two, depending on your interface.

Thanks,

Shanmathi

0 Likes

Isn't the communication synchronous, RX data is clocked in AS the TX data is going out. So if 1 byte is being sent at a time, the next byte is already received once the tx byte has gone?

0 Likes

Yes, the communication is synchronous. However, when the tx byte from Tx FIFO is sent out, there may be a path delay due to which the Rx FIFO may be loaded with rx data a little later.

As mentioned in the datasheet, it is always recommended to confirm the Rxbuffer size before using ReadRxData API.

Thanks,
Shanmathi

0 Likes