26
26
27
27
#include < I2C_eeprom.h>
28
28
29
+ #if defined(ARDUINO) && ARDUINO >= 100
30
+ #define WIRE_WRITE Wire.write
31
+ #else
32
+ #define WIRE_WRITE Wire.send
33
+ #endif
34
+
35
+
29
36
I2C_eeprom::I2C_eeprom (uint8_t device)
30
37
{
31
38
_deviceAddress = device;
39
+ isAddressSizeTwoWords = true ;
40
+ this ->pageSize = I2C_EEPROM_PAGESIZE;
41
+ }
42
+
43
+ I2C_eeprom::I2C_eeprom (uint8_t device, unsigned int deviceSize) {
44
+ _deviceAddress = device;
45
+
46
+ // Chips 16Kbit (2048KB) or smaller only have one-word addresses.
47
+ // Also try to guess page size from device size (going by Microchip 24LCXX datasheets here).
48
+ if (deviceSize > 256 * 8 ) {
49
+ this ->isAddressSizeTwoWords = true ;
50
+ this ->pageSize = 32 ;
51
+ }
52
+ else {
53
+ this ->isAddressSizeTwoWords = false ;
54
+
55
+ if (deviceSize <= 256 ) {
56
+ this ->pageSize = 8 ;
57
+ }
58
+ else {
59
+ this ->pageSize = 16 ;
60
+ }
61
+ }
32
62
}
33
63
34
64
void I2C_eeprom::begin ()
35
65
{
36
66
Wire.begin ();
37
67
_lastWrite = 0 ;
68
+
69
+ // TWBR is not available on Arduino Due
70
+ #ifdef TWBR
38
71
TWBR = 72 ;
39
72
// 0=1000 1=888 2=800 8=500
40
73
// 12=400KHz 24=250 32=200 72=100 152=50
41
74
// F_CPU/16+(2*TWBR) // TWBR is a uint8_t
75
+ #endif
42
76
}
43
77
78
+
79
+
44
80
int I2C_eeprom::writeByte (uint16_t address, uint8_t data)
45
81
{
46
82
int rv = _WriteBlock (address, &data, 1 );
@@ -136,7 +172,7 @@ int I2C_eeprom::_pageBlock(uint16_t address, uint8_t* buffer, uint16_t length, b
136
172
int rv = 0 ;
137
173
while (length > 0 )
138
174
{
139
- uint8_t bytesUntilPageBoundary = I2C_EEPROM_PAGESIZE - address % I2C_EEPROM_PAGESIZE ;
175
+ uint8_t bytesUntilPageBoundary = this -> pageSize - address % this -> pageSize ;
140
176
uint8_t cnt = min (length, bytesUntilPageBoundary);
141
177
cnt = min (cnt, I2C_TWIBUFFERSIZE);
142
178
@@ -150,22 +186,27 @@ int I2C_eeprom::_pageBlock(uint16_t address, uint8_t* buffer, uint16_t length, b
150
186
return rv;
151
187
}
152
188
153
- // pre: length <= I2C_EEPROM_PAGESIZE && length <= I2C_TWIBUFFERSIZE;
189
+
190
+ void I2C_eeprom::_beginTransmission (uint16_t eeaddress){
191
+ Wire.beginTransmission (_deviceAddress);
192
+
193
+ if (this ->isAddressSizeTwoWords ) {
194
+ WIRE_WRITE ((eeaddress >> 8 )); // Address High Byte
195
+ }
196
+
197
+ WIRE_WRITE ((eeaddress & 0xFF )); // Address Low Byte (or only byte for chips 16K or smaller that only have one-word addresses)
198
+ }
199
+
200
+ // pre: length <= this->pageSize && length <= I2C_TWIBUFFERSIZE;
154
201
// returns 0 = OK otherwise error
155
202
int I2C_eeprom::_WriteBlock (uint16_t address, uint8_t * buffer, uint8_t length)
156
203
{
157
204
waitEEReady ();
158
205
159
- Wire.beginTransmission (_deviceAddress);
160
- #if defined(ARDUINO) && ARDUINO >= 100
161
- Wire.write (address >> 8 );
162
- Wire.write (address & 0xFF );
163
- Wire.write (buffer, length);
164
- #else
165
- Wire.send (address >> 8 );
166
- Wire.send (address & 0xFF );
167
- Wire.send (buffer, length);
168
- #endif
206
+ this ->_beginTransmission (address);
207
+
208
+ WIRE_WRITE (buffer, length);
209
+
169
210
int rv = Wire.endTransmission ();
170
211
_lastWrite = micros ();
171
212
return rv;
@@ -177,14 +218,8 @@ uint8_t I2C_eeprom::_ReadBlock(uint16_t address, uint8_t* buffer, uint8_t length
177
218
{
178
219
waitEEReady ();
179
220
180
- Wire.beginTransmission (_deviceAddress);
181
- #if defined(ARDUINO) && ARDUINO >= 100
182
- Wire.write (address >> 8 );
183
- Wire.write (address & 0xFF );
184
- #else
185
- Wire.send (address >> 8 );
186
- Wire.send (address & 0xFF );
187
- #endif
221
+ this ->_beginTransmission (address);
222
+
188
223
int rv = Wire.endTransmission ();
189
224
if (rv != 0 ) return 0 ; // error
190
225
0 commit comments