STM23L412 Flash Erase and Programming (EEPROM Emulation) Issue

73 views Asked by At

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 ?

Thanks.

I also tried alot of other ways but all of them been failed for example using this library https://github.com/nimaltd/ee and using the HAL functions based on this application note https://www.st.com/resource/en/application_note/an3969-eeprom-emulation-in-stm32f40xstm32f41x-microcontrollers-stmicroelectronics.pdf

0

There are 0 answers