Skip to main content
Associate
April 17, 2025
Solved

STM32L552CCT6 Unable to erase flash memory

  • April 17, 2025
  • 2 replies
  • 323 views

I can't erase the flash in the STM32L552CCT6 with the code below. I wonder if my code is wrong. When I check the memory, the 2kB from the flash address 0x803F800 is not 0xFF. The reason it wasn't 0xFF originally seems to be because something other than 0xFF happened to be written during debugging.

Thank you.

 
uint32_t error;
uint8_t ram[2048];

HAL_FLASH_Unlock();

FLASH_EraseInitTypeDef flash;
flash.NbPages = 1;
flash.Page = 127;
flash.TypeErase = FLASH_TYPEERASE_PAGES;
flash.Banks = 1;
if (HAL_FLASHEx_Erase(&flash, &error) == HAL_OK)
{
 HAL_FLASH_Lock();
 if (error != 0xFFFFFFFF) return false;
 else
 {
 if (keepRamData == false) memset(ram, 0xFF, 2048);
 return true;
 }
}
HAL_FLASH_Lock();
return false;

Code formatting applied - please see How to insert source code for future reference.

This topic has been closed for replies.
Best answer by karoyama2

I remembered that I wanted to use FLASH as EEPROM in the past. I used eeprom_emul.c to achieve this. I will try to code it again using this.
Thank you.

2 replies

TDK
April 17, 2025

Does the code execute successfully? Return all HAL_OK status codes?

How are you checking the memory at 0x803F800?

"If you feel a post has answered your question, please click ""Accept as Solution""."
karoyama2AuthorBest answer
Associate
April 18, 2025

I remembered that I wanted to use FLASH as EEPROM in the past. I used eeprom_emul.c to achieve this. I will try to code it again using this.
Thank you.