From b8392328282245542b9162d5ed6d1a2946d67abd Mon Sep 17 00:00:00 2001 From: Rop Gonggrijp Date: Tue, 5 Mar 2024 19:22:24 +0100 Subject: [PATCH] Added wasRestored(), bumped version to 1.2.1 --- README.md | 6 ++++-- examples/deep_sleep_boot_count/deep_sleep_boot_count.ino | 5 +++++ library.properties | 2 +- src/ESP32_RTC_EEPROM.cpp | 9 ++++++++- src/ESP32_RTC_EEPROM.h | 4 +++- 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 64c2b44..325ed74 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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** diff --git a/examples/deep_sleep_boot_count/deep_sleep_boot_count.ino b/examples/deep_sleep_boot_count/deep_sleep_boot_count.ino index 2564136..5001d21 100644 --- a/examples/deep_sleep_boot_count/deep_sleep_boot_count.ino +++ b/examples/deep_sleep_boot_count/deep_sleep_boot_count.ino @@ -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); diff --git a/library.properties b/library.properties index 988e7e1..d7fd2f3 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ESP32_RTC_EEPROM -version=0.2.0 +version=0.2.1 author=Rop Gonggrijp maintainer=Rop Gonggrijp sentence=EEPROM emulation that stores in RTC RAM. Survives deep sleep, but not reset or power loss. diff --git a/src/ESP32_RTC_EEPROM.cpp b/src/ESP32_RTC_EEPROM.cpp index cf23940..92e74df 100644 --- a/src/ESP32_RTC_EEPROM.cpp +++ b/src/ESP32_RTC_EEPROM.cpp @@ -4,7 +4,7 @@ -ported by Paolo Becchi to Esp32 from esp8266 EEPROM -Modified by Elochukwu Ifediora -Converted to nvs lbernstone@gmail.com - -Adapted for ESP32_RTC_EEPROM.cpp by Rop Gonggrijp + -Adapted for ESP32_RTC_EEPROM.cpp by Rop Gonggrijp This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -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) { @@ -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; @@ -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; diff --git a/src/ESP32_RTC_EEPROM.h b/src/ESP32_RTC_EEPROM.h index 0afc4aa..9fe0d4f 100644 --- a/src/ESP32_RTC_EEPROM.h +++ b/src/ESP32_RTC_EEPROM.h @@ -4,7 +4,7 @@ -ported by Paolo Becchi to Esp32 from esp8266 EEPROM -Modified by Elochukwu Ifediora -Converted to nvs lbernstone@gmail.com - -Adapted for ESP32_RTC_EEPROM.cpp by Rop Gonggrijp + -Adapted for ESP32_RTC_EEPROM.cpp by Rop Gonggrijp This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -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(); @@ -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)