Microchip® Advanced Software Framework

eeprom.c File Reference

SAM EEPROM Emulator.

Copyright (c) 2012-2018 Microchip Technology Inc. and its subsidiaries.

#include "eeprom.h"
#include <string.h>
#include <nvm.h>

Data Structures

struct  _eeprom_master_page
 Structure describing the EEPROM Emulation master page. More...
 
struct  _eeprom_module
 Internal device instance struct. More...
 
struct  _eeprom_page
 Structure describing emulated pages of EEPROM data. More...
 

Macros

#define EEPROM_MAGIC_KEY   {0x41744545, 0x50524f4d, 0x456d752e}
 
#define EEPROM_MAGIC_KEY_COUNT   3
 

Functions

static void _eeprom_emulator_check_logical_page (void)
 Check if there exist rows with same logical pages due to power drop when writing or erasing page. More...
 
static void _eeprom_emulator_create_master_page (void)
 Create master emulated EEPROM management page. More...
 
static void _eeprom_emulator_erase_invalid_page (uint16_t pre_phy_page, uint16_t next_phy_page)
 Erase one invalid page according to two invalid physical page. More...
 
static void _eeprom_emulator_format_memory (void)
 Initializes the emulated EEPROM memory, destroying the current contents. More...
 
static bool _eeprom_emulator_is_full_row (uint16_t phy_page)
 Check if a row is a full row because the page is a invalid page, so if two pages have data, it is the full row. More...
 
static bool _eeprom_emulator_is_page_free_on_row (const uint8_t start_physical_page, uint8_t *const free_physical_page)
 Finds the next free page in the given row if one is available. More...
 
static enum status_code _eeprom_emulator_move_data_to_spare (const uint8_t row_number, const uint8_t logical_page, const uint8_t *const data)
 Moves data from the specified logical page to the spare row. More...
 
static void _eeprom_emulator_nvm_commit_cache (const uint16_t physical_page)
 Commits the internal NVM controller page buffer to physical memory. More...
 
static void _eeprom_emulator_nvm_erase_row (const uint8_t row)
 Erases a given row within the physical EEPROM memory space. More...
 
static void _eeprom_emulator_nvm_fill_cache (const uint16_t physical_page, const void *const data)
 Fills the internal NVM controller page buffer in physical EEPROM memory space. More...
 
static void _eeprom_emulator_nvm_read_page (const uint16_t physical_page, void *const data)
 Reads a page of data stored in physical EEPROM memory space. More...
 
static void _eeprom_emulator_update_page_mapping (void)
 Creates a map in SRAM to translate logical EEPROM pages to physical FLASH pages. More...
 
static enum status_code _eeprom_emulator_verify_master_page (void)
 Verify the contents of a master EEPROM page. More...
 
 COMPILER_PACK_RESET ()
 
 COMPILER_PACK_SET (1)
 
enum status_code eeprom_emulator_commit_page_buffer (void)
 Commits any cached data to physical non-volatile memory. More...
 
void eeprom_emulator_erase_memory (void)
 Erases the entire emulated EEPROM memory space. More...
 
enum status_code eeprom_emulator_get_parameters (struct eeprom_emulator_parameters *const parameters)
 Retrieves the parameters of the EEPROM Emulator memory layout. More...
 
enum status_code eeprom_emulator_init (void)
 Initializes the EEPROM Emulator service. More...
 
enum status_code eeprom_emulator_read_buffer (const uint16_t offset, uint8_t *const data, const uint16_t length)
 Reads a buffer of data from the emulated EEPROM memory space. More...
 
enum status_code eeprom_emulator_read_page (const uint8_t logical_page, uint8_t *const data)
 Reads a page of data from an emulated EEPROM memory page. More...
 
enum status_code eeprom_emulator_write_buffer (const uint16_t offset, const uint8_t *const data, const uint16_t length)
 Writes a buffer of data to the emulated EEPROM memory space. More...
 
enum status_code eeprom_emulator_write_page (const uint8_t logical_page, const uint8_t *const data)
 Writes a page of data to an emulated EEPROM memory page. More...
 

Variables

static struct _eeprom_module _eeprom_instance
 Internal EEPROM emulator instance. More...
 

#define EEPROM_MAGIC_KEY   {0x41744545, 0x50524f4d, 0x456d752e}

Magic key is the sequence "AtEEPROMEmu." in ASCII. The key is encoded as a sequence of 32-bit values to speed up checking of the key, which can be implemented as a number of simple integer comparisons,

Referenced by _eeprom_emulator_create_master_page(), and _eeprom_emulator_verify_master_page().

#define EEPROM_MAGIC_KEY_COUNT   3

Length of the magic key, in 32-bit elements.

Referenced by _eeprom_emulator_create_master_page(), and _eeprom_emulator_verify_master_page().

static void _eeprom_emulator_check_logical_page ( void  )
static

Check if there exist rows with same logical pages due to power drop when writing or erasing page.

when existed same logical page, the old(full) row will be erased.

References _eeprom_emulator_erase_invalid_page(), _eeprom_module::flash, _eeprom_page::header, _eeprom_page::logical_page, and _eeprom_module::physical_pages.

Referenced by _eeprom_emulator_update_page_mapping().

static void _eeprom_emulator_create_master_page ( void  )
static
static void _eeprom_emulator_erase_invalid_page ( uint16_t  pre_phy_page,
uint16_t  next_phy_page 
)
static

Erase one invalid page according to two invalid physical page.

Parameters
[in]pre_phy_pageOne physical invalid page
[in]next_phy_pageAnother physical invalid page

References _eeprom_emulator_is_full_row(), and _eeprom_emulator_nvm_erase_row().

