I am currently working with an STM32L552 microcontroller and encountering some peculiar behavior when writing and reading double words to and from the flash memory. After writing data to a specific set of addresses, I find that the data read back immediately after is all set to 0xFFFFFFFF (the erased state). However, the written data can be successfully read after either a power cycle, a system reset, or via gdb debugging. I suspected that this issue is related to the ICACHE being enabled.
I have found a workaround by disabling the ICACHE before executing erase/write operations and then re-enabling it during the read. While this approach works, I'm wondering if there is a more efficient way to manage flash write/read operations without having to disable the ICACHE. Is there a way to flush the ICACHE, or any other best practices to ensure data integrity while keeping the ICACHE enabled?
Update: I use the HAL libraries provided by STM32. The ICACHE settings are defaults from STM32CubeMX output. Within the hardware init:
HAL_ICACHE_Enable();
The following code is used to erase the pages.
HAL_StatusTypeDef status = HAL_FLASH_Unlock();
if (status != HAL_OK)
{
return false;
}
FLASH_EraseInitTypeDef pEraseInit = {
.TypeErase = FLASH_TYPEERASE_PAGES,
.Banks = FLASH_BANK_1,
.Page = BTL_CONFIG_PAGE_ID,
.NbPages = BTL_CONFIG_SECTOR_SIZE / FLASH_PAGE_SIZE,
};
uint32_t SectorError = 0;
status |= HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
status |= HAL_FLASH_Lock();
Writing data to the flash:
HAL_StatusTypeDef status = HAL_FLASH_Unlock();
status |= HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, configAddress, *data);
status |= HAL_FLASH_Lock();
Reading data from flash:
struct pt_package row3;
memcpy(&row3, (void*)(__IO struct pt_package *)configAddress, sizeof(struct pt_package));
Where the output of row3 is all 1s while ICACHE is enabled.
HAL_ICACHE_Invalidate
after write. \$\endgroup\$