Use Cases

Read and Write Emulated EEPROM Variables (FLITF, NVM)

This use case applies to both FLITF and NVM and shows how to read and write variables using the EEPROM Emulation module. It requires:

  • Initialization of the EEPROM Emulation module.

  • Writing variables.

  • Reading back the written variables to verify correctness.

The code required to implement this use case is as follows:

/* This example assumes EEPROM Emulation has already been initialized
 * (e.g. EE_Init() has been called successfully).
 */

ee_status status;
uint16_t data_counter = 0U;
uint8_t data_write[EE_NB_OF_VARIABLES + 1U];

/* Step 1: Write all 8-bit variables */
for (uint16_t var_id = 1U; var_id <= EE_NB_OF_VARIABLES; var_id++)
{
  data_counter++;
  data_write[var_id] = (uint8_t)var_id;

  status = EE_WriteVariable8bits(var_id, data_write[var_id]);

 /* FLITF only: a cleanup may be required when pages become full. */
  if (status == EE_INFO_CLEANUP_REQUIRED)
  {
    status = EE_CleanUp();
  }

  if (status != EE_OK)
  {
    /* Handle write error */
    break;
  }
}

/* Step 2: Read back and verify all variables */
for (uint16_t var_id = 1U; var_id <= EE_NB_OF_VARIABLES; var_id++)
{
  uint8_t data_read = 0U;

  status = EE_ReadVariable8bits(var_id, &data_read);
  if (status != EE_OK)
  {
    /* Handle read error */
    break;
  }

  if (data_read != data_write[var_id])
  {
    /* Handle data mismatch */
    break;
  }
}

Note

This code snippet is a minimal reference implementation. You can adapt and extend it (error handling, variable mapping, cleanup strategy, etc.) to match your application needs.

Run Progressive Cleanup (FLITF)

This use case applies to FLITF only and shows how to spread a page cleanup over several calls instead of blocking the application in a single EE_CleanUp(). It requires:

  • Initialization of the EEPROM Emulation module.

  • Writing variables and recording that a cleanup has been requested.

  • Calling EE_CleanUpProgressive() from the main loop until the cleanup is complete.

When a write function such as EE_WriteVariable8bits() returns EE_INFO_CLEANUP_REQUIRED, the variable has already been written and a page cleanup is requested. A full EE_CleanUp() may take too long for a time-critical application. EE_CleanUpProgressive() spreads the work over several calls, each bounded by the EE_CLEANUP_PROGRESSIVE_TIME_MS time budget, so the cleanup can run from the main loop without blocking other processing. Call it again as long as it returns EE_INFO_CLEANUP_REQUIRED; EE_OK indicates that the cleanup is complete.

The progressive cleanup relies on the TIME interface ( eeprom_itftime_gettick.c or eeprom_itftime_template.c) to measure each time slice. This interface is initialized automatically by EE_Init().

The code required to implement this use case is as follows:

/* This example assumes EEPROM Emulation has already been initialized
 * (e.g. EE_Init() has been called successfully) and uses the FLITF algorithm.
 */

ee_status status;
uint16_t data_counter = 0U;
uint8_t data_write[EE_NB_OF_VARIABLES + 1U];
uint8_t cleanup_pending = 0U;

/* Step 1: Write all 8-bit variables */
for (uint16_t var_id = 1U; var_id <= EE_NB_OF_VARIABLES; var_id++)
{
  data_counter++;
  data_write[var_id] = (uint8_t)var_id;

  status = EE_WriteVariable8bits(var_id, data_write[var_id]);

  /* FLITF only: the variable is written, the cleanup is deferred to the main loop. */
  if (status == EE_INFO_CLEANUP_REQUIRED)
  {
    cleanup_pending = 1U;
    status = EE_OK;
  }

  if (status != EE_OK)
  {
    /* Handle write error */
    break;
  }
}

/* Step 2: Run the deferred cleanup from the application main loop */
while (1)
{
  /* ... time-critical application processing ... */

  if (cleanup_pending != 0U)
  {
    /* Each call performs a bounded amount of work (EE_CLEANUP_PROGRESSIVE_TIME_MS). */
    status = EE_CleanUpProgressive();

    if (status == EE_OK)
    {
      /* Cleanup complete: the earlier writes already succeeded. */
      cleanup_pending = 0U;
    }
    else if (status != EE_INFO_CLEANUP_REQUIRED)
    {
      /* Handle cleanup error */
      break;
    }
  }
}

Note

This code snippet is a minimal reference implementation. You can adapt and extend it (error handling, variable mapping, cleanup strategy, etc.) to match your application needs.

List of EEPROM Emulation examples

The following table lists the available examples:

Example Name

Description

read_write

Demonstrates basic read and write operations using the EEPROM Emulation module. It initializes the module, writes variables, and reads them back to verify correctness.