ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
EEPROM24.h
1 /*
2  * Copyright (C) 2012 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef EEPROM24_h
24 #define EEPROM24_h
25 
26 #include <inttypes.h>
27 #include <stddef.h>
28 
29 class I2CMaster;
30 
31 // Block select modes.
32 #define EE_BSEL_NONE 0
33 #define EE_BSEL_8BIT_ADDR 1
34 #define EE_BSEL_17BIT_ADDR 2
35 #define EE_BSEL_17BIT_ADDR_ALT 3
36 
37 // Create an EEPROM descriptor from byte size, page size, and block select mode.
38 #define _EE24(byteSize, pageSize, mode) \
39  (((byteSize) / (pageSize)) | (((unsigned long)(pageSize)) << 16) | \
40  (((unsigned long)(mode)) << 28))
41 
42 // Type descriptors for the 24LCXX range of EEPROM's.
43 #define EEPROM_24LC00 _EE24(16UL, 1, EE_BSEL_8BIT_ADDR)
44 #define EEPROM_24LC01 _EE24(128UL, 8, EE_BSEL_8BIT_ADDR)
45 #define EEPROM_24LC014 _EE24(128UL, 16, EE_BSEL_8BIT_ADDR)
46 #define EEPROM_24LC02 _EE24(256UL, 8, EE_BSEL_8BIT_ADDR)
47 #define EEPROM_24LC024 _EE24(256UL, 16, EE_BSEL_8BIT_ADDR)
48 #define EEPROM_24LC025 _EE24(256UL, 16, EE_BSEL_8BIT_ADDR)
49 #define EEPROM_24LC04 _EE24(512UL, 16, EE_BSEL_8BIT_ADDR)
50 #define EEPROM_24LC08 _EE24(1024UL, 16, EE_BSEL_8BIT_ADDR)
51 #define EEPROM_24LC16 _EE24(2048UL, 16, EE_BSEL_8BIT_ADDR)
52 #define EEPROM_24LC32 _EE24(4096UL, 32, EE_BSEL_NONE)
53 #define EEPROM_24LC64 _EE24(8192UL, 32, EE_BSEL_NONE)
54 #define EEPROM_24LC128 _EE24(16384UL, 32, EE_BSEL_NONE)
55 #define EEPROM_24LC256 _EE24(32768UL, 64, EE_BSEL_NONE)
56 #define EEPROM_24LC512 _EE24(65536UL, 128, EE_BSEL_NONE)
57 #define EEPROM_24LC1025 _EE24(131072UL, 128, EE_BSEL_17BIT_ADDR_ALT)
58 #define EEPROM_24LC1026 _EE24(131072UL, 128, EE_BSEL_17BIT_ADDR)
59 
60 class EEPROM24
61 {
62 public:
63  EEPROM24(I2CMaster &bus, unsigned long type, uint8_t bank = 0);
64 
65  unsigned long size() const { return _size; }
66  unsigned long pageSize() const { return _pageSize; }
67 
68  bool available();
69 
70  uint8_t read(unsigned long address);
71  size_t read(unsigned long address, void *data, size_t length);
72 
73  bool write(unsigned long address, uint8_t value);
74  size_t write(unsigned long address, const void *data, size_t length);
75 
76 private:
77  I2CMaster *_bus;
78  unsigned long _size;
79  unsigned long _pageSize;
80  uint8_t _mode;
81  uint8_t i2cAddress;
82 
83  void writeAddress(unsigned long address);
84  bool waitForWrite();
85 };
86 
87 #endif
unsigned long size() const
Returns the size of the EEPROM in bytes.
Definition: EEPROM24.h:65
uint8_t read(unsigned long address)
Reads a single byte from the EEPROM at address.
Definition: EEPROM24.cpp:167
Reading and writing EEPROM's from the 24LCXX family.
Definition: EEPROM24.h:60
EEPROM24(I2CMaster &bus, unsigned long type, uint8_t bank=0)
Constructs a new EEPROM access object on bus for an EEPROM of the specified type. ...
Definition: EEPROM24.cpp:95
bool write(unsigned long address, uint8_t value)
Writes a byte value to address in the EEPROM.
Definition: EEPROM24.cpp:213
unsigned long pageSize() const
Returns the size of a single EEPROM page in bytes.
Definition: EEPROM24.h:66
Abstract base class for I2C master implementations.
Definition: I2CMaster.h:28
bool available()
Returns true if the EEPROM is available on the I2C bus; false otherwise.
Definition: EEPROM24.cpp:152