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
25
//
24
26
// Released to the public domain
25
27
//
26
28
27
29
#include < I2C_eeprom.h>
28
30
29
31
#if defined(ARDUINO) && ARDUINO >= 100
30
- #define WIRE_WRITE Wire.write
32
+ #define WIRE_WRITE Wire.write
33
+ #define WIRE_READ Wire.read
31
34
#else
32
- #define WIRE_WRITE Wire.send
35
+ #define WIRE_WRITE Wire.send
36
+ #define WIRE_READ Wire.receive
33
37
#endif
34
38
35
39
36
- I2C_eeprom::I2C_eeprom (uint8_t device )
40
+ I2C_eeprom::I2C_eeprom (uint8_t deviceAddress )
37
41
{
38
- _deviceAddress = device;
39
- isAddressSizeTwoWords = true ;
40
- this ->pageSize = I2C_EEPROM_PAGESIZE;
42
+ I2C_eeprom (deviceAddress, I2C_EEPROM_PAGESIZE);
41
43
}
42
44
43
- I2C_eeprom::I2C_eeprom (uint8_t device, unsigned int deviceSize) {
44
- _deviceAddress = device;
45
+ I2C_eeprom::I2C_eeprom (uint8_t deviceAddress, unsigned int deviceSize)
46
+ {
47
+ _deviceAddress = deviceAddress;
45
48
46
49
// Chips 16Kbit (2048KB) or smaller only have one-word addresses.
47
50
// 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
+ if (deviceSize <= 256 )
52
+ {
53
+ this ->_isAddressSizeTwoWords = false ;
54
+ this ->_pageSize = 8 ;
51
55
}
52
- else {
53
- this -> isAddressSizeTwoWords = false ;
54
-
55
- if (deviceSize <= 256 ) {
56
- this -> pageSize = 8 ;
57
- }
58
- else {
59
- this ->pageSize = 16 ;
60
- }
56
+ else if (deviceSize <= 256 * 8 )
57
+ {
58
+ this -> _isAddressSizeTwoWords = false ;
59
+ this -> _pageSize = 16 ;
60
+ }
61
+ else
62
+ {
63
+ this ->_isAddressSizeTwoWords = true ;
64
+ this -> _pageSize = 32 ;
61
65
}
62
66
}
63
67
@@ -66,53 +70,53 @@ void I2C_eeprom::begin()
66
70
Wire.begin ();
67
71
_lastWrite = 0 ;
68
72
69
- // TWBR is not available on Arduino Due
70
- #ifdef TWBR
73
+ // TWBR is not available on Arduino Due
74
+ #ifdef TWBR
71
75
TWBR = 72 ;
72
76
// 0=1000 1=888 2=800 8=500
73
77
// 12=400KHz 24=250 32=200 72=100 152=50
74
78
// F_CPU/16+(2*TWBR) // TWBR is a uint8_t
75
- #endif
79
+ #endif
76
80
}
77
81
78
82
79
83
80
- int I2C_eeprom::writeByte (uint16_t address , uint8_t data)
84
+ int I2C_eeprom::writeByte (uint16_t memoryAddress , uint8_t data)
81
85
{
82
- int rv = _WriteBlock (address , &data, 1 );
86
+ int rv = _WriteBlock (memoryAddress , &data, 1 );
83
87
return rv;
84
88
}
85
89
86
- int I2C_eeprom::setBlock (uint16_t address , uint8_t data, uint16_t length)
90
+ int I2C_eeprom::setBlock (uint16_t memoryAddress , uint8_t data, uint16_t length)
87
91
{
88
92
uint8_t buffer[I2C_TWIBUFFERSIZE];
89
- for (uint8_t i =0 ; i< I2C_TWIBUFFERSIZE; i++) buffer[i] = data;
93
+ for (uint8_t i = 0 ; i< I2C_TWIBUFFERSIZE; i++) buffer[i] = data;
90
94
91
- int rv = _pageBlock (address , buffer, length, false );
95
+ int rv = _pageBlock (memoryAddress , buffer, length, false );
92
96
return rv;
93
97
}
94
98
95
- int I2C_eeprom::writeBlock (uint16_t address , uint8_t * buffer, uint16_t length)
99
+ int I2C_eeprom::writeBlock (uint16_t memoryAddress , uint8_t * buffer, uint16_t length)
96
100
{
97
- int rv = _pageBlock (address , buffer, length, true );
101
+ int rv = _pageBlock (memoryAddress , buffer, length, true );
98
102
return rv;
99
103
}
100
104
101
- uint8_t I2C_eeprom::readByte (uint16_t address )
105
+ uint8_t I2C_eeprom::readByte (uint16_t memoryAddress )
102
106
{
103
107
uint8_t rdata;
104
- _ReadBlock (address , &rdata, 1 );
108
+ _ReadBlock (memoryAddress , &rdata, 1 );
105
109
return rdata;
106
110
}
107
111
108
- uint16_t I2C_eeprom::readBlock (uint16_t address , uint8_t * buffer, uint16_t length)
112
+ uint16_t I2C_eeprom::readBlock (uint16_t memoryAddress , uint8_t * buffer, uint16_t length)
109
113
{
110
114
uint16_t rv = 0 ;
111
115
while (length > 0 )
112
116
{
113
117
uint8_t cnt = min (length, I2C_TWIBUFFERSIZE);
114
- rv += _ReadBlock (address , buffer, cnt);
115
- address += cnt;
118
+ rv += _ReadBlock (memoryAddress , buffer, cnt);
119
+ memoryAddress += cnt;
116
120
buffer += cnt;
117
121
length -= cnt;
118
122
}
@@ -172,7 +176,7 @@ int I2C_eeprom::_pageBlock(uint16_t address, uint8_t* buffer, uint16_t length, b
172
176
int rv = 0 ;
173
177
while (length > 0 )
174
178
{
175
- uint8_t bytesUntilPageBoundary = this ->pageSize - address % this ->pageSize ;
179
+ uint8_t bytesUntilPageBoundary = this ->_pageSize - address % this ->_pageSize ;
176
180
uint8_t cnt = min (length, bytesUntilPageBoundary);
177
181
cnt = min (cnt, I2C_TWIBUFFERSIZE);
178
182
@@ -186,24 +190,26 @@ int I2C_eeprom::_pageBlock(uint16_t address, uint8_t* buffer, uint16_t length, b
186
190
return rv;
187
191
}
188
192
193
+ // supports one and 2 bytes addresses
194
+ void I2C_eeprom::_beginTransmission (uint16_t memoryAddress)
195
+ {
196
+ Wire.beginTransmission (_deviceAddress);
189
197
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
- }
198
+ if (this ->_isAddressSizeTwoWords )
199
+ {
200
+ WIRE_WRITE ((memoryAddress >> 8 )); // Address High Byte
201
+ }
196
202
197
- WIRE_WRITE ((eeaddress & 0xFF )); // Address Low Byte (or only byte for chips 16K or smaller that only have one-word addresses)
203
+ WIRE_WRITE ((memoryAddress & 0xFF )); // Address Low Byte (or only byte for chips 16K or smaller that only have one-word addresses)
198
204
}
199
205
200
- // pre: length <= this->pageSize && length <= I2C_TWIBUFFERSIZE;
206
+ // pre: length <= this->_pageSize && length <= I2C_TWIBUFFERSIZE;
201
207
// returns 0 = OK otherwise error
202
- int I2C_eeprom::_WriteBlock (uint16_t address , uint8_t * buffer, uint8_t length)
208
+ int I2C_eeprom::_WriteBlock (uint16_t memoryAddress , uint8_t * buffer, uint8_t length)
203
209
{
204
210
waitEEReady ();
205
211
206
- this ->_beginTransmission (address );
212
+ this ->_beginTransmission (memoryAddress );
207
213
208
214
WIRE_WRITE (buffer, length);
209
215
@@ -214,11 +220,11 @@ int I2C_eeprom::_WriteBlock(uint16_t address, uint8_t* buffer, uint8_t length)
214
220
215
221
// pre: buffer is large enough to hold length bytes
216
222
// returns bytes read
217
- uint8_t I2C_eeprom::_ReadBlock (uint16_t address , uint8_t * buffer, uint8_t length)
223
+ uint8_t I2C_eeprom::_ReadBlock (uint16_t memoryAddress , uint8_t * buffer, uint8_t length)
218
224
{
219
225
waitEEReady ();
220
226
221
- this ->_beginTransmission (address );
227
+ this ->_beginTransmission (memoryAddress );
222
228
223
229
int rv = Wire.endTransmission ();
224
230
if (rv != 0 ) return 0 ; // error
@@ -228,11 +234,7 @@ uint8_t I2C_eeprom::_ReadBlock(uint16_t address, uint8_t* buffer, uint8_t length
228
234
uint32_t before = millis ();
229
235
while ((cnt < length) && ((millis () - before) < I2C_EEPROM_TIMEOUT))
230
236
{
231
- #if defined(ARDUINO) && ARDUINO >= 100
232
- if (Wire.available ()) buffer[cnt++] = Wire.read ();
233
- #else
234
- if (Wire.available ()) buffer[cnt++] = Wire.receive ();
235
- #endif
237
+ if (Wire.available ()) buffer[cnt++] = WIRE_READ ();
236
238
}
237
239
return cnt;
238
240
}
0 commit comments