Skip to main content
satyam9896
Associate III
Apr 16, 2025 2:32 PM UTC
Question

HAL_FLASHEx_Erase() – STM32L072

Hi STM32 Community,

I'm working with the STM32L072 MCU which has 192KB of Flash. I'm implementing a bootloader + dual app OTA architecture. Here's my current memory layout:

Region Start Address End Address Description

Bootloader0x080000000x08002FFFStatic bootloader
App10x08003000-0x08002FFFCurrently running app
App2 (OTA)0x080190000x0802EFFFTarget for OTA

as per detasheet: 
Each sector is 4KB, containing 32 pages of 128 bytes each.
From App1, I'm trying to erase App2’s flash region using HAL_FLASHEx_Erase().
so oviusly in system file of app 1 the vector offset is set at 0x3000.
flashbase is defines as default 0x08000000
inkiel option for setting conf: ROM1 start -> 0x3000 and size as 0x18FFF bcz i need 88kb atleast for app1.
Here’s my erase logic:
but my while checking in debug mode the code end at error and stuck at while(1)

#define FLASH_PAGE_SIZE 128U
#define FLASH_USER_START_ADDR 0x08019000U
#define FLASH_USER_END_ADDR 0x0802EFFFU

void Flash_Erase_CodeB(void)
{
 uint32_t PageError = 0;
 FLASH_EraseInitTypeDef EraseInitStruct;

 // Unlock Flash
 HAL_FLASH_Unlock();
	 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
		__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR );
		__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_FWWERR);
		__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_NOTZEROERR);

 // Setup erase parameters
 EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
 EraseInitStruct.PageAddress = FLASH_USER_START_ADDR/*(FLASH_USER_START_ADDR - 0x08000000U) / 128*/; // Page number
 EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR + 1) / 128;

 if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
 {
 // Erase Error
 /*uint32_t*/ err = HAL_FLASH_GetError();
 // Handle error here (e.g., blink LED or reset)
 while (1);
 }

 // Lock Flash again
 HAL_FLASH_Lock();

 

This topic has been closed for replies.

4 replies

TDK
Apr 16, 2025 3:48 PM UTC

What does HAL_FLASH_GetError say is the problem? What address is reported in PageError?

"If you feel a post has answered your question, please click ""Accept as Solution""."
satyam9896
Associate III
Apr 17, 2025 7:22 AM UTC

IN ERR VAR IM GETTING VALUE 0X00000002 WHICH IS 0X2 AND IT DENOTES WRITE PROTECTION ERR BUT I HAVE CHECKED IN CUBE PROGRAMMER BEFORE FLASHING BUT THERE WAS NO ob WAS ENABLED.

err = HAL_FLASH_GetError()

 

TDK
Apr 17, 2025 1:06 PM UTC

It sounds like write protection is enabled.

Show the values of the option byte registers.

"If you feel a post has answered your question, please click ""Accept as Solution""."