Mateusz Grzywacz / I2CEeprom

Dependents:   Nucleo_praktyki

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 i2c_inst The I2C bus instance. Pass a reference.
00034     /// @param address The 8bit I2C address of the chip in the range 0xA0 - 0xAE.
00035     /// @param pageSize The size of the page used in writing to the chip.
00036     /// @param chipSize The size of the memory in the chip to allow range checkng. Set to
00037     /// 0 to disable checks.
00038     /// @param busSpeed The frequency of the I2C bus defaults to 400K.
00039     I2CEeprom(I2C *i2c_inst, int address, size_t pageSize, size_t chipSize, int busSpeed = 400000);
00040     
00041     /// Read a single byte from the address in memory.
00042     /// @param address Memory address to read from.
00043     /// @param value Variable to receive value read.
00044     /// @returns Number of bytes read from memory.
00045     size_t read(size_t address, char &value);
00046     
00047     /// Read multiple bytes starting from the address in memory.
00048     /// @param address Memory address to start reading from.
00049     /// @param buffer Pointer to buffer to hold bytes read from memory.
00050     /// @param size Number of bytes to be read from memory.
00051     /// @returns Number of bytes read from memory.
00052     size_t read(size_t address, char *buffer, size_t size);
00053     
00054     /// Read either an instance or an array of a POD type from memory.
00055     /// Note the value of the type can't contain pointers.
00056     /// @param address Start address for reading memory.
00057     /// @param value Object to be read from memory.
00058     /// @returns Number of bytes read from memory.
00059     template<typename T> size_t read(size_t address, T &value) {
00060         return read(address, reinterpret_cast<char *>(&value), sizeof(T));
00061     }
00062     
00063     /// Write a single byte to the address in memory.
00064     /// @param address Memory address to write to.
00065     /// @param value Value to be written to memory.
00066     /// @returns Number of bytes written to memory.
00067     size_t write(size_t address, char value);
00068 
00069     /// Write multiple bytes starting from the address in memory.
00070     /// @param address Memory address to start writting to.
00071     /// @param buffer Pointer to buffer holding the bytes to write to memory.
00072     /// @param size Number of bytes to be written to memory.
00073     /// @returns Number of bytes written to memory.
00074     size_t write(size_t address, const char *buffer, size_t size);
00075     
00076     /// Write either an instance or an array of a POD type to memory.
00077     /// Note the value of the type can't contain pointers.
00078     /// @param address Start address to write to memory.
00079     /// @returns Number of bytes written to memory.
00080     template<typename T> size_t write(size_t address, const T &value) {
00081         return write(address, reinterpret_cast<const char *>(&value), sizeof(T));
00082     }
00083     
00084     /// Fill a range of memory with a single value. No memory is allocated
00085     /// so large areas can be filled with minimal memory usage.
00086     /// @param address Starting address to write to.
00087     /// @param value Value to be written to memory.
00088     /// @Param size Number of bytes to be written.
00089     /// @returns Number of bytes written to memory.
00090     size_t fill(size_t address, char value, size_t size);
00091     
00092 private:
00093     // Wait for a write cycle to complete using polling and small waits.
00094     void waitForWrite();
00095     
00096     // Validate that the proposed opperation will fit in the size of
00097     // the chip.
00098     bool checkSpace(size_t address, size_t size);
00099     
00100 private:
00101     I2C *m_i2c;
00102     int m_i2cAddress;
00103     size_t m_chipSize;
00104     size_t m_pageSize;
00105 };
00106 
00107 #endif