Skip to content

Commit d09c462

Browse files
committed
Add external EEPROM support for ESP8266
and support setting with MY_EXT_EEPROM definition
1 parent 6e4206b commit d09c462

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

MySensors.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
#include "hal/architecture/MyHwHAL.h"
5656
#include "hal/crypto/MyCryptoHAL.h"
5757
#if defined(ARDUINO_ARCH_ESP8266)
58+
#if defined(MY_EXT_EEPROM)
59+
#include "drivers/extEEPROM/extEEPROM.cpp"
60+
#endif
5861
#include "hal/architecture/ESP8266/MyHwESP8266.cpp"
5962
#include "hal/crypto/generic/MyCryptoGeneric.cpp"
6063
#elif defined(ARDUINO_ARCH_ESP32)

drivers/extEEPROM/extEEPROM.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,11 @@ byte extEEPROM::read(unsigned long addr, byte *values, unsigned int nBytes)
195195
return rxStatus; //read error
196196
}
197197

198+
#if defined(ARDUINO_ARCH_ESP8266)
199+
communication->requestFrom((int)ctrlByte, (int)nRead);
200+
#else
198201
communication->requestFrom(ctrlByte, nRead);
202+
#endif
199203
for (byte i=0; i<nRead; i++) {
200204
values[i] = communication->read();
201205
}

hal/architecture/ESP8266/MyHwESP8266.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,44 @@ bool hwInit(void)
2828
while (!MY_SERIALDEVICE) {}
2929
#endif
3030
#endif
31+
#if defined(MY_EXT_EEPROM)
32+
const uint8_t eepInit = eep.begin(MY_EXT_EEPROM_TWI_CLOCK, &Wire);
33+
// check connection to external EEPROM and return result
34+
return eepInit==0;
35+
#else
3136
EEPROM.begin(EEPROM_size);
3237
return true;
38+
#endif
3339
}
3440

41+
#if defined(MY_EXT_EEPROM)
42+
void hwReadConfigBlock(void *buf, void *addr, size_t length)
43+
{
44+
uint8_t *dst = static_cast<uint8_t *>(buf);
45+
const int offs = reinterpret_cast<int>(addr);
46+
(void)eep.read(offs, dst, length);
47+
}
48+
49+
void hwWriteConfigBlock(void *buf, void *addr, size_t length)
50+
{
51+
uint8_t *src = static_cast<uint8_t *>(buf);
52+
const int offs = reinterpret_cast<int>(addr);
53+
// use update() instead of write() to reduce e2p wear off
54+
(void)eep.update(offs, src, length);
55+
}
56+
57+
uint8_t hwReadConfig(const int addr)
58+
{
59+
return eep.read(addr);
60+
}
61+
62+
void hwWriteConfig(const int addr, uint8_t value)
63+
{
64+
(void)eep.update(addr, value);
65+
}
66+
67+
#else // !MY_EXT_EEPROM
68+
3569
void hwReadConfigBlock(void *buf, void *addr, size_t length)
3670
{
3771
uint8_t *dst = static_cast<uint8_t *>(buf);
@@ -63,6 +97,7 @@ void hwWriteConfig(const int addr, uint8_t value)
6397
{
6498
hwWriteConfigBlock(&value, reinterpret_cast<void *>(addr), 1);
6599
}
100+
#endif //MY_EXT_EEPROM
66101

67102
bool hwUniqueID(unique_id_t *uniqueID)
68103
{

hal/architecture/ESP8266/MyHwESP8266.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#include <SPI.h>
2323
#include <ESP8266WiFi.h>
2424
#include <WiFiUdp.h>
25+
#if !defined(MY_EXT_EEPROM)
2526
#include <EEPROM.h>
27+
#endif
2628

2729
#ifdef __cplusplus
2830
#include <Arduino.h>
@@ -38,7 +40,19 @@
3840
#define MY_DEBUGDEVICE MY_SERIALDEVICE
3941
#endif
4042

43+
#if defined(MY_EXT_EEPROM)
44+
// defines for external i2c EEPROM
45+
#define MY_EXT_EEPROM_I2C_ADDRESS (0x50u)
46+
#define MY_EXT_EEPROM_SIZE (kbits_1024)
47+
#define MY_EXT_EEPROM_PAGE_SIZE (32u)
48+
49+
extEEPROM eep(MY_EXT_EEPROM_SIZE, 1, MY_EXT_EEPROM_PAGE_SIZE,
50+
MY_EXT_EEPROM_I2C_ADDRESS); //device size, number of devices, page size
51+
52+
#define MY_EXT_EEPROM_TWI_CLOCK (eep.twiClock100kHz) // can be set to 400kHz with precaution if other i2c devices on bus
53+
#else
4154
#define EEPROM_size (1024)
55+
#endif
4256

4357
// Define these as macros to save valuable space
4458
#define hwDigitalWrite(__pin, __value) digitalWrite(__pin, __value)

0 commit comments

Comments
 (0)