cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 Flash erase fail with HAL_FLASH_ERROR_PGS and HAL_FLASH_ERROR_WRP

rshar.21
Associate II

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?

4 REPLIES 4
TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".

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?

> 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.

If you feel a post has answered your question, please click "Accept as Solution".

@TDK wrote:

The issue is likely with the programming, not the erase code you've posted.


+1

 


@rshar.21 wrote:

giving error HAL_FLASH_ERROR_PGS and HAL_FLASH_ERROR_WRP.


@rshar.21  Where, exactly, do those errors occur?

Did you look those codes up?

HAL_FLASH_ERROR_PGS =  Programming Sequence error;

HAL_FLASH_ERROR_WRP = Write protection error.