cancel
Showing results for 
Search instead for 
Did you mean: 

Overwriting Internal flash after erasing previous data doesn't work

Meghana
Associate II

Hi 

I was trying to store the status of certain pins on flash memory so that I can read from flash and resume after power cycle, I have successfully written data in flash memory, I chose the last page starting address to store my data. 

Meghana_0-1738324263612.png

void write_array_to_flash(uint8_t* data, uint32_t size) {
    check_write_protection(STATUS_ARRAY_ADDRESS);
    HAL_FLASH_Unlock();
    // Erase the sector before writing (if necessary)
    FLASH_EraseInitTypeDef eraseInitStruct;
    uint32_t pageError = 0;
    eraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
    eraseInitStruct.Page = (STATUS_ARRAY_ADDRESS - FLASH_BASE) / FLASH_PAGE_SIZE;
    eraseInitStruct.NbPages = 1;
    if (HAL_FLASHEx_Erase(&eraseInitStruct, &pageError) != HAL_OK) {
            // Handle error
            HAL_FLASH_Lock();
            return;
        }
    // Write the array to flash
    uint64_t data_quadword[2] = {0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF};
        memcpy(data_quadword, data, size);

        // Ensure the address is aligned for a quad word write
           if ((STATUS_ARRAY_ADDRESS % 16) != 0) {
               // Handle error
               HAL_FLASH_Lock();
               return;
           }
        HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD, STATUS_ARRAY_ADDRESS, (uint32_t)data_quadword);

        HAL_FLASH_Lock();
}

But when trying to overwrite data after erasing it is not happening, when observed in debug state, the memory sector is not under write protection, when erase function HAL_FLASHEx_Erase(&eraseInitStruct, &pageError)  is called it returns HAL_OK but when memory segment observed, the data in that memory segment did not become all 1's, later when HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD, STATUS_ARRAY_ADDRESS, (uint32_t)data_quadword) is called not data change is observed.

I've attached the main.c file below. Let me know where I'm going wrong.
Thankyou.

1 ACCEPTED SOLUTION

Accepted Solutions

The Banks value is invalid. Probably should be FLASH_BANK_1.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

6 REPLIES 6

@Meghana wrote:

But when trying to overwrite data after erasing it is not happening, .


Are you sure the erase is actually happening?

Hi @Andrew Neil  
Erase is not happening when checked in debug mode, all the bits should become 1 that's not happening but it is returning status is HAL_OK.

TDK
Guru

Show the content of eraseInitStruct when HAL_FLASHEx_Erase is called.

Show the value of STATUS_ARRAY_ADDRESS.

If you feel a post has answered your question, please click "Accept as Solution".

Hi @TDK 

Meghana_1-1738331454151.png

I have attached the ss above after doing stepover also the contents of eraseInitStruct didn't change.

 

The Banks value is invalid. Probably should be FLASH_BANK_1.

If you feel a post has answered your question, please click "Accept as Solution".

Thank you for the response. After changing the eraseInitStruct.Banks parameter to FLASH_BANK_1 value, Erase happened successfully. Thankyou.