CANopenNode
CANopen protocol stack
Loading...
Searching...
No Matches
Data storage in eeprom

Eeprom specific data storage functions. More...

+ Collaboration diagram for Data storage in eeprom:

Files

file  CO_eeprom.h
 Eeprom interface for use with CO_storageEeprom.
 
file  CO_storageEeprom.h
 CANopen data storage object for storing data into block device (eeprom)
 

Functions

bool_t CO_eeprom_init (void *storageModule)
 Initialize eeprom device, target system specific function.
 
size_t CO_eeprom_getAddr (void *storageModule, bool_t isAuto, size_t len, bool_t *overflow)
 Get free address inside eeprom, target system specific function.
 
void CO_eeprom_readBlock (void *storageModule, uint8_t *data, size_t eepromAddr, size_t len)
 Read block of data from the eeprom, target system specific function.
 
bool_t CO_eeprom_writeBlock (void *storageModule, uint8_t *data, size_t eepromAddr, size_t len)
 Write block of data to the eeprom, target system specific function.
 
uint16_t CO_eeprom_getCrcBlock (void *storageModule, size_t eepromAddr, size_t len)
 Get CRC checksum of the block of data stored in the eeprom, target system specific function.
 
bool_t CO_eeprom_updateByte (void *storageModule, uint8_t data, size_t eepromAddr)
 Update one byte of data in the eeprom, target system specific function.
 
CO_ReturnError_t CO_storageEeprom_init (CO_storage_t *storage, CO_CANmodule_t *CANmodule, void *storageModule, OD_entry_t *OD_1010_StoreParameters, OD_entry_t *OD_1011_RestoreDefaultParam, CO_storage_entry_t *entries, uint8_t entriesCount, uint32_t *storageInitError)
 Initialize data storage object (block device (eeprom) specific)
 
void CO_storageEeprom_auto_process (CO_storage_t *storage, bool_t saveAll)
 Automatically update data if differs inside eeprom.
 

Detailed Description

Eeprom specific data storage functions.

This is an interface into generic CANopenNode Data storage base for usage with eeprom chip like 25LC256. Functions CO_storageEeprom_init() and CO_storageEeprom_auto_process are target system independent. Functions specified by CO_eeprom.h file, must be defined by target system. For example implementation see CANopenPIC/PIC32.

Storage principle: This function first reads 'signatures' for all entries from the known address from the eeprom. If signature for each entry is correct, then data is read from correct address from the eeprom into storage location. If signature is wrong, then data for that entry is indicated as corrupt and CANopen emergency message is sent.

Signature also includes 16-bit CRC checksum of the data stored in eeprom. If it differs from CRC checksum calculated from the data actually loaded (on program startup), then entry is indicated as corrupt and CANopen emergency message is sent.

Signature is written to eeprom, when data block is stored via CANopen SDO write command to object 0x1010. Signature is erased, with CANopen SDO write command to object 0x1011. If signature is not valid or is erased for any entry, emergency message is sent. If eeprom is new, then all signatures are wrong, so it is best to store all parameters by writing to 0x1010, sub 1.

If entry attribute has CO_storage_auto set, then data block is stored autonomously, byte by byte, on change, during program run. Those data blocks are stored into write unprotected location. For auto storage to work, its signature in eeprom must be correct. CRC checksum for the data is not used.

Function Documentation

◆ CO_eeprom_init()

bool_t CO_eeprom_init ( void * storageModule)

Initialize eeprom device, target system specific function.

Parameters
storageModulePointer to storage module.
Returns
True on success

◆ CO_eeprom_getAddr()

size_t CO_eeprom_getAddr ( void * storageModule,
bool_t isAuto,
size_t len,
bool_t * overflow )

Get free address inside eeprom, target system specific function.

Function is called several times for each storage block in the initialization phase after CO_eeprom_init().

Parameters
storageModulePointer to storage module.
isAutoTrue, if variable is auto stored or false if protected
lenLength of data, which will be stored to that location
[out]overflowset to true, if not enough eeprom memory
Returns
Asigned eeprom address

