Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Firmware/FFBoard/Src/flash_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,22 @@ cpp_freertos::MutexStandard flashMutex;
/**
* Formats the eeprom emulation section to delete all data
*/
/**
* Invalidate the ART data cache after flash operations.
* The STM32F4 ART accelerator caches flash reads on the D-bus.
* After erasing or programming flash, stale cached values would be
* returned unless the cache is explicitly reset. (See ST AN3969)
*/
static void Flash_InvalidateDataCache(){
__HAL_FLASH_DATA_CACHE_DISABLE();
__HAL_FLASH_DATA_CACHE_RESET();
__HAL_FLASH_DATA_CACHE_ENABLE();
}

bool Flash_Format(){
HAL_FLASH_Unlock();
bool res = (EE_Format() == HAL_OK);
Flash_InvalidateDataCache();
HAL_FLASH_Lock();
return res;
}
Expand All @@ -41,6 +54,7 @@ __weak bool Flash_Init(){
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGSERR);

bool state = (EE_Init() == EE_OK);
Flash_InvalidateDataCache();
HAL_FLASH_Lock();
return state;
}
Expand All @@ -61,9 +75,11 @@ bool Flash_Write(uint16_t adr,uint16_t dat){
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGPERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGSERR);
Flash_InvalidateDataCache(); // Invalidate before writing because EE_ReadVariable just cached the flash page

@Ultrawipf Ultrawipf Jun 13, 2026

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of invalidating both before and after the write only disable before writing and reset and reenable after writing. Or does it make sense to keep the cache active during the access?

The most likely cause is probably the init section if it occurs with fresh chips that have not been erased properly yet.

if(EE_WriteVariable(adr, dat) == HAL_OK){
res = true;
}
Flash_InvalidateDataCache();
HAL_FLASH_Lock();

}
Expand Down Expand Up @@ -97,7 +113,9 @@ bool Flash_ReadWriteDefault(uint16_t adr,uint16_t *buf,uint16_t def){
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGPERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGSERR);
Flash_InvalidateDataCache(); // Invalidate before writing because EE_ReadVariable just cached the flash page
EE_WriteVariable(adr, def);
Flash_InvalidateDataCache();
HAL_FLASH_Lock();
return false;
}
Expand Down
Loading