Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
I2CEeprom Class Reference
Based on the original Mbed I2CEeprom library by Robin Hourahane. More...
#include <I2CEeprom.h>
Public Member Functions | |
| I2CEeprom (I2C &i2c, int address, size_t pageSize, size_t chipSize, uint8_t writeCycleTime_ms) | |
| Constructor to create a new instance of the class. | |
| size_t | read (size_t address, char &value) |
| Read a single byte from the EEprom. | |
| size_t | read (size_t address, char *buffer, size_t size) |
| Read multiple bytes from the EEprom. | |
| template<typename T > | |
| size_t | read (size_t address, T &value) |
| Deserialise object of type T from Eeprom memory. | |
| size_t | write (size_t address, char value) |
| Write a char to EEprom. | |
| size_t | write (size_t address, const char *buffer, size_t size) |
| Write multiple bytes to EEprom. | |
| template<typename T > | |
| size_t | write (size_t address, const T &value) |
| Serialise an object of type T. | |
Detailed Description
Based on the original Mbed I2CEeprom library by Robin Hourahane.
Class to provide simple access to I2C EEPROM chips like Microchip's 24LC or AMTELS AT24C series. Chips up to 64Kb in size are directly supported.
The library was tested on a Microchip 24LC128.
The I2CEeprom class handles multiple page writes so any amount of data can be written in a single call to write.
The code is modified from the original to better support RTOS. At start of a write(addr,buffer,size), a buffer of up to pageSize + 2 is allocated on the heap, and the data and address are copied to it. This allows the cuurent write chunk to proceed without unlocking, which prevents another device on the bus access and so aborting the write.
This allocation per write fits my usecase, where eeeprom variables are written in a special menu mode at start up, but it may be better to preallocate a buffer once at startup, if writes are occurring at arbitrary times during execution, to prevent surprises with out of memory issues when trying to write during normal execution.
The constructor takes the I2C bus by non-const reference argument. This enables a single I2C bus to be shared among many i2c devices. Mbed OS enforces a lock on the bus during a bus read/write so reads are atomic. Writes are split into separate atomic chunks that only write one eeprom page. After a chunk is sent, the thread is sent to sleep for writeCycleTime_ms specified in the constructor arg, and is then polled at 1 ms intervals to discover whether the write has completed.
Definition at line 58 of file I2CEeprom.h.
Constructor & Destructor Documentation
| I2CEeprom | ( | I2C & | i2c, |
| int | address, | ||
| size_t | pageSize, | ||
| size_t | chipSize, | ||
| uint8_t | writeCycleTime_ms | ||
| ) |
Constructor to create a new instance of the class.
- Parameters:
-
i2c A reference to the i2c bus that the chip is connected to. address The 8bit device I2C address pageSize The device page size. chipSize The device size for range check. writeCycleTime_ms The device write cycle time in ms.
Definition at line 55 of file I2CEeprom.cpp.
Member Function Documentation
| size_t read | ( | size_t | address, |
| char & | value | ||
| ) |
Read a single byte from the EEprom.
- Parameters:
-
address EEprom read address. value Memory reference of char to read into.
- Returns:
- Number of bytes read .
| size_t read | ( | size_t | address, |
| char * | buffer, | ||
| size_t | size | ||
| ) |
Read multiple bytes from the EEprom.
- Parameters:
-
address EEprom start read address. buffer buffer to read into. size Number of bytes to read.
- Returns:
- Number of bytes read.
| size_t read | ( | size_t | address, |
| T & | value | ||
| ) |
Deserialise object of type T from Eeprom memory.
- Parameters:
-
address EEprom start address value Memory Reference of object to deserialise into .
- Returns:
- Number of bytes read.
Definition at line 91 of file I2CEeprom.h.
| size_t write | ( | size_t | address, |
| char | value | ||
| ) |
Write a char to EEprom.
- Parameters:
-
address Eeprom write address. value Char value to write.
- Returns:
- Number of bytes written.
| size_t write | ( | size_t | address, |
| const char * | buffer, | ||
| size_t | size | ||
| ) |
Write multiple bytes to EEprom.
In this implementation, the write is split into chunks of up to pageSize and for each chunk a buffer of up to pageSize + 2 is temporarily allocated on the heap while the write is in progress.
- Parameters:
-
address EEprom start address. buffer Buffer to write. size Number of bytes to write.
- Returns:
- Number of bytes written.
| size_t write | ( | size_t | address, |
| const T & | value | ||
| ) |
Serialise an object of type T.
- Parameters:
-
address EEprom start address. value Object to be serialised.
- Returns:
- Number of bytes written.
Definition at line 116 of file I2CEeprom.h.
Generated on Wed Jul 27 2022 18:15:30 by
1.7.2