Skip to main content
Hrishikesh
Associate III
December 30, 2020
Solved

Running SPI receive in the background

  • December 30, 2020
  • 4 replies
  • 944 views

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?

This topic has been closed for replies.
Best answer by TDK

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.

4 replies

TDK
TDKBest answer
December 30, 2020

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""."
Hrishikesh
Associate III
December 30, 2020

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).

TDK
December 30, 2020
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""."