HAL_FLASH_Program not writing to flash permanently
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-04-29 12:13 PM
Hi all,
I am using HAL_FLASH_Program() to program an uuid into a specific address. I can verify it write successfully by reading from the address. However, if I power cycle the MCU, the memory at that address returns to the original value. If I write it directly through ST-Link then it stays permanently. Does anyone know why is that? Do I need to erase the memory location before write to it using HAL_FLASH_Program()? I am using STM32F745.
Thank you
Solved! Go to Solution.
- Labels:
-
Flash
-
STM32F7 Series
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-04-30 10:45 AM
How exactly do you write it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-04-30 10:46 AM
I posted the code in the comment below.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-04-30 10:48 AM
The address must be aligned, 0x080BFFFB is not aligned for halfword writes, use an even number (and for full words, one divisible by 4).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-04-30 10:52 AM
I dont think HAL is the problem here.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-04-30 10:59 AM
()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-04-30 11:02 AM
> set PG before STRT.
No need for that on the F7
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-04-30 11:35 AM
Thank you. Changing the address solved the PGPERR problem. However, my original problem still remains. The flash write is not permanent, and when I power cycle the MCU, it goes back to 0xFFFF.
What is interesting is if I run the MCU in debugger mode, put a break point anywhere inside the FA_program_uuid() above, it works. I then can stop the debug session, power reset, and the memory data remains.
However if I run in debugger mode without any break point, the memory data is reverted back to 0xFFFF.
What could be the difference between running in debugger mode, with a break point, vs running in normal (release) mode (and vs running in debugger mode without a break point)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-04-30 11:40 AM
Fixed it:
FLASH->CR |= 0x1; //Set PG bit
__DMB();
*(volatile uint16_t*) 0x080BFFFA = uuid;
__DMB();
while(FLASH->SR & 0x0100)
- Don't limit comparison to >0, when you can do !=0 (the same as leaving it out completely) - it's safer relative to potential code changes in future.
- As berendi said, the address for half-word writes must be half-word aligned.
- Target type of casting must be volatile to prevent compiler from reordering the write operation relative to other volatile accesses - in this case register accesses.
- As flash memory region is not configured as device or strongly ordered memory, the store instruction must be encapsulated with DMB instructions to force CPU to complete previous stores and prevent from reordering instructions and doing speculative reads. This is recommended for all ARM CPU cores, but Cortex-M7 is the first from Cortex-M series, for which it is mandatory!
P.S. And of course the HAL code is broken in this regard...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-04-30 11:55 AM
Thank you! It now works properly.

- « Previous
-
- 1
- 2
- Next »