cancel
Showing results for 
Search instead for 
Did you mean: 

why stm32g0b1cetx series flash erase example code is not working ? I got this error code from flash " pFlash.ErrorCode = 0x000000A0".

Ali110
Associate II

Hello everyone I try erase and write the flash of stm32g0b1cetx series. every time I try I got this error code from flash " pFlash.ErrorCode = 0x000000A0".

is anybody have idea what problem is here i try example code too.

I am totally beginner to stm32g0 series

this is my code

 HAL_FLASH_Unlock();

HAL_GPIO_WritePin(STATUS_LED_GPIO_Port,STATUS_LED_Pin,1);

//__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);

 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR| FLASH_CR_PER | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR | FLASH_FLAG_BSY);

 /* Program the user Flash area word by word

  (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

FirstPage = GetPage(FLASH_USER_START_ADDR);

 /* Get the number of pages to erase from 1st page */

 NbOfPages = GetPage(FLASH_USER_END_ADDR) - FirstPage + 1;

 /* Get the bank */

 BankNumber = GetBank(FLASH_USER_START_ADDR);

 /* Fill EraseInit structure*/

 EraseInitStruct.TypeErase  = FLASH_TYPEERASE_PAGES;

 EraseInitStruct.Banks    = BankNumber;

 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()'

  */

  /* Infinite loop */

HAL_FLASH_GetError();

  while (1)

  {

   /* Make LED4 blink (100ms on, 2s off) to indicate error in Erase operation */

    HAL_GPIO_WritePin(SERVER_LED_GPIO_Port,SERVER_LED_Pin,1);

   HAL_Delay(100);

   HAL_GPIO_WritePin(SERVER_LED_GPIO_Port,SERVER_LED_Pin,0);

   HAL_Delay(2000);

  }

 }

 Address = FLASH_USER_START_ADDR;

 while (Address < FLASH_USER_END_ADDR)

 {

  if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, DATA_64) == HAL_OK)

  {

   Address = Address + 8; /* increment to next double word*/

  }

  else

  {

   /* Error occurred while writing data in Flash memory.

     User can add here some code to deal with this error */

   while (1)

   {

    /* Make LED4 blink (100ms on, 2s off) to indicate error in Write operation */

    HAL_GPIO_WritePin(SERVER_LED_GPIO_Port,SERVER_LED_Pin,1);

    HAL_Delay(100);

     HAL_GPIO_WritePin(SERVER_LED_GPIO_Port,SERVER_LED_Pin,0);

    HAL_Delay(2000);

   }

  }

 }

 /* Lock the Flash to disable the flash control register access (recommended

   to protect the FLASH memory against possible unwanted operation) *********/

 /* Check if the programmed data is OK

   MemoryProgramStatus = 0: data programmed correctly

   MemoryProgramStatus != 0: number of words not programmed correctly ******/

 Address = FLASH_USER_START_ADDR;

 MemoryProgramStatus = 0x0;

 while (Address < FLASH_USER_END_ADDR)

 {

  data32 = *(__IO uint32_t *)Address;

  if (data32 != DATA_32)

  {

   MemoryProgramStatus++;

  }

  Address = Address + 4;

 }

 /*Check if there is an issue to program data*/

 if (MemoryProgramStatus == 0)

 {

  /* No error detected. Switch on LED4*/

  HAL_GPIO_WritePin(SERVER_LED_GPIO_Port,SERVER_LED_Pin,1);;

 }

 else

 {

  /* Error detected. LED4 will blink with 1s period */

  while (1)

  {

    HAL_GPIO_WritePin(SERVER_LED_GPIO_Port,SERVER_LED_Pin,1);

   HAL_Delay(500);

   HAL_GPIO_WritePin(SERVER_LED_GPIO_Port,SERVER_LED_Pin,0);

   HAL_Delay(500);

  }

 }

 HAL_FLASH_Lock();

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

//get page and get bank function

/**

 * @brief Gets the page of a given address

 * @param Addr: Address of the FLASH Memory

 * @retval The page of a given address

 */

static uint32_t GetPage(uint32_t Addr)

