2024-06-20 03:28 AM
I'm working with an STM32G474 microcontroller and need guidance on how to write 32-bit data to flash memory. Could someone provide me with a proper procedure or example code that demonstrates how to accomplish this task efficiently and correctly? Any insights or best practices would be greatly appreciated.
2024-07-01 01:12 PM
Hello Savan,
I suggest beginning with the FLASH_EraseProgram example found in the STM32 Cube Firmware package at "STM32Cube_FW_G4_Vxxx\Projects\NUCLEO-G474RE\Examples\FLASH". This example outlines the correct procedures for erasing and writing to the Flash memory.
B.R.
Mohamed
2024-07-01 04:43 PM
>>Any insights or best practices would be greatly appreciated.
Don't use memory that your application is physically residing in. Perhaps shrink the memory seen by the Linker via the Linker Script, so you can have a unused portion at the top-of-memory to use.
Erase prior to usage.
You can only write to an erased location once, but can write across the whole sector before needing to erase. You can journal data/configuration items byte writing new content to the next available slot, and then finding the latest when you want to recover it.
Write on 4-byte aligned addresses, perhaps look if the Flash write-line is wider, say 8 or 16-bytes wide. I don't recall on the G4, but it will be discussed in the Reference Manual, and the example appears to directly support 4-byte / 32-bit word writes.
STM32Cube_FW_G4_V1.5.1\Projects\NUCLEO-G474RE\Examples\FLASH\FLASH_EraseProgram\Src\main.c