Class to provide simple access to I2C EEPROM chiles like Microchip's 24LC range or AMTELS AT24C range. Chips up to 64Kb in size are directly supported. Updated to Mbed OS v 5.1
Diff: I2CEeprom.h
- Revision:
- 2:f3188aaccf80
- Parent:
- 1:b23f5561266c
- Child:
- 4:d8f51b136dbd
--- a/I2CEeprom.h Sun Jul 19 09:34:04 2015 +0000 +++ b/I2CEeprom.h Sat Mar 28 00:53:14 2020 +0000 @@ -1,3 +1,5 @@ +#ifndef __I2CEEPROM_H__ +#define __I2CEEPROM_H__ /* Simple access class for I2C EEPROM chips like Microchip 24LC * Copyright (c) 2015 Robin Hourahane * @@ -13,31 +15,33 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef __I2CEEPROM_H__ -#define __I2CEEPROM_H__ #include <mbed.h> -/// Class to provide simple access to I2C EEPROM chiles like Microchip's 24LC range +/// Class to provide simple access to I2C EEPROM chips like Microchip's 24LC range /// or AMTELS AT24C range. /// Chips up to 64Kb in size are directly supported. /// The class handles multiple page writes so any amount of data can be written in -/// a single call to write. The writes are not buffered so additional memory use -/// is kept to a minimum. +/// a single call to write. /// /// Although not tested it should work with the MAC versions of Microchip range /// as well but the chipSize will need to be set to include the ROM area as well. class I2CEeprom { public: /// Constructor to create a new instance of the class. - /// @param sda The pin name for the sda line of the I2C bus. - /// @param scl The pin name for the scl line of the I2C bus. + /// @param i2c a reference to the i2c bus the chip is connected to. /// @param address The 8bit I2C address of the chip in the range 0xA0 - 0xAE. /// @param pageSize The size of the page used in writing to the chip. /// @param chipSize The size of the memory in the chip to allow range checkng. Set to /// 0 to disable checks. - /// @param busSpeed The frequency of the I2C bus defaults to 400K. - I2CEeprom(PinName sda, PinName scl, int address, size_t pageSize, size_t chipSize, int busSpeed = 400000); + /// @param The write cycle time in ms. + /// TODO : Could use bus frequency to cap the speed of the bus + I2CEeprom( + I2C& i2c, + int address, + size_t pageSize, + size_t chipSize, + uint8_t write_cycle_time_ms); /// Read a single byte from the address in memory. /// @param address Memory address to read from. @@ -73,7 +77,7 @@ /// @param size Number of bytes to be written to memory. /// @returns Number of bytes written to memory. size_t write(size_t address, const char *buffer, size_t size); - + /// Write either an instance or an array of a POD type to memory. /// Note the value of the type can't contain pointers. /// @param address Start address to write to memory. @@ -81,28 +85,22 @@ template<typename T> size_t write(size_t address, const T &value) { return write(address, reinterpret_cast<const char *>(&value), sizeof(T)); } - - /// Fill a range of memory with a single value. No memory is allocated - /// so large areas can be filled with minimal memory usage. - /// @param address Starting address to write to. - /// @param value Value to be written to memory. - /// @Param size Number of bytes to be written. - /// @returns Number of bytes written to memory. - size_t fill(size_t address, char value, size_t size); - + private: // Wait for a write cycle to complete using polling and small waits. void waitForWrite(); + size_t ll_write(size_t address, const char *buffer, size_t size); // Validate that the proposed opperation will fit in the size of // the chip. bool checkSpace(size_t address, size_t size); private: - I2C m_i2c; - int m_i2cAddress; - size_t m_chipSize; - size_t m_pageSize; + I2C & m_i2c; + int const m_i2cAddress; + size_t const m_chipSize; + size_t const m_pageSize; + uint8_t const m_writeCycleTime_ms; }; #endif \ No newline at end of file