Preventing buffer underrun at SPI slave

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Preventing buffer underrun at SPI slave

1,911 Views
perdrix
Contributor II

Assuming I have the following code at the SPI master:

           if (length && (SSP0SR & TNF)) // Data to send and transmit FIFO isn’t full?
           {
                SSPODR = *sendBuffer++;
                length--;
           }

where length is the number of SPI message to send.   If I understand things right the lower layer SPI code will transmit as fast as it can.

If the slave reads the first message and cannot reply before the next message from the master arrives then buffer underrun can occur.   How to change the above code to avoid this problem please?

0 Kudos
Reply
2 Replies

1,889 Views
perdrix
Contributor II

If there are a number of messages in the Tx fifo, I cannot do that.  Yes I can delay after adding each message to the fifo so that there's only ever a maximum of one message in the fifo, but that seems less than ideal as it rather defeats the purpose of having a fifo in the first place.

0 Kudos
Reply

1,903 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

I have to say that there is NOT hand-shaking signal between SPI master and slave, it is possible that the master transfers data fast enough so that the spi slave has not enough time to read data from slave SPI receiver FIFO to memory, in the case, overflow event will happen, the received data maybe lost.

So you can add a delay with such instruction as on the master side.

__asm("nop");

__asm("nop");

__asm("nop");

to delay the master transfer.

Hope it can help you

BR

XiangJun Rong

 

 

0 Kudos
Reply