ArduinoLibs
EEPROM24.h
00001 /*
00002  * Copyright (C) 2012 Southern Storm Software, Pty Ltd.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00019  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00020  * DEALINGS IN THE SOFTWARE.
00021  */
00022 
00023 #ifndef EEPROM24_h
00024 #define EEPROM24_h
00025 
00026 #include <inttypes.h>
00027 #include <stddef.h>
00028 
00029 class I2CMaster;
00030 
00031 // Block select modes.
00032 #define EE_BSEL_NONE            0
00033 #define EE_BSEL_8BIT_ADDR       1
00034 #define EE_BSEL_17BIT_ADDR      2
00035 #define EE_BSEL_17BIT_ADDR_ALT  3
00036 
00037 // Create an EEPROM descriptor from byte size, page size, and block select mode.
00038 #define _EE24(byteSize, pageSize, mode) \
00039     (((byteSize) / (pageSize)) | (((unsigned long)(pageSize)) << 16) | \
00040      (((unsigned long)(mode)) << 28))
00041 
00042 // Type descriptors for the 24LCXX range of EEPROM's.
00043 #define EEPROM_24LC00   _EE24(16UL, 1, EE_BSEL_8BIT_ADDR)
00044 #define EEPROM_24LC01   _EE24(128UL, 8, EE_BSEL_8BIT_ADDR)
00045 #define EEPROM_24LC014  _EE24(128UL, 16, EE_BSEL_8BIT_ADDR)
00046 #define EEPROM_24LC02   _EE24(256UL, 8, EE_BSEL_8BIT_ADDR)
00047 #define EEPROM_24LC024  _EE24(256UL, 16, EE_BSEL_8BIT_ADDR)
00048 #define EEPROM_24LC025  _EE24(256UL, 16, EE_BSEL_8BIT_ADDR)
00049 #define EEPROM_24LC04   _EE24(512UL, 16, EE_BSEL_8BIT_ADDR)
00050 #define EEPROM_24LC08   _EE24(1024UL, 16, EE_BSEL_8BIT_ADDR)
00051 #define EEPROM_24LC16   _EE24(2048UL, 16, EE_BSEL_8BIT_ADDR)
00052 #define EEPROM_24LC32   _EE24(4096UL, 32, EE_BSEL_NONE)
00053 #define EEPROM_24LC64   _EE24(8192UL, 32, EE_BSEL_NONE)
00054 #define EEPROM_24LC128  _EE24(16384UL, 32, EE_BSEL_NONE)
00055 #define EEPROM_24LC256  _EE24(32768UL, 64, EE_BSEL_NONE)
00056 #define EEPROM_24LC512  _EE24(65536UL, 128, EE_BSEL_NONE)
00057 #define EEPROM_24LC1025 _EE24(131072UL, 128, EE_BSEL_17BIT_ADDR_ALT)
00058 #define EEPROM_24LC1026 _EE24(131072UL, 128, EE_BSEL_17BIT_ADDR)
00059 
00060 class EEPROM24
00061 {
00062 public:
00063     EEPROM24(I2CMaster &bus, unsigned long type, uint8_t bank = 0);
00064 
00065     unsigned long size() const { return _size; }
00066     unsigned long pageSize() const { return _pageSize; }
00067 
00068     bool available();
00069 
00070     uint8_t read(unsigned long address);
00071     size_t read(unsigned long address, void *data, size_t length);
00072 
00073     bool write(unsigned long address, uint8_t value);
00074     size_t write(unsigned long address, const void *data, size_t length);
00075 
00076 private:
00077     I2CMaster *_bus;
00078     unsigned long _size;
00079     unsigned long _pageSize;
00080     uint8_t _mode;
00081     uint8_t i2cAddress;
00082 
00083     void writeAddress(unsigned long address);
00084     bool waitForWrite();
00085 };
00086 
00087 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator