#include "../../../Platform.h" #include #include #include #include #define __NOINIT_ATTR __attribute__ ((section (".noinit"))) __NOINIT_ATTR static uint8_t s_rebootCount; __NOINIT_ATTR static uint16_t s_forceSafeMode; #define SAFE_MODE_MAGIC 6942 WiFiUDP wifiUdp; NTPClient timeClient(wifiUdp, "pool.ntp.org", 0); template<> const char* PlatformImpl::name() { return "ESP8266"; } template<> const char* PlatformImpl::version() { return ESP.getSdkVersion(); } template<> int PlatformImpl::freeRam() { return ESP.getFreeHeap(); } template<> void PlatformImpl::startWatchdog() { ESP.wdtEnable(0); } template<> bool PlatformImpl::getLocalTime(struct tm* timedata, int timezone) { timedata->tm_hour = (timeClient.getHours() + timezone) % 23; timedata->tm_min = timeClient.getMinutes(); return true; } template<> void PlatformImpl::startNTP() { timeClient.begin(); } template<> void PlatformImpl::loop() { if (WiFi.status() == WL_CONNECTED) { timeClient.update(); } ESP.wdtFeed(); } template<> const char* PlatformImpl::deviceID() { static char s_deviceID[15]; static uint16_t chipid = ESP.getChipId(); snprintf(s_deviceID, sizeof(s_deviceID), "%08X", (uint32_t)chipid); return s_deviceID; } template<> void PlatformImpl::restart() { ESP.wdtDisable(); ESP.restart(); } template<> void PlatformImpl::bootSplash() { Log.trace("ESP8266 startup reason: %d", Platform::bootopts.resetReason); } template<> void PlatformImpl::printCrashInfo() { auto rInfo = ESP.getResetInfoPtr(); if (Platform::bootopts.resetReason == REASON_EXCEPTION_RST) { Log.error("Fatal exception (%d):", rInfo->exccause); } Log.error("epc1=%X, epc2=%X, epc3=%X, excvaddr=%X, depc=%X", rInfo->epc1, rInfo->epc2, rInfo->epc3, rInfo->excvaddr, rInfo->depc); } template<> void PlatformImpl::initBootOptions(BootOptions& opts) { struct rst_info resetInfo = *ESP.getResetInfoPtr(); opts.resetReason = resetInfo.reason; opts.crashCount = s_rebootCount; if (resetInfo.reason == REASON_SOFT_WDT_RST || resetInfo.reason == REASON_WDT_RST || resetInfo.reason == REASON_EXCEPTION_RST) { if (opts.crashCount++ >= 3) { // Boot into safe mode if the watchdog reset us three times in a row. opts.isSafeMode = true; } } else { opts.crashCount = 0; } s_rebootCount = opts.crashCount; if (resetInfo.reason > 0 && s_forceSafeMode == SAFE_MODE_MAGIC) { opts.isSafeMode = true; s_forceSafeMode = 0; } } template<> void PlatformImpl::forceSafeMode() { s_forceSafeMode = SAFE_MODE_MAGIC; }