Skip to content

Commit 1f8c5f0

Browse files
committed
+ refactored
+ code style + names more explicit e.g. address -> memoryAddress
1 parent 773a1fd commit 1f8c5f0

File tree

4 files changed

+112
-108
lines changed

4 files changed

+112
-108
lines changed

libraries/I2C_EEPROM/I2C_eeprom.cpp

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,48 @@
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
}

libraries/I2C_EEPROM/I2C_eeprom.h

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,51 +42,50 @@ class I2C_eeprom
4242
{
4343
public:
4444
/**
45-
* Initializes the EEPROM with a default pagesize of I2C_EEPROM_PAGESIZE.
46-
*/
45+
* Initializes the EEPROM with a default pagesize of I2C_EEPROM_PAGESIZE.
46+
*/
4747
I2C_eeprom(uint8_t deviceAddress);
4848

4949
/**
50-
* Initializes the EEPROM for the given device address.
51-
*
52-
* It will try to guess page size and address word size based on the size of the device.
53-
*
54-
* @param deviceAddress Byte address of the device.
55-
* @param deviceSize Max size in bytes of the device (divide your device size in Kbits by 8)
56-
*/
50+
* Initializes the EEPROM for the given device address.
51+
*
52+
* It will try to guess page size and address word size based on the size of the device.
53+
*
54+
* @param deviceAddress Byte address of the device.
55+
* @param deviceSize Max size in bytes of the device (divide your device size in Kbits by 8)
56+
*/
5757
I2C_eeprom(uint8_t deviceAddress, unsigned int deviceSize);
5858

5959
void begin();
60-
int writeByte(uint16_t address, uint8_t value);
61-
int writeBlock(uint16_t address, uint8_t* buffer, uint16_t length);
62-
int setBlock(uint16_t address, uint8_t value, uint16_t length);
60+
int writeByte(uint16_t memoryAddress, uint8_t value);
61+
int writeBlock(uint16_t memoryAddress, uint8_t* buffer, uint16_t length);
62+
int setBlock(uint16_t memoryAddress, uint8_t value, uint16_t length);
6363

64-
uint8_t readByte(uint16_t address);
65-
uint16_t readBlock(uint16_t address, uint8_t* buffer, uint16_t length);
64+
uint8_t readByte(uint16_t memoryAddress);
65+
uint16_t readBlock(uint16_t memoryAddress, uint8_t* buffer, uint16_t length);
6666

6767
#ifdef I2C_EEPROM_EXTENDED
6868
uint8_t determineSize();
6969
#endif
7070

7171
private:
7272
uint8_t _deviceAddress;
73-
uint32_t _lastWrite; // for waitEEReady
74-
75-
uint8_t pageSize;
73+
uint32_t _lastWrite; // for waitEEReady
74+
uint8_t _pageSize;
7675

7776
// for some smaller chips that use one-word addresses
78-
bool isAddressSizeTwoWords;
77+
bool _isAddressSizeTwoWords;
7978

8079
/**
81-
* Begins wire transmission and selects the given address to write/read.
82-
*
83-
* @param eeaddress Address to write/read
84-
*/
85-
void _beginTransmission(uint16_t eeaddress);
86-
87-
int _pageBlock(uint16_t address, uint8_t* buffer, uint16_t length, bool incrBuffer);
88-
int _WriteBlock(uint16_t address, uint8_t* buffer, uint8_t length);
89-
uint8_t _ReadBlock(uint16_t address, uint8_t* buffer, uint8_t length);
80+
* Begins wire transmission and selects the given address to write/read.
81+
*
82+
* @param memoryAddress Address to write/read
83+
*/
84+
void _beginTransmission(uint16_t memoryAddress);
85+
86+
int _pageBlock(uint16_t memoryAddress, uint8_t* buffer, uint16_t length, bool incrBuffer);
87+
int _WriteBlock(uint16_t memoryAddress, uint8_t* buffer, uint8_t length);
88+
uint8_t _ReadBlock(uint16_t memoryAddress, uint8_t* buffer, uint8_t length);
9089

9190
void waitEEReady();
9291
};

0 commit comments

Comments
 (0)