2021-03-29 08:10:55 +00:00
|
|
|
#include "BootOptions.h"
|
2023-02-19 17:40:16 +00:00
|
|
|
#include "Config.h"
|
|
|
|
|
|
|
|
#include <EEPROM.h>
|
|
|
|
|
2021-04-10 18:10:25 +00:00
|
|
|
#ifdef BOARD_ESP8266
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#endif
|
2021-03-29 08:10:55 +00:00
|
|
|
|
|
|
|
#ifdef PLATFORM_PHOTON
|
|
|
|
LEDStatus serialStatus = LEDStatus(RGB_COLOR_ORANGE, LED_PATTERN_FADE, LED_SPEED_FAST, LED_PRIORITY_BACKGROUND);
|
|
|
|
LEDStatus configStatus = LEDStatus(RGB_COLOR_YELLOW, LED_PATTERN_FADE, LED_SPEED_NORMAL, LED_PRIORITY_IMPORTANT);
|
|
|
|
retained bool LAST_BOOT_WAS_FLASH;
|
|
|
|
retained bool LAST_BOOT_WAS_SERIAL;
|
|
|
|
#endif
|
|
|
|
|
2023-12-11 06:54:08 +00:00
|
|
|
#ifndef __NOINIT_ATTR // Pre-defined on esp32
|
|
|
|
#define __NOINIT_ATTR __attribute__ ((section (".noinit")))
|
2023-02-19 17:40:16 +00:00
|
|
|
#endif
|
|
|
|
|
2023-12-11 06:54:08 +00:00
|
|
|
#define SAFE_MODE_MAGIC 6942
|
|
|
|
|
|
|
|
__NOINIT_ATTR uint8_t s_rebootCount;
|
|
|
|
__NOINIT_ATTR uint16_t s_forceSafeMode;
|
|
|
|
|
|
|
|
void
|
|
|
|
BootOptions::forceSafeMode()
|
|
|
|
{
|
|
|
|
s_forceSafeMode = SAFE_MODE_MAGIC;
|
|
|
|
}
|
|
|
|
|
2021-03-29 08:10:55 +00:00
|
|
|
void
|
|
|
|
BootOptions::initPins()
|
|
|
|
{
|
|
|
|
#ifdef PLATFORM_PHOTON
|
|
|
|
pinMode(2, INPUT_PULLDOWN);
|
|
|
|
pinMode(3, INPUT_PULLDOWN);
|
|
|
|
pinMode(4, INPUT_PULLDOWN);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
BootOptions::BootOptions()
|
|
|
|
{
|
|
|
|
#ifdef PLATFORM_PHOTON
|
|
|
|
isSetup = digitalRead(2) == HIGH;
|
|
|
|
isSerial = digitalRead(3) == HIGH || LAST_BOOT_WAS_SERIAL;
|
|
|
|
isFlash = digitalRead(4) == HIGH;
|
|
|
|
|
|
|
|
LAST_BOOT_WAS_FLASH = isFlash;
|
|
|
|
LAST_BOOT_WAS_SERIAL |= isSerial;
|
|
|
|
lastBootWasFlash = LAST_BOOT_WAS_FLASH;
|
|
|
|
|
|
|
|
configStatus.setActive(isSetup);
|
|
|
|
serialStatus.setActive(isSerial);
|
|
|
|
#endif
|
2023-02-19 17:40:16 +00:00
|
|
|
#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
|
2021-04-10 18:10:25 +00:00
|
|
|
#ifdef BOARD_ESP8266
|
|
|
|
struct rst_info resetInfo = *ESP.getResetInfoPtr();
|
2023-02-19 17:40:16 +00:00
|
|
|
resetReason = resetInfo.reason;
|
2023-12-11 06:54:08 +00:00
|
|
|
crashCount = s_rebootCount;
|
|
|
|
if (resetInfo.reason == REASON_SOFT_WDT_RST || resetInfo.reason == REASON_WDT_RST || resetInfo.reason == REASON_EXCEPTION_RST) {
|
2021-04-10 18:10:25 +00:00
|
|
|
if (crashCount++ >= 3) {
|
|
|
|
// Boot into safe mode if the watchdog reset us three times in a row.
|
|
|
|
isSafeMode = true;
|
|
|
|
}
|
2023-12-11 06:54:08 +00:00
|
|
|
} else {
|
2021-04-10 18:10:25 +00:00
|
|
|
crashCount = 0;
|
2023-12-11 06:54:08 +00:00
|
|
|
}
|
|
|
|
s_rebootCount = crashCount;
|
|
|
|
|
|
|
|
if (resetInfo.reason > 0 && s_forceSafeMode == SAFE_MODE_MAGIC) {
|
|
|
|
isSafeMode = true;
|
|
|
|
s_forceSafeMode = 0;
|
2021-04-10 18:10:25 +00:00
|
|
|
}
|
|
|
|
#endif
|
2021-03-29 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BootOptions::waitForRelease()
|
|
|
|
{
|
|
|
|
#ifdef PLATFORM_PHOTON
|
|
|
|
while(digitalRead(2) == HIGH || digitalRead(3) == HIGH) {};
|
|
|
|
#endif
|
|
|
|
}
|