+ version 1.0.04 (stable)

+ moved the waitEEReady() so writing profits more than reading looses.
  Performance numbers have increased quite well! both 100 and 400 KHz
This commit is contained in:
Rob Tillaart 2013-11-03 19:43:10 +01:00
parent 586a9017ee
commit afcb60364c
3 changed files with 52 additions and 14 deletions

View File

@ -1,7 +1,7 @@
//
// FILE: I2C_eeprom.cpp
// AUTHOR: Rob Tillaart
// VERSION: 1.0.03
// VERSION: 1.0.04
// PURPOSE: Simple I2C_eeprom library for Arduino with EEPROM 24LC256 et al.
//
// HISTORY:
@ -13,6 +13,7 @@
// 1.0.01 - 2013-11-01 fixed writeBlock bug, refactor
// 1.0.02 - 2013-11-03 optimize internal buffers, refactor
// 1.0.03 - 2013-11-03 refactor 5 millis() write-latency
// 1.0.04 - 2013-11-03 fix bug in readBlock, moved waitEEReady() -> more efficient.
//
// Released to the public domain
//
@ -28,7 +29,7 @@ I2C_eeprom::I2C_eeprom(uint8_t device)
{
_deviceAddress = device;
Wire.begin();
// TWBR = 12; // 12 = 400Khz 72 = 100 FCPU/16+(2*TWBR)
TWBR = 12; // 12 = 400Khz 72 = 100 FCPU/16+(2*TWBR)
}
int I2C_eeprom::writeByte(uint16_t address, uint8_t data)
@ -46,7 +47,7 @@ int I2C_eeprom::setBlock(uint16_t address, uint8_t data, uint16_t length)
return rv;
}
void I2C_eeprom::writeBlock(uint16_t address, uint8_t* buffer, uint16_t length)
int I2C_eeprom::writeBlock(uint16_t address, uint8_t* buffer, uint16_t length)
{
int rv = _pageBlock(address, buffer, length, true); // todo check return value..
return rv;
@ -66,9 +67,7 @@ int I2C_eeprom::readBlock(uint16_t address, uint8_t* buffer, uint16_t length)
while (length > 0)
{
uint8_t cnt = min(length, I2C_TWIBUFFERSIZE);
rv = _ReadBlock(address, buffer, cnt); // todo check return value..
if (rv != 0) return rv;
rv += _ReadBlock(address, buffer, cnt);
address += cnt;
buffer += cnt;
length -= cnt;
@ -105,6 +104,8 @@ int I2C_eeprom::_pageBlock(uint16_t address, uint8_t* buffer, uint16_t length, b
// pre: length <= I2C_EEPROM_PAGESIZE && length <= I2C_TWIBUFFERSIZE;
int I2C_eeprom::_WriteBlock(uint16_t address, uint8_t* buffer, uint8_t length)
{
waitEEReady();
Wire.beginTransmission(_deviceAddress);
#if defined(ARDUINO) && ARDUINO >= 100
Wire.write((int)(address >> 8));
@ -119,8 +120,6 @@ int I2C_eeprom::_WriteBlock(uint16_t address, uint8_t* buffer, uint8_t length)
#endif
int rv = Wire.endTransmission();
waitEEReady();
return rv;
}
@ -128,6 +127,8 @@ int I2C_eeprom::_WriteBlock(uint16_t address, uint8_t* buffer, uint8_t length)
// pre: buffer is large enough to hold length bytes
int I2C_eeprom::_ReadBlock(uint16_t address, uint8_t* buffer, uint8_t length)
{
waitEEReady();
Wire.beginTransmission(_deviceAddress);
#if defined(ARDUINO) && ARDUINO >= 100
Wire.write((int)(address >> 8));

View File

@ -4,7 +4,7 @@
// FILE: I2C_eeprom.h
// AUTHOR: Rob Tillaart
// PURPOSE: Simple I2C_eeprom library for Arduino with EEPROM 24LC256 et al.
// VERSION: 1.0.03
// VERSION: 1.0.04
// HISTORY: See I2C_eeprom.cpp
// URL: http://arduino.cc/playground/Main/LibraryForI2CEEPROM
//
@ -21,7 +21,7 @@
#include "Wiring.h"
#endif
#define I2C_EEPROM_VERSION "1.0.03"
#define I2C_EEPROM_VERSION "1.0.04"
// I2C_EEPROM_PAGESIZE must be multiple of 2 e.g. 16, 32 or 64
// 24LC256 -> 64 bytes
@ -38,12 +38,12 @@ class I2C_eeprom
public:
I2C_eeprom(uint8_t deviceAddress);
void writeByte(uint16_t address, uint8_t value);
void writeBlock(uint16_t address, uint8_t* buffer, uint16_t length);
void setBlock(uint16_t address, uint8_t value, uint16_t length);
int writeByte(uint16_t address, uint8_t value);
int writeBlock(uint16_t address, uint8_t* buffer, uint16_t length);
int setBlock(uint16_t address, uint8_t value, uint16_t length);
uint8_t readByte(uint16_t address);
void readBlock(uint16_t address, uint8_t* buffer, uint16_t length);
int readBlock(uint16_t address, uint8_t* buffer, uint16_t length);
private:
uint8_t _deviceAddress;

View File

@ -93,6 +93,41 @@ void setup()
diff = micros() - start;
Serial.println(diff);
// same tests but now with a 5 millisec delay in between.
delay(5);
Serial.print("\nTEST: timing writeByte()\t");
start = micros();
ee.writeByte(10, 1);
diff = micros() - start;
Serial.println(diff);
delay(5);
Serial.print("TEST: timing writeBlock(50)\t");
start = micros();
ee.writeBlock(10, (uint8_t *) &data2, 50);
diff = micros() - start;
Serial.println(diff);
delay(5);
Serial.print("TEST: timing readByte()\t\t");
start = micros();
ee.readByte(10);
diff = micros() - start;
Serial.println(diff);
delay(5);
Serial.print("TEST: timing readBlock(50)\t");
start = micros();
int xx = ee.readBlock(10, (uint8_t *) &data2, 50);
diff = micros() - start;
Serial.println(diff);
// does it go well?
Serial.println(xx);
Serial.println("\tDone...");
}
@ -126,3 +161,5 @@ void dumpEEPROM(uint16_t addr, uint16_t length)
}
// END OF FILE