bootoptions: use NVRAM instead of wearing out flash for crash detection in esp32

This commit is contained in:
Torrie Fischer 2023-02-19 18:40:16 +01:00
parent ae3abc3aa3
commit 160cbc5ea9
2 changed files with 27 additions and 3 deletions

View File

@ -1,9 +1,11 @@
#include "BootOptions.h"
#include "Config.h"
#include <EEPROM.h>
#ifdef BOARD_ESP8266
#include <ESP8266WiFi.h>
#endif
#include <EEPROM.h>
#include "Config.h"
#ifdef PLATFORM_PHOTON
LEDStatus serialStatus = LEDStatus(RGB_COLOR_ORANGE, LED_PATTERN_FADE, LED_SPEED_FAST, LED_PRIORITY_BACKGROUND);
@ -12,6 +14,10 @@ retained bool LAST_BOOT_WAS_FLASH;
retained bool LAST_BOOT_WAS_SERIAL;
#endif
#ifdef BOARD_ESP32
__NOINIT_ATTR uint8_t s_rebootCount = 0;
#endif
void
BootOptions::initPins()
{
@ -36,9 +42,24 @@ BootOptions::BootOptions()
configStatus.setActive(isSetup);
serialStatus.setActive(isSerial);
#endif
#ifdef BOARD_ESP32
resetReason = esp_reset_reason();
crashCount = s_rebootCount;
if (resetReason >= 4) { // TODO: These values are defined in
// esp32/rom/rtc.h, but not sure if that's included
// on platformio builds
if (crashCount++ >= 3) {
// Boot into safe mode if the watchdog reset us three times in a row.
isSafeMode = true;
}
} else {
crashCount = 0;
}
s_rebootCount = crashCount;
#endif
#ifdef BOARD_ESP8266
struct rst_info resetInfo = *ESP.getResetInfoPtr();
uint8_t crashCount;
resetReason = resetInfo.reason;
EEPROM.begin(sizeof(crashCount));
EEPROM.get(sizeof(HardwareConfig) + 32, crashCount);
EEPROM.end();

View File

@ -1,4 +1,5 @@
#pragma once
#include <Arduino.h>
struct BootOptions {
static void initPins();
@ -12,4 +13,6 @@ struct BootOptions {
bool isFlash = false;
bool lastBootWasFlash = false;
bool isSafeMode = false;
uint8_t crashCount = 0;
uint8_t resetReason = 0;
};