2019-11-05 07:51 AM
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
Solved! Go to Solution.
2019-11-05 10:13 AM
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)
2019-11-05 07:56 AM
ART disabled/flushed?
2019-11-05 08:07 AM
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.
2019-11-05 09:26 AM
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.
2019-11-05 10:13 AM
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)
2019-11-06 01:16 AM
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.