2025-01-24 05:33 AM - last edited on 2025-01-24 05:48 AM by Andrew Neil
Hi everyone
I'm using a STM32F401RCT6 to control a valve actuator
There is a counter of number of cycles for the actuator (RunTime), and when its value changes, the value of that counter must be stored in the memory for tracking and support.
The actuator is powered by an external power supply and additionally there's a battery in case the power fails.
In some rare cases, the memory storage process is corrupted, and we lose the complete storage values previously saved.
We are trying to replicate the fail, but it is really impossible
Do you have any idea of what can cause that loss of information in the memory and how could we replicate this failure in order to fix the bug
below is part of the code that writes to memory
Thank you very much
int WriteToFlash(bool force)
{
uint8_t NibleH=0;
uint8_t NibleL=0;
int16_t AuxData=0;
uint32_t Variable_Guardar;
uint32_t PAGError = 0;
FLASH_EraseInitTypeDef EraseInitStruct;
if((Entrada_Control_Seleccionada!=Problem || force) && Scale.VoltMain >20 ) //Force write in memory when a fail occurs
{
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.Banks = FLASH_BANK_1;
EraseInitStruct.Sector = FLASH_SECTOR_1;
EraseInitStruct.NbSectors = 1;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
HAL_FLASH_Unlock();
// Erase the USER_FLASH
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGError) != HAL_OK)
{
return 1;
}
// Write the delay value in flash memory //0x5051
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t) &BackUp_1, 0x5051) != HAL_OK)
{
return 1;
}
Index.Modo_Entrada=Entrada_Control_Seleccionada_M;
Variable_Guardar=AjustarDatos_8bits_ParaBackUp(0,0,Index.Modo_Entrada,Index.ModeWork);
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t) &BackUp_1+4, Variable_Guardar) != HAL_OK)
{
HAL_FLASH_Lock();
return 1;
}
AuxData=Scale.Temp_SP;//Save Anticondensation temperature value
NibleL=0xFF & AuxData;
NibleH=AuxData>>8;
Variable_Guardar=AjustarDatos_8bits_ParaBackUp(0,0,NibleH,NibleL);
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t) &BackUp_1+8, Variable_Guardar) != HAL_OK)
{
HAL_FLASH_Lock();
return 1;
}
Variable_Guardar=AjustarDatos_8bits_ParaBackUp(Index.FactorScal,TEMP_AlmHH,Index.LED,Index.Modo_Giro); //Save setting TEMP_AlmHH
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t) &BackUp_1+12, Variable_Guardar) != HAL_OK)
{
HAL_FLASH_Lock();
return 1;
}
Variable_Guardar=AjustarDatos_8bits_ParaBackUp(0,0,Index.LED_Update,Index.SP_Fault_Power); //LFH, 2023/05/26 Ver 2.1.0: Save setting LED_Update (indicate the state for the LED)
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t) &BackUp_1+16, Variable_Guardar) != HAL_OK)
{
HAL_FLASH_Lock();
return 1;
Variable_Guardar=RunTime;
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t) &BackUp_1+88, Variable_Guardar) != HAL_OK)
{
HAL_FLASH_Lock();
return 1;
}
.
.
.
HAL_FLASH_Lock();
return 0;
}else{
return 1;
}
}