cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_FLASH_ERROR_SIZ error when calling EE_Init on STM32L4 (Iot Discovery Node Kit)

DSabo
Associate III

I'm trying to init the EEPROM.. the code is returning HAL_FLASH_ERROR_SIZ from within FLASH_WaitForLastOperation.. I haven't really found much info about this error code, other than it is a size error... How do I fix it? I believe this used to work OK in my code.. I know the example binary works OK without issue.

void InitEEPROM(void)
{
	EE_Status ee_status = EE_OK;
 
	/* Enable and set FLASH Interrupt priority */
	/* FLASH interrupt is used for the purpose of pages clean up under interrupt */
	HAL_NVIC_SetPriority(FLASH_IRQn, 0, 0);
	HAL_NVIC_EnableIRQ(FLASH_IRQn);
 
	/* Unlock the Flash Program Erase controller */
	HAL_FLASH_Unlock();
 
	/* Configure Programmable Voltage Detector (PVD) (optional) */
	/* PVD interrupt is used to suspend the current application flow in case
	 a power-down is detected, allowing the flash interface to finish any
	 ongoing operation before a reset is triggered. */
	PVD_Config();
 
	/* Set user List of Virtual Address variables: 0x0000 and 0xFFFF values are prohibited */
	for (int i = 0; i < NB_OF_VARIABLES; i++)
	{
		VirtAddVarTab[i] = (uint16_t)(10*i + 1);
	}
 
	ee_status = EE_Init(VirtAddVarTab, EE_FORCED_ERASE);
 
	if(ee_status != EE_OK)
	{
		Error_Handler(__LINE__, __FILE__);
	}
 
	/* Set EEPROM emulation firmware to erase all potentially incompletely erased
	 pages if the system came from an asynchronous reset. Conditional erase is
	 safe to use if all Flash operations where completed before the system reset */
	if(__HAL_PWR_GET_FLAG(PWR_FLAG_SB) == RESET)
	{
		/* System reset comes from a power-on reset: Forced Erase */
		/* Initialize EEPROM emulation driver (mandatory) */
		ee_status = EE_Init(VirtAddVarTab, EE_FORCED_ERASE);
 
		if(ee_status != EE_OK)
		{
			Error_Handler(__LINE__, __FILE__);
		}
	}
	else
	{
		/* Clear the Standby flag */
		__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
 
		/* Check and Clear the Wakeup flag */
		if (__HAL_PWR_GET_FLAG(PWR_FLAG_WUF) != RESET)
		{
		  __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WUF);
		}
 
		/* System reset comes from a STANDBY wakeup: Conditional Erase*/
		/* Initialize EEPROM emulation driver (mandatory) */
		ee_status = EE_Init(VirtAddVarTab, EE_CONDITIONAL_ERASE);
 
		if(ee_status != EE_OK)
		{
			Error_Handler(__LINE__, __FILE__);
		}
	}
 
	LoadEEPROMValues();
 
	/* Lock the Flash Program Erase controller */
	HAL_FLASH_Lock();
 
}
 
 
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
{
  /* 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 */
    
  uint32_t tickstart = HAL_GetTick();
     
  while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) 
  { 
    if(Timeout != HAL_MAX_DELAY)
    {
      if((HAL_GetTick() - tickstart) >= Timeout)
      {
        return HAL_TIMEOUT;
      }
    } 
  }
  
  if((__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR))  || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR)) || 
     (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))  || 
     (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR))  ||
     (__HAL_FLASH_GET_FLAG(FLASH_FLAG_MISERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_FASTERR)) || 
     (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))  || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)) ||
#if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
    defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
    defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
     (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD))   || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PEMPTY)))
#else
     (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD)))
#endif
  {
    /*Save the error code*/
    FLASH_SetErrorCode();
    
    return HAL_ERROR;
  }

2 REPLIES 2
DSabo
Associate III

Erasing the chip through ST-Link Utility seems fine.

Piranha
Chief II

The if condition in lines 94-105 has been written by dumb code monkey...

And by the way, this code is from some old version. In current version that condition is not so stupid.