Close

Code Overview 2: EEPROM Emulation

A project log for HEXABITZ - Modular Electronics for REAL

A new kind of electronic prototyping!

hexabitzHexabitz 04/03/2018 at 03:060 Comments

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 different between the MCU Flash and an EEPROM is that EEPROMs usually allow atomic access, i.e., storing, modifying and deleting a single byte/halfword/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 operation in the background (initialization, addressing. memory-wear leveling, etc.) The 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 1024 variables, each with 16-bit size and 16-bit virtual address. The BOS reserves at least 200 variables for system parameters. Users can use any remaining space to store 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 */
#define _EE_NBase                 1    
#define _EE_portDirBase           2    // Move to RO - 25 modules
#define _EE_aliasBase             28    // 25 modules
#define _EE_DMAStreamsBase        159                
#define _EE_ButtonBase            167    // 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_EmptyVarBase          207
#define _EE_ParamsBase            500    // Parameter base: BOS response
#define _EE_ParamsDebounce        501    // Parameter: Button debounce
#define _EE_ParamsSinClick        502    // Parameter: Button single click
#define _EE_ParamsDblClick        503    // Parameter: Button double click (inter-click min and max)

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

#define _EE_MyVar        207

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

status = EE_WriteVariable(VirtAddVarTab[_EE_MyVar], MyVarValue);

The write API returns a 16-bit status:

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

status = EE_ReadVariable(VirtAddVarTab[_EE_MyVar], &MyVarValue);

It returns another 16-bit status:

Discussions