2018-09-25 06:41 AM
I'm trying to erase a single page of flash memory at 0x0800F800 (on a 64kB STM32F103C8T6) by using the following code, which runs from RAM:
while(FLASH->SR & FLASH_SR_BSY);
FLASH->CR = FLASH_CR_PER;
FLASH->AR = 0x0800F800;
FLASH->CR |= FLASH_CR_STRT;
while (!(FLASH->SR & FLASH_SR_EOP));
FLASH->SR = FLASH_SR_EOP;
FLASH->CR &= ~FLASH_CR_PER;
The problem I am facing is that EOP is never set to 1, as well as BSY flag in FLASH_CR is never set to 0. The erase procedure hangs indefinitely, and when this code was previously executed from flash (not RAM), it also caused a full lock-up of the MCU with inaccessible SWD debugging.
I tend to think that this might be a hardware failure of my specific MCU. However, what makes me doubt about it is that mass erase and/or page erase, flash programming and verification all works from OpenOCD and ST-Link Utility when the MCU is reset and halted prior to trying to execute flash operations in firmware.
What could possibly cause such behaviour? I'm kinda lost as I thought moving the flash programming code out of flash into RAM would solve the issue, but apparently it still hangs, but only when trying to do it in FW, and not via SWD/JTAG.
Solved! Go to Solution.
2018-09-26 01:41 AM
In case anyone will face similar issue, I found the answer in RM0008 Reference Manual, page 59 one can find following sentence:
For write and erase operations on the Flash memory (write/erase), the internal RC oscillator (HSI) must be ON.
In my firmware the HSI was turned off after bringing up the HSE and PLL as this is what is used for SYSCLK. This also explains why flashing worked from OpenOCD/JTAG - is because the HSI is the default clock source after reset.
2018-09-25 08:31 AM
My general recommendation would be to not use the debugger in these situations, but rather instrument your code so it reports what it is seeing and what it is doing.
Review the library and examples, and confirm they function as expected.
2018-09-26 01:41 AM
In case anyone will face similar issue, I found the answer in RM0008 Reference Manual, page 59 one can find following sentence:
For write and erase operations on the Flash memory (write/erase), the internal RC oscillator (HSI) must be ON.
In my firmware the HSI was turned off after bringing up the HSE and PLL as this is what is used for SYSCLK. This also explains why flashing worked from OpenOCD/JTAG - is because the HSI is the default clock source after reset.