Skip to content

Commit 9f891dc

Browse files
committed
+ version 1.2.02
+ corrected version number + corrected history in .cpp + stricter interface where possible
1 parent a4312c1 commit 9f891dc

File tree

3 files changed

+100
-80
lines changed

3 files changed

+100
-80
lines changed

libraries/I2C_EEPROM/I2C_eeprom.cpp

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: I2C_eeprom.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 1.1.00
4+
// VERSION: 1.2.02
55
// PURPOSE: I2C_eeprom library for Arduino with EEPROM 24LC256 et al.
66
//
77
// HISTORY:
@@ -20,33 +20,34 @@
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
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
2526
//
2627
// Released to the public domain
2728
//
2829

2930
#include <I2C_eeprom.h>
3031

3132
#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
3435
#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
3738
#endif
3839

3940

40-
I2C_eeprom::I2C_eeprom(uint8_t deviceAddress)
41+
I2C_eeprom::I2C_eeprom(const uint8_t deviceAddress)
4142
{
4243
I2C_eeprom(deviceAddress, I2C_EEPROM_PAGESIZE);
4344
}
4445

45-
I2C_eeprom::I2C_eeprom(uint8_t deviceAddress, unsigned int deviceSize)
46+
I2C_eeprom::I2C_eeprom(const uint8_t deviceAddress, const unsigned int deviceSize)
4647
{
4748
_deviceAddress = deviceAddress;
4849

49-
// Chips 16Kbit (2048KB) or smaller only have one-word addresses.
50+
// Chips 16Kbit (2048 Bytes) or smaller only have one-word addresses.
5051
// Also try to guess page size from device size (going by Microchip 24LCXX datasheets here).
5152
if (deviceSize <= 256)
5253
{
@@ -70,7 +71,7 @@ void I2C_eeprom::begin()
7071
Wire.begin();
7172
_lastWrite = 0;
7273

73-
// TWBR is not available on Arduino Due
74+
// TWBR is not available on Arduino Due
7475
#ifdef TWBR
7576
TWBR = 72;
7677
// 0=1000 1=888 2=800 8=500
@@ -81,57 +82,63 @@ void I2C_eeprom::begin()
8182

8283

8384

84-
int I2C_eeprom::writeByte(uint16_t memoryAddress, uint8_t data)
85+
int I2C_eeprom::writeByte(const uint16_t memoryAddress, const uint8_t data)
8586
{
8687
int rv = _WriteBlock(memoryAddress, &data, 1);
8788
return rv;
8889
}
8990

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)
9192
{
9293
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;
9495

9596
int rv = _pageBlock(memoryAddress, buffer, length, false);
9697
return rv;
9798
}
9899

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)
100101
{
101102
int rv = _pageBlock(memoryAddress, buffer, length, true);
102103
return rv;
103104
}
104105

105-
uint8_t I2C_eeprom::readByte(uint16_t memoryAddress)
106+
uint8_t I2C_eeprom::readByte(const uint16_t memoryAddress)
106107
{
107108
uint8_t rdata;
108109
_ReadBlock(memoryAddress, &rdata, 1);
109110
return rdata;
110111
}
111112

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)
113114
{
115+
uint16_t addr = memoryAddress;
116+
uint16_t len = length;
114117
uint16_t rv = 0;
115-
while (length > 0)
118+
while (len > 0)
116119
{
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;
120123
buffer += cnt;
121-
length -= cnt;
124+
len -= cnt;
122125
}
123126
return rv;
124127
}
125128

126129
#ifdef I2C_EEPROM_EXTENDED
127130
// returns 64, 32, 16, 8, 4, 2, 1, 0
128131
// 0 is smaller than 1K
129-
uint8_t I2C_eeprom::determineSize()
132+
int I2C_eeprom::determineSize()
130133
{
131-
uint8_t rv = 0; // unknown
134+
int rv = 0; // unknown
132135
uint8_t orgValues[8];
133136
uint16_t addr;
134137

138+
// try to read a byte to see if connected
139+
rv += _ReadBlock(0x00, orgValues, 1);
140+
if (rv == 0) return -1;
141+
135142
// remember old values, non destructive
136143
for (uint8_t i=0; i<8; i++)
137144
{
@@ -171,41 +178,43 @@ uint8_t I2C_eeprom::determineSize()
171178
// _pageBlock aligns buffer to page boundaries for writing.
172179
// and to TWI buffer size
173180
// 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)
175182
{
183+
uint16_t addr = memoryAddress;
184+
uint16_t len = length;
176185
int rv = 0;
177186
while (length > 0)
178187
{
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);
181190
cnt = min(cnt, I2C_TWIBUFFERSIZE);
182191

183-
int rv = _WriteBlock(address, buffer, cnt);
192+
int rv = _WriteBlock(addr, buffer, cnt);
184193
if (rv != 0) return rv;
185194

186-
address += cnt;
195+
addr += cnt;
187196
if (incrBuffer) buffer += cnt;
188-
length -= cnt;
197+
len -= cnt;
189198
}
190199
return rv;
191200
}
192201

193202
// supports one and 2 bytes addresses
194-
void I2C_eeprom::_beginTransmission(uint16_t memoryAddress)
203+
void I2C_eeprom::_beginTransmission(const uint16_t memoryAddress)
195204
{
196-
Wire.beginTransmission(_deviceAddress);
205+
Wire.beginTransmission(_deviceAddress);
197206

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+
}
202211

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)
204213
}
205214

