Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d2af79e
[Storage] Add settings for External EEPROM
tonhuisman Aug 17, 2025
b310201
[Storage] Add settings for External EEPROM
tonhuisman Aug 17, 2025
e3864f1
[Storage] Implement initializing EEPROM and writing UserVars, several…
tonhuisman Aug 18, 2025
97d1308
[Storage] Reduce writing to only when data changes, add FRAM to I2C s…
tonhuisman Aug 19, 2025
759dff7
[Storage] Add WriteEE,slot,value command and [ReadEE#slot] for retrie…
tonhuisman Aug 19, 2025
872b628
[Storage] Add overview of all values in external EEPROM (new button o…
tonhuisman Aug 19, 2025
beced13
[Storage] Restore Task Values optionally on cold or warm boot, code i…
tonhuisman Aug 21, 2025
623664d
[Storage] Show Task Values in EEPROM by using tasks=1 parameter for E…
tonhuisman Aug 21, 2025
65f10f4
[Storage] Add check for WriteProtected EEPROM and show on Hardware page
tonhuisman Aug 22, 2025
ce8453e
[Storage] Add command to re-check for WriteProtected EEPROM
tonhuisman Aug 22, 2025
c56c3f6
[Storage] Improved initialization and format source
tonhuisman Aug 22, 2025
44e9d37
[Storage] [C016] Store Cache data in EEPROM
tonhuisman Aug 22, 2025
3f3205d
[Storage] Code improvements
tonhuisman Aug 23, 2025
11e9634
[Storage] Move Settings attributes to next available bits
tonhuisman Aug 25, 2025
7c1c263
[Storage] Limit Rules values storage to max. 1024 slots
tonhuisman Aug 27, 2025
8c7a88a
[Storage] Separate EEPROM and FRAM types in settings
tonhuisman Aug 27, 2025
bd67cc8
[Storage] Add setting per Task Value for saving in EEPROM (enabled by…
tonhuisman Aug 27, 2025
6a3cc81
[Storage] Add [ReadEE#WP] to retrieve the Write Protected state of th…
tonhuisman Aug 27, 2025
a113464
[Storage] Simplify EpromVarPage by only showing enabled tasks, and no…
tonhuisman Aug 27, 2025
1708c73
[Storage] Move EEPROM code to ESPEasy::eeprom namespace, store and ch…
tonhuisman Aug 28, 2025
495f902
[Storage] Store and check real UserVar checksum, temp. enable some EE…
tonhuisman Aug 28, 2025
a89685c
[Storage] Fix compilation for builds without P146 or C016 included
tonhuisman Aug 28, 2025
276d574
[Storage] Revert choice of saving all Task Values: Only selected Task…
tonhuisman Aug 29, 2025
bb34638
[Storage] Remove unneeded UserVar checksum write
tonhuisman Aug 30, 2025
a8f25c8
[Storage] Save only Enabled tasks values in EEPROM
tonhuisman Aug 30, 2025
d969c22
[Storage] Add some missing round brackets
tonhuisman Aug 30, 2025
f40334e
[Storage] Handle EEPROM saving in background, with optional save inte…
tonhuisman Aug 31, 2025
6f1a12b
[Storage] Fixes and improvements for Params check
tonhuisman Sep 1, 2025
add4c44
Merge branch 'mega' of https://github.com/letscontrolit/ESPEasy into …
tonhuisman Sep 10, 2025
817afc5
Merge branch 'mega' of https://github.com/letscontrolit/ESPEasy into …
tonhuisman Apr 6, 2026
c95689c
[Storage] Reduce functionality to only support writeee command and re…
tonhuisman Apr 6, 2026
3a31496
Merge branch 'mega' of https://github.com/letscontrolit/ESPEasy into …
tonhuisman Apr 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
345 changes: 345 additions & 0 deletions lib/AT24Cx/AT24CX.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,345 @@
/**

AT24CX.cpp
Library for using the EEPROM AT24C32/64

Copyright (c) 2014 Christian Paul

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/
#include "AT24CX.h"
#include <Wire.h>

/**
* Constructor with AT24Cx EEPROM at index 0
*/
AT24CX::AT24CX() {
init(0, 32, 4096);
}

/**
* Constructor with AT24Cx EEPROM at given index, size of page and max size in bytes
*/
AT24CX::AT24CX(uint8_t index, uint8_t pageSize, uint32_t maxSize) {
init(index, pageSize, maxSize);
}

/**
* Constructor with AT24C32 EEPROM at index 0
*/
AT24C32::AT24C32() {
init(0, 32, 4096);
}
/**
* Constructor with AT24Cx EEPROM at given index
*/
AT24C32::AT24C32(uint8_t index) {
init(index, 32, 4096);
}

/**
* Constructor with AT24C64 EEPROM at index 0
*/
AT24C64::AT24C64() {
init(0, 32, 8192);
}
/**
* Constructor with AT24C64 EEPROM at given index
*/
AT24C64::AT24C64(uint8_t index) {
init(index, 32, 8192);
}

/**
* Constructor with AT24C128 EEPROM at index 0
*/
AT24C128::AT24C128() {
init(0, 64, 16384);
}
/**
* Constructor with AT24C128 EEPROM at given index
*/
AT24C128::AT24C128(uint8_t index) {
init(index, 64, 16384);
}

/**
* Constructor with AT24C256 EEPROM at index 0
*/
AT24C256::AT24C256() {
init(0, 64, 32768);
}
/**
* Constructor with AT24C128 EEPROM at given index
*/
AT24C256::AT24C256(uint8_t index) {
init(index, 64, 32768);
}

/**
* Constructor with AT24C512 EEPROM at index 0
*/
AT24C512::AT24C512() {
init(0, 128, 65536);
}
/**
* Constructor with AT24C512 EEPROM at given index
*/
AT24C512::AT24C512(uint8_t index) {
init(index, 128, 65536);
}

/**
* Init
*/
void AT24CX::init(uint8_t index, uint8_t pageSize, uint32_t maxSize) {
_id = AT24CX_ID | (index & 0x7);
_pageSize = pageSize;
_maxSize = maxSize;
// Wire.begin();
}

/**
* Check address not reached
*/
bool AT24CX::checkSize(uint32_t address, uint32_t size) {
return (address + size - 1) <= _maxSize;
}

/**
* Write byte
*/
void AT24CX::write(uint32_t address, uint8_t data) {
if (!checkSize(address, 1)) {
return;
}
Wire.beginTransmission(_id);
if(Wire.endTransmission()==0) {
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
Wire.write(data);
Wire.endTransmission();
delay(20);
}
}

/**
* Write integer
*/
void AT24CX::writeInt(uint32_t address, uint16_t data) {
write(address, (uint8_t*)&data, 2);
}

/**
* Write long
*/
void AT24CX::writeLong(uint32_t address, uint32_t data) {
write(address, (uint8_t*)&data, 4);
}

/**
* Write float
*/
void AT24CX::writeFloat(uint32_t address, float data) {
write(address, (uint8_t*)&data, 4);
}

/**
* Write double
*/
void AT24CX::writeDouble(uint32_t address, double data) {
write(address, (uint8_t*)&data, 8);
}

/**
* Write chars
*/
void AT24CX::writeChars(uint32_t address, char *data, int length) {
write(address, (uint8_t*)data, length);
}

/**
* Write bytes
*/
void AT24CX::writeBytes(uint32_t address, uint8_t *data, int length) {
write(address, data, length);
}

/**
* Read integer
*/
uint16_t AT24CX::readInt(uint32_t address) {
memset(_b, 0, sizeof(_b));
read(address, _b, 2);
return *(uint32_t*)&_b[0];
}

/**
* Read long
*/
uint32_t AT24CX::readLong(uint32_t address) {
read(address, _b, 4);
return *(unsigned long*)&_b[0];
}

/**
* Read float
*/
float AT24CX::readFloat(uint32_t address) {
read(address, _b, 4);
return *(float*)&_b[0];
}

/**
* Read double
*/
double AT24CX::readDouble(uint32_t address) {
read(address, _b, 8);
return *(double*)&_b[0];
}

/**
* Read chars
*/
void AT24CX::readChars(uint32_t address, char *data, int n) {
read(address, (uint8_t*)data, n);
}

/**
* Read bytes
*/
void AT24CX::readBytes(uint32_t address, uint8_t *data, int n) {
read(address, data, n);
}

/**
* Write sequence of n bytes
*/
void AT24CX::write(uint32_t address, uint8_t *data, int n) {
if (!checkSize(address, n)) {
return;
}
// status quo
int c = n; // bytes left to write
int offD = 0; // current offset in data pointer
int offP; // current offset in page
int nc = 0; // next n bytes to write

// write alle bytes in multiple steps
while (c > 0) {
// calc offset in page
offP = address % _pageSize;
// maximal 30 bytes to write
nc = min(min(c, 30), _pageSize - offP);
write(address, data, offD, nc);
c-=nc;
offD+=nc;
address+=nc;
}
}

/**
* Write sequence of n bytes from offset
*/
void AT24CX::write(uint32_t address, uint8_t *data, int offset, int n) {
if (!checkSize(address, n)) {
return;
}
Wire.beginTransmission(_id);
if (Wire.endTransmission()==0) {
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
uint8_t *adr = data+offset;
Wire.write(adr, n);
Wire.endTransmission();
delay(20);
}
}

/**
* Read byte
*/
uint8_t AT24CX::read(uint32_t address) {
if (!checkSize(address, 1)) {
return 0;
}
uint8_t b = 0;
int r = 0;
Wire.beginTransmission(_id);
if (Wire.endTransmission()==0) {
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
if (Wire.endTransmission()==0) {
Wire.requestFrom(_id, 1);
while (Wire.available() > 0 && r<1) {
b = (uint8_t)Wire.read();
r++;
}
}
}
return b;
}

/**
* Read sequence of n bytes
*/
void AT24CX::read(uint32_t address, uint8_t *data, int n) {
if (!checkSize(address, n)) {
return;
}
int c = n;
int offD = 0;
// read until are n bytes read
while (c > 0) {
// read maximal 32 bytes
int nc = c;
if (nc > 32)
nc = 32;
read(address, data, offD, nc);
address+=nc;
offD+=nc;
c-=nc;
}
}


/**
* Read sequence of n bytes to offset
*/
void AT24CX::read(uint32_t address, uint8_t *data, int offset, int n) {
Wire.beginTransmission(_id);
if (Wire.endTransmission()==0) {
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
if (Wire.endTransmission()==0) {
int r = 0;
Wire.requestFrom(_id, n);
while (Wire.available() > 0 && r<n) {
data[offset+r] = (uint8_t)Wire.read();
r++;
}
}
}
}

Loading