From 17e3449b30276fbe67a59077175074fbca8b3f3c Mon Sep 17 00:00:00 2001 From: Mert Vatansever Date: Mon, 16 Dec 2024 16:45:40 +0300 Subject: [PATCH] Fix flash ecc issue on MAX32657 Erasing flash will also erase the ECC bits. These bits are not updated until a flash write. Reading from erased memory will signal a ECC error that is falsely corrected from 0xFF to 0xFD on the 16th byte of each 128-bit line. This commit applies a workaround by setting the 16th byte of each line to 0xFF to handle this issue on max32657. Co-authored-by: Mert Vatansever Co-authored-by: Kevin Gillespie --- Libraries/PeriphDrivers/Source/FLC/flc_me30.c | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Libraries/PeriphDrivers/Source/FLC/flc_me30.c b/Libraries/PeriphDrivers/Source/FLC/flc_me30.c index 555a2b4422..1e975338f0 100644 --- a/Libraries/PeriphDrivers/Source/FLC/flc_me30.c +++ b/Libraries/PeriphDrivers/Source/FLC/flc_me30.c @@ -233,6 +233,38 @@ int MXC_FLC_Write(uint32_t address, uint32_t length, uint32_t *buffer) void MXC_FLC_Read(int address, void *buffer, int len) { MXC_FLC_Com_Read(address, buffer, len); + + /* ECC error detected */ + if (MXC_GCR->eccerr & MXC_F_GCR_ECCERR_FLASH) { + + /* Clear the ECC error */ + MXC_GCR->eccerr = MXC_F_GCR_ECCERR_FLASH; + + /* + * Erasing flash will also erase the ECC bits. These bits are not + * updated until a flash write. Reading from erased memory will + * signal a ECC error that is falsely corrected from 0xFF to 0xFD + * on the 16th byte of each 128-bit line. + * + * Workaround by setting the 16th byte of each line to 0xFF. + */ + + /* Get to the 16th byte of each line */ + uint32_t addrOffset = (0xF - (address % 0x10)); + uint8_t* buffer8 = buffer; + + while(addrOffset < len) { + /* Check for the erased flash ECC correction */ + if(buffer8[addrOffset] == 0xFD) { + buffer8[addrOffset] = 0xFF; + } else { + /* This could be an actual ECC error */ + break; + } + /* Skip to the next 16th byte */ + addrOffset += 0x10; + } + } } //******************************************************************************