MySensors Library & Examples  2.3.2
extEEPROM.h
1 /*-----------------------------------------------------------------------------*
2  * extEEPROM.h - Arduino library to support external I2C EEPROMs. *
3  * *
4  * This library will work with most I2C serial EEPROM chips between 2k bits *
5  * and 2048k bits (2M bits) in size. Multiple EEPROMs on the bus are supported *
6  * as a single address space. I/O across block, page and device boundaries *
7  * is supported. Certain assumptions are made regarding the EEPROM *
8  * device addressing. These assumptions should be true for most EEPROMs *
9  * but there are exceptions, so read the datasheet and know your hardware. *
10  * *
11  * The library should also work for EEPROMs smaller than 2k bits, assuming *
12  * that there is only one EEPROM on the bus and also that the user is careful *
13  * to not exceed the maximum address for the EEPROM. *
14  * *
15  * Library tested with: *
16  * Microchip 24AA02E48 (2k bit) *
17  * 24xx32 (32k bit, thanks to Richard M) *
18  * Microchip 24LC256 (256k bit) *
19  * Microchip 24FC1026 (1M bit, thanks to Gabriele B on the Arduino forum) *
20  * ST Micro M24M02 (2M bit) *
21  * *
22  * Library will NOT work with Microchip 24xx1025 as its control byte does not *
23  * conform to the following assumptions. *
24  * *
25  * Device addressing assumptions: *
26  * 1. The I2C address sequence consists of a control byte followed by one *
27  * address byte (for EEPROMs <= 16k bits) or two address bytes (for *
28  * EEPROMs > 16k bits). *
29  * 2. The three least-significant bits in the control byte (excluding the R/W *
30  * bit) comprise the three most-significant bits for the entire address *
31  * space, i.e. all chips on the bus. As such, these may be chip-select *
32  * bits or block-select bits (for individual chips that have an internal *
33  * block organization), or a combination of both (in which case the *
34  * block-select bits must be of lesser significance than the chip-select *
35  * bits). *
36  * 3. Regardless of the number of bits needed to address the entire address *
37  * space, the three most-significant bits always go in the control byte. *
38  * Depending on EEPROM device size, this may result in one or more of the *
39  * most significant bits in the I2C address bytes being unused (or "don't *
40  * care"). *
41  * 4. An EEPROM contains an integral number of pages. *
42  * *
43  * To use the extEEPROM library, the Arduino Wire library must also *
44  * be included. *
45  * *
46  * Jack Christensen 23Mar2013 v1 *
47  * 29Mar2013 v2 - Updated to span page boundaries (and therefore also *
48  * device boundaries, assuming an integral number of pages per device) *
49  * 08Jul2014 v3 - Generalized for 2kb - 2Mb EEPROMs. *
50  * *
51  * Paolo Paolucci 22-10-2015 v3.2 *
52  * 09-01-2016 v3.2 Add update function. *
53  * *
54  * External EEPROM Library by Jack Christensen is licensed under CC BY-SA 4.0, *
55  * http://creativecommons.org/licenses/by-sa/4.0/ *
56  *-----------------------------------------------------------------------------*/
57 
58 /*
59  * tekka 2018:
60  * Re-implementing extEEPROM::update(unsigned long addr, byte value);
61  */
62 #ifndef extEEPROM_h
63 #define extEEPROM_h
64 
65 #include <Arduino.h>
66 #include <Wire.h>
67 
68 //EEPROM size in kilobits. EEPROM part numbers are usually designated in k-bits.
69 enum eeprom_size_t {
70  kbits_2 = 2,
71  kbits_4 = 4,
72  kbits_8 = 8,
73  kbits_16 = 16,
74  kbits_32 = 32,
75  kbits_64 = 64,
76  kbits_128 = 128,
77  kbits_256 = 256,
78  kbits_512 = 512,
79  kbits_1024 = 1024,
80  kbits_2048 = 2048
81 };
82 
83 //EEPROM addressing error, returned by write() or read() if upper address bound is exceeded
84 const uint8_t EEPROM_ADDR_ERR = 9;
85 
87 class extEEPROM
88 {
89 private:
90  // the private attribute used to comunicate with the correct I2C SERCOM
91  TwoWire *communication;
92 
93 public:
98  twiClock100kHz = 100000,
99  twiClock400kHz = 400000
100  };
108  extEEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pageSize,
109  byte eepromAddr = 0x50);
110 
111  // It is ready for every I2C Sercom, by default use the main Wire
112  byte begin(twiClockFreq_t twiFreq = twiClock100kHz, TwoWire *_comm=&Wire);
113  byte write(unsigned long addr, byte *values, unsigned int nBytes);
114  byte write(unsigned long addr, byte value);
115  byte read(unsigned long addr, byte *values, unsigned int nBytes);
116  int read(unsigned long addr);
117  byte update(unsigned long addr, byte *values, unsigned int nBytes);
118  byte update(unsigned long addr, byte value);
119  unsigned long length();
120 
121 private:
122  uint8_t _eepromAddr; //eeprom i2c address
123  uint16_t _dvcCapacity; //capacity of one EEPROM device, in kbits
124  uint8_t _nDevice; //number of devices on the bus
125  uint16_t _pageSize; //page size in bytes
126  uint8_t _csShift; //number of bits to shift address for chip select bits in control byte
127  uint16_t _nAddrBytes; //number of address bytes (1 or 2)
128  unsigned long _totalCapacity; //capacity of all EEPROM devices on the bus, in bytes
129 };
130 
131 #endif
extEEPROM::begin
byte begin(twiClockFreq_t twiFreq=twiClock100kHz, TwoWire *_comm=&Wire)
begin()
extEEPROM::length
unsigned long length()
length()
extEEPROM
Definition: extEEPROM.h:87
extEEPROM::read
byte read(unsigned long addr, byte *values, unsigned int nBytes)
read()
extEEPROM::write
byte write(unsigned long addr, byte *values, unsigned int nBytes)
write()
extEEPROM::extEEPROM
extEEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pageSize, byte eepromAddr=0x50)
Constructor.
extEEPROM::twiClock100kHz
@ twiClock100kHz
twiClock100kHz
Definition: extEEPROM.h:98
extEEPROM::update
byte update(unsigned long addr, byte *values, unsigned int nBytes)
update()
extEEPROM::twiClock400kHz
@ twiClock400kHz
twiClock400kHz
Definition: extEEPROM.h:99
values
Structure to be used in percentage and resistance values matrix to be filtered (have to be in pairs)
Definition: SoilMoistSensor.ino:86
extEEPROM::twiClockFreq_t
twiClockFreq_t
Definition: extEEPROM.h:97