1
1
//
2
2
// FILE: I2C_eeprom.cpp
3
3
// AUTHOR: Rob Tillaart
4
- // VERSION: 1.1.00
4
+ // VERSION: 1.2.02
5
5
// PURPOSE: I2C_eeprom library for Arduino with EEPROM 24LC256 et al.
6
6
//
7
7
// HISTORY:
20
20
// 1.1.00 - 2013-11-13 added begin() function (Note breaking interface)
21
21
// use faster block Wire.write()
22
22
// int casting removed
23
- // 1.2.00 - 2015-05-21 Added support for Arduino DUE ( thanks to Tyler F.)
24
- // 1.2.01 - 2013-05-21 Refactoring
23
+ // 1.2.00 - 2014-05-21 Added support for Arduino DUE ( thanks to Tyler F.)
24
+ // 1.2.01 - 2014-05-21 Refactoring
25
+ // 1.2.02 - 2015-03-06 stricter interface
25
26
//
26
27
// Released to the public domain
27
28
//
28
29
29
30
#include < I2C_eeprom.h>
30
31
31
32
#if defined(ARDUINO) && ARDUINO >= 100
32
- #define WIRE_WRITE Wire.write
33
- #define WIRE_READ Wire.read
33
+ #define WIRE_WRITE Wire.write
34
+ #define WIRE_READ Wire.read
34
35
#else
35
- #define WIRE_WRITE Wire.send
36
- #define WIRE_READ Wire.receive
36
+ #define WIRE_WRITE Wire.send
37
+ #define WIRE_READ Wire.receive
37
38
#endif
38
39
39
40
40
- I2C_eeprom::I2C_eeprom (uint8_t deviceAddress)
41
+ I2C_eeprom::I2C_eeprom (const uint8_t deviceAddress)
41
42
{
42
43
I2C_eeprom (deviceAddress, I2C_EEPROM_PAGESIZE);
43
44
}
44
45
45
- I2C_eeprom::I2C_eeprom (uint8_t deviceAddress, unsigned int deviceSize)
46
+ I2C_eeprom::I2C_eeprom (const uint8_t deviceAddress, const unsigned int deviceSize)
46
47
{
47
48
_deviceAddress = deviceAddress;
48
49
49
- // Chips 16Kbit (2048KB ) or smaller only have one-word addresses.
50
+ // Chips 16Kbit (2048 Bytes ) or smaller only have one-word addresses.
50
51
// Also try to guess page size from device size (going by Microchip 24LCXX datasheets here).
51
52
if (deviceSize <= 256 )
52
53
{
@@ -70,7 +71,7 @@ void I2C_eeprom::begin()
70
71
Wire.begin ();
71
72
_lastWrite = 0 ;
72
73
73
- // TWBR is not available on Arduino Due
74
+ // TWBR is not available on Arduino Due
74
75
#ifdef TWBR
75
76
TWBR = 72 ;
76
77
// 0=1000 1=888 2=800 8=500
@@ -81,57 +82,63 @@ void I2C_eeprom::begin()
81
82
82
83
83
84
84
- int I2C_eeprom::writeByte (uint16_t memoryAddress, uint8_t data)
85
+ int I2C_eeprom::writeByte (const uint16_t memoryAddress, const uint8_t data)
85
86
{
86
87
int rv = _WriteBlock (memoryAddress, &data, 1 );
87
88
return rv;
88
89
}
89
90
90
- int I2C_eeprom::setBlock (uint16_t memoryAddress, uint8_t data, uint16_t length)
91
+ int I2C_eeprom::setBlock (const uint16_t memoryAddress, const uint8_t data, const uint16_t length)
91
92
{
92
93
uint8_t buffer[I2C_TWIBUFFERSIZE];
93
- for (uint8_t i = 0 ; i< I2C_TWIBUFFERSIZE; i++) buffer[i] = data;
94
+ for (uint8_t i = 0 ; i < I2C_TWIBUFFERSIZE; i++) buffer[i] = data;
94
95
95
96
int rv = _pageBlock (memoryAddress, buffer, length, false );
96
97
return rv;
97
98
}
98
99
99
- int I2C_eeprom::writeBlock (uint16_t memoryAddress, uint8_t * buffer, uint16_t length)
100
+ int I2C_eeprom::writeBlock (const uint16_t memoryAddress, const uint8_t * buffer, const uint16_t length)
100
101
{
101
102
int rv = _pageBlock (memoryAddress, buffer, length, true );
102
103
return rv;
103
104
}
104
105
105
- uint8_t I2C_eeprom::readByte (uint16_t memoryAddress)
106
+ uint8_t I2C_eeprom::readByte (const uint16_t memoryAddress)
106
107
{
107
108
uint8_t rdata;
108
109
_ReadBlock (memoryAddress, &rdata, 1 );
109
110
return rdata;
110
111
}
111
112
112
- uint16_t I2C_eeprom::readBlock (uint16_t memoryAddress, uint8_t * buffer, uint16_t length)
113
+ uint16_t I2C_eeprom::readBlock (const uint16_t memoryAddress, uint8_t * buffer, const uint16_t length)
113
114
{
115
+ uint16_t addr = memoryAddress;
116
+ uint16_t len = length;
114
117
uint16_t rv = 0 ;
115
- while (length > 0 )
118
+ while (len > 0 )
116
119
{
117
- uint8_t cnt = min (length , I2C_TWIBUFFERSIZE);
118
- rv += _ReadBlock (memoryAddress , buffer, cnt);
119
- memoryAddress += cnt;
120
+ uint8_t cnt = min (len , I2C_TWIBUFFERSIZE);
121
+ rv += _ReadBlock (addr , buffer, cnt);
122
+ addr += cnt;
120
123
buffer += cnt;
121
- length -= cnt;
124
+ len -= cnt;
122
125
}
123
126
return rv;
124
127
}
125
128
126
129
#ifdef I2C_EEPROM_EXTENDED
127
130
// returns 64, 32, 16, 8, 4, 2, 1, 0
128
131
// 0 is smaller than 1K
129
- uint8_t I2C_eeprom::determineSize ()
132
+ int I2C_eeprom::determineSize ()
130
133
{
131
- uint8_t rv = 0 ; // unknown
134
+ int rv = 0 ; // unknown
132
135
uint8_t orgValues[8 ];
133
136
uint16_t addr;
134
137
138
+ // try to read a byte to see if connected
139
+ rv += _ReadBlock (0x00 , orgValues, 1 );
140
+ if (rv == 0 ) return -1 ;
141
+
135
142
// remember old values, non destructive
136
143
for (uint8_t i=0 ; i<8 ; i++)
137
144
{
@@ -171,41 +178,43 @@ uint8_t I2C_eeprom::determineSize()
171
178
// _pageBlock aligns buffer to page boundaries for writing.
172
179
// and to TWI buffer size
173
180
// returns 0 = OK otherwise error
174
- int I2C_eeprom::_pageBlock (uint16_t address, uint8_t * buffer, uint16_t length, bool incrBuffer)
181
+ int I2C_eeprom::_pageBlock (const uint16_t memoryAddress, const uint8_t * buffer, const uint16_t length, const bool incrBuffer)
175
182
{
183
+ uint16_t addr = memoryAddress;
184
+ uint16_t len = length;
176
185
int rv = 0 ;
177
186
while (length > 0 )
178
187
{
179
- uint8_t bytesUntilPageBoundary = this ->_pageSize - address % this ->_pageSize ;
180
- uint8_t cnt = min (length , bytesUntilPageBoundary);
188
+ uint8_t bytesUntilPageBoundary = this ->_pageSize - addr % this ->_pageSize ;
189
+ uint8_t cnt = min (len , bytesUntilPageBoundary);
181
190
cnt = min (cnt, I2C_TWIBUFFERSIZE);
182
191
183
- int rv = _WriteBlock (address , buffer, cnt);
192
+ int rv = _WriteBlock (addr , buffer, cnt);
184
193
if (rv != 0 ) return rv;
185
194
186
- address += cnt;
195
+ addr += cnt;
187
196
if (incrBuffer) buffer += cnt;
188
- length -= cnt;
197
+ len -= cnt;
189
198
}
190
199
return rv;
191
200
}
192
201
193
202
// supports one and 2 bytes addresses
194
- void I2C_eeprom::_beginTransmission (uint16_t memoryAddress)
203
+ void I2C_eeprom::_beginTransmission (const uint16_t memoryAddress)
195
204
{
196
- Wire.beginTransmission (_deviceAddress);
205
+ Wire.beginTransmission (_deviceAddress);
197
206
198
- if (this ->_isAddressSizeTwoWords )
199
- {
200
- WIRE_WRITE ((memoryAddress >> 8 )); // Address High Byte
201
- }
207
+ if (this ->_isAddressSizeTwoWords )
208
+ {
209
+ WIRE_WRITE ((memoryAddress >> 8 )); // Address High Byte
210
+ }
202
211
203
- WIRE_WRITE ((memoryAddress & 0xFF )); // Address Low Byte (or only byte for chips 16K or smaller that only have one-word addresses)
212
+ WIRE_WRITE ((memoryAddress & 0xFF )); // Address Low Byte (or only byte for chips 16K or smaller that only have one-word addresses)
204
213
}
205
214
206
215
// pre: length <= this->_pageSize && length <= I2C_TWIBUFFERSIZE;
207
216
// returns 0 = OK otherwise error
208
- int I2C_eeprom::_WriteBlock (uint16_t memoryAddress, uint8_t * buffer, uint8_t length)
217
+ int I2C_eeprom::_WriteBlock (const uint16_t memoryAddress, const uint8_t * buffer, const uint8_t length)
209
218
{
210
219
waitEEReady ();
211
220
@@ -220,7 +229,7 @@ int I2C_eeprom::_WriteBlock(uint16_t memoryAddress, uint8_t* buffer, uint8_t len
220
229
221
230
// pre: buffer is large enough to hold length bytes
222
231
// returns bytes read
223
- uint8_t I2C_eeprom::_ReadBlock (uint16_t memoryAddress, uint8_t * buffer, uint8_t length)
232
+ uint8_t I2C_eeprom::_ReadBlock (const uint16_t memoryAddress, uint8_t * buffer, const uint8_t length)
224
233
{
225
234
waitEEReady ();
226
235
0 commit comments