Skip to main content
Visitor II
January 16, 2024
Question

FLASH_WaitForLastOperation() returning HAL_ERROR

  • January 16, 2024
  • 2 replies
  • 3475 views

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;
}

 

    This topic has been closed for replies.

    2 replies

    Super User
    January 16, 2024

    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.

    WBria.1Author
    Visitor II
    January 17, 2024

    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)
    Graduate II
    March 31, 2025

    Is there a way to solve it? I am not able to write into the flash. Now I am able to erase the flash page but I am unable to program the flash. 

    Graduate II
    January 16, 2024

    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 */

    WBria.1Author
    Visitor II
    January 17, 2024

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