Fix issue #113 and #128

This commit is contained in:
RobTillaart 2019-09-03 11:41:28 +02:00
parent 939136867b
commit d68ca6b43a
4 changed files with 55 additions and 43 deletions

View File

@ -1,7 +1,7 @@
//
// FILE: I2C_eeprom.cpp
// AUTHOR: Rob Tillaart
// VERSION: 1.2.6
// VERSION: 1.2.7
// PURPOSE: I2C_eeprom library for Arduino with EEPROM 24LC256 et al.
//
// HISTORY:
@ -24,9 +24,10 @@
// 1.2.01 - 2014-05-21 Refactoring
// 1.2.02 - 2015-03-06 stricter interface
// 1.2.03 - 2015-05-15 bugfix in _pageBlock & example (thanks ifreislich )
// 1.2.4 - 2017-04-19 remove timeout - https://github.com/RobTillaart/Arduino/issues/63
// 1.2.4 - 2017-04-19 remove timeout - issue #63
// 1.2.5 - 2017-04-20 refactor the removed timeout (Thanks to Koepel)
// 1.2.6 - 2019-02-01 fix issue #121
// 1.2.7 - 2019-09-03 fix issue #113 and #128
//
// Released to the public domain
//
@ -121,7 +122,11 @@ uint16_t I2C_eeprom::readBlock(const uint16_t memoryAddress, uint8_t* buffer, co
uint16_t rv = 0;
while (len > 0)
{
#if defined(ESP8266) || defined(ESP32) // || defined(...)
uint8_t cnt = _min(len, I2C_TWIBUFFERSIZE);
#else
uint8_t cnt = min(len, I2C_TWIBUFFERSIZE);
#endif
rv += _ReadBlock(addr, buffer, cnt);
addr += cnt;
buffer += cnt;
@ -189,8 +194,14 @@ int I2C_eeprom::_pageBlock(const uint16_t memoryAddress, const uint8_t* buffer,
while (len > 0)
{
uint8_t bytesUntilPageBoundary = this->_pageSize - addr % this->_pageSize;
#if defined(ESP8266) || defined(ESP32) // || defined(...)
uint8_t cnt = _min(len, bytesUntilPageBoundary);
cnt = _min(cnt, I2C_TWIBUFFERSIZE);
#else
uint8_t cnt = min(len, bytesUntilPageBoundary);
cnt = min(cnt, I2C_TWIBUFFERSIZE);
#endif
int rv = _WriteBlock(addr, buffer, cnt);
if (rv != 0) return rv;
@ -222,10 +233,10 @@ int I2C_eeprom::_WriteBlock(const uint16_t memoryAddress, const uint8_t* buffer,
waitEEReady();
this->_beginTransmission(memoryAddress);
WIRE_WRITE(buffer, length);
int rv = Wire.endTransmission();
_lastWrite = micros();
return rv;
}
@ -262,6 +273,7 @@ void I2C_eeprom::waitEEReady()
Wire.beginTransmission(_deviceAddress);
int x = Wire.endTransmission();
if (x == 0) break;
yield();
}
}

View File

@ -4,7 +4,7 @@
// FILE: I2C_eeprom.h
// AUTHOR: Rob Tillaart
// PURPOSE: I2C_eeprom library for Arduino with EEPROM 24LC256 et al.
// VERSION: 1.2.6
// VERSION: 1.2.7
// 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.2.6"
#define I2C_EEPROM_VERSION "1.2.7"
// The DEFAULT page size. This is overriden if you use the second constructor.
// I2C_EEPROM_PAGESIZE must be multiple of 2 e.g. 16, 32 or 64
@ -38,53 +38,53 @@
class I2C_eeprom
{
public:
/**
* Initializes the EEPROM with a default pagesize of I2C_EEPROM_PAGESIZE.
*/
I2C_eeprom(const uint8_t deviceAddress);
/**
* Initializes the EEPROM with a default pagesize of I2C_EEPROM_PAGESIZE.
*/
I2C_eeprom(const uint8_t deviceAddress);
/**
* Initializes the EEPROM for the given device address.
*
* It will try to guess page size and address word size based on the size of the device.
*
* @param deviceAddress Byte address of the device.
* @param deviceSize Max size in bytes of the device (divide your device size in Kbits by 8)
*/
I2C_eeprom(const uint8_t deviceAddress, const unsigned int deviceSize);
/**
* Initializes the EEPROM for the given device address.
*
* It will try to guess page size and address word size based on the size of the device.
*
* @param deviceAddress Byte address of the device.
* @param deviceSize Max size in bytes of the device (divide your device size in Kbits by 8)
*/
I2C_eeprom(const uint8_t deviceAddress, const unsigned int deviceSize);
void begin();
int writeByte(const uint16_t memoryAddress, const uint8_t value);
int writeBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint16_t length);
int setBlock(const uint16_t memoryAddress, const uint8_t value, const uint16_t length);
void begin();
int writeByte(const uint16_t memoryAddress, const uint8_t value);
int writeBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint16_t length);
int setBlock(const uint16_t memoryAddress, const uint8_t value, const uint16_t length);
uint8_t readByte(const uint16_t memoryAddress);
uint16_t readBlock(const uint16_t memoryAddress, uint8_t* buffer, const uint16_t length);
uint8_t readByte(const uint16_t memoryAddress);
uint16_t readBlock(const uint16_t memoryAddress, uint8_t* buffer, const uint16_t length);
#ifdef I2C_EEPROM_EXTENDED
int determineSize();
int determineSize();
#endif
private:
uint8_t _deviceAddress;
uint32_t _lastWrite; // for waitEEReady
uint8_t _pageSize;
uint8_t _deviceAddress;
uint32_t _lastWrite; // for waitEEReady
uint8_t _pageSize;
// for some smaller chips that use one-word addresses
bool _isAddressSizeTwoWords;
// for some smaller chips that use one-word addresses
bool _isAddressSizeTwoWords;
/**
* Begins wire transmission and selects the given address to write/read.
*
* @param memoryAddress Address to write/read
*/
void _beginTransmission(const uint16_t memoryAddress);
/**
* Begins wire transmission and selects the given address to write/read.
*
* @param memoryAddress Address to write/read
*/
void _beginTransmission(const uint16_t memoryAddress);
int _pageBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint16_t length, const bool incrBuffer);
int _WriteBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint8_t length);
uint8_t _ReadBlock(const uint16_t memoryAddress, uint8_t* buffer, const uint8_t length);
int _pageBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint16_t length, const bool incrBuffer);
int _WriteBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint8_t length);
uint8_t _ReadBlock(const uint16_t memoryAddress, uint8_t* buffer, const uint8_t length);
void waitEEReady();
void waitEEReady();
};
#endif

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/Arduino.git"
},
"version":"1.2.6",
"version":"1.2.7",
"frameworks": "arduino",
"platforms": "*",
"export": {

View File

@ -1,5 +1,5 @@
name=I2C_EEPROM
version=1.2.6
version=1.2.7
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for I2C EEPROMS.