Skip to main content
Clonimus74
Senior II
January 25, 2018
Question

HAL_FLASH_Program returns with error

  • January 25, 2018
  • 9 replies
  • 6207 views
Posted on January 25, 2018 at 14:07

Hi,

I use STM43L476 and try to use fast flash program to program 256 bytes (

decripted_firmware_data buffer

).

The flash area is erased (all 0xFF), but the function 

HAL_FLASH_Program always returns with HAL_ERROR and no single bte is programmed.

My system clock is 80MHz.

Any ideas?

address = 0x0800A000;

HAL_FLASH_Unlock();

__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);

if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FAST_AND_LAST, address, (uint32_t)&decripted_firmware_data[0]) != HAL_OK)

{

/* Lock the Flash to disable the flash control register access (recommended

to protect the FLASH memory against possible unwanted operation) *********/

HAL_FLASH_Lock();

return eFAIL;

}

Thank you

    This topic has been closed for replies.

    9 replies

    Doug Kehn
    Associate II
    January 25, 2018
    Posted on January 25, 2018 at 15:08

    Try calling HAL_FLASH_GetError() to get the flash error code when HAL_FLASH_Program() returns HAL_ERROR.

    Clonimus74
    Senior II
    January 28, 2018
    Posted on January 28, 2018 at 09:25

    I did, and the errors are:

    HAL_FLASH_ERROR_PGA - FLASH Programming Alignment error flag

    HAL_FLASH_ERROR_PGS - FLASH Programming Sequence error flag

    HAL_FLASH_ERROR_FAST - FLASH Fast programming error

    The datasheet states:

    Any attempt to write a double word which is not aligned with a double word address will

    set PGAERR flag in the FLASH_SR register.

    What does that mean exactly? the address 0x0800A000 is not double word aligned?

    Doug Kehn
    Associate II
    January 29, 2018
    Posted on January 29, 2018 at 14:27

    The Data parameter to HAL_FLASH_Program() is a uint64_t not uint32_t. Try typing it to a uint64_t.

    I use gcc and have Data in a character array; this construct works for me: (uint64_t)(uintptr_t)Data

    Ricardo Hassan
    Associate III
    August 3, 2018

    I am working with an STM32L496, and I see the exact same behavior, including the exact same flash error bits being set. Is there any resolution to this?

    yekme
    Associate
    September 12, 2018

    try to "FLASH_TYPEPROGRAM_DOUBLEWORD"

    Clonimus74
    Senior II
    October 3, 2018

    I got it working, can't remember how, it was a long time ago and I forgot to update the forum :o

    From what I see, I use 64 bit data as @Doug Kehn​ suggested, and optimization changed to medium.

    Here is my code:

    #pragma optimize=medium
    enum ResultEnum ProgramFirmwarePacket(uint32_t programming_address)
    {
     uint32_t address = 0;
     static uint64_t decripted_firmware_data[FIRMWARE_PACKET_SIZE];
     uint64_t *programmed_data = (uint64_t *)programming_address;
     uint16_t i;
     
    //here I decripted the data and store in decripted_firmware_data :-)
     
     address = programming_address;
     
     HAL_FLASH_Unlock();
     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
     
     for (i = 0; i < FIRMWARE_PACKET_SIZE; ++i)
     {
     if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, decripted_firmware_data[i]) != HAL_OK)
     {
     HAL_FLASH_GetError();
     /* Lock the Flash to disable the flash control register access (recommended
     to protect the FLASH memory against possible unwanted operation) *********/
     HAL_FLASH_Lock();
     return eFAIL;
     }
     else
     {
     if (*(programmed_data + i) != decripted_firmware_data[i])
     {
     /* Lock the Flash to disable the flash control register access (recommended
     to protect the FLASH memory against possible unwanted operation) *********/
     HAL_FLASH_Lock();
     
     return eFAIL;
     }
     }
     address += sizeof(uint64_t);
     }
     
     return eSUCCESS;
    }

    Gumer RoMa
    Visitor II
    April 12, 2022

    Hi, were you able to get it working with the FLASH_TYPEPROGRAM_FAST_AND_LAST? I have the same problem.

    Tesla DeLorean
    Guru
    April 12, 2022
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..