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