Fix ESP32 bootloop control using RTC memory

This commit is contained in:
Theo Arends 2020-04-19 14:18:12 +02:00
parent 0f3619238f
commit eab612871c
5 changed files with 26 additions and 44 deletions

View File

@ -65,42 +65,6 @@ uint32_t ESP_getBootVersion(void)
return 1;
}
// ESP32 RTC memory is kept ONLY on deep_sleep wake. Any other restart will erase RTC memory
// Current solution is using NVS hopig it survives many writes ;-)
bool ESP_rtcUserMemoryWrite(uint32_t offset, uint32_t *data, size_t size)
{
char sName[16];
snprintf_P(sName, sizeof(sName), PSTR("rtc%d"), offset);
nvs_handle handle;
noInterrupts();
nvs_open("tasrtc", NVS_READWRITE, &handle);
nvs_set_blob(handle, sName, data, size);
nvs_commit(handle);
nvs_close(handle);
interrupts();
return true;
}
// ESP32 RTC memory is kept ONLY on deep_sleep wake. Any other restart will erase RTC memory
// Current solution is using NVS hopig it survives many writes ;-)
bool ESP_rtcUserMemoryRead(uint32_t offset, uint32_t *data, size_t size)
{
char sName[16];
snprintf_P(sName, sizeof(sName), PSTR("rtc%d"), offset);
nvs_handle handle;
noInterrupts();
nvs_open("tasrtc", NVS_READONLY, &handle);
size_t tsize = size;
nvs_get_blob(handle, sName, data, &tsize);
nvs_close(handle);
interrupts();
return true;
}
void ESP_reset()
{
ESP.restart();

View File

@ -36,8 +36,6 @@
String ESP_getResetReason(void);
uint32_t ESP_ResetInfoReason(void);
uint32_t ESP_getBootVersion(void);
bool ESP_rtcUserMemoryWrite(uint32_t offset, uint32_t *data, size_t size);
bool ESP_rtcUserMemoryRead(uint32_t offset, uint32_t *data, size_t size);
void ESP_reset();
uint32_t ESP_getFlashChipId();
uint32_t ESP_getChipId();

View File

@ -551,6 +551,9 @@ typedef struct {
uint8_t free_003[1]; // 283
} TRtcReboot;
TRtcReboot RtcReboot;
#ifdef ESP32
RTC_NOINIT_ATTR TRtcReboot RtcDataReboot;
#endif
typedef struct {
uint16_t valid; // 290 (RTC memory offset 100)
@ -570,6 +573,9 @@ typedef struct {
// 2EC - 2FF free locations
} TRtcSettings;
TRtcSettings RtcSettings;
#ifdef ESP32
RTC_NOINIT_ATTR TRtcSettings RtcDataSettings;
#endif
struct TIME_T {
uint8_t second;

View File

@ -40,14 +40,22 @@ void RtcSettingsSave(void)
{
if (GetRtcSettingsCrc() != rtc_settings_crc) {
RtcSettings.valid = RTC_MEM_VALID;
ESP_rtcUserMemoryWrite(100, (uint32_t*)&RtcSettings, sizeof(RtcSettings));
#ifdef ESP8266
ESP.rtcUserMemoryWrite(100, (uint32_t*)&RtcSettings, sizeof(RtcSettings));
#else
RtcDataSettings = RtcSettings;
#endif
rtc_settings_crc = GetRtcSettingsCrc();
}
}
void RtcSettingsLoad(void)
{
ESP_rtcUserMemoryRead(100, (uint32_t*)&RtcSettings, sizeof(RtcSettings)); // 0x290
#ifdef ESP8266
ESP.rtcUserMemoryRead(100, (uint32_t*)&RtcSettings, sizeof(RtcSettings)); // 0x290
#else
RtcSettings = RtcDataSettings;
#endif
if (RtcSettings.valid != RTC_MEM_VALID) {
memset(&RtcSettings, 0, sizeof(RtcSettings));
RtcSettings.valid = RTC_MEM_VALID;
@ -87,7 +95,11 @@ void RtcRebootSave(void)
{
if (GetRtcRebootCrc() != rtc_reboot_crc) {
RtcReboot.valid = RTC_MEM_VALID;
ESP_rtcUserMemoryWrite(100 - sizeof(RtcReboot), (uint32_t*)&RtcReboot, sizeof(RtcReboot));
#ifdef ESP8266
ESP.rtcUserMemoryWrite(100 - sizeof(RtcReboot), (uint32_t*)&RtcReboot, sizeof(RtcReboot));
#else
RtcDataReboot = RtcReboot;
#endif
rtc_reboot_crc = GetRtcRebootCrc();
}
}
@ -100,7 +112,11 @@ void RtcRebootReset(void)
void RtcRebootLoad(void)
{
ESP_rtcUserMemoryRead(100 - sizeof(RtcReboot), (uint32_t*)&RtcReboot, sizeof(RtcReboot)); // 0x280
#ifdef ESP8266
ESP.rtcUserMemoryRead(100 - sizeof(RtcReboot), (uint32_t*)&RtcReboot, sizeof(RtcReboot)); // 0x280
#else
RtcReboot = RtcDataReboot;
#endif
if (RtcReboot.valid != RTC_MEM_VALID) {
memset(&RtcReboot, 0, sizeof(RtcReboot));
RtcReboot.valid = RTC_MEM_VALID;

View File

@ -11,8 +11,6 @@
#ifdef ESP8266
// ESP8266
#define PACKED
#define ESP_rtcUserMemoryWrite(offset, data, size) ESP.rtcUserMemoryWrite(offset, data, size)
#define ESP_rtcUserMemoryRead(offset, data, size) ESP.rtcUserMemoryRead(offset, data, size)
#define ESP_getResetReason() ESP.getResetReason()
#define ESP_reset() ESP.reset()
#define ESP_getBootVersion() ESP.getBootVersion()