2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
[![Arduino CI](https://github.com/RobTillaart/I2C_EEPROM/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
2021-11-08 07:26:03 -05:00
|
|
|
[![Arduino-lint](https://github.com/RobTillaart/I2C_EEPROM/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/I2C_EEPROM/actions/workflows/arduino-lint.yml)
|
|
|
|
[![JSON check](https://github.com/RobTillaart/I2C_EEPROM/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/I2C_EEPROM/actions/workflows/jsoncheck.yml)
|
2023-09-08 05:35:43 -04:00
|
|
|
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/I2C_EEPROM.svg)](https://github.com/RobTillaart/I2C_EEPROM/issues)
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/I2C_EEPROM/blob/master/LICENSE)
|
|
|
|
[![GitHub release](https://img.shields.io/github/release/RobTillaart/I2C_EEPROM.svg?maxAge=3600)](https://github.com/RobTillaart/I2C_EEPROM/releases)
|
2023-09-08 05:35:43 -04:00
|
|
|
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/I2C_EEPROM.svg)](https://registry.platformio.org/libraries/robtillaart/I2C_EEPROM)
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-12-19 14:05:24 -05:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
# I2C_EEPROM
|
|
|
|
|
2021-12-19 14:05:24 -05:00
|
|
|
Arduino Library for external I2C EEPROM - 24LC512, 24LC256, 24LC64/32/16/08/04/02/01.
|
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
|
|
|
|
## Description
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
This library is to access external I2C EEPROM up to 64KB (= 512 Kbit) in size.
|
|
|
|
MicroChip 24LC512, 24LC256, 24LC64, 24LC32, 24LC16, 24LC08, 24LC04, 24LC02, 24LC01 and equivalents.
|
|
|
|
|
|
|
|
|
|
|
|
The **I2C_eeprom_cyclic_store** interface is documented [here](README_cyclic_store.md)
|
|
|
|
|
|
|
|
|
2023-05-11 06:36:04 -04:00
|
|
|
#### RP2040
|
|
|
|
|
|
|
|
There are at least two boards modules for the RP2040 that use a different Wire libraries.
|
|
|
|
One from "Earle F. Philhower" and an "MBED" one. See issues #53 and #55 for details.
|
|
|
|
|
|
|
|
In 1.7.3 defines are checked to select between these two and as far as tested this seems
|
|
|
|
to solve the issue #53 while being backwards compatible.
|
|
|
|
If a better solution is found, it will be implemented.
|
|
|
|
|
|
|
|
|
2023-05-02 07:06:57 -04:00
|
|
|
#### Links
|
|
|
|
|
|
|
|
- https://github.com/RobTillaart/I2C_24LC1025
|
|
|
|
|
|
|
|
|
2022-10-01 07:17:26 -04:00
|
|
|
## Schematic
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
+---U---+
|
|
|
|
A0 | 1 8 | VCC = +5V
|
|
|
|
A1 | 2 7 | WP = write protect pin
|
|
|
|
A2 | 3 6 | SCL = I2C clock
|
|
|
|
GND | 4 5 | SDA = I2C data
|
|
|
|
+-------+
|
|
|
|
|
|
|
|
default address = 0x50 .. 0x57 depending on three address lines
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
## Interface
|
|
|
|
|
2023-05-02 07:06:57 -04:00
|
|
|
```cpp
|
|
|
|
#include "I2C_eeprom.h"
|
|
|
|
```
|
|
|
|
|
2022-06-12 06:47:07 -04:00
|
|
|
The interface is kept quite identical to the I2C_24LC1025 library.
|
|
|
|
https://github.com/RobTillaart/I2C_24LC1025
|
|
|
|
|
2023-01-12 11:52:04 -05:00
|
|
|
Most important difference is 32 bit memory addresses.
|
2022-06-12 06:47:07 -04:00
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
### Constructor
|
|
|
|
|
2022-10-31 11:53:19 -04:00
|
|
|
- **I2C_eeprom(uint8_t deviceAddress, TwoWire \*wire = &Wire)** constructor,
|
|
|
|
optional Wire interface.
|
|
|
|
- **I2C_eeprom(uint8_t deviceAddress, uint32_t deviceSize, TwoWire \*wire = &Wire)**
|
|
|
|
constructor, with optional Wire interface.
|
2023-09-08 05:35:43 -04:00
|
|
|
- **bool begin(uint8_t writeProtectPin = -1)** initializes the I2C bus with the default pins.
|
2022-10-31 11:53:19 -04:00
|
|
|
Furthermore it checks if the deviceAddress is available on the I2C bus.
|
|
|
|
Returns true if deviceAddress is found on the bus, false otherwise.
|
2023-09-08 05:35:43 -04:00
|
|
|
Optionally one can set the **WP** writeProtect pin. (see section below).
|
|
|
|
If the **WP** pin is defined the default will be to **not** allow writing.
|
|
|
|
- **bool begin(uint8_t sda, uint8_t scl, uint8_t writeProtectPin = -1)** for ESP32 / ESP8266 / RP2040 and alike.
|
2023-05-02 07:06:57 -04:00
|
|
|
Initializes the I2C bus with the specified pins, thereby overruling the default pins.
|
2022-10-31 11:53:19 -04:00
|
|
|
Furthermore it checks if the deviceAddress is available on the I2C bus.
|
|
|
|
Returns true if deviceAddress is found on the bus, false otherwise.
|
2023-09-08 05:35:43 -04:00
|
|
|
Optionally one can set the **WP** writeProtect pin. (see section below).
|
|
|
|
If the **WP** pin is defined the default will be to **not** allow writing.
|
2021-07-01 04:58:13 -04:00
|
|
|
- **bool isConnected()** test to see if deviceAddress is found on the bus.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2022-06-07 11:43:11 -04:00
|
|
|
### Write functions
|
|
|
|
|
2022-10-31 11:53:19 -04:00
|
|
|
- **int writeByte(uint16_t memoryAddress, uint8_t value)** write a single byte to
|
|
|
|
the specified memory address.
|
2022-06-12 06:47:07 -04:00
|
|
|
Returns I2C status, 0 = OK.
|
2022-10-31 11:53:19 -04:00
|
|
|
- **int writeBlock(uint16_t memoryAddress, uint8_t \* buffer, uint16_t length)**
|
|
|
|
write a buffer starting at the specified memory address.
|
2022-06-12 06:47:07 -04:00
|
|
|
Returns I2C status, 0 = OK.
|
2022-10-31 11:53:19 -04:00
|
|
|
- **int setBlock(uint16_t memoryAddress, uint8_t value, uint16_t length)** writes
|
|
|
|
the same byte to length places starting at the specified memory address.
|
2022-06-12 06:47:07 -04:00
|
|
|
Returns I2C status, 0 = OK.
|
2022-06-07 11:43:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
### Update functions
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2022-10-31 11:53:19 -04:00
|
|
|
- **int updateByte(uint16_t memoryAddress, uint8_t value)** write a single byte,
|
|
|
|
but only if changed.
|
2021-12-19 14:05:24 -05:00
|
|
|
Returns 0 if value was same or write succeeded.
|
2022-10-31 11:53:19 -04:00
|
|
|
- **uint16_t updateBlock(uint16_t memoryAddress, uint8_t \* buffer, uint16_t length)**
|
|
|
|
write a buffer starting at the specified memory address, but only if changed.
|
2022-06-12 06:47:07 -04:00
|
|
|
Returns bytes written.
|
2022-06-07 11:43:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
### Read functions
|
|
|
|
|
2022-10-31 11:53:19 -04:00
|
|
|
- **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.
|
2022-06-12 06:47:07 -04:00
|
|
|
Returns the number of bytes read, which should be length.
|
2022-06-07 11:43:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
### 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)**
|
2020-11-27 05:16:22 -05:00
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
### Other
|
|
|
|
|
|
|
|
- **uint32_t getDeviceSize()** idem
|
|
|
|
- **uint8_t getPageSize()** idem
|
|
|
|
- **uint8_t getPageSize(uint32_t deviceSize)** idem
|
|
|
|
- **uint32_t getLastWrite()** idem
|
2021-12-19 14:05:24 -05:00
|
|
|
- **uint32_t determineSize(bool debug = false)**
|
2023-05-02 07:06:57 -04:00
|
|
|
function that determines the size of the EEPROM by detecting when a memory address
|
|
|
|
is folded upon memory address 0.
|
2021-12-19 14:05:24 -05:00
|
|
|
It is based upon the observation that memory wraps around.
|
2022-10-31 11:53:19 -04:00
|
|
|
The debug flag prints some output to Serial.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-12-19 14:05:24 -05:00
|
|
|
**Warning**: this function has changed (again) in 1.4.0
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-12-19 14:05:24 -05:00
|
|
|
Test results
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
| Type | returns | Memory | Page Size | Notes |
|
2021-11-08 07:26:03 -05:00
|
|
|
|:--------|:--------|:---------|:---------:|:------|
|
|
|
|
| - | 0 | | | connect error, check device address / wiring |
|
2022-10-31 11:53:19 -04:00
|
|
|
| 24LC512 | 65536 | 64 KB | 128 | |
|
|
|
|
| 24LC256 | 32768 | 32 KB | 64 | |
|
|
|
|
| 24LC128 | 16384 | 16 KB | 64 | |
|
|
|
|
| 24LC64 | 8192 | 8 KB | 32 | |
|
|
|
|
| 24LC32 | 4096 | 4 KB | 32 | not tested with hardware |
|
|
|
|
| 24LC16 | 2048 | 2 KB | 16 | |
|
|
|
|
| 24LC08 | 1024 | 1 KB | 16 | |
|
|
|
|
| 24LC04 | 512 | 512 b | 16 | |
|
|
|
|
| 24LC02 | 256 | 256 b | 8 | |
|
|
|
|
| 24LC01 | 128 | 128 b | 8 | |
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
The function cannot detect smaller than 128 bit EEPROMS.
|
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2023-01-12 11:52:04 -05:00
|
|
|
Experimental since 1.7.1 can be used for debugging and overruling constructor.
|
|
|
|
|
|
|
|
- **uint32_t setDeviceSize(uint32_t deviceSize)** overrules constructor setting.
|
|
|
|
returns set size == 128, 256, ... 32768, 65536
|
|
|
|
- **uint8_t setPageSize(uint8_t pageSize)** overrules constructor setting.
|
|
|
|
returns set size == 8, 16, 32, 64, 128.
|
|
|
|
|
|
|
|
|
2023-09-08 05:35:43 -04:00
|
|
|
### UpdateBlock()
|
2021-02-03 11:20:20 -05:00
|
|
|
|
|
|
|
(new since 1.4.2)
|
|
|
|
|
|
|
|
The function **updateBlock()** reads the block of data and compares it with the new values to see if it needs rewriting.
|
|
|
|
|
2022-06-12 06:47:07 -04:00
|
|
|
As the function reads/writes the data in blocks with a maximum length of **I2C_TWIBUFFERSIZE**
|
|
|
|
(== 30 AVR limitation; 128 for ESP32)
|
2021-12-19 14:05:24 -05:00
|
|
|
It does this comparison in chunks if the length exceeds this number.
|
|
|
|
The result is that an **updateBlock()** call can result e.g. in 4 reads and only 2 writes under the hood.
|
2021-02-03 11:20:20 -05:00
|
|
|
|
|
|
|
If data is changed often between writes, **updateBlock()** is slower than **writeBlock()**.
|
|
|
|
So you should verify if your sketch can make use of the advantages of **updateBlock()**
|
|
|
|
|
|
|
|
|
2023-09-08 05:35:43 -04:00
|
|
|
### ExtraWriteCycleTime (experimental)
|
2021-11-08 07:26:03 -05:00
|
|
|
|
2022-10-31 11:53:19 -04:00
|
|
|
To improve support older I2C EEPROMs e.g. IS24C16 two functions were
|
|
|
|
added to increase the waiting time before a read and/or write as some
|
|
|
|
older devices have a larger timeout
|
2021-11-08 07:26:03 -05:00
|
|
|
than 5 milliseconds which is the minimum.
|
|
|
|
|
|
|
|
- **void setExtraWriteCycleTime(uint8_t ms)** idem
|
|
|
|
- **uint8_t getExtraWriteCycleTime()** idem
|
|
|
|
|
2023-05-02 07:06:57 -04:00
|
|
|
Since 1.7.2 it is also possible to adjust the **I2C_WRITEDELAY** in the .h file
|
|
|
|
or overrule the define on the command line.
|
|
|
|
|
2021-11-08 07:26:03 -05:00
|
|
|
|
2023-09-08 05:35:43 -04:00
|
|
|
### WriteProtectPin WP (experimental)
|
|
|
|
|
|
|
|
(since 1.7.4)
|
|
|
|
|
|
|
|
The library can control the **WP** = WriteProtect pin of the EEPROM.
|
|
|
|
To do this one should connect a GPIO pin of the MCU to the **WP** pin of the EEPROM.
|
|
|
|
Furthermore the **WP** should be defined as a parameter in **begin()**.
|
|
|
|
If the **WP** pin is defined the default will be to **not** allow writing.
|
|
|
|
The user has to enable writing either by manual or automatic control.
|
|
|
|
|
|
|
|
In the automatic mode the library only allows writing to the EEPROM when it
|
|
|
|
actually writes to the EEPROM.
|
|
|
|
So it keeps the EEPROM in a read only mode as much as possible.
|
|
|
|
This prevents accidental writes due to (noisy) signals on the I2C bus. (#57)
|
|
|
|
|
|
|
|
|
|
|
|
Status
|
|
|
|
- **bool hasWriteProtectPin()** returns true if **WP** has been set.
|
|
|
|
|
|
|
|
Automatic control
|
|
|
|
- **void setAutoWriteProtect(bool b)** if set to true, the library enables writing
|
|
|
|
only when the EEPROM is actually written. This setting **overrules** the manual control.
|
|
|
|
If **setAutoWriteProtect()** is set to false (== default) the manual control is leading.
|
|
|
|
- **bool getAutoWriteProtect()** get current setting.
|
|
|
|
|
|
|
|
Manual control
|
|
|
|
- **void allowWrite()** allows writing by setting **WP** to LOW.
|
|
|
|
- **void preventWrite()** disables writing by setting **WP** to HIGH.
|
|
|
|
|
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
## Limitation
|
|
|
|
|
2023-05-02 07:06:57 -04:00
|
|
|
The library does not offer multiple EEPROMS as one continuous storage device.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
2022-10-31 11:53:19 -04:00
|
|
|
## Operation
|
|
|
|
|
|
|
|
See examples
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
## Future
|
|
|
|
|
2023-05-02 07:06:57 -04:00
|
|
|
#### Must
|
|
|
|
|
2022-10-31 11:53:19 -04:00
|
|
|
- improve documentation
|
|
|
|
|
2023-05-02 07:06:57 -04:00
|
|
|
#### Should
|
|
|
|
|
2022-06-07 11:43:11 -04:00
|
|
|
- investigate multi-EEPROM storage
|
|
|
|
- wrapper class?
|
2023-05-02 07:06:57 -04:00
|
|
|
- improve error handling,
|
|
|
|
- write functions should return bytes written or so.
|
2023-01-12 11:52:04 -05:00
|
|
|
- make deviceSize explicit in examples?
|
2022-10-31 11:53:19 -04:00
|
|
|
|
2023-05-02 07:06:57 -04:00
|
|
|
|
|
|
|
#### Could
|
|
|
|
|
2022-06-07 11:43:11 -04:00
|
|
|
- investigate smarter strategy for **updateBlock()**
|
|
|
|
=> find first and last changed position could possibly result in less writes.
|
|
|
|
- can **setBlock()** use strategies from **updateBlock()**
|
2023-09-08 05:35:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
#### Wont
|
|
|
|
|
2022-10-31 11:53:19 -04:00
|
|
|
- investigate the print interface?
|
|
|
|
- circular buffer? (see FRAM library)
|
|
|
|
- dump function?
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2023-09-08 05:35:43 -04:00
|
|
|
|
|
|
|
## Support
|
|
|
|
|
|
|
|
If you appreciate my libraries, you can support the development and maintenance.
|
|
|
|
Improve the quality of the libraries by providing issues and Pull Requests, or
|
|
|
|
donate through PayPal or GitHub sponsors.
|
|
|
|
|
|
|
|
Thank you,
|
|
|
|
|