cancel
Showing results for 
Search instead for 
Did you mean: 

FLASH_WaitForLastOperation() returning HAL_ERROR

WBria.1
Associate II

Hi.. i'm stuck with this problem for days... i'm not able to store basic data in the G030 flash memory...

When i tried to erase a flash page using HAL_FLASHEx_Erase(), the FLASH_WaitForLastOperation() returns a HAL_ERROR... I did not found the issue...

page to user data = 0x08007800 to 0x08007FFF (it's the last page for my STM32G030K6)

 

Here are the main.c code (i have created a blank project to test it):

 

int main(void)
{
  /* USER CODE BEGIN 1 */

	uint32_t FirstPage = 0, NbOfPages = 0;
	uint32_t Address = 0, PageError = 0;
	__IO uint32_t MemoryProgramStatus = 0;
	__IO uint32_t data32 = 0;
	static FLASH_EraseInitTypeDef EraseInitStruct;

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */

	HAL_FLASH_Unlock();

	FirstPage = GetPage(0x08007800);
	NbOfPages = GetPage(0x08007FFF) - FirstPage + 1;

	EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
	EraseInitStruct.Page      = FirstPage;
	EraseInitStruct.NbPages   = NbOfPages;

	if(HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
	{
		Error_Handler();
	}

	Address = 0x08007800;

	uint64_t data = 0x1234567812345678;
	if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, data) != HAL_OK)
	{
		Error_Handler();
	}
	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 */
}

 

 

Here is the error that i found when debugging (line 33):

HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
{
  uint32_t error;
  uint32_t tickstart = HAL_GetTick();

  /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
     Even if the FLASH operation fails, the BUSY flag will be reset and an error
     flag will be set */

#if defined(FLASH_DBANK_SUPPORT)
  error = (FLASH_SR_BSY1 | FLASH_SR_BSY2);
#else
  error = FLASH_SR_BSY1;
#endif /* FLASH_DBANK_SUPPORT */

  while ((FLASH->SR & error) != 0x00U)
  {
    if(Timeout != HAL_MAX_DELAY)
    {
      if ((HAL_GetTick() - tickstart) >= Timeout)
      {
        return HAL_TIMEOUT;
      }
    }
  }

  /* check flash errors */
  error = (FLASH->SR & FLASH_SR_ERRORS);

  /* Clear SR register */
  FLASH->SR = FLASH_SR_CLEAR;

  if (error != 0x00U) <<<<<<<<<<<<<< HERE THE error = 0xe0 <<<<<<<<<<<<<<<
  {
    /*Save the error code*/
    pFlash.ErrorCode = error;
    return HAL_ERROR;
  }

  /* Wait for control register to be written */
  while ((FLASH->SR & FLASH_SR_CFGBSY) != 0x00U)
  {
    if(Timeout != HAL_MAX_DELAY)
    {
      if ((HAL_GetTick() - tickstart) >= Timeout)
      {
        return HAL_TIMEOUT;
      }
    }
  }

  return HAL_OK;
}

 

4 REPLIES 4
TDK
Guru

You're not initializing the EraseInitStruct.Banks parameter.

What is the error code in pFlash.ErrorCode?

You may need to clear the error flags prior to the first call to HAL_FLASHEx_Erase.

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

Error PGS, SIZ, PGA

Would strongly suggest instrumenting rather than single-step debugging or memory view over FLASH peripheral / memory. This would avoid any misalignment or sequencing issues.

FLASH_EraseInitTypeDef EraseInitStruct = {0};

HAL_FLASH_Unlock();

FLASH->SR = FLASH_SR_CLEAR; /* Clear SR register */

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..

You're not initializing the EraseInitStruct.Banks parameter.

Lines below are added to initialize the struct and set bank:

  • FLASH_EraseInitTypeDef EraseInitStruct = {0};
  • EraseInitStruct.Banks = FLASH_BANK_1;

What is the error code in pFlash.ErrorCode?

0xe0

You may need to clear the error flags prior to the first call to HAL_FLASHEx_Erase.

FLASH->SR = FLASH_SR_CLEAR; //is that enough?

 

Notes for now:

  • I have changed HAL_FLASHEx_Erase() to FLASH_PageErase() and it works;
  • but when i try to HAL_FLASH_Program() the error persists... (it's the same pFlash.ErrorCode=0xe0)

Hi Tesla, i have answer on the above TDK comment some of your sugestion. tks for that

BTW, please, what should be a correct "alignment" for this flash operation using HAL_FLASH_Program()?