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.

Dependents:   Test_I2CEeprom Alternator2020_06

This class provides a simple read write interface for I2C EEPROMs like Microchip's 24LC range and AMTELS AT24C range. The class ensure that writes respect the page size of the chip to ensure larger blocks can be written in one call. The class uses the supplied buffer to directly write to the chip so no extra RAM is used.

Committer:
rhourahane
Date:
Sat Jun 27 13:03:49 2015 +0000
Revision:
0:f275a33797f1
Initial commit of library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rhourahane 0:f275a33797f1 1 /* Simple access class for Microchip 24LC EEPROM chips library
rhourahane 0:f275a33797f1 2 * Copyright (c) 2015 Robin Hourahane
rhourahane 0:f275a33797f1 3 *
rhourahane 0:f275a33797f1 4 * Licensed under the Apache License, Version 2.0 (the "License");
rhourahane 0:f275a33797f1 5 * you may not use this file except in compliance with the License.
rhourahane 0:f275a33797f1 6 * You may obtain a copy of the License at
rhourahane 0:f275a33797f1 7 *
rhourahane 0:f275a33797f1 8 * http://www.apache.org/licenses/LICENSE-2.0
rhourahane 0:f275a33797f1 9 *
rhourahane 0:f275a33797f1 10 * Unless required by applicable law or agreed to in writing, software
rhourahane 0:f275a33797f1 11 * distributed under the License is distributed on an "AS IS" BASIS,
rhourahane 0:f275a33797f1 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rhourahane 0:f275a33797f1 13 * See the License for the specific language governing permissions and
rhourahane 0:f275a33797f1 14 * limitations under the License.
rhourahane 0:f275a33797f1 15 */
rhourahane 0:f275a33797f1 16 #ifndef __MEM24LCXX_H__
rhourahane 0:f275a33797f1 17 #define __MEM24LCXX_H__
rhourahane 0:f275a33797f1 18
rhourahane 0:f275a33797f1 19 #include <mbed.h>
rhourahane 0:f275a33797f1 20
rhourahane 0:f275a33797f1 21 /// Class to provide simple access to Microchip's 24LC range of I2C EEPROM memory chips.
rhourahane 0:f275a33797f1 22 /// Chips up to 64Kb in size are directly supported, the bank selected 128Kb chips
rhourahane 0:f275a33797f1 23 /// can be handled by creating to instances of the class one for each bank.
rhourahane 0:f275a33797f1 24 /// The class handles multiple page writes so any amount of data can be written in
rhourahane 0:f275a33797f1 25 /// a single call to write. The writes are not buffered so additional memory use
rhourahane 0:f275a33797f1 26 /// is kept to a minimum.
rhourahane 0:f275a33797f1 27 ///
rhourahane 0:f275a33797f1 28 /// Although not tested it should work with the MAC versions as well but the chipSize will
rhourahane 0:f275a33797f1 29 /// need to be set to include the ROM area as well.
rhourahane 0:f275a33797f1 30 class Mem24LCXX {
rhourahane 0:f275a33797f1 31 public:
rhourahane 0:f275a33797f1 32 /// Constructor to create a new instance of the class.
rhourahane 0:f275a33797f1 33 /// @param sda The pin name for the sda line of the I2C bus.
rhourahane 0:f275a33797f1 34 /// @param scl The pin name for the scl line of the I2C bus.
rhourahane 0:f275a33797f1 35 /// @param address The 8bit I2C address of the chip in the range 0xA0 - 0xAE.
rhourahane 0:f275a33797f1 36 /// @param chipSize The size of the memory in the chip to allow range checkng.
rhourahane 0:f275a33797f1 37 /// @param busSpeed The frequency of the I2C bus defaults to 400K.
rhourahane 0:f275a33797f1 38 Mem24LCXX(PinName sda, PinName scl, int address, size_t chipSize, int busSpeed = 400000);
rhourahane 0:f275a33797f1 39
rhourahane 0:f275a33797f1 40 /// Read a single byte from the address in memory.
rhourahane 0:f275a33797f1 41 /// @param address Memory address to read from.
rhourahane 0:f275a33797f1 42 /// @param value Variable to receive value read.
rhourahane 0:f275a33797f1 43 /// @returns Number of bytes read from memory.
rhourahane 0:f275a33797f1 44 size_t read(size_t address, char &value);
rhourahane 0:f275a33797f1 45
rhourahane 0:f275a33797f1 46 /// Read multiple bytes starting from the address in memory.
rhourahane 0:f275a33797f1 47 /// @param address Memory address to start reading from.
rhourahane 0:f275a33797f1 48 /// @param buffer Pointer to buffer to hold bytes read from memory.
rhourahane 0:f275a33797f1 49 /// @param size Number of bytes to be read from memory.
rhourahane 0:f275a33797f1 50 /// @returns Number of bytes read from memory.
rhourahane 0:f275a33797f1 51 size_t read(size_t address, char *buffer, size_t size);
rhourahane 0:f275a33797f1 52
rhourahane 0:f275a33797f1 53 /// Read either an instance or an array of a POD type from memory.
rhourahane 0:f275a33797f1 54 /// Note the value of the type can't contain pointers.
rhourahane 0:f275a33797f1 55 /// @param address Start address for reading memory.
rhourahane 0:f275a33797f1 56 /// @param value Object to be read from memory.
rhourahane 0:f275a33797f1 57 /// @returns Number of bytes read from memory.
rhourahane 0:f275a33797f1 58 template<typename T> size_t read(size_t address, T &value) {
rhourahane 0:f275a33797f1 59 return read(address, reinterpret_cast<char *>(&value), sizeof(T));
rhourahane 0:f275a33797f1 60 }
rhourahane 0:f275a33797f1 61
rhourahane 0:f275a33797f1 62 /// Write a single byte to the address in memory.
rhourahane 0:f275a33797f1 63 /// @param address Memory address to write to.
rhourahane 0:f275a33797f1 64 /// @param value Value to be written to memory.
rhourahane 0:f275a33797f1 65 /// @returns Number of bytes written to memory.
rhourahane 0:f275a33797f1 66 size_t write(size_t address, char value);
rhourahane 0:f275a33797f1 67
rhourahane 0:f275a33797f1 68 /// Write multiple bytes starting from the address in memory.
rhourahane 0:f275a33797f1 69 /// @param address Memory address to start writting to.
rhourahane 0:f275a33797f1 70 /// @param buffer Pointer to buffer holding the bytes to write to memory.
rhourahane 0:f275a33797f1 71 /// @param size Number of bytes to be written to memory.
rhourahane 0:f275a33797f1 72 /// @returns Number of bytes written to memory.
rhourahane 0:f275a33797f1 73 size_t write(size_t address, const char *buffer, size_t size);
rhourahane 0:f275a33797f1 74
rhourahane 0:f275a33797f1 75 /// Write either an instance or an array of a POD type to memory.
rhourahane 0:f275a33797f1 76 /// Note the value of the type can't contain pointers.
rhourahane 0:f275a33797f1 77 /// @param address Start address to write to memory.
rhourahane 0:f275a33797f1 78 /// @returns Number of bytes written to memory.
rhourahane 0:f275a33797f1 79 template<typename T> size_t write(size_t address, const T &value) {
rhourahane 0:f275a33797f1 80 return write(address, reinterpret_cast<const char *>(&value), sizeof(T));
rhourahane 0:f275a33797f1 81 }
rhourahane 0:f275a33797f1 82
rhourahane 0:f275a33797f1 83 /// Fill a range of memory with a single value. No memory is allocated
rhourahane 0:f275a33797f1 84 /// so large areas can be filled with minimal memory usage.
rhourahane 0:f275a33797f1 85 /// @param address Starting address to write to.
rhourahane 0:f275a33797f1 86 /// @param value Value to be written to memory.
rhourahane 0:f275a33797f1 87 /// @Param size Number of bytes to be written.
rhourahane 0:f275a33797f1 88 /// @returns Number of bytes written to memory.
rhourahane 0:f275a33797f1 89 size_t fill(size_t address, char value, size_t size);
rhourahane 0:f275a33797f1 90
rhourahane 0:f275a33797f1 91 private:
rhourahane 0:f275a33797f1 92 // Wait for a write cycle to complete using polling and small waits.
rhourahane 0:f275a33797f1 93 void waitForWrite();
rhourahane 0:f275a33797f1 94
rhourahane 0:f275a33797f1 95 // Validate that the proposed opperation will fit in the size of
rhourahane 0:f275a33797f1 96 // the chip.
rhourahane 0:f275a33797f1 97 bool checkSpace(size_t address, size_t size);
rhourahane 0:f275a33797f1 98
rhourahane 0:f275a33797f1 99 private:
rhourahane 0:f275a33797f1 100 I2C m_i2c;
rhourahane 0:f275a33797f1 101 int m_i2cAddress;
rhourahane 0:f275a33797f1 102 size_t m_chipSize;
rhourahane 0:f275a33797f1 103 };
rhourahane 0:f275a33797f1 104
rhourahane 0:f275a33797f1 105 #endif