cancel
Showing results for 
Search instead for 
Did you mean: 

Internal flash failing after 60 - 70 erase/write cycles

JHedges
Associate II

Hi,

I am currently facing an issue on a STM32F469NI MCU where the HAL_FLASH_PROGRAM() is succeeding yet fails to write to the flash memory.

I have verified this with a basic project for the STM32F469Discovery to isolate hardware. It appears that after 60 - 70 cycles of erasing a sector and writing words the flash write does not actually commit anything. The sector can only be recovered into a writable state if a full chip erase is performed through the ST-Link Utility. When the MCU is in this fail state there are no flags set in the registers (no error and program complete) and the HAL_FLASH_PROGRAM() returns HAL_OK. If the address is read either through a memory viewer or during code execution the memory address is still in the reset (erased) state.

Here is the test code:

   const uint32_t dataToProgram = 0x54329876;
   const uint32_t sect23Addr = 0x81E0000;
 
   while(1)
   {
      HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
      HAL_FLASH_Unlock();
 
      uint32_t u32SectorErr = 0; 
      FLASH_EraseInitTypeDef tDef;
      tDef.TypeErase = FLASH_TYPEERASE_SECTORS;
      tDef.Banks = 0;
      tDef.NbSectors = 1;
      tDef.Sector = 23;
      tDef.VoltageRange = FLASH_VOLTAGE_RANGE_4;
 
      if (HAL_FLASHEx_Erase(&tDef, &u32SectorErr) != HAL_OK)
      {
         ErrorHandler();
      }
 
      HAL_FLASH_Lock();
      HAL_FLASH_Unlock();
 
      if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, sect23Addr, dataToProgram) != HAL_OK)
      {
         ErrorHandler();
      }
 
      HAL_FLASH_Lock();
 
      uint32_t data = (*(volatile uint32_t*)sect23Addr);
 
      if(data != dataToProgram)
      {
         ErrorHandler();
      }
   }

The test always fails on the data comparison, all HAL functions return OK with no error flags in FLASH->SR.

Thanks in advance.

Jack

1 ACCEPTED SOLUTION

Accepted Solutions
Piranha
Chief II

If mass erase fixes the problem, then it should be the sector erase operation which is failing. And indeed, if you have not modified the board, then you are using the wrong voltage range. You must use range 3 not 4.

  *            @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
  *                                  the operation will be done by word (32-bit)
  *            @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp, 
  *                                  the operation will be done by double word (64-bit)

https://github.com/STMicroelectronics/STM32CubeF4/blob/b5abca20c9676b04f8d2885a668a9b653ee65705/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c#L965

View solution in original post

5 REPLIES 5

ART disabled/flushed?

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

Prefetch, Instruction Cache and Data Cache all enabled at the time of writing.

HAL_FLASHEx_Erase() does a IC and DC flush after the erase has completed.

Prefetch, Instruction Cache and Data Cache all enabled at the time of writing.

HAL_FLASHEx_Erase() does a IC and DC flush after the erase has completed.

Piranha
Chief II

If mass erase fixes the problem, then it should be the sector erase operation which is failing. And indeed, if you have not modified the board, then you are using the wrong voltage range. You must use range 3 not 4.

  *            @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
  *                                  the operation will be done by word (32-bit)
  *            @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp, 
  *                                  the operation will be done by double word (64-bit)

https://github.com/STMicroelectronics/STM32CubeF4/blob/b5abca20c9676b04f8d2885a668a9b653ee65705/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c#L965

Unfortunately mass erase is not an option for us as we require a portion of the flash to be maintined as a custom bootloader.

Thankyou, setting the voltage range to 3 means the flash erase/write no longer fails.