mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
+ fixed 64 bit boundary bug
+ refactor _readBlock + return value
This commit is contained in:
parent
d406766659
commit
aab07947c8
@ -1,12 +1,14 @@
|
||||
//
|
||||
// FILE: I2C_eeprom.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.00
|
||||
// VERSION: 0.2.01
|
||||
// PURPOSE: Simple I2C_eeprom library for Arduino with EEPROM 24LC256 et al.
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.00 - 2011-01-21 initial version
|
||||
// 0.1.01 - 2011-02-07 added setBlock function
|
||||
// 0.2.00 - 2011-02-11 fixed 64 bit boundary bug
|
||||
// 0.2.01 - 2011-08-13 _readBlock made more robust + return value
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
@ -38,16 +40,16 @@ void I2C_eeprom::writeBlock(unsigned int address, uint8_t* buffer, int length)
|
||||
int le = endOfPage(address);
|
||||
if (le > 0)
|
||||
{
|
||||
_WriteBlock(address, buffer, le);
|
||||
_WriteBlock(address, buffer, le); // todo check return value..
|
||||
address += le;
|
||||
buffer += le;
|
||||
length -= le;
|
||||
}
|
||||
|
||||
// write the rest at BLOCKSIZE (16) byte boundaries
|
||||
// write the rest at BLOCKSIZE (16) byte boundaries ///
|
||||
while (length > 0)
|
||||
{
|
||||
_WriteBlock(address, buffer, min(length, BLOCKSIZE));
|
||||
_WriteBlock(address, buffer, min(length, BLOCKSIZE)); // todo check return value..
|
||||
address += BLOCKSIZE;
|
||||
buffer += BLOCKSIZE;
|
||||
length -= BLOCKSIZE;
|
||||
@ -70,7 +72,7 @@ void I2C_eeprom::setBlock(unsigned int address, uint8_t data, int length)
|
||||
|
||||
while (length > 0)
|
||||
{
|
||||
_WriteBlock(address, buffer, min(length, BLOCKSIZE));
|
||||
_WriteBlock(address, buffer, min(length, BLOCKSIZE)); // // todo check return value..
|
||||
address += BLOCKSIZE;
|
||||
length -= BLOCKSIZE;
|
||||
}
|
||||
@ -80,7 +82,7 @@ void I2C_eeprom::setBlock(unsigned int address, uint8_t data, int length)
|
||||
uint8_t I2C_eeprom::readByte(unsigned int address)
|
||||
{
|
||||
uint8_t rdata;
|
||||
_ReadBlock(address, &rdata, 1);
|
||||
_ReadBlock(address, &rdata, 1); // todo check return value..
|
||||
return rdata;
|
||||
}
|
||||
|
||||
@ -89,7 +91,7 @@ void I2C_eeprom::readBlock(unsigned int address, uint8_t* buffer, int length)
|
||||
{
|
||||
while (length > 0)
|
||||
{
|
||||
_ReadBlock(address, buffer, min(length, BLOCKSIZE));
|
||||
_ReadBlock(address, buffer, min(length, BLOCKSIZE)); // todo check return value..
|
||||
address += BLOCKSIZE;
|
||||
buffer += BLOCKSIZE;
|
||||
length -= BLOCKSIZE;
|
||||
@ -125,15 +127,21 @@ void I2C_eeprom::_WriteBlock(unsigned int address, uint8_t* buffer, uint8_t leng
|
||||
}
|
||||
|
||||
// pre: buffer is large enough to hold length bytes
|
||||
void I2C_eeprom::_ReadBlock(unsigned int address, uint8_t* buffer, uint8_t length)
|
||||
int I2C_eeprom::_ReadBlock(unsigned int address, uint8_t* buffer, uint8_t length)
|
||||
{
|
||||
Wire.beginTransmission(_Device);
|
||||
Wire.send((int)(address >> 8));
|
||||
Wire.send((int)(address & 0xFF));
|
||||
Wire.endTransmission();
|
||||
Wire.requestFrom(_Device, length);
|
||||
for (int c = 0; c < length; c++ )
|
||||
if (Wire.available()) buffer[c] = Wire.receive();
|
||||
|
||||
int c = 0;
|
||||
unsigned long before = millis();
|
||||
while ((c < length) && ((millis() - before) < 1000))
|
||||
{
|
||||
if (Wire.available()) buffer[c++] = Wire.receive();
|
||||
}
|
||||
return c;
|
||||
}
|
||||
//
|
||||
// END OF FILE
|
||||
|
@ -5,6 +5,7 @@
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: Simple I2C_eeprom library for Arduino with EEPROM 24LC256 et al.
|
||||
// HISTORY: See I2C_eeprom.cpp
|
||||
// URL: http://arduino.cc/playground/Main/LibraryForI2CEEPROM
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
@ -14,7 +15,7 @@
|
||||
// BLOCKSIZE must be 16
|
||||
#define BLOCKSIZE 16
|
||||
|
||||
#define I2C_EEPROM_VERSION "0.2"
|
||||
#define I2C_EEPROM_VERSION "0.2.01"
|
||||
|
||||
// interface
|
||||
class I2C_eeprom
|
||||
@ -39,7 +40,7 @@ class I2C_eeprom
|
||||
int endOfPage(unsigned int);
|
||||
// (mem_address, buffer, length)
|
||||
void _WriteBlock(unsigned int, uint8_t*, uint8_t );
|
||||
void _ReadBlock(unsigned int, uint8_t*, uint8_t );
|
||||
int _ReadBlock(unsigned int, uint8_t*, uint8_t );
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user