Laurent Meunier / I2CEeprom

Fork of I2CEeprom by Robin Hourahane

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CEeprom.h Source File

I2CEeprom.h

00001 /* Simple access class for I2C EEPROM chips like Microchip 24LC
00002  * Copyright (c) 2015 Robin Hourahane
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef __I2CEEPROM_H__
00017 #define __I2CEEPROM_H__
00018 
00019 #include <mbed.h>
00020 
00021 /// Class to provide simple access to I2C EEPROM chiles like Microchip's 24LC range
00022 /// or AMTELS AT24C range.
00023 /// Chips up to 64Kb in size are directly supported.
00024 /// The class handles multiple page writes so any amount of data can be written in
00025 /// a single call to write. The writes are not buffered so additional memory use 
00026 /// is kept to a minimum.
00027 ///
00028 /// Although not tested it should work with the MAC versions of Microchip range
00029 /// as well but the chipSize will need to be set to include the ROM area as well.
00030 class I2CEeprom {
00031 public:
00032     /// Constructor to create a new instance of the class.
00033     /// @param sda The pin name for the sda line of the I2C bus.
00034     /// @param scl The pin name for the scl line of the I2C bus.
00035     /// @param address The 8bit I2C address of the chip in the range 0xA0 - 0xAE.
00036     /// @param pageSize The size of the page used in writing to the chip.
00037     /// @param chipSize The size of the memory in the chip to allow range checkng. Set to
00038     /// 0 to disable checks.
00039     /// @param busSpeed The frequency of the I2C bus defaults to 400K.
00040     I2CEeprom(PinName sda, PinName scl, int address, size_t pageSize, size_t chipSize, int busSpeed = 400000);
00041     
00042     /// Read a single byte from the address in memory.
00043     /// @param address Memory address to read from.
00044     /// @param value Variable to receive value read.
00045     /// @returns Number of bytes read from memory.
00046     size_t read(size_t address, char &value);
00047     
00048     /// Read multiple bytes starting from the address in memory.
00049     /// @param address Memory address to start reading from.
00050     /// @param buffer Pointer to buffer to hold bytes read from memory.
00051     /// @param size Number of bytes to be read from memory.
00052     /// @returns Number of bytes read from memory.
00053     size_t read(size_t address, char *buffer, size_t size);
00054     
00055     /// Read either an instance or an array of a POD type from memory.
00056     /// Note the value of the type can't contain pointers.
00057     /// @param address Start address for reading memory.
00058     /// @param value Object to be read from memory.
00059     /// @returns Number of bytes read from memory.
00060     template<typename T> size_t read(size_t address, T &value) {
00061         return read(address, reinterpret_cast<char *>(&value), sizeof(T));
00062     }
00063     
00064     /// Write a single byte to the address in memory.
00065     /// @param address Memory address to write to.
00066     /// @param value Value to be written to memory.
00067     /// @returns Number of bytes written to memory.
00068     size_t write(size_t address, char value);
00069 
00070     /// Write multiple bytes starting from the address in memory.
00071     /// @param address Memory address to start writting to.
00072     /// @param buffer Pointer to buffer holding the bytes to write to memory.
00073     /// @param size Number of bytes to be written to memory.
00074     /// @returns Number of bytes written to memory.
00075     size_t write(size_t address, const char *buffer, size_t size);
00076     
00077     /// Write either an instance or an array of a POD type to memory.
00078     /// Note the value of the type can't contain pointers.
00079     /// @param address Start address to write to memory.
00080     /// @returns Number of bytes written to memory.
00081     template<typename T> size_t write(size_t address, const T &value) {
00082         return write(address, reinterpret_cast<const char *>(&value), sizeof(T));
00083     }
00084     
00085     /// Fill a range of memory with a single value. No memory is allocated
00086     /// so large areas can be filled with minimal memory usage.
00087     /// @param address Starting address to write to.
00088     /// @param value Value to be written to memory.
00089     /// @Param size Number of bytes to be written.
00090     /// @returns Number of bytes written to memory.
00091     size_t fill(size_t address, char value, size_t size);
00092     
00093 private:
00094     // Wait for a write cycle to complete using polling and small waits.
00095     void waitForWrite();
00096     
00097     // Validate that the proposed opperation will fit in the size of
00098     // the chip.
00099     bool checkSpace(size_t address, size_t size);
00100     
00101 private:
00102     I2C m_i2c;
00103     int m_i2cAddress;
00104     size_t m_chipSize;
00105     size_t m_pageSize;
00106 };
00107 
00108 #endif