Skip to main content
Associate
Nov 19, 2026 9:31 PM UTC
Question

STM32U073 Flash Erase Reports success but does nothing

Hello, 

In my application using the STM32U073 series MCU I must erase one of the last pages of flash (actually several of them but for this example I’ll just use one).Page 62, addresses 0x0801f000 - 0x0801f800, and when I call HAL_FLASHEx_Erase I get no errors either from the function return or the pageError parameter that gets written to inside the erase function, both report success.

However when I look at the memory with the memory viewer I see that the first words at that page are still not erased. I moved the erase function out of the many layers of other code and tried to execute it directly in main after the peripherals and initializations have been done, but the outcome was the same. This version of the code is attached, as well as screenshots of the debugger with memory view and the UART terminal for debug output. 

Memory View after erase
Memory view before erase 
Start of Debug
UART Output

​​​​​​
int main(void)
{

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */
SystemClock_Config();

/* USER CODE BEGIN SysInit */
//
/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_COMP1_Init();
MX_I2C2_Init();
MX_RTC_Init();
MX_COMP2_Init();
MX_LPUART3_UART_Init();
MX_RNG_Init();



/* USER CODE BEGIN 2 */

//Starts the application RTC OP_timer (DEFAULT -> FAST)
set_default_ARTC_timer();

set_test_led(TESTPIN_LOW);

debug_send_string(dbg_GENERAL, "------ "FW_REV_STR);

//Set the initial pin mainSM_state
set_initial_pinstate();

//set_test_pin1(false);

//southcoFlashInit(main_call_back);

FLASH_EraseInitTypeDef eraseInitStruct ={
.Page = 62,
.NbPages = 1,
.TypeErase = FLASH_TYPEERASE_PAGES,
.Banks = FLASH_BANK_1
};

uint32_t PageError = 0;

uint8_t word_holder[4];
memcpy(word_holder, (void*)0x0801f000, 4);
debug_send_bytes("Controller Header Before Erase: ", word_holder, 4);

HAL_FLASH_Unlock();

HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&eraseInitStruct, &PageError);
//FLASH_PageErase(eraseInitStruct.Page);

HAL_FLASH_Lock();

if(status)
debug_send_string(dbg_GENERAL, "HAL Error");
else
debug_send_string(dbg_GENERAL, "HAL Success");

memcpy(word_holder, &PageError, 4);
debug_send_bytes("Page Error Value: ", word_holder, 4);

memcpy(word_holder, (void*)0x0801f000, 4);

debug_send_bytes("Controller Header After Erase: ", word_holder, 4);
}

Not Sure if this is related either but for some reason when I try to connect to this device with theSTM32CubeProgrammer application, the connection always fails unless I first run a debug session from the IDE and then terminate and try to connect from the Programmer. Could there be an issue with the debugger tool that is setting/clearing something in a register to make this happen? I know its not directly an issue with the debugger itself as when I run the code without the debugger I get the same behavior

 

Any help is greatly appreciated!

2 replies

ST Technical Moderator
Nov 20, 2026 10:33 AM UTC

Hello ​@pshannon 

Could you try clearing the OPTVERR flag before erasing the Flash, as shown in the STM32CubeU0 FLASH_EraseProgram example?

Also, have you tried erasing and reading back another unused, unprotected page to determine whether the issue is specific to page 62?

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om
pshannonAuthor
Associate
Nov 23, 2026 7:08 PM UTC

Hello ​@Saket_Om 

I have added the the line to clear the OPTVERR flag from the flash erase example directly after the HAL_FLASH_Unlock() but the flash page is still not being erased. 

Likewise I tried erasing a different page (page 61) but I had to write data to it to see if the flash was actually being erased or not. Again the HAL stats returned by the flash erase function did not raise any errors and the pageError parameter is returned as 0xFFFFFFFF but again the actually flash data at that address is not erased. 

 

Any other things I should try?