I am writing to the flash memory of an STM32G474RE using the HAL libraries.

Here is the section of code I've been using:

   /* Unlock the Flash to enable the flash control register access *************/

   /* Clear OPTVERR bit set on virgin samples */

   EraseInitStruct.TypeErase    = FLASH_TYPEERASE_PAGES;
   EraseInitStruct.Page         = ADDR_FLASH_PAGE_255;
   EraseInitStruct.NbPages      = 1;
   EraseInitStruct.Banks        = 2;

   HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError);


   HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError);


The first program function works, if I erase all flash on debugging. However the following erase function does not work. I can monitor the values in the flash locations using STM32CubeIDE.

When I change

   EraseInitStruct.TypeErase    = FLASH_TYPEERASE_MASSERASE;
   EraseInitStruct.Page         = ADDR_FLASH_PAGE_255;

The erase function will start to work. But of course the whole bank gets erased. Which is not what I want.

What I don't understand is if I change

   EraseInitStruct.TypeErase    = FLASH_TYPEERASE_PAGES;
   EraseInitStruct.Page         = ADDR_FLASH_PAGE_128;

Only page 128 gets erased?

Can anyone explain why I can page erase page 128 but not 255? They're the same bank, the program memory is in a different bank, I can erase other pages in this bank? The datasheet also specifies that I can do pages erases?

Any insight is appreciated.


1 Answer 1


I've found the issue.


is supposed to be the page number of the bank (0-127). ADDR_FLASH_PAGE_XXX is the memory address of the start of the page.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.