STM32 Internal Flash Write/Read Failure
Hello everyone!
I'm writing a STM32F446RE-based flight computer for a rocket. I want to use internal flash memory to keep some key data like base pressure or flight time. So if a power failure happens, the system can keep the base data.
These are the two main functions that I use:
void writeInternalFlash()
{
uint32_t Flash_Address = 0x08060000;
HAL_FLASH_Unlock();
FLASH_Erase_Sector(FLASH_SECTOR_7,VOLTAGE_RANGE_3);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE,Flash_Address,systemStartCount); // These values are uint8_t
HAL_Delay(1);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE,(Flash_Address+4),systemState);
HAL_Delay(1);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,(Flash_Address+8),(uint32_t)previousFlightTime); // These values are float
HAL_Delay(1);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,(Flash_Address+12),(uint32_t)basePressure);
HAL_Delay(1);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,(Flash_Address+16),(uint32_t)offsetax);
HAL_Delay(1);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,(Flash_Address+20),(uint32_t)offsetay);
HAL_Delay(1);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,(Flash_Address+24),(uint32_t)offsetaz);
HAL_FLASH_Lock();
}
void readInternalFlash()
{
uint32_t Flash_Address = 0x08060000;
systemStartCount = *(uint8_t*) (Flash_Address);
if (systemStartCount >= 1) {
systemState = *(uint8_t*) (Flash_Address+4);
previousFlightTime = *(uint32_t*) (Flash_Address+8);
basePressure = *(uint32_t*) (Flash_Address+12);
offsetax = *(uint32_t*) (Flash_Address+16);
offsetay = *(uint32_t*) (Flash_Address+20);
offsetaz = *(uint32_t*) (Flash_Address+24);
}
}These are the steps that I follow, basically:
1-) I check the *systemStartCount* when the system is started. Doing this with the flash read function.
2-) If the system is never started before; I measure the base pressure, accelerometer offsets, etc. And systemStartCount++;
3-) But if the *systemStartCount* is greater than one, I take the data from flash.
4-) When it comes to the while loop, I measure the sensor data, check some algorithm things, and write the data to SD and internal flash.
The errors that I come across:
* It really doesn't work quite well, like sometimes it keeps the data and sometimes not.
* I encountered a lot of debugging failures on CubeIDE. Target not found, flash error, etc.
* It's slowing the while loop. Loop have about 100ms interval but when I add the read flash thing it becomes about 1 second.
And also I have a timer interrupt to get flightTime data. This one can affect the flash?
What can be the wrong thing that I do?
Thanks!
