Skip to content

Commit

Permalink
Added wasRestored(), bumped version to 1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ropg committed Mar 5, 2024
1 parent 1291f05 commit b839232
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ This library will emulate EEPROM in RTC RAM, which stays powered during deep sle

### Optional saving to NVS (flash)

If you call `EEPROM.toNVS()`, a copy of your EEPROM data will be save to the key "eeprom" in NVS flash, exactly like when you would use the original `EEPROM.h`. Whenever the ESP32_RTC_EEPROM wakes up with an empty RTC RAM (which is detects by the presence of a magic word), it will try to see if there's a saved copy of the right size. If so, that is loaded. You can also manually revert to the last save by calling `EEPROM.fromNVS()`.
If you call `EEPROM.toNVS()`, a copy of your EEPROM data will be save to the key "eeprom" in NVS flash, exactly like when you would use the original `EEPROM.h`. Whenever the ESP32_RTC_EEPROM wakes up with an empty RTC RAM (which is detects by the presence of a magic word), it will try to see if there's a saved copy of the right size. If so, that is loaded. You can also manually revert to the last save by calling `EEPROM.fromNVS()`.

The function `EEPROM.wasRestored()` will tell you whether or not the contents were automaticcaly restored from NVS in this boot cycle.

### Usage

Expand All @@ -32,6 +34,6 @@ If you call `EEPROM.toNVS()`, a copy of your EEPROM data will be save to the key

### Details

* This library will always claim `EEPROM_SIZE` (set at 2048, change in the .h file) bytes of the RTC's RAM (it has 8k). You can set a lower number when calling `EEPROM.begin()`, but that number only limits what your code can read/write, not the actual RAM used.
* This library will always claim `EEPROM_SIZE` (set at 2048, change in the .h file) bytes of the RTC's RAM (it has 8k). You can set a lower number when calling `EEPROM.begin()`, but that number only limits what your code can read/write (and how much is written to NVS flash every time when backing up), not the actual RTC RAM used.

* Cannot repeat it often enough: **only useful in combination with the ESP's deep sleep, data in this fake EEPROM does not survive powercycling, resetting or reflashing**
5 changes: 5 additions & 0 deletions examples/deep_sleep_boot_count/deep_sleep_boot_count.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ void setup() {

// Print boot count
Serial.println("Boot count: " + String(bootcount));

// See if this was restored from NVS
if (EEPROM.wasRestored()) {
Serial.println("(This value was restored from NVS, so we may have missed a few.)");
}

// Write new value
EEPROM.write(0, bootcount);
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ESP32_RTC_EEPROM
version=0.2.0
version=0.2.1
author=Rop Gonggrijp <[email protected]>
maintainer=Rop Gonggrijp <[email protected]>
sentence=EEPROM emulation that stores in RTC RAM. Survives deep sleep, but not reset or power loss.
Expand Down
9 changes: 8 additions & 1 deletion src/ESP32_RTC_EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-ported by Paolo Becchi to Esp32 from esp8266 EEPROM
-Modified by Elochukwu Ifediora <[email protected]>
-Converted to nvs [email protected]
-Adapted for ESP32_RTC_EEPROM.cpp by Rop Gonggrijp
-Adapted for ESP32_RTC_EEPROM.cpp by Rop Gonggrijp <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -31,6 +31,8 @@ uint8_t RTC_DATA_ATTR EEPROMClass::_data[EEPROM_SIZE] = { 0x23, 0x42, 0x23, 0x42

size_t EEPROMClass::_size = EEPROM_SIZE;

bool EEPROMClass::_restored = false;


EEPROMClass::EEPROMClass(void)
{
Expand Down Expand Up @@ -92,6 +94,10 @@ bool EEPROMClass::toNVS() {
return true;
}

bool EEPROMClass::wasRestored() {
return _restored;
}

bool EEPROMClass::begin(size_t size) {
if (!size || size > EEPROM_SIZE) {
return false;
Expand All @@ -109,6 +115,7 @@ bool EEPROMClass::begin(size_t size) {
*magic_word = 0;
} else {
log_w("RTC EEPROM contents recovered from NVS backup.");
_restored = true;
}
}
return true;
Expand Down
4 changes: 3 additions & 1 deletion src/ESP32_RTC_EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-ported by Paolo Becchi to Esp32 from esp8266 EEPROM
-Modified by Elochukwu Ifediora <[email protected]>
-Converted to nvs [email protected]
-Adapted for ESP32_RTC_EEPROM.cpp by Rop Gonggrijp
-Adapted for ESP32_RTC_EEPROM.cpp by Rop Gonggrijp <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -41,6 +41,7 @@ class EEPROMClass {
static bool begin(size_t size);
static bool fromNVS();
static bool toNVS();
static bool wasRestored();
static uint8_t read(int address);
static void write(int address, uint8_t val);
static uint16_t length();
Expand Down Expand Up @@ -108,6 +109,7 @@ class EEPROMClass {
protected:
static size_t _size;
static uint8_t RTC_DATA_ATTR _data[EEPROM_SIZE];
static bool _restored;
};

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM)
Expand Down

0 comments on commit b839232

Please sign in to comment.