DMA to Flash for STM32
Asked Answered
M

2

5

I am using an stm32f40x microcontroller (UC), and I have to use its internal flash to store some data. The write operation, as you know, is very slow and occupies the UC for very long time. So I thought to DMA and here is the question:

Is it possible to use DMA to transfer data from SRAM to FLASH?

Thank you for your time.

Melton answered 12/10, 2018 at 6:57 Comment(1)
Some specialized microcontrollers have a RAM area which is an image of the flash. Your program writes to the RAM and then the hardware takes care of burning it to flash. Possibly in combination with ECC. For such a part you should be able to DMA the data. I'm not sure if ST has something like that though. You should probably just look for a part with data flash ("emulated eeprom") - such flash has smaller segments that don't take an eternity to erase before programming. I know that several STM32 come with data flash.Latoshalatouche
D
10

In short: No. At least not in a useful way.

Longer: The slow writing to flash is not caused by the CPU being so slow, but rather caused by time-consuming page write cycles to flash memory. (So, DMA isn't any faster). You can theoretically use DMA to write to flash (even if that is a bit tricky and has quite some pitfalls), but you won't gain any speed. Reason is, the CPU will be halted when it tries to access flash memory while it is written. Thus, the CPU will not be able to execute code from there while DMA writes it - No time gained.

The manual says:

Any attempt to read the Flash memory on STM32F4xx while it is being written or erased, causes the bus to stall. Read operations are processed correctly once the program operation has completed. This means that code or data fetches cannot be performed while a write/erase operation is ongoing.

That means executing code from any flash area while DMA runs to it is not possible - The CPU will be halted for that time period. You could, however, complicate things even more and copy parts of your code to RAM to execute it there to work around this.

Some of the F4 series have 4k of battery-backable RAM. Use that, it's much faster and much simpler to handle.

Diapause answered 12/10, 2018 at 7:33 Comment(3)
Thank you! I was not thinking about this aspect that you underlined! But I have another question for you: this applies also if the code is in a different flash sector from the one used for data storage? Thank you.Melton
The manual I have says Any attempt to read the Flash memory on STM32F4xx while it is being written or erased, causes the bus to stall. Read operations are processed correctly once the program operation has completed. This means that code or data fetches cannot be performed while a write/erase operation is ongoing. I think that answers the question, writing to any part of flash stops the CPU. You might be able to work around this by copying part of the code you want to execute during the DMA transfer to RAM, however - But that opens up a can of worms better left closed.Diapause
While it is true that DMA will not help in this case, it is not quite true that the CPU cannot execute code (as mentioned in the subsequent comment); rather that it cannot execute code resident in the flash memory being written to - which for STM32 is generally the case. For example if interrupt servicing is critical during Flash writes, the ISR can be placed in RAM (CCM is a good choice for this where available).Baptistery
S
7

Another possibility would be to use a dual-bank device with read-while-write capability. In these devices there are two banks of flash. It is possible to execute code from one bank while writing data to the second bank.

School answered 19/12, 2018 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.