cancel
Showing results for 
Search instead for 
Did you mean: 

Running SPI receive in the background

Hrishikesh
Senior

Hi, I have a STM32F407 board that is running as a full duplex slave. it receives data from a Raspberry Pi configured as a SPI master.

Is there a way to receive data from the RPi in a non-blocking mode? One way I could think of was to set the CSN line as a falling interrupt and then call the receive function within the callback. Unfortunately, I've run out of interrupts (many many endstops!).

I tried starting the SPI comm using interrupt mode but that does not seem to work. It transferred only the first two bytes (of the 42 bytes I wanted to transfer). I will try the DMA but I believe that too does a one time transfer and then stops(?)

Any other ideas?

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

You can receive with SPI either in interrupt or DMA mode. One particularly efficient implementation is to receive into a circular buffer and periodically monitor how many bytes have been received (by examining the NDTR register) and process the data.

Normal DMA will do one time transfer, circular DMA will receive indefinitely.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

4 REPLIES 4
TDK
Guru

You can receive with SPI either in interrupt or DMA mode. One particularly efficient implementation is to receive into a circular buffer and periodically monitor how many bytes have been received (by examining the NDTR register) and process the data.

Normal DMA will do one time transfer, circular DMA will receive indefinitely.

If you feel a post has answered your question, please click "Accept as Solution".

Thanks. The DMA with circular buffer worked. However, since its a circular buffer, wouldn't the NDTR always show the number of bytes of the array you would be storing the incoming data in? I put the NDTR check in my while 1 loop and it never changed to any value beyond the array size I had defined (42 bytes).

NDTR shows the number of transfers left before it wraps back around. NDTR decrements by one for each transfer completed. So it'll start at 42, then decrement until it reaches 1. In circular mode, instead of decrementing down to 0, it will reset to 42.
If it's stuck at 42, it's not receiving data (or it wrapped back around already).
If you feel a post has answered your question, please click "Accept as Solution".

Wrapped back around seems more likely since data in the RX buffers is updating correctly. Thanks again for the suggestion. This was helpful!