Microchip® Advanced Software Framework

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NVM driver EEPROM handling

Functions for handling internal EEPROM memory.

The internal EEPROM can be used to store data that will persist after power is removed. This can typically be used to store calibration data, application state, encryption keys or other data that need to be preserved when power is removed.

The functions in this module uses IO register access to manipulate the EEPROM.

Note
The functions in this module are modifying the NVM.CMD register. If the application are using program space access in interrupts (__flash pointers in IAR EW or pgm_read_byte in GCC) interrupts needs to be disabled when running EEPROM access functions. If not the program space reads will be corrupted.

Typedefs

typedef uint16_t eeprom_addr_t
 Data type for holding eeprom memory addresses. More...
 

Functions

static void eeprom_disable_mapping (void)
 Disable EEPROM mapping into data space. More...
 
static void eeprom_enable_mapping (void)
 Enable EEPROM mapping into data space. More...
 
void nvm_eeprom_atomic_write_page (uint8_t page_addr)
 Erase and write bytes from page buffer into EEPROM. More...
 
void nvm_eeprom_erase_all (void)
 Erase entire EEPROM memory. More...
 
void nvm_eeprom_erase_and_write_buffer (eeprom_addr_t address, const void *buf, uint16_t len)
 Write buffer within the eeprom. More...
 
void nvm_eeprom_erase_bytes_in_all_pages (void)
 Erase bytes from all EEPROM pages. More...
 
void nvm_eeprom_erase_bytes_in_page (uint8_t page_addr)
 Erase bytes from EEPROM page. More...
 
void nvm_eeprom_erase_page (uint8_t page_addr)
 Erase EEPROM page. More...
 
void nvm_eeprom_fill_buffer_with_value (uint8_t value)
 Fill temporary EEPROM page buffer with value. More...
 
void nvm_eeprom_flush_buffer (void)
 Flush temporary EEPROM page buffer. More...
 
void nvm_eeprom_load_byte_to_buffer (uint8_t byte_addr, uint8_t value)
 Load single byte into temporary page buffer. More...
 
void nvm_eeprom_load_page_to_buffer (const uint8_t *values)
 Load entire page into temporary EEPROM page buffer. More...
 
void nvm_eeprom_read_buffer (eeprom_addr_t address, void *buf, uint16_t len)
 Read buffer within the eeprom. More...
 
uint8_t nvm_eeprom_read_byte (eeprom_addr_t addr)
 Read one byte from EEPROM using mapped access. More...
 
void nvm_eeprom_split_write_page (uint8_t page_addr)
 Write (without erasing) EEPROM page. More...
 
void nvm_eeprom_write_byte (eeprom_addr_t address, uint8_t value)
 Write one byte to EEPROM using IO mapping. More...
 

typedef uint16_t eeprom_addr_t

Data type for holding eeprom memory addresses.

static void eeprom_disable_mapping ( void  )
inlinestatic

Disable EEPROM mapping into data space.

This macro disables mapping of EEPROM into data space. IO mapped access is now enabled.

Referenced by nvm_eeprom_load_byte_to_buffer(), nvm_eeprom_read_buffer(), and nvm_eeprom_read_byte().

static void eeprom_enable_mapping ( void  )
inlinestatic

Enable EEPROM mapping into data space.

This macro enables mapping of EEPROM into data space. EEPROM starts at EEPROM_START in data memory. Read access can be done similar to ordinary SRAM access.

Note
This disables IO-mapped access to EEPROM, although page erase and write operations still needs to be done through IO register.

Referenced by nvm_eeprom_load_byte_to_buffer(), nvm_eeprom_read_buffer(), and nvm_eeprom_read_byte().

void nvm_eeprom_atomic_write_page ( uint8_t  page_addr)

Erase and write bytes from page buffer into EEPROM.

This function writes the contents of an already loaded EEPROM page buffer into EEPROM memory.

As this is an atomic write, the page in EEPROM will be erased automatically before writing. Note that only the page buffer locations that have been loaded will be used when writing to EEPROM. Page buffer locations that have not been loaded will be left untouched in EEPROM.

Parameters
page_addrEEPROM Page address, between 0 and EEPROM_SIZE/EEPROM_PAGE_SIZE

