Skip to content

Commit edcc439

Browse files
committed
Erase EE if major software version differs or can't be written
1 parent a76f816 commit edcc439

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

Firmware/FFBoard/Src/cppmain.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,25 @@ void cppmain() {
4747
#endif
4848

4949
// Flash init
50+
// TODO verify why or if flash does not erase or initialize correctly on some new chips
5051
HAL_FLASH_Unlock();
5152
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
53+
5254
if( EE_Init() != EE_OK){
5355
Error_Handler();
5456
}
57+
// Check if flash is initialized
58+
uint16_t lastVersion = 0;
59+
if(!Flash_Read(ADR_SW_VERSION, &lastVersion)){ // Version never written
60+
Flash_Write(ADR_SW_VERSION, (SW_VERSION_INT[0]<<8) | SW_VERSION_INT[1]);
61+
}
62+
Flash_Read(ADR_SW_VERSION,&lastVersion);
63+
if((lastVersion & 0xff00) != (SW_VERSION_INT[0]<<8)){
64+
EE_Format(); // Major version changed or could not write initial value. force a format
65+
Flash_Write(ADR_SW_VERSION, (SW_VERSION_INT[0]<<8) | SW_VERSION_INT[1]);
66+
}
5567
HAL_FLASH_Lock();
68+
// ------------------------
5669

5770
TIM_MICROS.Instance->CR1 = 1; // Enable microsecond clock
5871

0 commit comments

Comments
 (0)