2020// 1.1.00 - 2013-11-13 added begin() function (Note breaking interface)
2121// use faster block Wire.write()
2222// 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
2325//
2426// Released to the public domain
2527//
2628
2729#include < I2C_eeprom.h>
2830
2931#if defined(ARDUINO) && ARDUINO >= 100
30- #define WIRE_WRITE Wire.write
32+ #define WIRE_WRITE Wire.write
33+ #define WIRE_READ Wire.read
3134#else
32- #define WIRE_WRITE Wire.send
35+ #define WIRE_WRITE Wire.send
36+ #define WIRE_READ Wire.receive
3337#endif
3438
3539
36- I2C_eeprom::I2C_eeprom (uint8_t device )
40+ I2C_eeprom::I2C_eeprom (uint8_t deviceAddress )
3741{
38- _deviceAddress = device;
39- isAddressSizeTwoWords = true ;
40- this ->pageSize = I2C_EEPROM_PAGESIZE;
42+ I2C_eeprom (deviceAddress, I2C_EEPROM_PAGESIZE);
4143}
4244
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;
4548
4649 // Chips 16Kbit (2048KB) or smaller only have one-word addresses.
4750 // 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 ;
5155 }
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 ;
6165 }
6266}
6367
@@ -66,53 +70,53 @@ void I2C_eeprom::begin()
6670 Wire.begin ();
6771 _lastWrite = 0 ;
6872
69- // TWBR is not available on Arduino Due
70- #ifdef TWBR
73+ // TWBR is not available on Arduino Due
74+ #ifdef TWBR
7175 TWBR = 72 ;
7276 // 0=1000 1=888 2=800 8=500
7377 // 12=400KHz 24=250 32=200 72=100 152=50
7478 // F_CPU/16+(2*TWBR) // TWBR is a uint8_t
75- #endif
79+ #endif
7680}
7781
7882
7983
80- int I2C_eeprom::writeByte (uint16_t address , uint8_t data)
84+ int I2C_eeprom::writeByte (uint16_t memoryAddress , uint8_t data)
8185{
82- int rv = _WriteBlock (address , &data, 1 );
86+ int rv = _WriteBlock (memoryAddress , &data, 1 );
8387 return rv;
8488}
8589
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)
8791{
8892 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;
9094
91- int rv = _pageBlock (address , buffer, length, false );
95+ int rv = _pageBlock (memoryAddress , buffer, length, false );
9296 return rv;
9397}
9498
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)
96100{
97- int rv = _pageBlock (address , buffer, length, true );
101+ int rv = _pageBlock (memoryAddress , buffer, length, true );
98102 return rv;
99103}
100104
101- uint8_t I2C_eeprom::readByte (uint16_t address )
105+ uint8_t I2C_eeprom::readByte (uint16_t memoryAddress )
102106{
103107 uint8_t rdata;
104- _ReadBlock (address , &rdata, 1 );
108+ _ReadBlock (memoryAddress , &rdata, 1 );
105109 return rdata;
106110}
107111
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)
109113{
110114 uint16_t rv = 0 ;
111115 while (length > 0 )
112116 {
113117 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;
116120 buffer += cnt;
117121 length -= cnt;
118122 }
@@ -172,7 +176,7 @@ int I2C_eeprom::_pageBlock(uint16_t address, uint8_t* buffer, uint16_t length, b
172176 int rv = 0 ;
173177 while (length > 0 )
174178 {
175- uint8_t bytesUntilPageBoundary = this ->pageSize - address % this ->pageSize ;
179+ uint8_t bytesUntilPageBoundary = this ->_pageSize - address % this ->_pageSize ;
176180 uint8_t cnt = min (length, bytesUntilPageBoundary);
177181 cnt = min (cnt, I2C_TWIBUFFERSIZE);
178182
@@ -186,24 +190,26 @@ int I2C_eeprom::_pageBlock(uint16_t address, uint8_t* buffer, uint16_t length, b
186190 return rv;
187191}
188192
193+ // supports one and 2 bytes addresses
194+ void I2C_eeprom::_beginTransmission (uint16_t memoryAddress)
195+ {
196+ Wire.beginTransmission (_deviceAddress);
189197
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+ }
196202
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)
198204}
199205
200- // pre: length <= this->pageSize && length <= I2C_TWIBUFFERSIZE;
206+ // pre: length <= this->_pageSize && length <= I2C_TWIBUFFERSIZE;
201207// 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)
203209{
204210 waitEEReady ();
205211
206- this ->_beginTransmission (address );
212+ this ->_beginTransmission (memoryAddress );
207213
208214 WIRE_WRITE (buffer, length);
209215
@@ -214,11 +220,11 @@ int I2C_eeprom::_WriteBlock(uint16_t address, uint8_t* buffer, uint8_t length)
214220
215221// pre: buffer is large enough to hold length bytes
216222// 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)
218224{
219225 waitEEReady ();
220226
221- this ->_beginTransmission (address );
227+ this ->_beginTransmission (memoryAddress );
222228
223229 int rv = Wire.endTransmission ();
224230 if (rv != 0 ) return 0 ; // error
@@ -228,11 +234,7 @@ uint8_t I2C_eeprom::_ReadBlock(uint16_t address, uint8_t* buffer, uint8_t length
228234 uint32_t before = millis ();
229235 while ((cnt < length) && ((millis () - before) < I2C_EEPROM_TIMEOUT))
230236 {
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 ();
236238 }
237239 return cnt;
238240}
0 commit comments