mirror of
https://github.com/RobTillaart/I2C_EEPROM.git
synced 2026-10-26 22:10:15 +00:00
Fix determineSize + some refactor (#16)
* 1.3.2 fix determineSize + isConnected() * update readme.md
This commit is contained in:
1 parent
24fbd0d7a7
commit
63faf48a13
9 files changed
+450
-84
No files matched your search
+78
-45
@@ -1,36 +1,37 @@
|
||||
//
|
||||
// FILE: I2C_eeprom.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 1.3.1
|
||||
// VERSION: 1.3.2
|
||||
// PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al.
|
||||
// URL: https://github.com/RobTillaart/I2C_EEPROM.git
|
||||
//
|
||||
// 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
|
||||
// 1.0.00 - 2013-06-09 support for Arduino 1.0.x
|
||||
// 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()
|
||||
// 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
|
||||
// 1.0.00 2013-06-09 support for Arduino 1.0.x
|
||||
// 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.
|
||||
// 1.0.05 - 2013-11-06 improved waitEEReady(),
|
||||
// 1.0.05 2013-11-06 improved waitEEReady(),
|
||||
// added determineSize()
|
||||
// 1.1.00 - 2013-11-13 added begin() function (Note breaking interface)
|
||||
// 1.1.00 2013-11-13 added begin() function (Note breaking interface)
|
||||
// use faster block Wire.write()
|
||||
// int casting removed
|
||||
// 1.2.00 - 2014-05-21 Added support for Arduino DUE ( thanks to Tyler F.)
|
||||
// 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 - 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
|
||||
// 1.3.0 2020-06-19 refactor; removed pre 1.0 support; added ESP32 support.
|
||||
// 1.3.1 2020-12-22 arduino-ci + unit tests + updateByte()
|
||||
// 1.2.00 2014-05-21 Added support for Arduino DUE ( thanks to Tyler F.)
|
||||
// 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 - 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
|
||||
// 1.3.0 2020-06-19 refactor; removed pre 1.0 support; added ESP32 support.
|
||||
// 1.3.1 2020-12-22 arduino-ci + unit tests + updateByte()
|
||||
// 1.3.2 2021-01-18 cyclic store functionality (Thanks to Tomas Hübner)
|
||||
|
||||
|
||||
#include <I2C_eeprom.h>
|
||||
@@ -41,6 +42,7 @@ I2C_eeprom::I2C_eeprom(const uint8_t deviceAddress)
|
||||
I2C_eeprom(deviceAddress, I2C_EEPROM_PAGESIZE);
|
||||
}
|
||||
|
||||
|
||||
I2C_eeprom::I2C_eeprom(const uint8_t deviceAddress, const unsigned int deviceSize)
|
||||
{
|
||||
_deviceAddress = deviceAddress;
|
||||
@@ -71,26 +73,39 @@ I2C_eeprom::I2C_eeprom(const uint8_t deviceAddress, const unsigned int deviceSiz
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#if defined (ESP8266) || defined(ESP32)
|
||||
void I2C_eeprom::begin(uint8_t sda, uint8_t scl)
|
||||
bool I2C_eeprom::begin(uint8_t sda, uint8_t scl)
|
||||
{
|
||||
Wire.begin(sda, scl);
|
||||
_lastWrite = 0;
|
||||
return isConnected();
|
||||
}
|
||||
#endif
|
||||
|
||||
void I2C_eeprom::begin()
|
||||
|
||||
bool I2C_eeprom::begin()
|
||||
{
|
||||
Wire.begin();
|
||||
_lastWrite = 0;
|
||||
return isConnected();
|
||||
}
|
||||
|
||||
|
||||
bool I2C_eeprom::isConnected()
|
||||
{
|
||||
Wire.beginTransmission(_deviceAddress);
|
||||
return (Wire.endTransmission() == 0);
|
||||
}
|
||||
|
||||
|
||||
int I2C_eeprom::writeByte(const uint16_t memoryAddress, const uint8_t data)
|
||||
{
|
||||
int rv = _WriteBlock(memoryAddress, &data, 1);
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
int I2C_eeprom::setBlock(const uint16_t memoryAddress, const uint8_t data, const uint16_t length)
|
||||
{
|
||||
uint8_t buffer[I2C_TWIBUFFERSIZE];
|
||||
@@ -102,12 +117,14 @@ int I2C_eeprom::setBlock(const uint16_t memoryAddress, const uint8_t data, const
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
int I2C_eeprom::writeBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint16_t length)
|
||||
{
|
||||
int rv = _pageBlock(memoryAddress, buffer, length, true);
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
uint8_t I2C_eeprom::readByte(const uint16_t memoryAddress)
|
||||
{
|
||||
uint8_t rdata;
|
||||
@@ -115,6 +132,7 @@ uint8_t I2C_eeprom::readByte(const uint16_t memoryAddress)
|
||||
return rdata;
|
||||
}
|
||||
|
||||
|
||||
uint16_t I2C_eeprom::readBlock(const uint16_t memoryAddress, uint8_t* buffer, const uint16_t length)
|
||||
{
|
||||
uint16_t addr = memoryAddress;
|
||||
@@ -138,33 +156,42 @@ int I2C_eeprom::updateByte(const uint16_t memoryAddress, const uint8_t data)
|
||||
if (data == readByte(memoryAddress)) return 0;
|
||||
return writeByte(memoryAddress, data);
|
||||
}
|
||||
|
||||
|
||||
// returns 64, 32, 16, 8, 4, 2, 1, 0
|
||||
|
||||
|
||||
// returns 64, 32, 16, 8, 4, 2, 1
|
||||
// 0 is smaller than 1K
|
||||
int I2C_eeprom::determineSize()
|
||||
int I2C_eeprom::determineSize(const bool debug)
|
||||
{
|
||||
int rv = 0; // unknown
|
||||
uint8_t orgValues[8];
|
||||
uint16_t addr;
|
||||
const int TESTSIZE = 16;
|
||||
|
||||
uint8_t orginalValues[TESTSIZE];
|
||||
uint32_t address;
|
||||
|
||||
// try to read a byte to see if connected
|
||||
rv += _ReadBlock(0x00, orgValues, 1);
|
||||
if (rv == 0) return -1;
|
||||
if (_ReadBlock(0x00, orginalValues, 1) == 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
// remember old values, non destructive
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
for (uint8_t i = 0; i < TESTSIZE; i++)
|
||||
{
|
||||
addr = (512 << i) + 1;
|
||||
orgValues[i] = readByte(addr);
|
||||
address = (1 << i) - 1;
|
||||
orginalValues[i] = readByte(address);
|
||||
}
|
||||
|
||||
// scan page folding
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
int size = 0;
|
||||
for (uint8_t i = 7; i < TESTSIZE; i++) // smallest are 128 bit
|
||||
{
|
||||
rv = i;
|
||||
uint16_t addr1 = (512 << i) + 1;
|
||||
uint16_t addr2 = (512 << (i+1)) + 1;
|
||||
size = i;
|
||||
if (debug)
|
||||
{
|
||||
Serial.print("TEST : ");
|
||||
Serial.println(size);
|
||||
}
|
||||
uint16_t addr1 = (1 << i) - 1;
|
||||
uint16_t addr2 = (1 << (i+1)) - 1;
|
||||
writeByte(addr1, 0xAA);
|
||||
writeByte(addr2, 0x55);
|
||||
if (readByte(addr1) == 0x55) // folded!
|
||||
@@ -174,14 +201,16 @@ int I2C_eeprom::determineSize()
|
||||
}
|
||||
|
||||
// restore original values
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
for (uint8_t i = 0; i < TESTSIZE; i++)
|
||||
{
|
||||
uint16_t addr = (512 << i) + 1;
|
||||
writeByte(addr, orgValues[i]);
|
||||
uint32_t address = (512 << i) - 1;
|
||||
writeByte(address, orginalValues[i]);
|
||||
}
|
||||
return 0x01 << (rv - 1);
|
||||
if (size >= 10) return 1 << (size - 10);
|
||||
return 1 << size;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// PRIVATE
|
||||
@@ -212,6 +241,7 @@ int I2C_eeprom::_pageBlock(const uint16_t memoryAddress, const uint8_t* buffer,
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// supports one and 2 bytes addresses
|
||||
void I2C_eeprom::_beginTransmission(const uint16_t memoryAddress)
|
||||
{
|
||||
@@ -227,6 +257,7 @@ void I2C_eeprom::_beginTransmission(const uint16_t memoryAddress)
|
||||
Wire.write((memoryAddress & 0xFF));
|
||||
}
|
||||
|
||||
|
||||
// pre: length <= this->_pageSize && length <= I2C_TWIBUFFERSIZE;
|
||||
// returns 0 = OK otherwise error
|
||||
int I2C_eeprom::_WriteBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint8_t length)
|
||||
@@ -241,6 +272,7 @@ int I2C_eeprom::_WriteBlock(const uint16_t memoryAddress, const uint8_t* buffer,
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
// pre: buffer is large enough to hold length bytes
|
||||
// returns bytes read
|
||||
uint8_t I2C_eeprom::_ReadBlock(const uint16_t memoryAddress, uint8_t* buffer, const uint8_t length)
|
||||
@@ -261,6 +293,7 @@ uint8_t I2C_eeprom::_ReadBlock(const uint16_t memoryAddress, uint8_t* buffer, co
|
||||
return readBytes;
|
||||
}
|
||||
|
||||
|
||||
void I2C_eeprom::_waitEEReady()
|
||||
{
|
||||
#define I2C_WRITEDELAY 5000
|
||||
|
||||
+21
-7
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: I2C_eeprom.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 1.3.1
|
||||
// VERSION: 1.3.2
|
||||
// PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al.
|
||||
// URL: https://github.com/RobTillaart/I2C_EEPROM.git
|
||||
//
|
||||
@@ -12,21 +12,26 @@
|
||||
#include "Arduino.h"
|
||||
#include "Wire.h"
|
||||
|
||||
#define I2C_EEPROM_VERSION "1.3.1"
|
||||
|
||||
#define I2C_EEPROM_VERSION (F("1.3.2"))
|
||||
|
||||
|
||||
// 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
|
||||
// 24LC256 -> 64 bytes
|
||||
#define I2C_EEPROM_PAGESIZE 64
|
||||
#define I2C_EEPROM_PAGESIZE 64
|
||||
|
||||
|
||||
// TWI buffer needs max 2 bytes for eeprom address
|
||||
// 1 byte for eeprom register address is available in txbuffer
|
||||
#define I2C_TWIBUFFERSIZE 30
|
||||
#define I2C_TWIBUFFERSIZE 30
|
||||
|
||||
|
||||
#ifndef UNIT_TEST_FRIEND
|
||||
#define UNIT_TEST_FRIEND
|
||||
#endif
|
||||
|
||||
|
||||
class I2C_eeprom
|
||||
{
|
||||
public:
|
||||
@@ -46,9 +51,11 @@ public:
|
||||
I2C_eeprom(const uint8_t deviceAddress, const unsigned int deviceSize);
|
||||
|
||||
#if defined (ESP8266) || defined(ESP32)
|
||||
void begin(uint8_t sda, uint8_t scl);
|
||||
bool begin(uint8_t sda, uint8_t scl);
|
||||
#endif
|
||||
void begin();
|
||||
bool begin();
|
||||
|
||||
bool isConnected();
|
||||
|
||||
// writes a byte to memaddr
|
||||
int writeByte(const uint16_t memoryAddress, const uint8_t value);
|
||||
@@ -57,16 +64,19 @@ public:
|
||||
// set length bytes in the EEPROM to the same value.
|
||||
int setBlock(const uint16_t memoryAddress, const uint8_t value, const uint16_t length);
|
||||
|
||||
|
||||
// returns the value stored in memaddr
|
||||
uint8_t readByte(const uint16_t memoryAddress);
|
||||
// reads length bytes into buffer
|
||||
uint16_t readBlock(const uint16_t memoryAddress, uint8_t* buffer, const uint16_t length);
|
||||
|
||||
|
||||
// updates a byte at memory address, writes only if there is a new value.
|
||||
// return 0 if data is same or written OK, error code otherwise.
|
||||
int updateByte(const uint16_t memoryAddress, const uint8_t value);
|
||||
|
||||
int determineSize();
|
||||
int determineSize(const bool debug = false);
|
||||
|
||||
|
||||
private:
|
||||
uint8_t _deviceAddress;
|
||||
@@ -76,6 +86,7 @@ private:
|
||||
// for some smaller chips that use one-word addresses
|
||||
bool _isAddressSizeTwoWords;
|
||||
|
||||
|
||||
/**
|
||||
* Begins wire transmission and selects the given address to write/read.
|
||||
*
|
||||
@@ -83,12 +94,15 @@ private:
|
||||
*/
|
||||
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);
|
||||
|
||||
|
||||
void _waitEEReady();
|
||||
|
||||
|
||||
UNIT_TEST_FRIEND;
|
||||
};
|
||||
|
||||
|
||||
@@ -9,26 +9,64 @@ Arduino Library for external I2C EEPROM - 24LC256, 24LC64
|
||||
|
||||
## Description
|
||||
|
||||
Library to access external I2C EEPROM.
|
||||
|
||||
The interface is pretty straightforward
|
||||
|
||||
- **begin()** constructor
|
||||
- **begin(sda, scl)** constructor for ESP32
|
||||
- **writeByte(address, value)** write a single byte
|
||||
- **writeBlock(address, buffer, length)**
|
||||
- **setBlock(address, value, length)** e.g. use to clear I2C EEPROM
|
||||
- **readByte(address)** - read a single byte from a given address
|
||||
- **readBlock(address, buffer, length)**
|
||||
- **updateByte(address, value)** write a single byte, but only if changed.
|
||||
- **determineSize()**
|
||||
This library is to access external I2C EEPROM up to 64KB (= 512 Kbit) in size.
|
||||
|
||||
The **I2C_eeprom_cyclic_store** interface is documented [here](README_cyclic_store.md)
|
||||
|
||||
|
||||
## Interface
|
||||
|
||||
### Constructor
|
||||
|
||||
- **I2C_eeprom(deviceAddress)** constructor.
|
||||
- **bool begin()** initializes the I2C bus and checks if the device is available on the I2C bus.
|
||||
- **bool begin(sda, scl)** idem for ESP32 / ESP8266 and alike.
|
||||
- **bool isConnected()** test to see if device is on the bus.
|
||||
|
||||
|
||||
### Core functions
|
||||
|
||||
- **int writeByte(memoryAddress, value)** write a single byte to the specified memory address.
|
||||
- **int writeBlock(memoryAddress, buffer, length)** write a buffer starting at the specified memory address.
|
||||
- **int setBlock(memoryAddress, value, length)** writes the same byte to length places starting at the specified memory address. Returns 0 if OK.
|
||||
- **uint8_t readByte(memoryAddress)** read a single byte from a given address
|
||||
- **uint16_treadBlock(memoryAddress, buffer, length)** read length bytes into buffer starting at specified memory address. Returns the number of bytes read, which should be length.
|
||||
- **int updateByte(memoryAddress, value)** write a single byte, but only if changed. Returns 0 if value was same or write succeeded.
|
||||
|
||||
|
||||
### Other
|
||||
|
||||
- **int determineSize(bool debug = false)** function that determins the size of the EEPROM by detecting when a memory address is folded upon memory address divided by 2.
|
||||
It is based upon the behavior that memory wraps around.
|
||||
The debug flag gives output to Serial.
|
||||
|
||||
| return value | Size in KB | Notes |
|
||||
|:-------------|:-----------|:------|
|
||||
| -1 | could not connect | check device address / wiring |
|
||||
| 1 .. 64 | 1 KB .. 64 KB |
|
||||
| 128 | 128 bytes |
|
||||
| 256 | 256 bytes |
|
||||
| 512 | 512 bytes |
|
||||
|
||||
The function cannot detect smaller than 128 bit EEPROMS (yet).
|
||||
|
||||
**Warning**: this function has changed in 1.3.2 and is not tested for all sizes.
|
||||
|
||||
|
||||
## Limitation
|
||||
|
||||
The library does not offer multiple EEPROMS as one
|
||||
continuous storage device.
|
||||
The library does not offer multiple EEPROMS as one continuous storage device.
|
||||
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
- look into support of > 64KB EEPROM
|
||||
- improve error handling, write functions should return bytes written or so.
|
||||
- what can we do with the print interface? (investigate)
|
||||
- Wire0 .. WireN support
|
||||
- investigate multi-EEPROM storage,
|
||||
- investigate format / clear all function.
|
||||
|
||||
## Operational
|
||||
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
//
|
||||
// FILE: I2C_eeprom_determineSize.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: test determinsize function
|
||||
//
|
||||
|
||||
|
||||
#include "Wire.h"
|
||||
#include "I2C_eeprom.h"
|
||||
|
||||
|
||||
#define MEMORY_SIZE 0x2000 //total bytes can be accessed 24LC64 = 0x2000 (maximum address = 0x1FFF)
|
||||
|
||||
I2C_eeprom ee(0x50, MEMORY_SIZE);
|
||||
|
||||
uint32_t start, diff;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
|
||||
ee.begin();
|
||||
if (! ee.isConnected())
|
||||
{
|
||||
Serial.println("ERROR: Can't find eeprom\nstopped...");
|
||||
while (1);
|
||||
}
|
||||
|
||||
Serial.print("I2C_EEPROM_VERSION: ");
|
||||
Serial.println(I2C_EEPROM_VERSION);
|
||||
|
||||
Serial.println("\nDetermine size");
|
||||
delay(10);
|
||||
|
||||
start = micros();
|
||||
int size = ee.determineSize(true); // debug param
|
||||
diff = micros() - start;
|
||||
Serial.print("TIME: ");
|
||||
Serial.print(diff);
|
||||
Serial.println(" us.");
|
||||
if (size > 0)
|
||||
{
|
||||
Serial.print("SIZE: ");
|
||||
Serial.print(size);
|
||||
Serial.println(" KB.");
|
||||
}
|
||||
if (size == 0)
|
||||
{
|
||||
Serial.println("WARNING: Can't determine eeprom size");
|
||||
}
|
||||
Serial.println("Done...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
@@ -0,0 +1,207 @@
|
||||
//
|
||||
// FILE: I2C_eeprom_test_performance.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: show/test I2C_EEPROM library
|
||||
//
|
||||
|
||||
|
||||
#include "Wire.h"
|
||||
#include "I2C_eeprom.h"
|
||||
|
||||
|
||||
//for decimal display uncomment below two lines
|
||||
#define DISPLAY_DECIMAL
|
||||
#define BLOCK_TO_LENGTH 10
|
||||
|
||||
//for hex display uncomment below two lines
|
||||
//#define DISPLAY_HEX
|
||||
//#define BLOCK_TO_LENGTH 16
|
||||
|
||||
|
||||
#define MEMORY_SIZE 0x2000 // total bytes can be accessed 24LC64 = 0x2000 (maximum address = 0x1FFF)
|
||||
|
||||
|
||||
I2C_eeprom ee(0x50, MEMORY_SIZE);
|
||||
|
||||
uint32_t start, diff, totals = 0;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
|
||||
while (!Serial); // wait for Serial port to connect. Needed for Leonardo only
|
||||
|
||||
ee.begin();
|
||||
|
||||
for (uint32_t speed = 100000; speed <= 1000000; speed += 100000)
|
||||
{
|
||||
Wire.setClock(speed);
|
||||
Serial.print("SPEED: ");
|
||||
Serial.println(speed);
|
||||
delay(10);
|
||||
test();
|
||||
}
|
||||
|
||||
Serial.println("\ndone...");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
void test()
|
||||
{
|
||||
char data2[] = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999A";
|
||||
|
||||
totals = 0;
|
||||
Serial.print("\nTEST: timing writeByte()\t");
|
||||
uint32_t start = micros();
|
||||
ee.writeByte(10, 1);
|
||||
uint32_t diff = micros() - start;
|
||||
Serial.print("TIME: ");
|
||||
Serial.println(diff);
|
||||
totals += diff;
|
||||
|
||||
Serial.print("TEST: timing writeBlock(50)\t");
|
||||
start = micros();
|
||||
ee.writeBlock(10, (uint8_t *) &data2, 50);
|
||||
diff = micros() - start;
|
||||
Serial.print("TIME: ");
|
||||
Serial.println(diff);
|
||||
totals += diff;
|
||||
|
||||
Serial.print("TEST: timing readByte()\t\t");
|
||||
start = micros();
|
||||
ee.readByte(10);
|
||||
diff = micros() - start;
|
||||
Serial.print("TIME: ");
|
||||
Serial.println(diff);
|
||||
totals += diff;
|
||||
|
||||
Serial.print("TEST: timing readBlock(50)\t");
|
||||
start = micros();
|
||||
ee.readBlock(10, (uint8_t *) &data2, 50);
|
||||
diff = micros() - start;
|
||||
Serial.print("TIME: ");
|
||||
Serial.println(diff);
|
||||
totals += diff;
|
||||
|
||||
Serial.print("TOTALS: ");
|
||||
Serial.println(totals);
|
||||
totals = 0;
|
||||
|
||||
// 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.print("TIME: ");
|
||||
Serial.println(diff);
|
||||
totals += diff;
|
||||
|
||||
delay(5);
|
||||
|
||||
Serial.print("TEST: timing writeBlock(50)\t");
|
||||
start = micros();
|
||||
ee.writeBlock(10, (uint8_t *) &data2, 50);
|
||||
diff = micros() - start;
|
||||
Serial.print("TIME: ");
|
||||
Serial.println(diff);
|
||||
totals += diff;
|
||||
|
||||
delay(5);
|
||||
|
||||
Serial.print("TEST: timing readByte()\t\t");
|
||||
start = micros();
|
||||
ee.readByte(10);
|
||||
diff = micros() - start;
|
||||
Serial.print("TIME: ");
|
||||
Serial.println(diff);
|
||||
totals += 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.print("TIME: ");
|
||||
Serial.println(diff);
|
||||
totals += diff;
|
||||
|
||||
Serial.print("TOTALS: ");
|
||||
Serial.println(totals);
|
||||
totals = 0;
|
||||
|
||||
// does it go well?
|
||||
Serial.println(xx);
|
||||
}
|
||||
|
||||
|
||||
void dumpEEPROM(uint16_t memoryAddress, uint16_t length)
|
||||
{
|
||||
#ifdef DISPLAY_DECIMAL
|
||||
Serial.print("\t ");
|
||||
#endif
|
||||
#ifdef DISPLAY_HEX
|
||||
Serial.print("\t ");
|
||||
#endif
|
||||
for (int x = 0; x < BLOCK_TO_LENGTH; x++) {
|
||||
if (x != 0) {
|
||||
#ifdef DISPLAY_DECIMAL
|
||||
Serial.print(" ");
|
||||
#endif
|
||||
#ifdef DISPLAY_HEX
|
||||
Serial.print(" ");
|
||||
#endif
|
||||
}
|
||||
#ifdef DISPLAY_DECIMAL
|
||||
Serial.print(x);
|
||||
#endif
|
||||
#ifdef DISPLAY_HEX
|
||||
Serial.print(x, HEX);
|
||||
#endif
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// block to defined length
|
||||
memoryAddress = memoryAddress / BLOCK_TO_LENGTH * BLOCK_TO_LENGTH;
|
||||
length = (length + BLOCK_TO_LENGTH - 1) / BLOCK_TO_LENGTH * BLOCK_TO_LENGTH;
|
||||
|
||||
byte b = ee.readByte(memoryAddress);
|
||||
for (unsigned int i = 0; i < length; i++)
|
||||
{
|
||||
char buf[6];
|
||||
if (memoryAddress % BLOCK_TO_LENGTH == 0)
|
||||
{
|
||||
if (i != 0) {
|
||||
Serial.println();
|
||||
}
|
||||
#ifdef DISPLAY_DECIMAL
|
||||
sprintf(buf, "%05d", memoryAddress);
|
||||
#endif
|
||||
#ifdef DISPLAY_HEX
|
||||
sprintf(buf, "%04X", memoryAddress);
|
||||
#endif
|
||||
Serial.print(buf);
|
||||
Serial.print(":\t");
|
||||
}
|
||||
#ifdef DISPLAY_DECIMAL
|
||||
sprintf(buf, "%03d", b);
|
||||
#endif
|
||||
#ifdef DISPLAY_HEX
|
||||
sprintf(buf, "%02X", b);
|
||||
#endif
|
||||
Serial.print(buf);
|
||||
b = ee.readByte(++memoryAddress);
|
||||
Serial.print(" ");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
// END OF FILE
|
||||
@@ -7,6 +7,8 @@ I2C_eeprom_cyclic_store KEYWORD1
|
||||
# Methods and Functions (KEYWORD2)
|
||||
# Common
|
||||
begin KEYWORD2
|
||||
isConnected KEYWORD2
|
||||
|
||||
# I2C_eeprom
|
||||
readByte KEYWORD2
|
||||
writeByte KEYWORD2
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/I2C_EEPROM.git"
|
||||
},
|
||||
"version":"1.3.1",
|
||||
"version":"1.3.2",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
name=I2C_EEPROM
|
||||
version=1.3.1
|
||||
version=1.3.2
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Library for I2C EEPROMS.
|
||||
|
||||
+25
-15
@@ -8,18 +8,26 @@
|
||||
//
|
||||
|
||||
// supported assertions
|
||||
// https://github.com/Arduino-CI/arduino_ci/blob/master/cpp/unittest/Assertion.h#L33-L42
|
||||
// ----------------------------
|
||||
// assertEqual(expected, actual)
|
||||
// assertNotEqual(expected, actual)
|
||||
// assertLess(expected, actual)
|
||||
// assertMore(expected, actual)
|
||||
// assertLessOrEqual(expected, actual)
|
||||
// assertMoreOrEqual(expected, actual)
|
||||
// assertTrue(actual)
|
||||
// assertFalse(actual)
|
||||
// assertNull(actual)
|
||||
// assertNotNull(actual)
|
||||
// assertEqual(expected, actual); // a == b
|
||||
// assertNotEqual(unwanted, actual); // a != b
|
||||
// assertComparativeEquivalent(expected, actual); // abs(a - b) == 0 or (!(a > b) && !(a < b))
|
||||
// assertComparativeNotEquivalent(unwanted, actual); // abs(a - b) > 0 or ((a > b) || (a < b))
|
||||
// assertLess(upperBound, actual); // a < b
|
||||
// assertMore(lowerBound, actual); // a > b
|
||||
// assertLessOrEqual(upperBound, actual); // a <= b
|
||||
// assertMoreOrEqual(lowerBound, actual); // a >= b
|
||||
// assertTrue(actual);
|
||||
// assertFalse(actual);
|
||||
// assertNull(actual);
|
||||
|
||||
// // special cases for floats
|
||||
// assertEqualFloat(expected, actual, epsilon); // fabs(a - b) <= epsilon
|
||||
// assertNotEqualFloat(unwanted, actual, epsilon); // fabs(a - b) >= epsilon
|
||||
// assertInfinity(actual); // isinf(a)
|
||||
// assertNotInfinity(actual); // !isinf(a)
|
||||
// assertNAN(arg); // isnan(a)
|
||||
// assertNotNAN(arg); // !isnan(a)
|
||||
|
||||
#include <ArduinoUnitTests.h>
|
||||
|
||||
@@ -51,10 +59,12 @@ unittest(test_new_operator)
|
||||
|
||||
unittest(test_constructor)
|
||||
{
|
||||
I2C_eeprom EE(0x50);
|
||||
EE.begin();
|
||||
|
||||
|
||||
Wire.resetMocks();
|
||||
|
||||
I2C_eeprom EE(0x50, 0x8000);
|
||||
|
||||
assertTrue(EE.begin());
|
||||
|
||||
assertEqual(1, 1);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user