2025-04-16 7:32 AM
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();
2025-04-16 8:48 AM
What does HAL_FLASH_GetError say is the problem? What address is reported in PageError?
2025-04-17 12:22 AM - edited 2025-04-17 2:07 AM
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()
2025-04-17 6:06 AM
It sounds like write protection is enabled.
Show the values of the option byte registers.
2025-04-17 7:08 AM
THESE ARE THE MENTION VALUES
#define HAL_FLASH_ERROR_NONE 0x00U /*!< No error */
#define HAL_FLASH_ERROR_PGA 0x01U /*!< Programming alignment error */
#define HAL_FLASH_ERROR_WRP 0x02U /*!< Write protection error */
#define HAL_FLASH_ERROR_OPTV 0x04U /*!< Option validity error */
#define HAL_FLASH_ERROR_SIZE 0x08U /*!< */
#define HAL_FLASH_ERROR_RD 0x10U /*!< Read protected error */
#define HAL_FLASH_ERROR_FWWERR 0x20U /*!< FLASH Write or Erase operation aborted */
#define HAL_FLASH_ERROR_NOTZERO 0x40U /*!< FLASH Write operation is done in a not-erased region */