cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_FLASH_Program returns with error

Clonimus74
Senior II
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

9 REPLIES 9
Doug Kehn
Senior
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.

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?

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

Posted on January 30, 2018 at 10:42

You can pass 32bit value to 64bit parameter. in any case in fast mode you pass address which is always 32bit in this processor.

In any case I tried that too before and it didn't work.

In the examples they mass erase the entire Bank and start the programming from the beginning of the Bank. I run this example successfully, once I changed the address to 0x0800A000 it also failed.... Does fast mode work only from the beginning of the Bank?

I ended up using double word mode instead.

Ricardo Hassan
Associate III

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

try to "FLASH_TYPEPROGRAM_DOUBLEWORD"

Clonimus74
Senior II

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
Associate II

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

https://community.st.com/s/question/0D53W00000ZWpgQSAT/writing-full-flash-page-does-not-work

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..