From 58202a97f3608dd8dd67a226b49715e6357de3b7 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Fri, 30 Jul 2021 13:52:55 -0700 Subject: [PATCH] flash erase cache invalidate (#3073) Co-authored-by: Matthew Kennedy --- firmware/hw_layer/ports/stm32/flash_int.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/firmware/hw_layer/ports/stm32/flash_int.c b/firmware/hw_layer/ports/stm32/flash_int.c index 8d0e92ba94..478c9ac7ee 100644 --- a/firmware/hw_layer/ports/stm32/flash_int.c +++ b/firmware/hw_layer/ports/stm32/flash_int.c @@ -170,6 +170,13 @@ int intFlashErase(flashaddr_t address, size_t size) { } bool intFlashIsErased(flashaddr_t address, size_t size) { +#if CORTEX_MODEL == 7 + // If we have a cache, invalidate the relevant cache lines. + // They may still contain old data, leading us to believe that the + // flash erase failed. + SCB_InvalidateDCache_by_Addr((uint32_t*)address, size); +#endif + /* Check for default set bits in the flash memory * For efficiency, compare flashdata_t values as much as possible, * then, fallback to byte per byte comparison. */ @@ -211,6 +218,12 @@ bool intFlashCompare(flashaddr_t address, const char* buffer, size_t size) { } int intFlashRead(flashaddr_t address, char* buffer, size_t size) { +#if CORTEX_MODEL == 7 + // If we have a cache, invalidate the relevant cache lines. + // They may still contain old data, leading us to read invalid data. + SCB_InvalidateDCache_by_Addr((uint32_t*)address, size); +#endif + memcpy(buffer, (char*) address, size); return FLASH_RETURN_SUCCESS; }