mirror of
https://github.com/RobTillaart/I2C_EEPROM.git
synced 2026-10-26 22:10:15 +00:00
1 parent
63faf48a13
commit
2d60816fe8
10 files changed
+348
-242
No files matched your search
+107
-64
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// FILE: I2C_eeprom.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 1.3.2
|
||||
// VERSION: 1.4.0
|
||||
// PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al.
|
||||
// URL: https://github.com/RobTillaart/I2C_EEPROM.git
|
||||
//
|
||||
@@ -32,42 +32,64 @@
|
||||
// 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)
|
||||
// 1.4.0 2021-01-27 rewritten addressing scheme + determineSize
|
||||
// See determineSize for all tested.
|
||||
|
||||
|
||||
#include <I2C_eeprom.h>
|
||||
|
||||
// Not used directly
|
||||
#define I2C_PAGESIZE_24LC512 128
|
||||
#define I2C_PAGESIZE_24LC256 64
|
||||
#define I2C_PAGESIZE_24LC128 64
|
||||
#define I2C_PAGESIZE_24LC64 32
|
||||
#define I2C_PAGESIZE_24LC32 32
|
||||
#define I2C_PAGESIZE_24LC16 16
|
||||
#define I2C_PAGESIZE_24LC08 16
|
||||
#define I2C_PAGESIZE_24LC04 16
|
||||
#define I2C_PAGESIZE_24LC02 8
|
||||
#define I2C_PAGESIZE_24LC01 8
|
||||
|
||||
|
||||
// TWI buffer needs max 2 bytes for eeprom address
|
||||
// 1 byte for eeprom register address is available in txbuffer
|
||||
// NOTE this is typical on an UNO.
|
||||
// TODO investigate
|
||||
#define I2C_TWIBUFFERSIZE 30
|
||||
|
||||
|
||||
I2C_eeprom::I2C_eeprom(const uint8_t deviceAddress)
|
||||
{
|
||||
I2C_eeprom(deviceAddress, I2C_EEPROM_PAGESIZE);
|
||||
I2C_eeprom(deviceAddress, I2C_PAGESIZE_24LC256);
|
||||
}
|
||||
|
||||
|
||||
I2C_eeprom::I2C_eeprom(const uint8_t deviceAddress, const unsigned int deviceSize)
|
||||
I2C_eeprom::I2C_eeprom(const uint8_t deviceAddress, const uint32_t deviceSize)
|
||||
{
|
||||
_deviceAddress = deviceAddress;
|
||||
_deviceSize = deviceSize;
|
||||
|
||||
// Chips 16Kbit (2048 Bytes) or smaller only have one-word addresses.
|
||||
this->_isAddressSizeTwoWords = deviceSize <= 2048 ? false : true;
|
||||
this->_isAddressSizeTwoWords = (deviceSize <= I2C_DEVICESIZE_24LC16) ? false : true;
|
||||
|
||||
// Try to guess page size from device size (going by Microchip 24LCXX datasheets here).
|
||||
if (deviceSize <= 256) // 2Kbit
|
||||
if (deviceSize <= I2C_DEVICESIZE_24LC02) // 2Kbit
|
||||
{
|
||||
this->_pageSize = 8;
|
||||
}
|
||||
else if (deviceSize <= 2048) // 16Kbit
|
||||
else if (deviceSize <= I2C_DEVICESIZE_24LC16) // 16Kbit
|
||||
{
|
||||
this->_pageSize = 16;
|
||||
}
|
||||
else if(deviceSize <= 8192) // 64Kbit
|
||||
else if(deviceSize <= I2C_DEVICESIZE_24LC64) // 64Kbit
|
||||
{
|
||||
this->_pageSize = 32;
|
||||
}
|
||||
else if(deviceSize <= 32768) // 256Kbit
|
||||
else if(deviceSize <= I2C_DEVICESIZE_24LC256) // 256Kbit
|
||||
{
|
||||
this->_pageSize = 64;
|
||||
}
|
||||
else
|
||||
else // I2C_DEVICESIZE_24LC512
|
||||
{
|
||||
this->_pageSize = 128;
|
||||
}
|
||||
@@ -77,7 +99,15 @@ I2C_eeprom::I2C_eeprom(const uint8_t deviceAddress, const unsigned int deviceSiz
|
||||
#if defined (ESP8266) || defined(ESP32)
|
||||
bool I2C_eeprom::begin(uint8_t sda, uint8_t scl)
|
||||
{
|
||||
Wire.begin(sda, scl);
|
||||
_wire = &Wire;
|
||||
if ((sda < 255) && (scl < 255))
|
||||
{
|
||||
_wire->begin(sda, scl);
|
||||
}
|
||||
else
|
||||
{
|
||||
_wire->begin();
|
||||
}
|
||||
_lastWrite = 0;
|
||||
return isConnected();
|
||||
}
|
||||
@@ -86,7 +116,8 @@ bool I2C_eeprom::begin(uint8_t sda, uint8_t scl)
|
||||
|
||||
bool I2C_eeprom::begin()
|
||||
{
|
||||
Wire.begin();
|
||||
_wire = &Wire;
|
||||
_wire->begin();
|
||||
_lastWrite = 0;
|
||||
return isConnected();
|
||||
}
|
||||
@@ -94,8 +125,8 @@ bool I2C_eeprom::begin()
|
||||
|
||||
bool I2C_eeprom::isConnected()
|
||||
{
|
||||
Wire.beginTransmission(_deviceAddress);
|
||||
return (Wire.endTransmission() == 0);
|
||||
_wire->beginTransmission(_deviceAddress);
|
||||
return (_wire->endTransmission() == 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -146,6 +177,7 @@ uint16_t I2C_eeprom::readBlock(const uint16_t memoryAddress, uint8_t* buffer, co
|
||||
addr += cnt;
|
||||
buffer += cnt;
|
||||
len -= cnt;
|
||||
yield(); // For OS scheduling etc
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
@@ -158,56 +190,61 @@ int I2C_eeprom::updateByte(const uint16_t memoryAddress, const uint8_t data)
|
||||
}
|
||||
|
||||
|
||||
// returns 64, 32, 16, 8, 4, 2, 1
|
||||
// 0 is smaller than 1K
|
||||
int I2C_eeprom::determineSize(const bool debug)
|
||||
// returns size in bytes
|
||||
// returns 0 if not connected
|
||||
//
|
||||
// tested for
|
||||
// 2 byte address
|
||||
// 24LC512 64 KB YES
|
||||
// 24LC256 32 KB YES
|
||||
// 24LC128 16 KB YES
|
||||
// 24LC64 8 KB YES
|
||||
// 24LC32 4 KB YES* - no hardware test, address scheme identical to 24LC64.
|
||||
//
|
||||
// 1 byte address (uses part of deviceAddress byte)
|
||||
// 24LC16 2 KB YES
|
||||
// 24LC08 1 KB YES
|
||||
// 24LC04 512 B YES
|
||||
// 24LC02 256 B YES
|
||||
// 24LC01 128 B YES
|
||||
uint32_t I2C_eeprom::determineSize(const bool debug)
|
||||
{
|
||||
const int TESTSIZE = 16;
|
||||
|
||||
uint8_t orginalValues[TESTSIZE];
|
||||
uint32_t address;
|
||||
|
||||
// try to read a byte to see if connected
|
||||
if (_ReadBlock(0x00, orginalValues, 1) == 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (! isConnected()) return 0;
|
||||
|
||||
// remember old values, non destructive
|
||||
for (uint8_t i = 0; i < TESTSIZE; i++)
|
||||
{
|
||||
address = (1 << i) - 1;
|
||||
orginalValues[i] = readByte(address);
|
||||
}
|
||||
uint8_t patAA = 0xAA;
|
||||
uint8_t pat55 = 0x55;
|
||||
|
||||
// scan page folding
|
||||
int size = 0;
|
||||
for (uint8_t i = 7; i < TESTSIZE; i++) // smallest are 128 bit
|
||||
for (uint32_t size = 128; size <= 65536; size *= 2)
|
||||
{
|
||||
size = i;
|
||||
bool folded = false;
|
||||
|
||||
// store old values
|
||||
bool addressSize = _isAddressSizeTwoWords;
|
||||
_isAddressSizeTwoWords = (size <= 2048) ? false : true;
|
||||
uint8_t buf = readByte(size);
|
||||
|
||||
// test folding
|
||||
uint8_t cnt = 0;
|
||||
writeByte(size, pat55);
|
||||
if (readByte(0) == pat55) cnt++;
|
||||
writeByte(size, patAA);
|
||||
if (readByte(0) == patAA) cnt++;
|
||||
folded = (cnt == 2);
|
||||
if (debug)
|
||||
{
|
||||
Serial.print("TEST : ");
|
||||
Serial.println(size);
|
||||
Serial.print(size, HEX);
|
||||
Serial.print('\t');
|
||||
Serial.println(readByte(size), HEX);
|
||||
}
|
||||
uint16_t addr1 = (1 << i) - 1;
|
||||
uint16_t addr2 = (1 << (i+1)) - 1;
|
||||
writeByte(addr1, 0xAA);
|
||||
writeByte(addr2, 0x55);
|
||||
if (readByte(addr1) == 0x55) // folded!
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// restore original values
|
||||
for (uint8_t i = 0; i < TESTSIZE; i++)
|
||||
{
|
||||
uint32_t address = (512 << i) - 1;
|
||||
writeByte(address, orginalValues[i]);
|
||||
// restore old values
|
||||
writeByte(size, buf);
|
||||
_isAddressSizeTwoWords = addressSize;
|
||||
|
||||
if (folded) return size;
|
||||
}
|
||||
if (size >= 10) return 1 << (size - 10);
|
||||
return 1 << size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -245,16 +282,20 @@ int I2C_eeprom::_pageBlock(const uint16_t memoryAddress, const uint8_t* buffer,
|
||||
// supports one and 2 bytes addresses
|
||||
void I2C_eeprom::_beginTransmission(const uint16_t memoryAddress)
|
||||
{
|
||||
Wire.beginTransmission(_deviceAddress);
|
||||
|
||||
if (this->_isAddressSizeTwoWords)
|
||||
{
|
||||
_wire->beginTransmission(_deviceAddress);
|
||||
// Address High Byte
|
||||
Wire.write((memoryAddress >> 8));
|
||||
_wire->write((memoryAddress >> 8));
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t addr = 0x50 | ((memoryAddress >> 8) & 0x07);
|
||||
_wire->beginTransmission(addr);
|
||||
}
|
||||
|
||||
// Address Low Byte (or only byte for chips 16K or smaller that only have one-word addresses)
|
||||
Wire.write((memoryAddress & 0xFF));
|
||||
_wire->write((memoryAddress & 0xFF));
|
||||
}
|
||||
|
||||
|
||||
@@ -265,8 +306,9 @@ int I2C_eeprom::_WriteBlock(const uint16_t memoryAddress, const uint8_t* buffer,
|
||||
_waitEEReady();
|
||||
|
||||
this->_beginTransmission(memoryAddress);
|
||||
Wire.write(buffer, length);
|
||||
int rv = Wire.endTransmission();
|
||||
_wire->write(buffer, length);
|
||||
int rv = _wire->endTransmission();
|
||||
yield();
|
||||
|
||||
_lastWrite = micros();
|
||||
return rv;
|
||||
@@ -280,15 +322,16 @@ uint8_t I2C_eeprom::_ReadBlock(const uint16_t memoryAddress, uint8_t* buffer, co
|
||||
_waitEEReady();
|
||||
|
||||
this->_beginTransmission(memoryAddress);
|
||||
int rv = Wire.endTransmission();
|
||||
int rv = _wire->endTransmission();
|
||||
if (rv != 0) return 0; // error
|
||||
|
||||
// readbytes will always be equal or smaller to length
|
||||
uint8_t readBytes = Wire.requestFrom(_deviceAddress, length);
|
||||
uint8_t readBytes = _wire->requestFrom(_deviceAddress, length);
|
||||
uint8_t cnt = 0;
|
||||
while (cnt < readBytes)
|
||||
{
|
||||
buffer[cnt++] = Wire.read();
|
||||
buffer[cnt++] = _wire->read();
|
||||
yield();
|
||||
}
|
||||
return readBytes;
|
||||
}
|
||||
@@ -302,8 +345,8 @@ void I2C_eeprom::_waitEEReady()
|
||||
// this is a bit faster than the hardcoded 5 milliSeconds
|
||||
while ((micros() - _lastWrite) <= I2C_WRITEDELAY)
|
||||
{
|
||||
Wire.beginTransmission(_deviceAddress);
|
||||
int x = Wire.endTransmission();
|
||||
_wire->beginTransmission(_deviceAddress);
|
||||
int x = _wire->endTransmission();
|
||||
if (x == 0) return;
|
||||
yield();
|
||||
}
|
||||
|
||||
+27
-21
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: I2C_eeprom.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 1.3.2
|
||||
// VERSION: 1.4.0
|
||||
// PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al.
|
||||
// URL: https://github.com/RobTillaart/I2C_EEPROM.git
|
||||
//
|
||||
@@ -13,18 +13,19 @@
|
||||
#include "Wire.h"
|
||||
|
||||
|
||||
#define I2C_EEPROM_VERSION (F("1.3.2"))
|
||||
#define I2C_EEPROM_VERSION (F("1.4.0"))
|
||||
|
||||
|
||||
// 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
|
||||
|
||||
|
||||
// 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_DEVICESIZE_24LC512 65536
|
||||
#define I2C_DEVICESIZE_24LC256 32768
|
||||
#define I2C_DEVICESIZE_24LC128 16384
|
||||
#define I2C_DEVICESIZE_24LC64 8192
|
||||
#define I2C_DEVICESIZE_24LC32 4096
|
||||
#define I2C_DEVICESIZE_24LC16 2048
|
||||
#define I2C_DEVICESIZE_24LC08 1024
|
||||
#define I2C_DEVICESIZE_24LC04 512
|
||||
#define I2C_DEVICESIZE_24LC02 256
|
||||
#define I2C_DEVICESIZE_24LC01 128
|
||||
|
||||
|
||||
#ifndef UNIT_TEST_FRIEND
|
||||
@@ -36,7 +37,7 @@ class I2C_eeprom
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Initializes the EEPROM with a default pagesize of I2C_EEPROM_PAGESIZE.
|
||||
* Initializes the EEPROM with a default deviceSize of I2C_DEVICESIZE_24LC256 (32K EEPROM)
|
||||
*/
|
||||
I2C_eeprom(const uint8_t deviceAddress);
|
||||
|
||||
@@ -48,14 +49,14 @@ public:
|
||||
* @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);
|
||||
I2C_eeprom(const uint8_t deviceAddress, const uint32_t deviceSize);
|
||||
|
||||
#if defined (ESP8266) || defined(ESP32)
|
||||
bool begin(uint8_t sda, uint8_t scl);
|
||||
bool begin(uint8_t sda, uint8_t scl);
|
||||
#endif
|
||||
bool begin();
|
||||
bool begin();
|
||||
|
||||
bool isConnected();
|
||||
bool isConnected();
|
||||
|
||||
// writes a byte to memaddr
|
||||
int writeByte(const uint16_t memoryAddress, const uint8_t value);
|
||||
@@ -75,15 +76,20 @@ public:
|
||||
// return 0 if data is same or written OK, error code otherwise.
|
||||
int updateByte(const uint16_t memoryAddress, const uint8_t value);
|
||||
|
||||
int determineSize(const bool debug = false);
|
||||
uint32_t determineSize(const bool debug = false);
|
||||
|
||||
uint32_t getDeviceSize() { return _deviceSize; };
|
||||
uint8_t getPageSize() { return _pageSize; };
|
||||
uint32_t getLastWrite() { return _lastWrite; };
|
||||
|
||||
private:
|
||||
uint8_t _deviceAddress;
|
||||
uint32_t _lastWrite; // for waitEEReady
|
||||
uint32_t _lastWrite; // for waitEEReady
|
||||
uint32_t _deviceSize;
|
||||
uint8_t _pageSize;
|
||||
|
||||
// for some smaller chips that use one-word addresses
|
||||
// 24LC32..24LC512 use two bytes for memory address
|
||||
// 24LC01..24LC16 use one-byte addresses + part of device address
|
||||
bool _isAddressSizeTwoWords;
|
||||
|
||||
|
||||
@@ -94,14 +100,14 @@ 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);
|
||||
|
||||
|
||||
// to optimize the write latency of the EEPROM
|
||||
void _waitEEReady();
|
||||
|
||||
TwoWire * _wire;
|
||||
|
||||
UNIT_TEST_FRIEND;
|
||||
};
|
||||
|
||||
@@ -30,28 +30,40 @@ The **I2C_eeprom_cyclic_store** interface is documented [here](README_cyclic_sto
|
||||
- **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.
|
||||
- **uint16_t readBlock(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.
|
||||
- **uint32_t getDeviceSize()** idem
|
||||
- **uint8_t getPageSize()** idem
|
||||
- **uint32_t getLastWrite()** idem
|
||||
- **uint32_t determineSize(bool debug = false)**
|
||||
function that determins the size of the EEPROM by detecting when a memory address is folded upon memory address 0.
|
||||
It is based upon the observation that memory wraps around.
|
||||
The debug flag gives some 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 |
|
||||
**Warning**: this function has changed (again) in 1.4.0
|
||||
|
||||
Testresults
|
||||
|
||||
| returns | Type | Memory | Page Size | Notes |
|
||||
|:--------|:--------|:---------|:-----:|:------|
|
||||
| 0 | - | | | connect error, check device address / wiring |
|
||||
| 65536 | 24LC512 | 64 KB | 128 | |
|
||||
| 32768 | 24LC256 | 32 KB | 64 | |
|
||||
| 16384 | 24LC128 | 16 KB | 64 | |
|
||||
| 8192 | 24LC64 | 8 KB | 32 | |
|
||||
| 4096 | 24LC32 | 4 KB | 32 | not tested with hardware |
|
||||
| 2048 | 24LC16 | 2 KB | 16 | |
|
||||
| 1024 | 24LC08 | 1 KB | 16 | |
|
||||
| 512 | 24LC04 | 512 b | 16 | |
|
||||
| 256 | 24LC02 | 256 b | 8 | |
|
||||
| 128 | 24LC01 | 128 b | 8 | |
|
||||
|
||||
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
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
//
|
||||
// FILE: I2C_eeprom_determineSize.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: test determinsize function
|
||||
// PURPOSE: test determinSize() function
|
||||
//
|
||||
|
||||
|
||||
@@ -10,9 +9,7 @@
|
||||
#include "I2C_eeprom.h"
|
||||
|
||||
|
||||
#define MEMORY_SIZE 0x2000 //total bytes can be accessed 24LC64 = 0x2000 (maximum address = 0x1FFF)
|
||||
|
||||
I2C_eeprom ee(0x50, MEMORY_SIZE);
|
||||
I2C_eeprom ee(0x50, I2C_DEVICESIZE_24LC256);
|
||||
|
||||
uint32_t start, diff;
|
||||
|
||||
@@ -21,6 +18,8 @@ void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("I2C_EEPROM_VERSION: ");
|
||||
Serial.println(I2C_EEPROM_VERSION);
|
||||
|
||||
ee.begin();
|
||||
if (! ee.isConnected())
|
||||
@@ -29,28 +28,32 @@ void setup()
|
||||
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
|
||||
uint32_t size = ee.determineSize(false); // debug param
|
||||
diff = micros() - start;
|
||||
Serial.print("TIME: ");
|
||||
Serial.print(diff);
|
||||
Serial.println(" us.");
|
||||
if (size > 0)
|
||||
if (size == 0)
|
||||
{
|
||||
Serial.println("SIZE: could not determine size");
|
||||
}
|
||||
else if (size > 1024)
|
||||
{
|
||||
Serial.print("SIZE: ");
|
||||
Serial.print(size / 1024);
|
||||
Serial.println(" KB.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("SIZE: ");
|
||||
Serial.print(size);
|
||||
Serial.println(" KB.");
|
||||
}
|
||||
if (size == 0)
|
||||
{
|
||||
Serial.println("WARNING: Can't determine eeprom size");
|
||||
Serial.println(" bytes.");
|
||||
}
|
||||
|
||||
Serial.println("Done...");
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
//
|
||||
// FILE: I2C_eeprom_format.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: demo format EEPROM
|
||||
//
|
||||
|
||||
|
||||
#include "Wire.h"
|
||||
#include "I2C_eeprom.h"
|
||||
|
||||
|
||||
I2C_eeprom ee(0x50, I2C_DEVICESIZE_24LC256);
|
||||
|
||||
uint32_t start, diff;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("I2C_EEPROM_VERSION: ");
|
||||
Serial.println(I2C_EEPROM_VERSION);
|
||||
|
||||
ee.begin();
|
||||
if (! ee.isConnected())
|
||||
{
|
||||
Serial.println("ERROR: Can't find eeprom\nstopped...");
|
||||
while (1);
|
||||
}
|
||||
|
||||
|
||||
Serial.println();
|
||||
uint32_t size = ee.determineSize(false); // debug param
|
||||
if (size == 0)
|
||||
{
|
||||
Serial.println("SIZE: could not determine size");
|
||||
}
|
||||
else if (size > 1024)
|
||||
{
|
||||
Serial.print("SIZE: ");
|
||||
Serial.print(size / 1024);
|
||||
Serial.println(" KB.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("SIZE: ");
|
||||
Serial.print(size);
|
||||
Serial.println(" bytes.");
|
||||
}
|
||||
|
||||
|
||||
// flush input
|
||||
while (Serial.available()) Serial.read();
|
||||
|
||||
Serial.println("Sure to format EEPROM? [Y | N]");
|
||||
while (!Serial.available());
|
||||
int ans = Serial.read();
|
||||
|
||||
if ( ans == 'Y')
|
||||
{
|
||||
start = millis();
|
||||
for (uint32_t i = 0; i < size; i += 128)
|
||||
{
|
||||
if (i % 1024 == 0) Serial.print('.');
|
||||
ee.setBlock(0, 0xFF, 128);
|
||||
}
|
||||
diff = millis() - start;
|
||||
Serial.print("\nTIME: ");
|
||||
Serial.print(diff);
|
||||
Serial.println(" ms.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("not formatted");
|
||||
}
|
||||
|
||||
Serial.println("Done...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
@@ -1,46 +1,46 @@
|
||||
//
|
||||
// FILE: I2C_eeprom_test.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.09
|
||||
// PURPOSE: show/test I2C_EEPROM library
|
||||
//
|
||||
|
||||
#include <Wire.h> //I2C library
|
||||
#include <I2C_eeprom.h>
|
||||
// uses a 24LC256 (32KB) EEPROM
|
||||
|
||||
#include "Wire.h"
|
||||
#include "I2C_eeprom.h"
|
||||
|
||||
|
||||
// UNO
|
||||
#define SERIAL_OUT Serial
|
||||
// Due
|
||||
// #define SERIAL_OUT SerialUSB
|
||||
|
||||
//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);
|
||||
I2C_eeprom ee(0x50, I2C_DEVICESIZE_24LC256);
|
||||
|
||||
uint32_t start, diff, totals = 0;
|
||||
|
||||
void setup()
|
||||
{
|
||||
ee.begin();
|
||||
|
||||
SERIAL_OUT.begin(57600);
|
||||
void setup()
|
||||
{
|
||||
SERIAL_OUT.begin(115200);
|
||||
while (!SERIAL_OUT); // wait for SERIAL_OUT port to connect. Needed for Leonardo only
|
||||
|
||||
SERIAL_OUT.print("Demo I2C eeprom library ");
|
||||
SERIAL_OUT.println(__FILE__);
|
||||
|
||||
ee.begin();
|
||||
if (! ee.isConnected())
|
||||
{
|
||||
SERIAL_OUT.println("ERROR: Can't find eeprom\nstopped...");
|
||||
while (1);
|
||||
}
|
||||
|
||||
SERIAL_OUT.print("I2C eeprom library: ");
|
||||
SERIAL_OUT.print(I2C_EEPROM_VERSION);
|
||||
SERIAL_OUT.println("\n");
|
||||
|
||||
SERIAL_OUT.println("\nTEST: determine size");
|
||||
start = micros();
|
||||
int size = ee.determineSize();
|
||||
uint32_t size = ee.determineSize();
|
||||
diff = micros() - start;
|
||||
SERIAL_OUT.print("TIME: ");
|
||||
SERIAL_OUT.println(diff);
|
||||
@@ -56,7 +56,7 @@ void setup()
|
||||
else
|
||||
{
|
||||
SERIAL_OUT.println("ERROR: Can't find eeprom\nstopped...");
|
||||
while(1);
|
||||
while (1);
|
||||
}
|
||||
|
||||
SERIAL_OUT.println("\nTEST: 64 byte page boundary writeBlock");
|
||||
@@ -86,21 +86,21 @@ void setup()
|
||||
|
||||
SERIAL_OUT.println("\nTEST: write large string readback in small steps");
|
||||
ee.setBlock(0, 0, 128);
|
||||
char data2[] = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999A";
|
||||
char data2[] = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999A";
|
||||
ee.writeBlock(10, (uint8_t *) &data2, sizeof(data2));
|
||||
dumpEEPROM(0, 128);
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
if (i % 10 == 0 ) SERIAL_OUT.println();
|
||||
SERIAL_OUT.print(' ');
|
||||
SERIAL_OUT.print(ee.readByte(10+i));
|
||||
SERIAL_OUT.print(ee.readByte(10 + i));
|
||||
}
|
||||
SERIAL_OUT.println();
|
||||
|
||||
|
||||
SERIAL_OUT.println("\nTEST: check almost endofPage writeBlock");
|
||||
ee.setBlock(0, 0, 128);
|
||||
char data3[] = "6666";
|
||||
char data3[] = "6666";
|
||||
ee.writeBlock(60, (uint8_t *) &data3, sizeof(data3));
|
||||
dumpEEPROM(0, 128);
|
||||
|
||||
@@ -115,7 +115,7 @@ void setup()
|
||||
SERIAL_OUT.print("\nTEST: timing writeByte()\t");
|
||||
uint32_t start = micros();
|
||||
ee.writeByte(10, 1);
|
||||
uint32_t diff = micros() - start;
|
||||
uint32_t diff = micros() - start;
|
||||
SERIAL_OUT.print("TIME: ");
|
||||
SERIAL_OUT.println(diff);
|
||||
totals += diff;
|
||||
@@ -123,7 +123,7 @@ void setup()
|
||||
SERIAL_OUT.print("TEST: timing writeBlock(50)\t");
|
||||
start = micros();
|
||||
ee.writeBlock(10, (uint8_t *) &data2, 50);
|
||||
diff = micros() - start;
|
||||
diff = micros() - start;
|
||||
SERIAL_OUT.print("TIME: ");
|
||||
SERIAL_OUT.println(diff);
|
||||
totals += diff;
|
||||
@@ -131,7 +131,7 @@ void setup()
|
||||
SERIAL_OUT.print("TEST: timing readByte()\t\t");
|
||||
start = micros();
|
||||
ee.readByte(10);
|
||||
diff = micros() - start;
|
||||
diff = micros() - start;
|
||||
SERIAL_OUT.print("TIME: ");
|
||||
SERIAL_OUT.println(diff);
|
||||
totals += diff;
|
||||
@@ -139,7 +139,7 @@ void setup()
|
||||
SERIAL_OUT.print("TEST: timing readBlock(50)\t");
|
||||
start = micros();
|
||||
ee.readBlock(10, (uint8_t *) &data2, 50);
|
||||
diff = micros() - start;
|
||||
diff = micros() - start;
|
||||
SERIAL_OUT.print("TIME: ");
|
||||
SERIAL_OUT.println(diff);
|
||||
totals += diff;
|
||||
@@ -154,7 +154,7 @@ void setup()
|
||||
SERIAL_OUT.print("\nTEST: timing writeByte()\t");
|
||||
start = micros();
|
||||
ee.writeByte(10, 1);
|
||||
diff = micros() - start;
|
||||
diff = micros() - start;
|
||||
SERIAL_OUT.print("TIME: ");
|
||||
SERIAL_OUT.println(diff);
|
||||
totals += diff;
|
||||
@@ -164,7 +164,7 @@ void setup()
|
||||
SERIAL_OUT.print("TEST: timing writeBlock(50)\t");
|
||||
start = micros();
|
||||
ee.writeBlock(10, (uint8_t *) &data2, 50);
|
||||
diff = micros() - start;
|
||||
diff = micros() - start;
|
||||
SERIAL_OUT.print("TIME: ");
|
||||
SERIAL_OUT.println(diff);
|
||||
totals += diff;
|
||||
@@ -174,7 +174,7 @@ void setup()
|
||||
SERIAL_OUT.print("TEST: timing readByte()\t\t");
|
||||
start = micros();
|
||||
ee.readByte(10);
|
||||
diff = micros() - start;
|
||||
diff = micros() - start;
|
||||
SERIAL_OUT.print("TIME: ");
|
||||
SERIAL_OUT.println(diff);
|
||||
totals += diff;
|
||||
@@ -184,7 +184,7 @@ void setup()
|
||||
SERIAL_OUT.print("TEST: timing readBlock(50)\t");
|
||||
start = micros();
|
||||
int xx = ee.readBlock(10, (uint8_t *) &data2, 50);
|
||||
diff = micros() - start;
|
||||
diff = micros() - start;
|
||||
SERIAL_OUT.print("TIME: ");
|
||||
SERIAL_OUT.println(diff);
|
||||
totals += diff;
|
||||
@@ -199,33 +199,21 @@ void setup()
|
||||
SERIAL_OUT.println("\tDone...");
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void dumpEEPROM(uint16_t memoryAddress, uint16_t length)
|
||||
{
|
||||
#ifdef DISPLAY_DECIMAL
|
||||
const int BLOCK_TO_LENGTH = 10;
|
||||
|
||||
SERIAL_OUT.print("\t ");
|
||||
#endif
|
||||
#ifdef DISPLAY_HEX
|
||||
SERIAL_OUT.print("\t ");
|
||||
#endif
|
||||
for(int x = 0; x < BLOCK_TO_LENGTH; x++) {
|
||||
if(x != 0) {
|
||||
#ifdef DISPLAY_DECIMAL
|
||||
SERIAL_OUT.print(" ");
|
||||
#endif
|
||||
#ifdef DISPLAY_HEX
|
||||
SERIAL_OUT.print(" ");
|
||||
#endif
|
||||
}
|
||||
#ifdef DISPLAY_DECIMAL
|
||||
for (int x = 0; x < 10; x++)
|
||||
{
|
||||
if (x != 0) SERIAL_OUT.print(" ");
|
||||
SERIAL_OUT.print(x);
|
||||
#endif
|
||||
#ifdef DISPLAY_HEX
|
||||
SERIAL_OUT.print(x,HEX);
|
||||
#endif
|
||||
}
|
||||
SERIAL_OUT.println();
|
||||
|
||||
@@ -233,34 +221,23 @@ void dumpEEPROM(uint16_t memoryAddress, uint16_t 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);
|
||||
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_OUT.println();
|
||||
}
|
||||
#ifdef DISPLAY_DECIMAL
|
||||
if (i != 0) SERIAL_OUT.println();
|
||||
sprintf(buf, "%05d", memoryAddress);
|
||||
#endif
|
||||
#ifdef DISPLAY_HEX
|
||||
sprintf(buf, "%04X", memoryAddress);
|
||||
#endif
|
||||
SERIAL_OUT.print(buf);
|
||||
SERIAL_OUT.print(":\t");
|
||||
}
|
||||
#ifdef DISPLAY_DECIMAL
|
||||
sprintf(buf, "%03d", b);
|
||||
#endif
|
||||
#ifdef DISPLAY_HEX
|
||||
sprintf(buf, "%02X", b);
|
||||
#endif
|
||||
SERIAL_OUT.print(buf);
|
||||
b = ee.readByte(++memoryAddress);
|
||||
b = ee.readByte(++memoryAddress);
|
||||
SERIAL_OUT.print(" ");
|
||||
}
|
||||
SERIAL_OUT.println();
|
||||
}
|
||||
|
||||
// END OF FILE
|
||||
@@ -1,8 +1,7 @@
|
||||
//
|
||||
// FILE: I2C_eeprom_test_performance.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: show/test I2C_EEPROM library
|
||||
// PURPOSE: test I2C_EEPROM library
|
||||
//
|
||||
|
||||
|
||||
@@ -10,30 +9,28 @@
|
||||
#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);
|
||||
I2C_eeprom ee(0x50, I2C_DEVICESIZE_24LC256);
|
||||
|
||||
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
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("VERSION: ");
|
||||
Serial.println(I2C_EEPROM_VERSION);
|
||||
|
||||
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);
|
||||
|
||||
for (uint32_t speed = 100000; speed <= 1000000; speed += 100000)
|
||||
{
|
||||
@@ -43,14 +40,15 @@ void setup()
|
||||
delay(10);
|
||||
test();
|
||||
}
|
||||
|
||||
Serial.println("\ndone...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
void test()
|
||||
@@ -146,27 +144,13 @@ void test()
|
||||
|
||||
void dumpEEPROM(uint16_t memoryAddress, uint16_t length)
|
||||
{
|
||||
#ifdef DISPLAY_DECIMAL
|
||||
const int BLOCK_TO_LENGTH = 10;
|
||||
|
||||
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
|
||||
for (int x = 0; x < 10; x++)
|
||||
{
|
||||
if (x != 0) Serial.print(" ");
|
||||
Serial.print(x);
|
||||
#endif
|
||||
#ifdef DISPLAY_HEX
|
||||
Serial.print(x, HEX);
|
||||
#endif
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
@@ -180,28 +164,17 @@ void dumpEEPROM(uint16_t memoryAddress, uint16_t length)
|
||||
char buf[6];
|
||||
if (memoryAddress % BLOCK_TO_LENGTH == 0)
|
||||
{
|
||||
if (i != 0) {
|
||||
Serial.println();
|
||||
}
|
||||
#ifdef DISPLAY_DECIMAL
|
||||
if (i != 0) Serial.println();
|
||||
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
|
||||
@@ -1,22 +1,22 @@
|
||||
//
|
||||
// FILE: I2C_eeprom_update.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: demo I2C_EEPROM library - updateByte
|
||||
//
|
||||
|
||||
#include <Wire.h>
|
||||
#include <I2C_eeprom.h>
|
||||
// uses a 24LC256 (32KB) EEPROM
|
||||
|
||||
#define MEMORY_SIZE 0x2000 // total bytes can be accessed 24LC64 = 0x2000 (maximum address = 0x1FFF)
|
||||
#include "Wire.h"
|
||||
#include "I2C_eeprom.h"
|
||||
|
||||
I2C_eeprom ee(0x50, MEMORY_SIZE);
|
||||
|
||||
I2C_eeprom ee(0x50, I2C_DEVICESIZE_24LC256);
|
||||
|
||||
uint32_t start, dur1, dur2;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
ee.begin();
|
||||
Serial.begin(115200);
|
||||
while (!Serial); // wait for SERIAL_OUT port to connect. Needed for Leonardo only
|
||||
|
||||
@@ -24,8 +24,15 @@ void setup()
|
||||
Serial.print("VERSION: ");
|
||||
Serial.println(I2C_EEPROM_VERSION);
|
||||
|
||||
ee.begin();
|
||||
if (! ee.isConnected())
|
||||
{
|
||||
Serial.println("ERROR: Can't find eeprom\nstopped...");
|
||||
while (1);
|
||||
}
|
||||
|
||||
Serial.println("\nTEST: determine size");
|
||||
int size = ee.determineSize();
|
||||
uint32_t size = ee.determineSize();
|
||||
if (size > 0)
|
||||
{
|
||||
Serial.print("SIZE: ");
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "I2C_EEPROM",
|
||||
"keywords": "EEPROM, 24LC256, 24LC64",
|
||||
"description": "Library for I2C EEPROMS.",
|
||||
"description": "Library for I2C EEPROMS.\n24LC512, 24LC256, 24LC128, 24LC64, 24LC32, 24LC16, 24LC08, 24LC04, 24LC02, 24LC01 and equivalents.",
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
@@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/I2C_EEPROM.git"
|
||||
},
|
||||
"version":"1.3.2",
|
||||
"version":"1.4.0",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
}
|
||||
+3
-3
@@ -1,9 +1,9 @@
|
||||
name=I2C_EEPROM
|
||||
version=1.3.2
|
||||
version=1.4.0
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Library for I2C EEPROMS.
|
||||
paragraph=24LC256 et al
|
||||
sentence=Library for I2C EEPROMS
|
||||
paragraph=24LC512, 24LC256, 24LC128, 24LC64, 24LC32, 24LC16, 24LC08, 24LC04, 24LC02, 24LC01 and equivalents.
|
||||
category=Data Storage
|
||||
url=https://github.com/RobTillaart/I2C_EEPROM.git
|
||||
architectures=*
|
||||
|
||||
Reference in new issue
Block a user