cancel
Showing results for 
Search instead for 
Did you mean: 

Error Code Writing to FLASH On STM32L496

jvavra
Associate III

I have a project based on the L496. The application does a few writes to FLASH. The initial write works correctly, however subsequent writes fail. The code is directly from STCube:

/**
  * @brief  Wait for a FLASH operation to complete.
  * @param  Timeout maximum flash operation timeout
  * @retval HAL_StatusTypeDef HAL Status
  */
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();
  uint32_t error;
 
  while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
  {
    if(Timeout != HAL_MAX_DELAY)
    {
      if((HAL_GetTick() - tickstart) >= Timeout)
      {
        return HAL_TIMEOUT;
      }
    }
  }
 
  error = (FLASH->SR & FLASH_FLAG_SR_ERRORS);
 
  if(error != 0u)               //Error code is 0xA8
  {
    /*Save the error code*/
    pFlash.ErrorCode |= error;
 
    /* Clear error programming flags */
    __HAL_FLASH_CLEAR_FLAG(error);
 
    return HAL_ERROR;
  }
 
  /* Check FLASH End of Operation flag  */
  if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  {
    /* Clear FLASH End of Operation pending bit */
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  }
 
  /* If there is an error flag set */
  return HAL_OK;
}

Some Googling mentions this error code may be related to trying to access a FLASH location that hasn't been cleared. However, in checking the contents at this address, memory shows it's filled with FFFFs as expected.

Anyone have any idea what the problem could be?

11 REPLIES 11
jvavra
Associate III

Additional info:

I have a project originally built for the STM32L432 that I am porting to the STM32L496. However,the HAL_FLASHEx_Erase function provided by ST as part of the HAL works on one device but not the other. The two FLASH spaces of these micros are organized virtually identically (same 72-bit wide data read/writes). Only difference is the L496 has a second bank of FLASH. I have seen some users run into issues with this; I am NOT attempting to use Bank 2 or come anywhere close to it; my last address is at 0x801FFFF.

I can manually erase the FLASH using the STCube Programmer, which fills it with FFs. This then satisfies the requirement for the FLASH to be "erased" before writing, and I can write one block of data. But I cannot modify it again via code. As soon as I write the first time, I cannot clear the block of data that I'd just written (verified using the Memory window view in IAR).

Again, exact same piece of code works for one L-series, but not another. Anyone have any ideas?

HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
{
  HAL_StatusTypeDef status = HAL_ERROR;
  uint32_t page_index = 0;
 
  /* Process Locked */
  __HAL_LOCK(&pFlash);
 
  /* Check the parameters */
  assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
 
  /* Wait for last operation to be completed */
  status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
 
if (status == HAL_OK)
{
  pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
 
  if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  {
    /* Mass erase to be done */
    FLASH_MassErase(pEraseInit->Banks);
 
    /* Wait for last operation to be completed */
    status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
 
#if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx)
  /* If the erase operation is completed, disable the MER1 and MER2 Bits */
  CLEAR_BIT(FLASH->CR, (FLASH_CR_MER1 | FLASH_CR_MER2));
#else
  /* If the erase operation is completed, disable the MER1 Bit */
  CLEAR_BIT(FLASH->CR, (FLASH_CR_MER1));
#endif      
}

TDK
Guru

Verify that your STM32L4xxx-type define is appropriate for the chip you're working on so that the correct CMSIS header gets included.

You haven't given the arguments you're passing to your functions.

Create a minimal working example that produces the error. Verify all HAL functions return HAL_OK and if not, specify when and where those occur.

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