2023-04-08 03:12 PM - edited 2023-11-20 08:41 AM
TLDR: application tried to write to a FLASH address without accessing FLASH peripheral, so the Flash bit CFGBSY in the SR register got set and further accesses to FLASH CR register leads to a Bus Error (not documented). Of course, the main action is to correct the application behavior, but also tried to reset the bit (which from the documentation can only be reset by hardware) but didn't work.
Long story.
The application tried something as the follows.
static const uint32_t var = 0;
uint32_t * const p_var = (uint32_t * const)&var;
*p_var = 12;
the p_var contents assignment didn't generate a Bus Error, rather it set the CFGBSY bit in FLASH SR register.
From the documentation:
Interestingly the doc says nothing of the CR LOCK/OPTLOCK bits being inaccessible when CFGBSY.
The application at that point is not aware of the issue. Further a FLASH write is done and the CPU enters a Bus Error when trying to access the CR register to Lock the FLASH Memory.
From de Documentation:
The documentation says nothing about Bus Error in case the CFGBSY bit is set when accessing the CR register LOCK/OPTLOCK bits.
The solution:
But as much as the problem is already solved, there are some questions pending:
Thank you in advance.
2023-04-09 04:20 AM
Run in debug configuration, set, eventually sequential if lots of watchpoints are needed, watchpoints on all registers that could affect CFGBSY an run the program. I guess some bad access happens.
2024-08-13 05:16 AM
We are facing this same issue and code size is really big. It is not possible to identify by putting watchpoint like you suggested. Any other possible way to find the solution? Is this not the Bug ? Should it not generate the hard fault in such scenario?
2024-08-13 05:29 AM - edited 2024-08-13 05:35 AM
The program in the opening post is invalid as it tries to write to flash with no special precautions or setup.
CFGBSY is readonly. It can't be set or cleared in software. Presumably it would be cleared when the rest of the flash word is attempted to be written (which will fail if flash is locked).
If you're still having issues open a new post with all relevant details. Fixing the bug is going to be the primary solution here.