2023-08-11 07:21 AM - edited 2023-08-11 07:24 AM
Team,
i'm trying to store data - by use of HAL lib - during Hardware,Memory.. and so on Fault Interrupt routine. Idea is to write some data at the very end of flash - allowable area.
So, the code is tested druing normal operation. However, placed in Hard Fault ISR its stooped/ failed during erase inside function HAL_FLASHEx_Erase(addr,bank) precisley at line __HAL_LOCK(&pFlash);
I was trying to figure it our on my own, however seems i'm not able to clear the Flash SR register bits - or IDE is not refresing correctly - doubtful.
FLASH->SR ^= 0x3FF; //or SR=0x0 // clears error bits in SR reg
FLASH->CR ^= 0x8404; // clears mer1 mer 2 and bker bit
Initializing erase procedure sets WPRERR bit anyway and after erase, errors are set back.
Result is, flash bank is not erased nor data stored. Im woring on STM32G474
Can anyone support me ?
2023-08-11 07:56 AM
Show your code and show what's in the pFlash variable at the time it tries to lock it. Likely the pointer is broken.
2023-08-11 12:00 PM
> However, placed in Hard Fault ISR its stooped/ failed during erase inside function HAL_FLASHEx_Erase
You don't want to erase in a fault handler. Have the write area erased and ready to write, and the status bits already cleared. Preventing possibility of a problem is more robust than solving it.
2023-08-11 12:31 PM
Could you stash data in NVRAM /BKPRAM or end-of SRAM?
Must you use HAL, couldn't you just do some minimal assembler or LL, where you just walk through the FLASH programming without pointers, HAL instances, locks, or need for timing interrupts?
As Pavel suggests, pre-erase, or journal this data across the sector. Here we spill data immediately to a USART