4
votes

I wonder if it is safe to read from the memory that dma is writing data to?

I have a stm32F1 with an adc setup to continuously perform conversions and transfer the data to a ram buffer using dma. I know I can use an adc interrupt to access the buffer safely, but how about accessing the buffer from a non-interrupt context? Could data be corrupted if I try to read from the same location that dma is writing?

1
While it's safe, if your access pointer or the dma's pass one another, you'll either reread stale data, or have unread data replaced before you see it. Usually what people do is read one half of the buffer while dma is filling the other.Chris Stratton

1 Answers

8
votes

Your data will not be corrupted - these chips have bus arbiter that grants access to bus (so also to RAM memory) to either DMA or the CPU (your code), so each transaction (single access to RAM, not necessarily access to whole variable) is atomic.

See this info in the RM0008 Reference manual:

3.1 System architecture

...

BusMatrix

The BusMatrix manages the access arbitration between the core system bus and the DMA master bus. The arbitration uses a Round Robin algorithm. In connectivity line devices, the BusMatrix is composed of five masters (CPU DCode, System bus, Ethernet DMA, DMA1 and DMA2 bus) and three slaves (FLITF, SRAM and AHB2APB bridges). In other devices, the BusMatrix is composed of four masters (CPU DCode, System bus, DMA1 bus and DMA2 bus) and four slaves (FLITF, SRAM, FSMC and AHB2APB bridges). AHB peripherals are connected on system bus through a BusMatrix to allow DMA access.