{

 uint32_t page = 0;

 if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))

 {

  /* Bank 1 */

  page = (Addr - FLASH_BASE) / FLASH_PAGE_SIZE;

 }

 else

 {

  /* Bank 2 */

  page = (Addr - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_PAGE_SIZE;

 }

 return page;

}

/**

 * @brief Gets the bank of a given address

 * @param Addr: Address of the FLASH Memory

 * @retval The bank of a given address

 */

static uint32_t GetBank(uint32_t Addr)

{

 return FLASH_BANK_1;

}

2 REPLIES 2
Bubbles
ST Employee

Hi @Ali110​,

what exactly is this pFlash.ErrorCode? I don't see this structure pointer defined in your code and neither it's from CMSIS.

If it's somehow linked to the FLASH_SR, then 0x000000A would mean attempt to program unerased flash.

What example did you try?

BR,

J

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hello @JHOUD

Thank you for quick answer, I try to write some important data on flash of stm32g0(I am using stm32g0b1 series) to update my device but when I try to store new data and erase old data from flash, I got the FLASH_FLAG_PGAERR ERROR, FLASH_FLAG_PGSERR ERROR. I am using this examples of stm32g0b1 for reference but still get some issue. Sometimes erase and write and happen and after the next try operation is failed. I don’t understand why this happen. Please help me to debug the problem why some time erase and write is complete and next try is not.

I am using the FLASH BANK 1

And starting address of bootloader 0800 0000 (i put bootloader at starting address of flash for my requirements).

End address of bootloader - 0800 1FFF

And starting address of data – 0800 1800

End address of data - 0803 FFFF

Erasing Method – page erase        

@JHOUD​  what exactly is this pFlash.ErrorCode?

i got this variable in HAL_FLASH G0 header file. i put screenshot of function 0693W00000SuwZwQAJ.png 

#define USER_FLASH_END_ADDRESS                                                                0x0803FFFF      //End of Address             

#define ACTUAL_IMAGE_ADDRESS                                                                   0X08001800       //

#define NEW_IMAGE_ADDRESS                                                                         0x08020000                                 

#define MAGIC_BYTE_ADDRESS                                                                        0x0803F800       

#define NEW_IMAGE_CHKSUM_ADDRESS                                                     0x0803F808      

#define NEW_IMAGE_PG_SIZE_ADDRESS                                                        0x0803F810                     

uint8_t flash_app_erase(uint32_t page_address, uint32_t no_of_pages)

{

 uint8_t ret_value = ERROR;

 FLASH_EraseInitTypeDef flash_struct;

 uint32_t flash_error;    

uint32_t first_page;

uint32_t bank_number; 

bank_number = get_bank(page_address);

first_page = GetPage(page_address);

 flash_struct.Page = first_page;

flash_struct.Banks = bank_number;

 flash_struct.TypeErase = FLASH_TYPEERASE_PAGES;

 flash_struct.NbPages = no_of_pages;

 if (HAL_FLASHEx_Erase(&flash_struct, &flash_error) == HAL_OK)

 {

   ret_value = SUCCESS;

 }

 return ret_value;

}

uint8_t flash_app_write(uint32_t flash_write_address, uint8_t* write_data ,uint16_t data_size)

{

uint16_t local_data_size = 0;

 uint16_t write_counter = 0;

 uint8_t ret_value = ERROR;

 uint64_t write_var;

 uint32_t temp_flash_addr = flash_write_address;

local_data_size = data_size/8;                                           // STM32G0 writes in double word(8 bytes)

if((data_size % 😎 != 0)

{

local_data_size += 1;

}                                           

 for(write_counter = 0; write_counter < local_data_size; write_counter++)

 {

   memcpy(&write_var,(uint8_t*)&write_data[8*write_counter],8);

   //the operation will be done by word

   if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,temp_flash_addr,write_var) == HAL_OK) {

     //Check the written value

     if(memcmp((uint8_t*)temp_flash_addr,(uint8_t*)&write_var,8) != 0) {

       break; // Error condition

     }

     temp_flash_addr += 8; // increment flash address

   }

else

{

temp_flash_addr = flash_write_address;

     break; // Error condition

   }

 }

 if(write_counter == local_data_size) {

   ret_value = SUCCESS;

 }

 return ret_value;

}