STM32G0: Flash SR register have "sticky" error bits PGAERR (5) and PGSERR (7)
- February 22, 2021
- 2 replies
- 2604 views
I always get errors when trying to erase a flash page on STM32G0. I traced through the HAL code, and the error is caused by the fact that the FLASH->SR register almost always has two error bits set, PGAERR and PGSERR.
So before spending more time on erasing the flash page, I first tried to simply clear the error bits from the FLASH->SR register.
In the attached file, you can see the execution log at the end.
When the function "erasePageAt" is called, we can clearly see that:
- At the beginning of the execution, the FLASH->SR errors were 000000A0 (bits 5 and 7 set).
- After clearing the FLASH->SR errors, they became 00000000 as expected.
- BUT just few cycles after, at the very start of the "lock" function which is called next, we can see that the FLASH->SR errors are back to 000000A0...!
I built my test code to do retries when unlocking and locking the flash fail, and I wait on two flash busy conditions between steps. Please note that I wrote my own waiting function because the library function FLASH_WaitForLastOperation was *always* failing due to these two sticky error bits.
How can these two error bits suddenly appear when no operation is conducted on the flash in the meantime? It is as if they were spontanneously appearing.
Finally, what can cause the hard fault in the HAL_FLASH_Lock function? We can see that when the method was called, the flash was unlocked (bit 31 unset). Very strange.
Many thanks !
JL