References Assert, nvm_issue_command(), and nvm_wait_until_ready().

Referenced by nvm_eeprom_erase_and_write_buffer(), run_eeprom_atomic_write_test(), run_eeprom_split_write_test(), save_gamedata(), test_atomic_write(), and test_split_write().

void nvm_eeprom_erase_all ( void  )

Erase entire EEPROM memory.

This function erases the entire EEPROM memory block to 0xFF.

References nvm_eeprom_erase_bytes_in_all_pages(), and nvm_eeprom_fill_buffer_with_value().

Referenced by run_eeprom_erase_test(), and test_erase().

void nvm_eeprom_erase_and_write_buffer ( eeprom_addr_t  address,
const void *  buf,
uint16_t  len 
)

Write buffer within the eeprom.

Parameters
addressthe address to where to write
bufpointer to the data
lenthe number of bytes to write

References nvm_eeprom_atomic_write_page(), nvm_eeprom_load_page_to_buffer(), and nvm_eeprom_write_byte().

Referenced by nvm_write(), and run_eeprom_write_buffer_test().

void nvm_eeprom_erase_bytes_in_all_pages ( void  )

Erase bytes from all EEPROM pages.

This function erases bytes from all EEPROM pages, so that every location written to in the page buffer reads 0xFF.

References nvm_issue_command(), and nvm_wait_until_ready().

Referenced by nvm_eeprom_erase_all(), run_eeprom_erase_byte_test(), and test_erase_bytes().

void nvm_eeprom_erase_bytes_in_page ( uint8_t  page_addr)

Erase bytes from EEPROM page.

This function erases bytes from one EEPROM page, so that every location written to in the page buffer reads 0xFF.

Parameters
page_addrEEPROM Page address, between 0 and EEPROM_SIZE/EEPROM_PAGE_SIZE

References Assert, nvm_issue_command(), and nvm_wait_until_ready().

Referenced by nvm_eeprom_erase_page(), run_eeprom_erase_byte_test(), run_eeprom_read_buffer_test(), run_eeprom_write_buffer_test(), and test_erase_bytes().

void nvm_eeprom_erase_page ( uint8_t  page_addr)

Erase EEPROM page.

This function erases one EEPROM page, so that every location reads 0xFF.

Parameters
page_addrEEPROM Page address, between 0 and EEPROM_SIZE/EEPROM_PAGE_SIZE

References nvm_eeprom_erase_bytes_in_page(), and nvm_eeprom_fill_buffer_with_value().

Referenced by nvm_page_erase(), run_eeprom_atomic_write_test(), run_eeprom_write_test(), test_atomic_write(), and test_write().

void nvm_eeprom_fill_buffer_with_value ( uint8_t  value)

Fill temporary EEPROM page buffer with value.

This fills the the EEPROM page buffers with a given value. If memory mapped EEPROM is enabled, this function will not work.

Note
Only the lower part of the address is used to address the buffer. Therefore, no address parameter is needed. In the end, the data is written to the EEPROM page given by the address parameter to the EEPROM write page operation.
Parameters
valueValue to copy to the page buffer.

References nvm_eeprom_flush_buffer(), nvm_eeprom_load_byte_to_buffer(), and nvm_wait_until_ready().

Referenced by nvm_eeprom_erase_all(), and nvm_eeprom_erase_page().

void nvm_eeprom_flush_buffer ( void  )

Flush temporary EEPROM page buffer.

This function flushes the EEPROM page buffers. This function will cancel any ongoing EEPROM page buffer loading operations, if any. This function also works for memory mapped EEPROM access.

Note
An EEPROM write operations will automatically flush the buffer for you.
The function does not preserve the value of the NVM.CMD register

References nvm_exec(), and nvm_wait_until_ready().

Referenced by nvm_eeprom_fill_buffer_with_value(), nvm_eeprom_write_byte(), run_eeprom_atomic_write_test(), save_gamedata(), and test_atomic_write().

void nvm_eeprom_load_byte_to_buffer ( uint8_t  byte_addr,
uint8_t  value 
)

Load single byte into temporary page buffer.

