SPI + DMA Chip Select Data Synchronization?
Hi everyone,
I am currently working with an STM32G071. It receives data over the SPI in slave mode. The data is packet based, each currently has 4 bytes, but that will differ in the future. A single chip select sequence may contain multiple packets, but a single packet is never split over multiple CS.
The SPI is configured to work with the DMA to transfer the 4 bytes of a packet and I get an interrupt from the DMA once finished. Everything works as expected.
However, I would like to make sure that the receiver is robust, even in case of wrong data on the bus, especially not the correct number of bytes. Currently, the DMA will transfer 4 bytes into the RAM, while ignoring the CS. In case there is one byte missing, the DMA will receive the 3 remaining bytes. Although the CS is then deasserted, it will wait for the next transmission to get the last byte. From this point on the data will be shifted by one byte. That is not acceptable for me. I want the DMA to re-sync at some point.
As far as I can see there is no way to get the information of a CS-event from the SPI. There is no interrupt or status flag for being selected/unselected. Is that correct?
The only solution I can think of is to set up an additional GPIO EXTI interrupt on the CS line and use it to re-initialize the DMA every time the CS is asserted or de-asserted. If I implement it for the assertion case, I will have to make sure that the ISR is fast enough to be ready before data is received over the SPI, which might not be possible. If I implement the interrupt for the de-assertion, I have to make sure that the DMA is finished transferring the data and the interrupt is called, which might be a bit tricky.
Am I missing something obvious? How would you implement this? Should be a "standard" problem I guess ;)
Thanks a lot!
