cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G070RB flash pages erase and write problem

K_K
Associate II

Hi,

I am trying to save some device specific configuration in one of the page of flash memory but it gives HAL_FLASH_ERROR_PGA error.

I have tried running Cube repository example code but found same problem.

If I skip (just for test) erase page then it gives error in the flash write.

Am I missing something? Any help on this would be really appreciated.

My code:

#define FLASH_USER_START_ADDR   (FLASH_BASE + (62 * FLASH_PAGE_SIZE))   /* Start @ of user Flash area */
#define FLASH_USER_END_ADDR     (FLASH_BASE + FLASH_SIZE - 1)   /* End @ of user Flash area */
 
static uint32_t GetPage(uint32_t Addr)
{
  return (Addr - FLASH_BASE) / FLASH_PAGE_SIZE;;
}
 
void save_data_to_flash()
{
  HAL_FLASH_Unlock();
   
  /* Erase the user Flash area
    (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
	__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGSERR |FLASH_FLAG_PGAERR| FLASH_FLAG_WRPERR);
	
  /* Get the 1st page to erase */
  FirstPage = GetPage(FLASH_USER_START_ADDR);
 
  /* Get the number of pages to erase from 1st page */
  NbOfPages = GetPage(FLASH_USER_END_ADDR) - FirstPage + 1;
 
  /* Fill EraseInit structure*/
  EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
  EraseInitStruct.Page        = FirstPage;
  EraseInitStruct.NbPages     = NbOfPages;
 
  /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
     you have to make sure that these data are rewritten before they are accessed during code
     execution. If this cannot be done safely, it is recommended to flush the caches by setting the
     DCRST and ICRST bits in the FLASH_CR register. */
  if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
  {
    /*
      Error occurred while page erase.
      User can add here some code to deal with this error.
      PageError will contain the faulty page and then to know the code error on this page,
      user can call function 'HAL_FLASH_GetError()'
    */
		erase_error=HAL_FLASH_GetError();	
		
			/* Infinite loop */
		 while (1)
			{
				
			}
		
  }
 
  /* Program the user Flash area word by word
    (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
 
  Address = FLASH_USER_START_ADDR;
 
  //while (Address < FLASH_USER_END_ADDR)
	DATA_64=0;
	for(int i=0;i<BYTE_TO_BE_STORED;i=i+8)
  {
		for (int j=0;j<8;j++)
		{
			DATA_64|= (Usr_Memory_Buffer[j+i]<<8*j);
		}  
    if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, DATA_64) == HAL_OK)
    {
      Address = Address + 8;
    }
   else
    {
      /* Error occurred while writing data in Flash memory.
         User can add here some code to deal with this error */
      while (1)
      {
				
      }
    }
  }
 
  /* Lock the Flash to disable the flash control register access (recommended
     to protect the FLASH memory against possible unwanted operation) *********/
  HAL_FLASH_Lock();
}

0 REPLIES 0