1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Please post code you use for flashing and details about your configuration of ICACHE. Do you use LL or HAL or other API? Do you use generated project or do you write codes from scratch? \$\endgroup\$
    – Misaz
    Commented Nov 2, 2023 at 12:23
  • 1
    \$\begingroup\$ You will need to invalidate the cache for the addresses that you wrote. That will force a reload of the data when you read it. I don't use the STM line of microcontrollers, so you will have to determine how to invalidate the cache by reading the datasheet. \$\endgroup\$ Commented Nov 2, 2023 at 14:14
  • 1
    \$\begingroup\$ Try call HAL_ICACHE_Invalidate after write. \$\endgroup\$
    – Misaz
    Commented Nov 3, 2023 at 10:01

0

Browse other questions tagged or ask your own question.