[ARM] STM32G4 save data to Flash issue

Status
Not open for further replies.

tynnor

Junior Member level 1
Joined
Jun 16, 2020
Messages
15
Helped
1
Reputation
2
Reaction score
0
Trophy points
1
Activity points
133
Hello,
I use a STM32G473 on my board, and I need to save a few bytes of data when the 24V power supply falls. I have more than 400ms to perform the saving, more than enough.
I am facing a weird issue : when I call the following function, from the Main loop (or ISR, same behavior), at power down, the only thing written in flash is a set of 0x0, such as my data buffer is empty. But I am pretty sure it is not.
However, when I use the following function, directly in my code, to save my data in Flash, it works. Or, when I directly write the buffer content inside this function, it works, too ! This drive my crazy


C++:
bool Config::saveConfigFlash(){

    uint32_t pageError;
    FLASH_EraseInitTypeDef pEraseInit;
    pEraseInit.Banks = FLASH_BANK_2;
    pEraseInit.NbPages = 1;
    pEraseInit.Page = 127;
    pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;

    //currentConfig is the struct I want to save in Flash
    static uint64_t data[1 + sizeof(TerrariumConfig)/sizeof(uint64_t)];
    memcpy(data, (void*) &currentConfig, sizeof(TerrariumConfig));


    HAL_StatusTypeDef status = HAL_FLASH_Unlock();

    SET_BIT(FLASH->SR, FLASH_SR_PGSERR);
    status = HAL_FLASHEx_Erase(&pEraseInit, &pageError);


    for(unsigned int i = 0; i < 1 + sizeof(TerrariumConfig)/sizeof(uint64_t); i++)
    {
        //write the data, with 64bit alignment
        status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addressDataFlash + i*8, data[i]);
    }

    FLASH_FlushCaches();
    HAL_FLASH_Lock();

    return true;

}

Reference manual, page 92

Do you have an idea to solve this problem ?
Thank you for your help
 

Hello,
What other information do you need ?
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…