2022-01-13 12:56 PM
I am currently using an STM32L452 MCU that operates using a custom bootloader and firmware package for operation. I have leveraged the bootloader on other projects before, so I knew (or at least thought i knew) it was solid.
I'm learning that when i attempt to use the built in HAL flash memory writing functions (such as HAL_FLASH_Program()), after my bootloader has launched and jumped memory locations to the firmware address, any write to flash memory in the firmware operation fails. (error codes detailed at bottom of message).
I know that if i remove the bootloader entirely, I have no issues writing to flash memory using the interface in the firmware. I also know that if I don't do anything in the bootloader (literally launch the startup script and then jump to the firmware image immediately), this issue where the flash write fails still occurs. This tells me the issue isn't with any code in the bootloader or unintentional memory writes or anything, but the project itself and its interaction with the firmware in memory....which seems crazy and weird to me.
If it helps, the three flash memory errors I get from the MCU Flash-SR register after attempting to write are FLASH_FLAG_PROGERR, FLASH_FLAG_PGAERR, and FLASH_FLAG_PGSERR.
Any and all thoughts are appreciated!
2022-01-13 12:58 PM
Ok, edit with some content/context
2022-01-13 02:20 PM
Check you're not trying to overwrite memory that has already been written. You get one shot per word once erased.
Clear any pending/hanging status, from debugger, etc.
Make sure memory writes are aligned, and of at least minimum supported width.
Make sure the memory is unlocked, and you know the page size.
Instrument your code to understand execution and flow. Don't try to understand it single-stepping in the debugger. Print out the FLASH unit registers/status if you want to know content.
Check the memory is reading blank before attempting a write.
Show the code that's not working
2022-01-13 02:41 PM
Boil the problem down to the bare minimum FLASH access calls that cause the error. No doubt you are performing some sort of invalid access. Writing twice to the same memory without erasing in between doesn't work. Writing less than a double word doesn't work. The RM gives exact conditions for these errors.
2022-01-14 08:59 AM
Yeah, so i figured out my issue -- i was inspecting memory/registers as i completed my erase prior to write, and noticed that my memory erased was a few pages off.
Digging into this more, i learned this offset was due to my utilization of the uint32_t GetPage(uint32_t Addr) built in function which determines the page number based on an address location and bank memory setup (Bank 1 or 2). In that function, it depends on the FLASH_BASE define, which is typically 0x08000000. However, in my bootloader enabled firmware packages, I set that to where i launch my firmware from for the firmware image. I did not make the connection (probably because this is the first time im using internal flash memory for anything other than a bootloader project) between the GetPage function and FLASH_BASE. Once i fixed that aspect, I was good to go. Dumb error on my part.
Thanks for the input! Erasing suggestion made me think harder on what I was actually erasing.