2026-01-07 8:06 AM - last edited on 2026-01-07 8:19 AM by Andrew Neil
I'm using the X-CUBE-EEPROM library on my STM32C031G6Ux to store keys in flash memory. I configured eeprom_emul_conf.h so that the emulated EEPROM starts at 0x08007000, with 2 pages of 2 KB each, and set NB_OF_VARIABLES to 240.
When I tried saving 240 variables once using EE_WriteVariable32bits and reading them back with EE_ReadVariable8bits, everything worked fine.
However, when I repeat this process three times, writing stops once both pages are full, and the data is no longer read correctly.
I experienced similar issues even with 120 variables. Interestingly, adding EE_CleanUp after all writes solved the problem for 120 variables.
I then tried the same approach with 240 variables, calling EE_CleanUp after all write operations, but it still didn’t work.
Is there a reason for this behavior? Am I limited to storing only 120 variables in flash?
2026-01-28 3:27 AM
Hello @JVSS_Cermob ,
Based on your description and based on the configurations you've provided, the page definitions should look like this for you:
/* Page definitions */
#define PAGE_SIZE FLASH_PAGE_SIZE /*!< Page size */
#define PAGE_HEADER_SIZE EE_ELEMENT_SIZE * 4U /*!< Page Header is 4 elements to save page state */
#define NB_MAX_ELEMENTS_BY_PAGE ((PAGE_SIZE - PAGE_HEADER_SIZE) / EE_ELEMENT_SIZE) /*!< Max number of elements by page */
#define PAGES_NUMBER 2 // (Set to 2 pages instead) (((((NB_OF_VARIABLES + NB_MAX_ELEMENTS_BY_PAGE - 1U) / NB_MAX_ELEMENTS_BY_PAGE) * 2U) * CYCLES_NUMBER) + GUARD_PAGES_NUMBER )
/*!< Number of consecutives pages used by the application */
#define NB_MAX_WRITTEN_ELEMENTS ((NB_MAX_ELEMENTS_BY_PAGE * PAGES_NUMBER) / 2U) /*!< Max number of elements written before triggering pages transfer */
#define START_PAGE PAGE(START_PAGE_ADDRESS) /*!< Page index of the 1st page used for EEPROM emul, in the bank */With this setup, and keeping the main.c logic from X‑CUBE‑EEPROM unchanged:
However, with a 2 KB page and NB_OF_VARIABLES = 240:
With only two pages, this creates a ping‑pong effect:
Because you always have a small extra capacity after each full 240‑variable snapshot, the most recent writes of the third round can be the ones that finally dominate what gets transferred. After multiple rounds, the final page content may contain a mixture of values from different rounds (earlier and later updates), but this is still consistent with the X‑CUBE‑EEPROM logic and should not break the internal data‑integrity checks.
Kind regards,