cancel
Showing results for 
Search instead for 
Did you mean: 

DMA on ST32l151 skips bytes when copying from SPI

srdjan
Associate II
Posted on March 10, 2014 at 16:17

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 #dma
3 REPLIES 3
stm322399
Senior
Posted on March 10, 2014 at 17:05

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.

Posted on March 10, 2014 at 17:21

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?

JW

srdjan
Associate II
Posted on January 25, 2016 at 15:40

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?