Referenced by _eeprom_emulator_check_logical_page().

static void _eeprom_emulator_format_memory ( void  )
static
static bool _eeprom_emulator_is_full_row ( uint16_t  phy_page)
static

Check if a row is a full row because the page is a invalid page, so if two pages have data, it is the full row.

Parameters
[in]phy_pagePhysical page that in a row

References _eeprom_module::flash, _eeprom_page::header, and _eeprom_page::logical_page.

Referenced by _eeprom_emulator_erase_invalid_page().

static bool _eeprom_emulator_is_page_free_on_row ( const uint8_t  start_physical_page,
uint8_t *const  free_physical_page 
)
static

Finds the next free page in the given row if one is available.

Parameters
[in]start_physical_pagePhysical FLASH page index of the row to search
[out]free_physical_pageIndex of the physical FLASH page that is currently free (if one was found)
Returns
Whether a free page was found in the specified row.
Return values
\ctrue If a free page was found
\cfalse If the specified row was full and needs an erase

References _eeprom_module::flash, _eeprom_page::header, and _eeprom_page::logical_page.

Referenced by eeprom_emulator_write_page().

static enum status_code _eeprom_emulator_move_data_to_spare ( const uint8_t  row_number,
const uint8_t  logical_page,
const uint8_t *const  data 
)
static

Moves data from the specified logical page to the spare row.

Moves the contents of the specified row into the spare row, so that the original row can be erased and re-used. The contents of the given logical page is replaced with a new buffer of data.

Parameters
[in]row_numberPhysical row to examine
[in]logical_pageLogical EEPROM page number in the row to update
[in]dataNew data to replace the old in the logical page
Returns
Status code indicating the status of the operation.

References _eeprom_emulator_nvm_erase_row(), _eeprom_emulator_nvm_fill_cache(), _eeprom_emulator_nvm_read_page(), _eeprom_module::cache, _eeprom_module::cache_active, _eeprom_page::data, eeprom_emulator_commit_page_buffer(), EEPROM_PAGE_SIZE, _eeprom_module::flash, _eeprom_page::header, _eeprom_page::logical_page, _eeprom_module::page_map, _eeprom_module::spare_row, and STATUS_OK.

Referenced by eeprom_emulator_write_page().

static void _eeprom_emulator_nvm_commit_cache ( const uint16_t  physical_page)
static

Commits the internal NVM controller page buffer to physical memory.

Parameters
[in]physical_pagePhysical page in EEPROM space to commit

References _eeprom_module::flash, NVM_COMMAND_WRITE_PAGE, nvm_execute_command(), STATUS_BUSY, and STATUS_OK.

Referenced by _eeprom_emulator_create_master_page(), _eeprom_emulator_format_memory(), and eeprom_emulator_commit_page_buffer().

static void _eeprom_emulator_nvm_erase_row ( const uint8_t  row)
static

Erases a given row within the physical EEPROM memory space.

Parameters
[in]rowPhysical row in EEPROM space to erase

References _eeprom_module::flash, nvm_erase_row(), STATUS_BUSY, and STATUS_OK.

Referenced by _eeprom_emulator_create_master_page(), _eeprom_emulator_erase_invalid_page(), _eeprom_emulator_format_memory(), and _eeprom_emulator_move_data_to_spare().

static void _eeprom_emulator_nvm_fill_cache ( const uint16_t  physical_page,
const void *const  data 
)
static

Fills the internal NVM controller page buffer in physical EEPROM memory space.

Parameters
[in]physical_pagePhysical page in EEPROM space to fill
[in]dataData to write to the physical memory page

References _eeprom_module::flash, nvm_write_buffer(), STATUS_BUSY, and STATUS_OK.

Referenced by _eeprom_emulator_create_master_page(), _eeprom_emulator_format_memory(), _eeprom_emulator_move_data_to_spare(), and eeprom_emulator_write_page().

static void _eeprom_emulator_nvm_read_page ( const uint16_t  physical_page,
void *const  data 
)
static

Reads a page of data stored in physical EEPROM memory space.

Parameters
[in]physical_pagePhysical page in EEPROM space to read
[out]dataDestination buffer to fill with the read data

References _eeprom_module::flash, nvm_read_buffer(), STATUS_BUSY, and STATUS_OK.

Referenced by _eeprom_emulator_move_data_to_spare(), _eeprom_emulator_verify_master_page(), and eeprom_emulator_read_page().

static void _eeprom_emulator_update_page_mapping ( void  )
static
static enum status_code _eeprom_emulator_verify_master_page ( void  )
static

Verify the contents of a master EEPROM page.

Verify the contents of a master EEPROM page to ensure that it contains the correct information for this version of the EEPROM emulation service.

Return values
STATUS_OKGiven master page contents is valid
STATUS_ERR_BAD_FORMATMaster page contents was invalid
STATUS_ERR_IOMaster page indicates the data is incompatible with this version of the EEPROM emulator

References _eeprom_emulator_nvm_read_page(), EEPROM_EMULATOR_ID, EEPROM_MAGIC_KEY, EEPROM_MAGIC_KEY_COUNT, EEPROM_MAJOR_VERSION, EEPROM_MINOR_VERSION, _eeprom_master_page::emulator_id, _eeprom_master_page::magic_key, _eeprom_master_page::major_version, _eeprom_master_page::minor_version, STATUS_ERR_BAD_FORMAT, STATUS_ERR_IO, and STATUS_OK.

Referenced by eeprom_emulator_init().

COMPILER_PACK_RESET ( )
COMPILER_PACK_SET ( )

struct _eeprom_module _eeprom_instance
static
Initial value:
= {
.initialized = false,
}

Internal EEPROM emulator instance.