2023-11-10 05:19 AM
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)
https://www.st.com/resource/en/reference_manual/rm0394-stm32l41xxx42xxx43xxx44xxx45xxx46xxx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf
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
FLASH->SR &= ~(FLASH_FLAG_PGSERR | FLASH_FLAG_PGAERR | FLASH_FLAG_WRPERR |FLASH_FLAG_OPERR | FLASH_FLAG_EOP); // Clear all error programming flags
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
clearFlash(1,63);
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.
2023-11-10 06:45 AM
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
2023-11-10 09:20 AM
@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 ?
2023-11-10 09:28 AM - edited 2023-11-10 09:29 AM
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.
2023-11-11 04:22 AM
@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.