Showing results for 
Search instead for 
Did you mean: 

STM23L412 Flash Erase and Programming (EEPROM Emulation) Issue

Associate II

I'm trying to write a code to store some values at the final flash page (no. 63) of STM32L412 I succeeded into erasing the page, but I'm failing to write anything to it, the code is based on the reference manual (RM0394)
page 83 and 84

uint32_t *address_to_write = (uint32_t *)0x0801F800; // Address to write the data page 63
uint64_t data_to_write = 0x1122334455667788; //Data to be written to the flash
uint32_t data_been_read; //read the data which been written

void clearFlash(uint8_t pagesCount, uint8_t firstPage){
	HAL_FLASH_Unlock(); //Unlock the flash
    for (int i = 0; i<pagesCount; i++){ //Looping for clearing enough space.
        while(FLASH->SR & FLASH_SR_BSY); // Wait until the BSY bit is cleared
        while((FLASH->SR & FLASH_SR_PGSERR)); //PGSERR should not be set
        FLASH->CR |= FLASH_CR_PER; 		//Set erase process
        FLASH->CR &= ~FLASH_CR_PNB_Msk; //Clear address mask
        FLASH->CR |= ((firstPage+i)<<FLASH_CR_PNB_Pos); //place in address
        FLASH->CR |= FLASH_CR_STRT; //Start clearing the flash
    HAL_FLASH_Lock(); //lock the flash

void flash_program_double_word(uint32_t *address, uint64_t data) {
	HAL_FLASH_Unlock(); //Unlock the flash
    while (FLASH->SR & FLASH_SR_BSY);//Check that no Flash main memory operation is ongoing
    FLASH->SR = FLASH_SR_PGSERR | FLASH_SR_PGAERR | FLASH_SR_SIZERR | FLASH_SR_PROGERR; //Clear all error programming flags
    FLASH->CR |= FLASH_CR_PG; //Set the PG bit in the Flash control register
    //Perform the data write operation
    *(__IO uint32_t*)address = (uint32_t)(data & 0xFFFFFFFF); // Write the first word
    address += 4; // Move to the next aligned double word address
    *(__IO uint32_t*)address = (uint32_t)(data >> 32); // Write the second word
    while (FLASH->SR & FLASH_SR_BSY);	 // Wait until the BSY bit is cleared
    FLASH->SR = FLASH_SR_EOP;    		 // Check and clear EOP flag
    FLASH->CR &= ~FLASH_CR_PG;			// Clear the PG bit in the Flash control register
    HAL_FLASH_Lock(); 					//lock the flash

uint32_t flashReadData(uint32_t *address){

  __IO uint32_t read_data = *(__IO uint32_t *)address;
  return (uint32_t)read_data;


and this is my test code 

	flash_program_double_word(address_to_write, data_to_write);
	data_been_read = flashReadData(address_to_write);

so from the memory in STM32CubeIDE I can see that all the addresses in the page has been erased to 0xFFFFFFFF but nothing been written there can anyone help with this ?

Thank you so much.


Florian LR
ST Employee

Hi @Mkotb !

About your flash write issues, you can find an example of write function in the HAL, more precisely in the stm32l4xx_hal_flash.c file there is a function named HAL_FLASH_Program. This can provide you with the necessary information regarding the creation of your function.
I also noticed that you refer to EEPROM Emulation in your title, you may also find information on that topic within our X-CUBE-EEPROM Expansion Package which provide software and example to demonstrate how to emulate an EEPROM using the internal flash memory. There is an example tailored for STM32L476 in this package that you can port to your STM32L412 if this software fits your needs.

I hope this will help you, 

Best Regards,
Florian LR

@Florian LR I tried all what you mentioned and all of them didn't work, that is why I took the approach of writing my own functions and still having some issues.

Can you provide more help ?

Establish the HAL examples work as expected, if they don't hand crafted ones are unlikely too either.

Which specific STM32L412 ? Any errors or status reported by the FLASH peripheral?

Is this a custom board? Running at what voltage? Check VCAP capacitors, and voltage.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Associate II

@Tesla DeLorean 
I didn't notice that there is an example provided inside CubeL4 which I tested with my hardware, and it worked very well now :D, I'm not sure what was the error will investigate it more to understand why that was happening.
I'm using STM32L412CBU6 and powering from LDO 3.3Volt as attached and no VCAP in this case.


Thank you so much for your help and support.