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