mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
1.6.0 I2C_EEPROM
This commit is contained in:
parent
f9f4129f50
commit
e59f65b8d9
@ -1,7 +1,7 @@
|
||||
//
|
||||
// FILE: I2C_eeprom.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 1.5.2
|
||||
// VERSION: 1.6.0
|
||||
// PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al.
|
||||
// URL: https://github.com/RobTillaart/I2C_EEPROM.git
|
||||
//
|
||||
@ -41,6 +41,7 @@
|
||||
// 1.5.0 2021-06-30 #28 fix addressing 24LC04/08/16
|
||||
// 1.5.1 2021-10-14 function to add extra for write cycle (experimental)
|
||||
// 1.5.2 2021-12-19 update library.json, license, minor edits
|
||||
// 1.6.0 2022-06-02 add verify functions.
|
||||
|
||||
|
||||
#include <I2C_eeprom.h>
|
||||
@ -116,7 +117,10 @@ bool I2C_eeprom::isConnected()
|
||||
return (_wire->endTransmission() == 0);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//
|
||||
// WRITE SECTION
|
||||
//
|
||||
int I2C_eeprom::writeByte(const uint16_t memoryAddress, const uint8_t data)
|
||||
{
|
||||
int rv = _WriteBlock(memoryAddress, &data, 1);
|
||||
@ -143,6 +147,10 @@ int I2C_eeprom::writeBlock(const uint16_t memoryAddress, const uint8_t* buffer,
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//
|
||||
// READ SECTION
|
||||
//
|
||||
uint8_t I2C_eeprom::readByte(const uint16_t memoryAddress)
|
||||
{
|
||||
uint8_t rdata;
|
||||
@ -170,12 +178,19 @@ uint16_t I2C_eeprom::readBlock(const uint16_t memoryAddress, uint8_t* buffer, co
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//
|
||||
// UPDATE SECTION
|
||||
//
|
||||
// returns 0 == OK
|
||||
int I2C_eeprom::updateByte(const uint16_t memoryAddress, const uint8_t data)
|
||||
{
|
||||
if (data == readByte(memoryAddress)) return 0;
|
||||
return writeByte(memoryAddress, data);
|
||||
}
|
||||
|
||||
|
||||
// returns bytes updated
|
||||
int I2C_eeprom::updateBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint16_t length)
|
||||
{
|
||||
uint16_t addr = memoryAddress;
|
||||
@ -201,6 +216,61 @@ int I2C_eeprom::updateBlock(const uint16_t memoryAddress, const uint8_t* buffer,
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//
|
||||
// VERIFY SECTION
|
||||
//
|
||||
bool I2C_eeprom::writeByteVerify(const uint16_t memoryAddress, const uint8_t value)
|
||||
{
|
||||
if (writeByte(memoryAddress, value) != 0 ) return false;
|
||||
uint8_t data = readByte(memoryAddress);
|
||||
return (data == value);
|
||||
}
|
||||
|
||||
|
||||
bool I2C_eeprom::writeBlockVerify(const uint16_t memoryAddress, const uint8_t* buffer, const uint16_t length)
|
||||
{
|
||||
if (writeBlock(memoryAddress, buffer, length) != 0) return false;
|
||||
uint8_t data[length];
|
||||
if (readBlock(memoryAddress, data, length) != length) return false;
|
||||
return memcmp(data, buffer, length) == 0;
|
||||
}
|
||||
|
||||
|
||||
bool I2C_eeprom::setBlockVerify(const uint16_t memoryAddress, const uint8_t value, const uint16_t length)
|
||||
{
|
||||
if (setBlock(memoryAddress, value, length) != 0) return false;
|
||||
uint8_t data[length];
|
||||
if (readBlock(memoryAddress, data, length) != length) return false;
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
if (data[i] != value) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool I2C_eeprom::updateByteVerify(const uint16_t memoryAddress, const uint8_t value)
|
||||
{
|
||||
if (updateByte(memoryAddress, value) != 0 ) return false;
|
||||
uint8_t data = readByte(memoryAddress);
|
||||
return (data == value);
|
||||
}
|
||||
|
||||
|
||||
bool I2C_eeprom::updateBlockVerify(const uint16_t memoryAddress, const uint8_t* buffer, const uint16_t length)
|
||||
{
|
||||
if (updateBlock(memoryAddress, buffer, length) != length) return false;
|
||||
uint8_t data[length];
|
||||
if (readBlock(memoryAddress, data, length) != length) return false;
|
||||
return memcmp(data, buffer, length) == 0;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//
|
||||
// METADATA SECTION
|
||||
//
|
||||
|
||||
// returns size in bytes
|
||||
// returns 0 if not connected
|
||||
//
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: I2C_eeprom.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 1.5.2
|
||||
// VERSION: 1.6.0
|
||||
// PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al.
|
||||
// URL: https://github.com/RobTillaart/I2C_EEPROM.git
|
||||
//
|
||||
@ -13,7 +13,7 @@
|
||||
#include "Wire.h"
|
||||
|
||||
|
||||
#define I2C_EEPROM_VERSION (F("1.5.2"))
|
||||
#define I2C_EEPROM_VERSION (F("1.6.0"))
|
||||
|
||||
|
||||
#define I2C_DEVICESIZE_24LC512 65536
|
||||
@ -62,7 +62,7 @@ public:
|
||||
// writes a byte to memoryAddress
|
||||
int writeByte(const uint16_t memoryAddress, const uint8_t value);
|
||||
// writes length bytes from buffer to EEPROM
|
||||
int writeBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint16_t length);
|
||||
int writeBlock(const uint16_t memoryAddress, const uint8_t * buffer, const uint16_t length);
|
||||
// set length bytes in the EEPROM to the same value.
|
||||
int setBlock(const uint16_t memoryAddress, const uint8_t value, const uint16_t length);
|
||||
|
||||
@ -70,7 +70,7 @@ public:
|
||||
// returns the value stored in memoryAddress
|
||||
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);
|
||||
uint16_t readBlock(const uint16_t memoryAddress, uint8_t * buffer, const uint16_t length);
|
||||
|
||||
|
||||
// updates a byte at memoryAddress, writes only if there is a new value.
|
||||
@ -79,8 +79,17 @@ public:
|
||||
// updates a block in memory, writes only if there is a new value.
|
||||
// only to be used when you expect to write same buffer multiple times.
|
||||
// test your performance gains!
|
||||
int updateBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint16_t length);
|
||||
int updateBlock(const uint16_t memoryAddress, const uint8_t * buffer, const uint16_t length);
|
||||
|
||||
// same functions as above but with verify
|
||||
// return true if write or verify failed.
|
||||
bool writeByteVerify(const uint16_t memoryAddress, const uint8_t value);
|
||||
bool writeBlockVerify(const uint16_t memoryAddress, const uint8_t * buffer, const uint16_t length);
|
||||
bool setBlockVerify(const uint16_t memoryAddress, const uint8_t value, const uint16_t length);
|
||||
bool updateByteVerify(const uint16_t memoryAddress, const uint8_t value);
|
||||
bool updateBlockVerify(const uint16_t memoryAddress, const uint8_t * buffer, const uint16_t length);
|
||||
|
||||
// Meta data functions
|
||||
uint32_t determineSize(const bool debug = false);
|
||||
|
||||
uint32_t getDeviceSize() { return _deviceSize; };
|
||||
|
@ -13,17 +13,29 @@ isConnected KEYWORD2
|
||||
|
||||
|
||||
# I2C_eeprom
|
||||
readByte KEYWORD2
|
||||
writeByte KEYWORD2
|
||||
setBlock KEYWORD2
|
||||
readBlock KEYWORD2
|
||||
writeBlock KEYWORD2
|
||||
determineSize KEYWORD2
|
||||
setBlock KEYWORD2
|
||||
|
||||
readByte KEYWORD2
|
||||
readBlock KEYWORD2
|
||||
|
||||
updateByte KEYWORD2
|
||||
updateBlock KEYWORD2
|
||||
|
||||
writeByteVerify KEYWORD2
|
||||
writeBlockVerify KEYWORD2
|
||||
setBlockVerify KEYWORD2
|
||||
updateByteVerify KEYWORD2
|
||||
updateBlockVerify KEYWORD2
|
||||
|
||||
determineSize KEYWORD2
|
||||
getDeviceSize KEYWORD2
|
||||
getPageSize KEYWORD2
|
||||
getLastWrite KEYWORD2
|
||||
|
||||
setExtraWriteCycleTime KEYWORD2
|
||||
getExtraWriteCycleTime KEYWORD2
|
||||
|
||||
# I2C_eeprom_cyclic_store
|
||||
format KEYWORD2
|
||||
@ -31,4 +43,18 @@ read KEYWORD2
|
||||
write KEYWORD2
|
||||
getMetrics KEYWORD2
|
||||
|
||||
|
||||
# Constants (LITERAL1)
|
||||
I2C_EEPROM_VERSION LITERAL1
|
||||
|
||||
I2C_DEVICESIZE_24LC512 LITERAL1
|
||||
I2C_DEVICESIZE_24LC256 LITERAL1
|
||||
I2C_DEVICESIZE_24LC128 LITERAL1
|
||||
I2C_DEVICESIZE_24LC64 LITERAL1
|
||||
I2C_DEVICESIZE_24LC32 LITERAL1
|
||||
I2C_DEVICESIZE_24LC16 LITERAL1
|
||||
I2C_DEVICESIZE_24LC08 LITERAL1
|
||||
I2C_DEVICESIZE_24LC04 LITERAL1
|
||||
I2C_DEVICESIZE_24LC02 LITERAL1
|
||||
I2C_DEVICESIZE_24LC01 LITERAL1
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/I2C_EEPROM.git"
|
||||
},
|
||||
"version": "1.5.2",
|
||||
"version": "1.6.0",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=I2C_EEPROM
|
||||
version=1.5.2
|
||||
version=1.6.0
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Library for I2C EEPROMS
|
||||
|
@ -31,17 +31,38 @@ The **I2C_eeprom_cyclic_store** interface is documented [here](README_cyclic_sto
|
||||
- **bool isConnected()** test to see if deviceAddress is found on the bus.
|
||||
|
||||
|
||||
### Core functions
|
||||
### Write functions
|
||||
|
||||
- **int writeByte(uint16_t memoryAddress, uint8_t value)** write a single byte to the specified memory address. Returns 0 if OK.
|
||||
- **int writeBlock(uint16_t memoryAddress, uint8_t \* buffer, uint16_t length)** write a buffer starting at the specified memory address. Returns 0 if OK.
|
||||
- **int setBlock(uint16_t memoryAddress, uint8_t value, uint16_t length)** writes the same byte to length places starting at the specified memory address. Returns 0 if OK.
|
||||
|
||||
|
||||
### Update functions
|
||||
|
||||
- **int writeByte(uint16_t memoryAddress, uint8_t value)** write a single byte to the specified memory address.
|
||||
- **int updateByte(uint16_t memoryAddress, uint8_t value)** write a single byte, but only if changed.
|
||||
Returns 0 if value was same or write succeeded.
|
||||
- **int writeBlock(uint16_t memoryAddress, uint8_t \* buffer, uint16_t length)** write a buffer starting at the specified memory address.
|
||||
- **int updateBlock(uint16_t memoryAddress, uint8_t \* buffer, uint16_t length)** write a buffer starting at the specified memory address, but only if changed.
|
||||
- **int setBlock(uint16_t memoryAddress, uint8_t value, uint16_t length)** writes the same byte to length places starting at the specified memory address. Returns 0 if OK.
|
||||
|
||||
|
||||
### Read functions
|
||||
|
||||
- **uint8_t readByte(uint16_t memoryAddress)** read a single byte from a given address
|
||||
- **uint16_t readBlock(uint16_t memoryAddress, uint8_t \* buffer, uint16_t length)** read length bytes into buffer starting at specified memory address.
|
||||
Returns the number of bytes read, which should be length.
|
||||
Returns the number of bytes read, which should equal length.
|
||||
|
||||
|
||||
### Verify functions
|
||||
|
||||
Since 1.6.0. - experimental, needs extensive testing.
|
||||
|
||||
Same as write and update functions above. Returns true if successful, false indicates an error.
|
||||
|
||||
- **bool writeByteVerify(uint16_t memoryAddress, uint8_t value)**
|
||||
- **bool writeBlockVerify(uint16_t memoryAddress, uint8_t \* buffer, uint16_t length)**
|
||||
- **bool setBlockVerify(uint16_t memoryAddress, uint8_t value, uint16_t length)**
|
||||
- **bool updateByteVerify(uint16_t memoryAddress, uint8_t value)**
|
||||
- **bool updateBlockVerify(uint16_t memoryAddress, uint8_t \* buffer, uint16_t length)**
|
||||
|
||||
|
||||
### Other
|
||||
@ -110,11 +131,17 @@ The library does not offer multiple EEPROMS as one continuous storage device.
|
||||
## Future
|
||||
|
||||
- improve error handling, write functions should return bytes written or so.
|
||||
- what can we do with the print interface? (investigate)
|
||||
- investigate multi-EEPROM storage,
|
||||
- investigate smarter strategy for updateBlock() => find first and last changed position could possibly result in less writes.
|
||||
- can setBlock use strategies from update
|
||||
- investigate the print interface?
|
||||
- circular buffer?
|
||||
- investigate multi-EEPROM storage
|
||||
- wrapper class?
|
||||
- investigate smarter strategy for **updateBlock()**
|
||||
=> find first and last changed position could possibly result in less writes.
|
||||
- can **setBlock()** use strategies from **updateBlock()**
|
||||
- Add changelog
|
||||
|
||||
- internals
|
||||
- **\_waitEEReady();** can return bool and could use isConnected() internally.
|
||||
|
||||
## Operational
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user