1
\$\begingroup\$

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 *************/
   HAL_FLASH_Unlock();

   /* Clear OPTVERR bit set on virgin samples */
   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);

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

   HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError);

   HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, ADDR_FLASH_PAGE_255, 0);

   HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError);

   HAL_FLASH_Lock();

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.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I've found the issue.

 EraseInitStruct.Page  

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.

\$\endgroup\$

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.