1. Home
  2. Docs
  3. Code Overview
  4. EEPROM Emulation

EEPROM Emulation

STM32F0 MCUs in standard Hexabitz modules do not have a dedicated EEPROM for non-volatile (i.e., permanent) storage. The MCU Flash memory, however, is used to emulate an actual EEPROM to store system and user parameters. The main difference between the MCU Flash and an EEPROM is that EEPROMs usually allow atomic access, i.e., storing, modifying and deleting a single byte/half word/word, while MCU Flash memory can be erased only page by page (one page is 2 KBytes in STM32F09 MCUs). A dedicated code in BOS_eeprom.c/.h solves this problem by using a double-page system and handles all operations in the background (initialization, addressing, memory-wear leveling, etc.) MCU Flash memory can retain data up to 10-20 years and guarantee up to 10000 erase cycle (i.e., 10000 EEPROM variable updates).

Current emulated-EEPROM has a capacity to hold up to 1024 variables, each with 16-bit size and 16-bit virtual address. The EEPROM virtual addressing space is divided into three sections (BOS_eeprom.h):

  1. BOS addressing space (1-499): This space is shared between all modules. It holds BOS parameters and other information related to topology and module and group names, among others. Note that the actual topology routing table is not stored in the emulated EEPROM, but rather in a Flash read-only (RO) area.
  2. Module addressing space (500-599): It holds module parameters. The virtual addresses for these parameters are defined in the module header file (e.g., H01R0.h).
  3. User addressing section (600-1024): Can be used to store any non-volatile user variables.

BOS reserves about 300 variables for system and module parameters. Users can use remaining space to store their non-volatile data. You can check out available virtual addresses in BOS_eeprom.h as shown below. Note that virtual addresses are arbitrary, and they do not have to be adjacent as long as they are unique (and separated by enough distance in case the variable is larger than 16 bits).

/* EEPROM virtual addresses - Consider MaxNumOfModules is 25 */
// BOS Addressing Space 1 - 499
#define _EE_NBASE			1	
#define _EE_PORT_DIR_BASE		2	// To-do: Move to RO - 25 modules - 25 variables
#define _EE_ALIAS_BASE			28	// 25 modules/10 chars - 125 variables
#define _EE_GROUP_ALIAS_BASE		153	// 10 groups/10 chars - 50 variables
#define _EE_GROUP_MODULES_BASE		203	// 25 modules - 25 variables
#define _EE_DMA_STREAM_BASE		228	// 8 variables
#define _EE_BUTTON_BASE			236	// 4 * MaxNumOfPorts (10) variables for buttons: port(4 bits), type (4 bits), events (8 bits): pressed_for_x_1 (8 bits), released_for_y_1 (8 bits), etc.
#define _EE_PARAMS_BASE			276	// Parameter base: BOS trace (MSB) | BOS response - 1 variable 
#define _EE_PARAMS_DEBOUNCE		277	// Parameter: Button debounce - 1 variable
#define _EE_PARAMS_SINGLE_CLICK		278	// Parameter: Button single click - 1 variable
#define _EE_PARAMS_DBL_CLICK		279	// Parameter: Button double-click (inter-click min and max) - 1 variable
#define _EE_CLI_BAUD			280	// Parameter: CLI baudrate - LSB halfword, MSB halfword - 2 variables
#define _EE_PARAMS_RTC			282	// Parameter: RTC hourformat | RTC daylightsaving - 1 variable

// Module Addressing Space 500 - 599
#define _EE_MODULE_BASE			500

// User Addressing Space 600 - 1024
#define _EE_EMPTY_VAR_BASE		600

To add a new variable, just assign a unique virtual address in project.h:

#define _EE_MyVar        607

Use the following API to store a new value to the emulated-EEPROM:

status = EE_WriteVariable(_EE_MyVar, MyVarValue);

The EE_WriteVariable API returns a 16-bit status:

  • 0x0000: Variable was written successfully.
  • 0x00AB: No valid page was found.
  • Any other codes: Flash write failed.

Read the stored value using this API (where &MyVarValue is the address of your variable in RAM):

status = EE_ReadVariable(_EE_MyVar, &MyVarValue);

It returns another 16-bit status:

  • 0x0000: Variable was found and read successfully.
  • 0x0001: Variable was not found.
  • 0x00AB: No valid page was found.

How can we help?