6 #ifndef ACE_TIME_HW_CRC_EEPROM_H 7 #define ACE_TIME_HW_CRC_EEPROM_H 9 #if ! defined(UNIX_HOST_DUINO) 15 #if !defined(AVR) && !defined(ESP8266) && !defined(ESP32) && \ 17 #error Unsupported architecture 49 #if defined(ESP8266) || defined(ESP32) 54 void begin(uint16_t ) {
62 const uint16_t dataSize)
const {
63 uint16_t byteCount = dataSize;
64 const uint8_t* d = (
const uint8_t*) data;
67 while (byteCount-- > 0) {
68 write(address++, *d++);
72 uint32_t crc = FastCRC32().crc32((
const uint8_t*) data, dataSize);
75 write(address++, buf[0]);
76 write(address++, buf[1]);
77 write(address++, buf[2]);
78 write(address++, buf[3]);
80 bool success = commit();
81 return (success) ? dataSize +
sizeof(crc) : 0;
89 const uint16_t dataSize)
const {
90 uint16_t byteCount = dataSize;
91 uint8_t* d = (uint8_t*) data;
94 while (byteCount-- > 0) {
95 *d++ = read(address++);
100 buf[0] = read(address++);
101 buf[1] = read(address++);
102 buf[2] = read(address++);
103 buf[3] = read(address++);
105 memcpy(&crc, buf, 4);
107 uint32_t dataCrc = FastCRC32().crc32((
const uint8_t*) data, dataSize);
108 return crc == dataCrc;
112 void write(
int address, uint8_t val)
const {
113 #if defined(ESP8266) || defined(ESP32) 114 EEPROM.write(address, val);
116 EEPROM.update(address, val);
120 uint8_t read(
int address)
const {
121 return EEPROM.read(address);
124 bool commit()
const {
125 #if defined(ESP8266) || defined(ESP32) 126 return EEPROM.commit();
uint16_t writeWithCrc(int address, const void *const data, const uint16_t dataSize) const
Write the data with its CRC.
void begin(uint16_t size)
Call from global setup() function.
Thin wrapper around the EEPROM object (from the the built-in EEPROM library) to read and write a give...
bool readWithCrc(int address, void *const data, const uint16_t dataSize) const
Read the data from EEPROM along with its CRC.