2014-03-10 08:17 AM
Hi all,
I have a product where DMA copies 10 bytes from external ADC chip over SPI, and puts it into memory. Sampling speed is 2000 sps.This works well most of the time, but very rarely (several times per minute), DMA skips a byte.For example, bytes 3, 4, 5 are copied, 6 is skipped, and 7,8,9,10 are OK.What is even more strange is that this happens only when I have compiler optimizations turned ON. When the optimizations are turned OFF, everything works as expected.I did spend a considerable amount of time debugging this, but I couldn't find any source of problems in the code. Do you have any ideas or solutions, something that I can try to do? #stm32 #stm32-stm32l151 #spi #dma2014-03-10 09:05 AM
Turning optimization ON just increases the chances to see a bug, because it make bus access, interruption and other event closest from each others.
Assuming the HW is OK, DMA shall report an error when only 9/10 bytes have been transferred. Can you tell us more ? What's your CPU ? What is the speed of the bus ? Give us code snippet and explain your observation.2014-03-10 09:21 AM
Also, if it is an SPI overrun (i.e. DMA does not pick the received byte fast enough from the SPI data register, and another arrives and overwrites the former), the respective SPI status bit would be set.
So, what's the content of SPIx->SR after the problem occurs? JW2016-01-25 06:40 AM
It looks like indeed this is the case. I'm looking at SPI using logic analyzer, and I can see that when I order 512 bytes, indeed 512 bytes are clocked over SPI, but the DMA doesn't copy all bytes to memory. It copies first few bytes and then it skips between 1 and 8 bytes, and then copies the rest properly to a memory buffer. The SR register at the end of transfer has the OVR flag set.
I'm running my SPI at 8Mhz, and my CPU clock is at 16MHz. I'm using 2 DMA channels for transfer, 1 is for reading and 1 for writing. So during 1 transfer operation I'm sending 512 bytes, and I'm reading back 512 bytes. (using this for SDCard communication). Could it be that the TX DMA is working faster than RX DMA? How can I synchronize the two so that no bytes are dropped?