Skip to main content
Associate III
February 21, 2024
Solved

Flash memory erase

  • February 21, 2024
  • 1 reply
  • 909 views

I am using STM32L476RGT6. I am trying to read and write operation in the embedded flash memory. I am unable to erase the stored data in the embedded flash memory. 

I am following method to erase :

FLASH_EraseInitTypeDef EraseInitStruct;

 

HAL_FLASH_Unlock();

EraseInitStruct.TypeErase= FLASH_TYPEERASE_PAGES;

EraseInitStruct. Page = 0x0807F800;

EraseInitStruct. NbPages=1;

EraseInitStruct. Banks= 1;//

uint32_t  PAGE Error;

 

if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) {

}

HAL_FLASH_Lock();

 

 

Best answer by Elatewendy

hello,

 

 

 

EraseInitStruct. Page = 0x0807F800;

 

the page is the number of page,not the address.

 
 

The number of page numbers can be obtained by calculation.

For example:

 

unsigned int STMFLASH_GetFlashPage(unsigned int addr)
{
 uint32_t page = 0;

 if (addr < (FLASH_BASE + FLASH_BANK_SIZE))
 {
 /* Bank 1 */
 page = (addr - FLASH_BASE) / FLASH_PAGE_SIZE;
 }
 else
 {
 /* Bank 2 */
 page = (addr - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_PAGE_SIZE;
 }

 return page;
}

 

1 reply

Associate
February 21, 2024

hello,

 

 

 

EraseInitStruct. Page = 0x0807F800;

 

the page is the number of page,not the address.

 
 

The number of page numbers can be obtained by calculation.

For example:

 

unsigned int STMFLASH_GetFlashPage(unsigned int addr)
{
 uint32_t page = 0;

 if (addr < (FLASH_BASE + FLASH_BANK_SIZE))
 {
 /* Bank 1 */
 page = (addr - FLASH_BASE) / FLASH_PAGE_SIZE;
 }
 else
 {
 /* Bank 2 */
 page = (addr - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_PAGE_SIZE;
 }

 return page;
}