cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_FLASHEx_Erase() – STM32L072

satyam9896
Associate II

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();

 

4 REPLIES 4
TDK
Guru

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".

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()

 

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".

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 */