206215
// pre: length <= this->_pageSize && length <= I2C_TWIBUFFERSIZE;
207216
// 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)
209218
{
210219
waitEEReady();
211220

@@ -220,7 +229,7 @@ int I2C_eeprom::_WriteBlock(uint16_t memoryAddress, uint8_t* buffer, uint8_t len
220229

221230
// pre: buffer is large enough to hold length bytes
222231
// 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)
224233
{
225234
waitEEReady();
226235

libraries/I2C_EEPROM/I2C_eeprom.h

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// FILE: I2C_eeprom.h
55
// AUTHOR: Rob Tillaart
66
// PURPOSE: I2C_eeprom library for Arduino with EEPROM 24LC256 et al.
7-
// VERSION: 1.1.00
7+
// VERSION: 1.2.02
88
// HISTORY: See I2C_eeprom.cpp
99
// URL: http://arduino.cc/playground/Main/LibraryForI2CEEPROM
1010
//
@@ -21,7 +21,7 @@
2121
#include "Wiring.h"
2222
#endif
2323

24-
#define I2C_EEPROM_VERSION "1.1.00"
24+
#define I2C_EEPROM_VERSION "1.2.02"
2525

2626
// The DEFAULT page size. This is overriden if you use the second constructor.
2727
// I2C_EEPROM_PAGESIZE must be multiple of 2 e.g. 16, 32 or 64
@@ -35,37 +35,37 @@
3535
// to break blocking read/write after n millis()
3636
#define I2C_EEPROM_TIMEOUT 1000
3737

38-
// comment next line to keep lib small
38+
// comment next line to keep lib small (idea a read only lib?)
3939
#define I2C_EEPROM_EXTENDED
4040

4141
class I2C_eeprom
4242
{
4343
public:
4444
/**
45-
* Initializes the EEPROM with a default pagesize of I2C_EEPROM_PAGESIZE.
46-
*/
47-
I2C_eeprom(uint8_t deviceAddress);
45+
* Initializes the EEPROM with a default pagesize of I2C_EEPROM_PAGESIZE.
46+
*/
47+
I2C_eeprom(const 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-
*/
57-
I2C_eeprom(uint8_t deviceAddress, unsigned int deviceSize);
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+
*/
57+
I2C_eeprom(const uint8_t deviceAddress, const unsigned int deviceSize);
5858

5959
void begin();
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);
60+
int writeByte(const uint16_t memoryAddress, const uint8_t value);
61+
int writeBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint16_t length);
62+
int setBlock(const uint16_t memoryAddress, const uint8_t value, const uint16_t length);
6363

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

6767
#ifdef I2C_EEPROM_EXTENDED
68-
uint8_t determineSize();
68+
int determineSize();
6969
#endif
7070

7171
private:
@@ -77,15 +77,15 @@ class I2C_eeprom
7777
bool _isAddressSizeTwoWords;
7878

7979
/**
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);
80+
* Begins wire transmission and selects the given address to write/read.
81+
*
82+
* @param memoryAddress Address to write/read
83+
*/
84+
void _beginTransmission(const uint16_t memoryAddress);
85+
86+
int _pageBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint16_t length, const bool incrBuffer);
87+
int _WriteBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint8_t length);
88+
uint8_t _ReadBlock(const uint16_t memoryAddress, uint8_t* buffer, const uint8_t length);
8989

9090
void waitEEReady();
9191
};

libraries/I2C_EEPROM/examples/I2C_eeprom_test/I2C_eeprom_test.ino

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: I2C_eeprom_test.ino
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.07
4+
// VERSION: 0.1.08
55
// PURPOSE: show/test I2C_EEPROM library
66
//
77

@@ -15,7 +15,7 @@
1515

1616
I2C_eeprom ee(0x50);
1717

18-
uint32_t totals = 0;
18+
uint32_t start, diff, totals = 0;
1919

2020
void setup()
2121
{
@@ -28,6 +28,27 @@ void setup()
2828
SERIAL_OUT.print(I2C_EEPROM_VERSION);
2929
SERIAL_OUT.println("\n");
3030

31+
SERIAL_OUT.println("\nTEST: determine size");
32+
start = micros();
33+
int size = ee.determineSize();
34+
diff = micros() - start;
35+
SERIAL_OUT.print("TIME: ");
36+
SERIAL_OUT.println(diff);
37+
if (size > 0)
38+
{
39+
SERIAL_OUT.print("SIZE: ");
40+
SERIAL_OUT.print(size);
41+
SERIAL_OUT.println(" KB");
42+
} else if (size = 0)
43+
{
44+
SERIAL_OUT.println("WARNING: Can't determine eeprom size");
45+
}
46+
else
47+
{
48+
SERIAL_OUT.println("ERROR: Can't find eeprom\nstopped...");
49+
while(1);
50+
}
51+
3152
SERIAL_OUT.println("\nTEST: 64 byte page boundary writeBlock");
3253
ee.setBlock(0, 0, 128);
3354
dumpEEPROM(0, 128);
@@ -165,16 +186,6 @@ void setup()
165186
// does it go well?
166187
SERIAL_OUT.println(xx);
167188

168-
SERIAL_OUT.println("\nTEST: determine size");
169-
start = micros();
170-
int size = ee.determineSize();
171-
diff = micros() - start;
172-
SERIAL_OUT.print("TIME: ");
173-
SERIAL_OUT.println(diff);
174-
SERIAL_OUT.print("SIZE: ");
175-
SERIAL_OUT.print(size);
176-
SERIAL_OUT.println(" KB");
177-
178189
SERIAL_OUT.println("\tDone...");
179190
}
180191

0 commit comments

Comments
 (0)