Flash page erase operation never completes
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.
