2026-04-29 5:20 AM - edited 2026-04-29 5:24 AM
Hello,
I am using X-CUBE-EEPROM as a mean to store variable in internal FLASH memory on STM32U5G9-DK2 board. Here is my function where a variable in flash:
static FLASH_STATUS flash_slot_write_shared_key(const FLASH_INDEX *index, uint8_t slot, uint8_t *shared_key) {
if (slot > FLASH_SLOTS_NUMBER)
return FLASH_WRONG_PARAM;
if (shared_key == NULL)
return FLASH_NULL_PARAM;
EE_Status ee_status = EE_WRITE_ERROR;
HAL_FLASH_Unlock();
taskENTER_CRITICAL();
ee_status = EE_WriteVariable96bits((uint16_t)index->entries[slot].addr, (uint64_t *)(shared_key));
if (ee_status != EE_OK)
return FLASH_ERROR;
ee_status = EE_WriteVariable96bits((uint16_t)index->entries[slot].addr + 1, (uint64_t *)(shared_key + 12));
if (ee_status != EE_OK)
return FLASH_ERROR;
ee_status = EE_WriteVariable32bits((uint16_t)index->entries[slot].addr + 2, *(uint32_t *)(shared_key + 24));
if (ee_status != EE_OK)
return FLASH__ERROR;
ee_status = EE_WriteVariable32bits((uint16_t)index->entries[slot].addr + 3, *(uint32_t *)(shared_key + 28));
if (ee_status != EE_OK)
return FLASH_ERROR;
taskEXIT_CRITICAL();
HAL_FLASH_Lock();
return FLASH_OK;
}Here is the function to read back the variable:
static FLASH_STATUS flash_slot_get_shared_key(const FLASH_INDEX *index, uint8_t slot, uint8_t *shared_key) {
if (index == NULL || shared_key == NULL)
return FLASH_NULL_PARAM;
if (slot > FLASH_SLOTS_NUMBER)
return FLASH_WRONG_PARAM;
EE_Status ee_status = EE_WRITE_ERROR;
uint8_t tmp[32] = {0};
memset(tmp, 0, 32);
HAL_FLASH_Unlock();
HAL_ICACHE_Invalidate(); // TODO: Configure MPU
ee_status = EE_ReadVariable96bits((uint16_t)index->entries[slot].addr, (uint64_t *)tmp);
if (ee_status != EE_OK)
return FLASH_ERROR;
ee_status = EE_ReadVariable96bits((uint16_t)index->entries[slot].addr + 1, (uint64_t *)(tmp + 12));
if (ee_status != EE_OK)
return FLASH_ERROR;
ee_status = EE_ReadVariable32bits((uint16_t)index->entries[slot].addr + 2, (uint32_t *)(tmp + 24));
if (ee_status != EE_OK)
return FLASH_ERROR;
ee_status = EE_ReadVariable32bits((uint16_t)index->entries[slot].addr + 3, (uint32_t *)(tmp + 28));
if (ee_status != EE_OK)
return FLASH_ERROR;
HAL_FLASH_Lock();
memcpy(shared_key, tmp, 32);
return FLASH_OK;
}Once I write a key to the defined slot, if I try to update it, I still get the first written value in memory. How I am supposed to update the variable. Am I misusing the API ?
2026-04-30 2:45 AM
Hello,
On AN4894 https://www.st.com/resource/en/application_note/an4894-how-to-use-eeprom-emulation-on-stm32-mcus-stmicroelectronics.pdf the data update flow is described in Figure 5.
If you update a variable, the first value of this variable is still written in flash as long as the first page is not full.
BR
Chloé