Skip to main content
Visitor II
August 22, 2024
Solved

STM32L4 Flash erase fail with HAL_FLASH_ERROR_PGS and HAL_FLASH_ERROR_WRP

  • August 22, 2024
  • 2 replies
  • 4563 views

Hi Everyone,

I have written a firmware which write some meta data in STM32L4 internal flash memory. But sometimes it happens that code gets stuck in a loop while erasing the flash memory. 

uint32_t UserFunctionForErasingFlash(void) {
 FLASH_EraseInitTypeDef EraseInitStruct;
 uint32_t SectorError = 0;
 uint32_t Success=1;

 EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
 EraseInitStruct.Banks = GetBank(MDM_FLASH_ADD);
 EraseInitStruct.Page = GetPage(MDM_FLASH_ADD);
 EraseInitStruct.NbPages = 2;

 /* Unlock the Flash to enable the flash control register access *************/
 g_unlock_erase_flash = HAL_FLASH_Unlock();
// clear_flash_flags();
 if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK){
 g_erase_error_code = HAL_FLASH_GetError();
 Success=0;
 Error_Handler();
 }

 /* Lock the Flash to disable the flash control register access (recommended
 to protect the FLASH memory against possible unwanted operation) *********/
 HAL_FLASH_Lock();

 return Success;
}

Above is the code snippet, where I checked FLASH_UNLOCK is happening perfectly. And also Bank and page number are also correct. But Erasing the flash memory is giving error HAL_FLASH_ERROR_PGS and HAL_FLASH_ERROR_WRP.

 

To understand the reason, I uploaded the code again, and it gets stuck here (2 times I checked it). But when I started debugging line by line, then it worked perfectly. After erasing, write also happen correctly. 

 

Has anyone encountered a similar issue or have insight, how can I recreate this issue and why erasing the internal flash is giving this issue and how can I resolve it?

    This topic has been closed for replies.
    Best answer by TDK

    After you unlock the flash but before you do anything with it, check for and clear all error flags. The debugger does stuff that can cause them to be set.

    2 replies

    TDKAnswer
    Super User
    August 22, 2024

    After you unlock the flash but before you do anything with it, check for and clear all error flags. The debugger does stuff that can cause them to be set.

    rshar.21Author
    Visitor II
    August 22, 2024

    Hi @TDK 

    Thanks for your reply. But this issue I faced in the working board and gets stuck in the infinite loop (as you can see in the code snippet) To debug this I have to put a debugger to know the reason, and code got stuck in same place. 

     

    I can assume that flag might be set getting because of the debugger, but can you suggest what can be the reason flags getting set without debugger because I have got this in multiple boards. 

    I debug 3 times without breakpoint and code used to hand there but when I started debugging step by step from that time it started working, can you suggest what can be the reason for it?

    Super User
    August 22, 2024

    > I can assume that flag might be set getting because of the debugger, but can you suggest what can be the reason flags getting set without debugger because I have got this in multiple boards. 

    Probably a software bug. Perhaps you are not completing a flash page write. Perhaps only writing a partial flash page. The issue is likely with the programming, not the erase code you've posted.

    ST Employee
    August 23, 2024

    Hello @rshar.21

    To resolve issues related to STM32L4 flash erase failures, specifically HAL_FLASH_ERROR_PGS (Programming Sequence error) and HAL_FLASH_ERROR_WRP (Write Protection error), it is crucial to ensure that flash memory operations are executed correctly and all necessary precautions are taken. Follow these steps:

    • Unlock the Flash: use HAL_FLASH_Unlock() to unlock the flash memory, allowing write and erase operations.
    • Clear Flash Error Flags: use the __HAL_FLASH_CLEAR_FLAG macro to clear all possible flash error flags before starting the erase operation.
    • Erase the Flash Pages: call the HAL_FLASHEx_Erase function to erase the specified pages.
    • Lock the Flash: after the erase operation, lock the flash memory using HAL_FLASH_Lock() to prevent accidental writes.

    Also, you can refer to: 

    • STM32L4 Reference Manual: for detailed information on the flash memory programming sequence and error handling.

    Dor_RH_0-1724413421153.png

     

    • STM32CubeL4: contains numerous examples that may assist you with FLASH operations, such as:
      • FLASH_EraseProgram
      • FLASH_FastProgram
      • FLASH_DualBoot
      • FLASH_WriteProtection

    I hope my answer has helped you. When your question is answered, please select this topic as solution that answered you, it will help others find that answer faster.

    Thanks for your contribution.

    Dor_RH