◆ CO_eeprom_readBlock()

void CO_eeprom_readBlock ( void * storageModule,
uint8_t * data,
size_t eepromAddr,
size_t len )

Read block of data from the eeprom, target system specific function.

Parameters
storageModulePointer to storage module.
dataPointer to data buffer, where data will be stored.
eepromAddrAddress in eeprom, from where data will be read.
lenLength of the data block to be read.

◆ CO_eeprom_writeBlock()

bool_t CO_eeprom_writeBlock ( void * storageModule,
uint8_t * data,
size_t eepromAddr,
size_t len )

Write block of data to the eeprom, target system specific function.

It is blocking function, so it waits, until all data is written.

Parameters
storageModulePointer to storage module.
dataPointer to data buffer which will be written.
eepromAddrAddress in eeprom, where data will be written. If data is stored across multiple pages, address must be aligned with page.
lenLength of the data block.
Returns
true on success

◆ CO_eeprom_getCrcBlock()

uint16_t CO_eeprom_getCrcBlock ( void * storageModule,
size_t eepromAddr,
size_t len )

Get CRC checksum of the block of data stored in the eeprom, target system specific function.

Parameters
storageModulePointer to storage module.
eepromAddrAddress of data in eeprom.
lenLength of the data.
Returns
CRC checksum

◆ CO_eeprom_updateByte()

bool_t CO_eeprom_updateByte ( void * storageModule,
uint8_t data,
size_t eepromAddr )

Update one byte of data in the eeprom, target system specific function.

Function is used by automatic storage. It updates byte in eeprom only if differs from data.

Parameters
storageModulePointer to storage module.
dataData byte to be written
eepromAddrAddress in eeprom, from where data will be updated.
Returns
true if write was successful or false, if still waiting previous data to finish writing.

◆ CO_storageEeprom_init()

CO_ReturnError_t CO_storageEeprom_init ( CO_storage_t * storage,
CO_CANmodule_t * CANmodule,
void * storageModule,
OD_entry_t * OD_1010_StoreParameters,
OD_entry_t * OD_1011_RestoreDefaultParam,
CO_storage_entry_t * entries,
uint8_t entriesCount,
uint32_t * storageInitError )

Initialize data storage object (block device (eeprom) specific)

This function should be called by application after the program startup, before CO_CANopenInit(). This function initializes storage object, OD extensions on objects 1010 and 1011, reads data from file, verifies them and writes data to addresses specified inside entries. This function internally calls CO_storage_init().

Parameters
storageThis object will be initialized. It must be defined by application and must exist permanently.
CANmoduleCAN device, for optional usage.
storageModulePointer to storage module passed to CO_eeprom functions.
OD_1010_StoreParametersOD entry for 0x1010 -"Store parameters". Entry is optional, may be NULL.
OD_1011_RestoreDefaultParamOD entry for 0x1011 -"Restore default parameters". Entry is optional, may be NULL.
entriesPointer to array of storage entries, see CO_storage_init.
entriesCountCount of storage entries, must not be larger than CO_CONFIG_STORAGE_MAX_ENTRIES_COUNT.
[out]storageInitErrorIf function returns CO_ERROR_DATA_CORRUPT, then this variable contains a bit mask from subIndexOD values, where data was not properly initialized. If other error, then this variable contains index or erroneous entry. If there is hardware error like missing eeprom, then storageInitError is 0xFFFFFFFF and function returns CO_ERROR_DATA_CORRUPT.
Returns
CO_ERROR_NO, CO_ERROR_DATA_CORRUPT if data can not be initialized, CO_ERROR_ILLEGAL_ARGUMENT or CO_ERROR_OUT_OF_MEMORY.

◆ CO_storageEeprom_auto_process()

void CO_storageEeprom_auto_process ( CO_storage_t * storage,
bool_t saveAll )

Automatically update data if differs inside eeprom.

Should be called cyclically by program. Each interval it updates one byte.

Parameters
storageThis object
saveAllIf true, all bytes are updated, useful on program end.