Skip to main content
Visitor II
November 27, 2019
Question

Trouble programming flash with HAL_FLASH_Program

  • November 27, 2019
  • 9 replies
  • 5188 views

I have troubles writing succesfully to flash on a STM32G07 chip.

The application is similar to this:

uint64_t data;

HAL_FLASH_Unlock();

HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addr, data);

HAL_FLASH_Lock();

I started getting hardfault in lock because of error in programming. I found out that this was related to timeout on the wait for operation to finish inside program function. So I added a check that program returning HAL_OK before calling lock . But this just moved the problem.

The behavious is very strange.

I can see that PGAERR and PGSERR in the FLASH SR register are being set.

My data is aligned at a 64-bit boundry I have added check that both data and the address i program to will have addr & 0x07 == 0.

I suspect some other interrupt could interefere in some way. But adding __disable_irq(); does not help.

Data is programmed like 25-50% of the times rest I simply get no data programmed.

    This topic has been closed for replies.

    9 replies

    Super User
    November 28, 2019

    What if instead of FLASH_TYPEPROGRAM_DOUBLEWORD you use something less? FLASH_TYPEPROGRAM_WORD?

    -- pa

    TMaltAuthor
    Visitor II
    November 28, 2019

    That is not an option on this CPU, it can only program 64-bit.

    TMaltAuthor
    Visitor II
    November 28, 2019

    I observed that the CFGBSY is going on even when no programming takes place..... Tried to disable nearly all code, but still CFGBSY keeps on toggling.

    Could this in any way be related to the debugger / breakpoints???

    Seems to change behavious when I have no breakpoints on target!

    Visitor II
    November 29, 2019

    Hi I also have this problem. I wrote the function for programming and page erasing sequences all over again according to reference manual. It has something to do with breakpoints definitely. After I put the breakpoints, FLASH->SR changes and CFGBSY bit never resets. If you try to write FLASH->CR while CFGBSY bit is set, you get hardfault. I also tried HAL_FLASHEx_EnableDebugger() but nothing changed.

    Super User
    November 30, 2019

    > I can see that PGAERR and PGSERR in the FLASH SR register are being set.

    From RM0444:

    PGAERR is set if one of the following conditions occurs:

    [...]

    – In fast programming: the data to program doesn’t belong to the same row than the

    previous programmed double words, or the address to program is not greater than

    the previous one.

    PGSERR may be a consequence.

    > After I put the breakpoints

    Debugging *is* intrusive. You usually don't have enough control over what exactly the debugger does when it breaks into the running program. Find other ways to monitor your program's paths (toggle pins, observe using oscilloscope/LA; use debug prints to whatever port is convenient).

    JW

    @kaan kılıç​  change your username from User15750360464975656633 to a normal nick.

    Visitor II
    December 18, 2019

    @TMalt​ do you had solved this problem? I meet the same problem too.

    TMaltAuthor
    Visitor II
    December 18, 2019

    Problem is not really solved. But it is clearly related to debugging, so I have changed my code so that I can debug without writing to flash. Little annoying but solvable.

    Visitor II
    December 19, 2019

    Thanks for you quickly reply. i solved my issue. since i don't init IWDG and always call HAL_IWDG_Refresh() in while loop. after init IWDG, there are no any issue now. hope can help you.

    TMaltAuthor
    Visitor II
    December 19, 2019

    Great feedback! This might solve my problem also.

    I comment out the IWDG init when debugging so I have a "unused" call to the HAL_IWDG_Refresh() in a thread loop still running. I will try to verify if removing this resolves the problem.

    Visitor II
    February 16, 2022

    Hello,

    Could you solve your problem ? I have a exactly same problem right now, even IWDG is not enabled.

    Visitor II
    February 19, 2023

    I could get HAL_FLASH_Program to write WORDS, as soon as I moved to doublewords the last error returned is PGA | PGS flags in the SR.

    PGA = alignment

    PGS = sequence

    AFAIK, I have done everything correctly. There seems to be a problem with trying to write to FLASH on a 32f411RE. Worked hard to figure it out, I think there is something wrong with the libary, or at least how I am using it. Here is my code:

     // STM32F411RE try to write doubleword into FLASH

     // The flashing works for WORDS but not DOUBLEWORDS

     #define SECTOR 7

     #define ADDRESS 0x08060000

     uint32_t lastError = 0;

     uint64_t valueToWrite = ~0ULL;

     uint64_t valueToRead = 0;

     HAL_StatusTypeDef status = HAL_ERROR;

     valueToRead = * ( uint64_t * ) ADDRESS;

     if ( valueToRead == ~0ULL )

     {

    FLASH_EraseInitTypeDef pEraseInit = { 0 };

    pEraseInit.TypeErase = TYPEERASE_SECTORS;

    pEraseInit.Sector = FLASH_SECTOR_7;

    pEraseInit.NbSectors = 1;

    pEraseInit.VoltageRange = FLASH_VOLTAGE_RANGE_3;

    uint32_t SectorError = 0;

    do

    {

    HAL_FLASH_Unlock();

    status = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);

    HAL_FLASH_Lock();

    valueToRead = * ( uint64_t * ) ADDRESS;

    } while( (status !=HAL_OK) || (SectorError != ~0UL ) || ( valueToRead != ~0ULL ) );

    HAL_FLASH_Unlock();

    status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, ADDRESS, valueToWrite );

    HAL_FLASH_Lock();

    lastError = 0;

    if ( status != HAL_OK )

    {

     lastError = HAL_FLASH_GetError();

    }

     }