This function loads one byte into the temporary EEPROM page buffers. If memory mapped EEPROM is enabled, this function will not work. Make sure that the buffer is flushed before starting to load bytes. Also, if multiple bytes are loaded into the same location, they will be ANDed together, thus 0x55 and 0xAA will result in 0x00 in the buffer.

Note
Only one page buffer exist, thus only one page can be loaded with data and programmed into one page. If data needs to be written to different pages, the loading and writing needs to be repeated.
Parameters
byte_addrEEPROM Byte address, between 0 and EEPROM_PAGE_SIZE.
valueByte value to write to buffer.

References eeprom_disable_mapping(), eeprom_enable_mapping(), and nvm_wait_until_ready().

Referenced by nvm_eeprom_fill_buffer_with_value(), nvm_eeprom_load_page_to_buffer(), nvm_eeprom_write_byte(), run_eeprom_atomic_write_test(), run_eeprom_erase_byte_test(), run_eeprom_split_write_test(), test_atomic_write(), test_erase_bytes(), and test_split_write().

void nvm_eeprom_load_page_to_buffer ( const uint8_t *  values)

Load entire page into temporary EEPROM page buffer.

This function loads an entire EEPROM page from an SRAM buffer to the EEPROM page buffers. If memory mapped EEPROM is enabled, this function will not work. Make sure that the buffer is flushed before starting to load bytes.

Note
Only the lower part of the address is used to address the buffer. Therefore, no address parameter is needed. In the end, the data is written to the EEPROM page given by the address parameter to the EEPROM write page operation.
Parameters
valuesPointer to SRAM buffer containing an entire page.

References nvm_eeprom_load_byte_to_buffer(), and nvm_wait_until_ready().

Referenced by nvm_eeprom_erase_and_write_buffer(), and save_gamedata().

void nvm_eeprom_read_buffer ( eeprom_addr_t  address,
void *  buf,
uint16_t  len 
)

Read buffer within the eeprom.

Parameters
addressthe address to where to read
bufpointer to the data
lenthe number of bytes to read

References eeprom_disable_mapping(), eeprom_enable_mapping(), and nvm_wait_until_ready().

Referenced by nvm_read(), and run_eeprom_read_buffer_test().

uint8_t nvm_eeprom_read_byte ( eeprom_addr_t  addr)

Read one byte from EEPROM using mapped access.

This function reads one byte from EEPROM using mapped access.

Parameters
addrEEPROM address, between 0 and EEPROM_SIZE
Returns
Byte value read from EEPROM.

References Assert, data, eeprom_disable_mapping(), eeprom_enable_mapping(), and nvm_wait_until_ready().

Referenced by is_eeprom_page_equal_to_buffer(), load_gamedata(), nvm_read_char(), read_info_from_eeprom(), and read_settings_from_eeprom().

void nvm_eeprom_split_write_page ( uint8_t  page_addr)

Write (without erasing) EEPROM page.

This function writes the contents of an already loaded EEPROM page buffer into EEPROM memory.

As this is a split write, the page in EEPROM will not be erased before writing.

Parameters
page_addrEEPROM Page address, between 0 and EEPROM_SIZE/EEPROM_PAGE_SIZE

References Assert, nvm_issue_command(), and nvm_wait_until_ready().

Referenced by run_eeprom_split_write_test(), and test_split_write().

void nvm_eeprom_write_byte ( eeprom_addr_t  address,
uint8_t  value 
)

Write one byte to EEPROM using IO mapping.

This function writes one byte to EEPROM using IO-mapped access. This function will cancel all ongoing EEPROM page buffer loading operations, if any.

Parameters
addressEEPROM address (max EEPROM_SIZE)
valueByte value to write to EEPROM.

References Assert, nvm_eeprom_flush_buffer(), nvm_eeprom_load_byte_to_buffer(), nvm_exec(), and nvm_wait_until_ready().

Referenced by nvm_eeprom_erase_and_write_buffer(), nvm_write_char(), run_eeprom_erase_byte_test(), run_eeprom_erase_test(), run_eeprom_read_buffer_test(), run_eeprom_write_test(), test_erase(), test_erase_bytes(), test_write(), write_global_settings_to_eeprom(), write_info_to_eeprom(), and write_sensor_settings_to_eeprom().