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
| Bootloader | 0x08000000 | 0x08002FFF | Static bootloader |
| App1 | 0x08003000 | -0x08002FFF | Currently running app |
| App2 (OTA) | 0x08019000 | 0x0802EFFF | Target 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();
