1

I have a project originally built for the STM32L432 that I am porting to the STM32L496. However, I am having an issue where 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      
}
else
{
  /*Initialization of PageError variable*/
  *PageError = 0xFFFFFFFF;
  
  for(page_index = pEraseInit->Page; page_index < (pEraseInit->Page + pEraseInit->NbPages); page_index++)
  {
    FLASH_PageErase(page_index, pEraseInit->Banks);

    /* Wait for last operation to be completed */
    status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);

    /* If the erase operation is completed, disable the PER Bit */
    CLEAR_BIT(FLASH->CR, (FLASH_CR_PER | FLASH_CR_PNB));

    if (status != HAL_OK)
    {
      /* In case of error, stop erase procedure and return the faulty address */
      *PageError = page_index;
      break;
    }
  }
}

/* Flush the caches to be sure of the data consistency */
FLASH_FlushCaches();
}

/* Process Unlocked */
__HAL_UNLOCK(&pFlash);

return status;

}

nobby
  • 373
  • 1
  • 3
  • 15
  • What error does HAL_FLASHEx_Erase() return? Did you recompile the HAL with STM32L496xx defined? Is the CPU executing code from the same flash/bank that is being erased? If so is that supported? – kkrambo Apr 12 '22 at 20:49
  • Try closing the debugger memory window during the erase. The debugger's commands to read from the flash may interfere with the flash erase/program commands. – kkrambo Apr 12 '22 at 20:52
  • @kkrambo - It returns no error, it thinks it worked. Yes, I built the HAL with the L496 as the target. The application code runs in a different section of the same FLASH bank, but I'm only trying to write to 1-2 pages (2k-4k). The application code lives quite a ways away from where this data is trying to be stored. – nobby Apr 12 '22 at 21:00
  • Check that the write protect option bytes have not been set. – Clifford Apr 13 '22 at 07:16
  • @Clifford No write protection enabled – nobby Apr 13 '22 at 11:59
  • @kkrambo - I tried this step of leaving the memory window closed until after the ErasePage function call. No change unfortunately. – nobby Apr 13 '22 at 12:41
  • 1
    You provided the internals of the library function. Please also show how you call it in your code. – Tagli Apr 16 '22 at 06:19
  • Review the examples is the STM32CubeL4 software package. It includes flash erase examples for both the NUCLEO-L496ZG and NUCLEO-L432KC. The examples appear to be basically the same. The problem is unlikely to be in library function and you haven't shared your calling code. – kkrambo Apr 18 '22 at 14:36

0 Answers0