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
I2CEeprom.h
- Committer:
- skyscraper
- Date:
- 2020-03-28
- Revision:
- 7:29e505d37158
- Parent:
- 6:0b18a49e03b9
- Child:
- 8:0c122f839dc9
File content as of revision 7:29e505d37158:
/* Simple access class for I2C EEPROM chips like Microchip 24LC * Copyright (c) 2015 Robin Hourahane * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 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 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. /// 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 write to proceed without unlocking, which might allow /// another device on the bus access and so corrupt 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. /// /// Tested on a Microchip 24LC128. class I2CEeprom { public: /// Constructor to create a new instance of the class. /// @param i2c a reference to the i2c bus the chip is connected to. /// @param address The 8bit I2C address of the chip. /// @param pageSize The size of the page used in writing to the chip. /// @param chipSize The size of the memory in the chip for range check. /// @param writeCycleTime_ms The write cycle time in ms. I2CEeprom( I2C& i2c, int address, size_t pageSize, size_t chipSize, uint8_t writeCycleTime_ms); /// Read a single byte from the address in memory. /// @param address Memory address to read from. /// @param value Variable to receive value read. /// @returns Number of bytes read from memory. size_t read(size_t address, char &value); /// Read multiple bytes starting from the address in memory. /// @param address Memory address to start reading from. /// @param buffer Pointer to buffer to hold bytes read from memory. /// @param size Number of bytes to be read from memory. /// @returns Number of bytes read from memory. size_t read(size_t address, char *buffer, size_t size); /// Read either an instance or an array of a POD type from memory. /// Note the value of the type can't contain pointers. /// @param address Start address for reading memory. /// @param value Object to be read from memory. /// @returns Number of bytes read from memory. template<typename T> size_t read(size_t address, T &value) { return read(address, reinterpret_cast<char *>(&value), sizeof(T)); } /// Write a single byte to the address in memory. /// @param address Memory address to write to. /// @param value Value to be written to memory. /// @returns Number of bytes written to memory. size_t write(size_t address, char value); /// Write multiple bytes starting from the address in memory. /// Note that in this implementation, a buffer is allocated on the heap /// for the /// @param address Memory address to start writting to. /// @param buffer Pointer to buffer holding the bytes to write to memory. /// @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. /// @returns Number of bytes written to memory. template<typename T> size_t write(size_t address, const T &value) { return write(address, reinterpret_cast<const char *>(&value), sizeof(T)); } 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) { return (address + size) < m_chipSize ; } private: I2C & m_i2c; int const m_i2cAddress; size_t const m_chipSize; size_t const m_pageSize; uint8_t const m_writeCycleTime_ms; }; #endif