build: clean up the mess of ifdefs from platform into a scons-configured hal
This commit is contained in:
134
src/platform/arduino/esp8266/Platform.cpp
Normal file
134
src/platform/arduino/esp8266/Platform.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
#include "../../../Platform.h"
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <NTPClient.h>
|
||||
#include <ctime>
|
||||
|
||||
#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", 3600 * -7);
|
||||
|
||||
template<>
|
||||
const char*
|
||||
PlatformImpl<HAL_ESP8266>::name()
|
||||
{
|
||||
return "ESP8266";
|
||||
}
|
||||
|
||||
template<>
|
||||
const char*
|
||||
PlatformImpl<HAL_ESP8266>::version()
|
||||
{
|
||||
return ESP.getSdkVersion();
|
||||
}
|
||||
|
||||
template<>
|
||||
int
|
||||
PlatformImpl<HAL_ESP8266>::freeRam()
|
||||
{
|
||||
return ESP.getFreeHeap();
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP8266>::startWatchdog()
|
||||
{
|
||||
ESP.wdtEnable(0);
|
||||
}
|
||||
|
||||
template<>
|
||||
bool
|
||||
PlatformImpl<HAL_ESP8266>::getLocalTime(struct tm* timedata, int timezone)
|
||||
{
|
||||
timedata->tm_hour = (timeClient.getHours() + timezone) % 23;
|
||||
timedata->tm_min = timeClient.getMinutes();
|
||||
return true;
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP8266>::startNTP()
|
||||
{
|
||||
timeClient.begin();
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP8266>::loop()
|
||||
{
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
timeClient.update();
|
||||
}
|
||||
ESP.wdtFeed();
|
||||
}
|
||||
|
||||
template<>
|
||||
const char*
|
||||
PlatformImpl<HAL_ESP8266>::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<HAL_ESP8266>::restart()
|
||||
{
|
||||
ESP.wdtDisable();
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP8266>::bootSplash()
|
||||
{
|
||||
Log.notice("ESP8266!");
|
||||
}
|
||||
|
||||
template<>
|
||||
void
|
||||
PlatformImpl<HAL_ESP8266>::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<HAL_ESP8266>::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<HAL_ESP8266>::forceSafeMode()
|
||||
{
|
||||
s_forceSafeMode = SAFE_MODE_MAGIC;
|
||||
}
|
||||
Reference in New Issue
Block a user