commit
c3d9163463
100
src/boot.cpp
100
src/boot.cpp
@ -4,34 +4,11 @@
|
|||||||
// Local logging tag
|
// Local logging tag
|
||||||
static const char TAG[] = __FILE__;
|
static const char TAG[] = __FILE__;
|
||||||
|
|
||||||
// start local web server with user interface for maintenance mode
|
static hw_timer_t *wdTimer = NULL;
|
||||||
// used for manually uploading a firmware file via wifi
|
static WebServer server(80);
|
||||||
|
static TaskHandle_t RestartHandle;
|
||||||
|
|
||||||
void start_boot_menu(void) {
|
static const char *loginMenu =
|
||||||
|
|
||||||
uint8_t mac[6];
|
|
||||||
char clientId[20];
|
|
||||||
|
|
||||||
// hash 6 byte MAC to 4 byte hash
|
|
||||||
esp_eth_get_mac(mac);
|
|
||||||
const uint32_t hashedmac = myhash((const char *)mac, 6);
|
|
||||||
snprintf(clientId, 20, "paxcounter_%08x", hashedmac);
|
|
||||||
|
|
||||||
const char *host = clientId;
|
|
||||||
const char *ssid = WIFI_SSID;
|
|
||||||
const char *password = WIFI_PASS;
|
|
||||||
|
|
||||||
RTC_runmode = RUNMODE_NORMAL; // if watchdog fires, boot to production
|
|
||||||
|
|
||||||
hw_timer_t *timer = NULL;
|
|
||||||
timer = timerBegin(2, 80, true); // timer 2, div 80, countup
|
|
||||||
timerAttachInterrupt(timer, &esp_restart, true); // apply watchdog reset
|
|
||||||
timerAlarmWrite(timer, BOOTDELAY * 1000000, false); // watchdog time in us
|
|
||||||
timerAlarmEnable(timer); // enable watchdog
|
|
||||||
|
|
||||||
WebServer server(80);
|
|
||||||
|
|
||||||
const char *loginMenu =
|
|
||||||
"<form name='loginForm'>"
|
"<form name='loginForm'>"
|
||||||
"<table width='20%' bgcolor='A09F9F' align='center'>"
|
"<table width='20%' bgcolor='A09F9F' align='center'>"
|
||||||
"<tr>"
|
"<tr>"
|
||||||
@ -52,7 +29,7 @@ void start_boot_menu(void) {
|
|||||||
"function start(form) {window.open('/serverIndex')}"
|
"function start(form) {window.open('/serverIndex')}"
|
||||||
"</script>";
|
"</script>";
|
||||||
|
|
||||||
const char *serverIndex =
|
static const char *serverIndex =
|
||||||
"<script "
|
"<script "
|
||||||
"src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/"
|
"src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/"
|
||||||
"jquery.min.js'></script>"
|
"jquery.min.js'></script>"
|
||||||
@ -92,6 +69,44 @@ void start_boot_menu(void) {
|
|||||||
"});"
|
"});"
|
||||||
"</script>";
|
"</script>";
|
||||||
|
|
||||||
|
void IRAM_ATTR watchdog() { xTaskResumeFromISR(RestartHandle); }
|
||||||
|
|
||||||
|
// start local web server with user interface for maintenance mode
|
||||||
|
// used for manually uploading a firmware file via wifi
|
||||||
|
|
||||||
|
void start_boot_menu(void) {
|
||||||
|
|
||||||
|
uint8_t mac[6];
|
||||||
|
char clientId[20];
|
||||||
|
|
||||||
|
// hash 6 byte MAC to 4 byte hash
|
||||||
|
esp_eth_get_mac(mac);
|
||||||
|
const uint32_t hashedmac = myhash((const char *)mac, 6);
|
||||||
|
snprintf(clientId, 20, "paxcounter_%08x", hashedmac);
|
||||||
|
|
||||||
|
const char *host = clientId;
|
||||||
|
const char *ssid = WIFI_SSID;
|
||||||
|
const char *password = WIFI_PASS;
|
||||||
|
|
||||||
|
// set runmode normal makes watchdog booting to production if triggered
|
||||||
|
RTC_runmode = RUNMODE_NORMAL;
|
||||||
|
|
||||||
|
// setup restart handle task for resetting ESP32, which is callable from ISR
|
||||||
|
// (because esp_restart() from ISR would trigger the ESP32 task watchdog)
|
||||||
|
xTaskCreate(
|
||||||
|
[](void *p) {
|
||||||
|
vTaskSuspend(NULL);
|
||||||
|
ESP.restart();
|
||||||
|
},
|
||||||
|
"Restart", configMINIMAL_STACK_SIZE, NULL, (3 | portPRIVILEGE_BIT),
|
||||||
|
&RestartHandle);
|
||||||
|
|
||||||
|
// setup watchdog, based on esp32 timer2 interrupt
|
||||||
|
wdTimer = timerBegin(0, 80, true); // timer 0, div 80, countup
|
||||||
|
timerAttachInterrupt(wdTimer, &watchdog, true); // callback for device reset
|
||||||
|
timerAlarmWrite(wdTimer, BOOTDELAY * 1000000, false); // set time in us
|
||||||
|
timerAlarmEnable(wdTimer); // enable watchdog
|
||||||
|
|
||||||
WiFi.disconnect(true);
|
WiFi.disconnect(true);
|
||||||
WiFi.config(INADDR_NONE, INADDR_NONE,
|
WiFi.config(INADDR_NONE, INADDR_NONE,
|
||||||
INADDR_NONE); // call is only a workaround for bug in WiFi class
|
INADDR_NONE); // call is only a workaround for bug in WiFi class
|
||||||
@ -111,42 +126,40 @@ void start_boot_menu(void) {
|
|||||||
// 2nd try
|
// 2nd try
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
|
||||||
delay(500);
|
delay(500);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
MDNS.begin(host);
|
MDNS.begin(host);
|
||||||
|
timerWrite(wdTimer, 0); // reset timer (feed watchdog)
|
||||||
|
|
||||||
// send start button page
|
server.on("/", HTTP_GET, []() {
|
||||||
server.on("/", HTTP_GET, [&server, &loginMenu]() {
|
|
||||||
server.sendHeader("Connection", "keep-alive");
|
server.sendHeader("Connection", "keep-alive");
|
||||||
server.send(200, "text/html", loginMenu);
|
server.send(200, "text/html", loginMenu);
|
||||||
});
|
});
|
||||||
|
|
||||||
// send upload button page
|
server.on("/serverIndex", HTTP_GET, []() {
|
||||||
server.on("/serverIndex", HTTP_GET, [&server, &serverIndex, &timer]() {
|
timerAlarmWrite(wdTimer, BOOTTIMEOUT * 1000000, false); // set time in us
|
||||||
timerAlarmWrite(timer, BOOTTIMEOUT * 1000000, false);
|
|
||||||
server.sendHeader("Connection", "keep-alive");
|
server.sendHeader("Connection", "keep-alive");
|
||||||
server.send(200, "text/html", serverIndex);
|
server.send(200, "text/html", serverIndex);
|
||||||
});
|
});
|
||||||
|
|
||||||
// handling uploading firmware file
|
|
||||||
server.on(
|
server.on(
|
||||||
"/update", HTTP_POST,
|
"/update", HTTP_POST,
|
||||||
[&server]() {
|
[]() {
|
||||||
server.sendHeader("Connection", "close");
|
server.sendHeader("Connection", "close");
|
||||||
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
|
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
|
||||||
WiFi.disconnect(true);
|
WiFi.disconnect(true);
|
||||||
if (!Update.hasError())
|
|
||||||
RTC_runmode = RUNMODE_POWERCYCLE;
|
|
||||||
esp_restart();
|
esp_restart();
|
||||||
},
|
},
|
||||||
|
|
||||||
[&server, &timer]() {
|
// handling uploading firmware file
|
||||||
|
[]() {
|
||||||
bool success = false;
|
bool success = false;
|
||||||
HTTPUpload &upload = server.upload();
|
HTTPUpload &upload = server.upload();
|
||||||
|
|
||||||
|
// did we get a file name?
|
||||||
|
if (upload.filename != NULL) {
|
||||||
|
|
||||||
switch (upload.status) {
|
switch (upload.status) {
|
||||||
|
|
||||||
case UPLOAD_FILE_START:
|
case UPLOAD_FILE_START:
|
||||||
@ -176,10 +189,11 @@ void start_boot_menu(void) {
|
|||||||
|
|
||||||
} // switch
|
} // switch
|
||||||
|
|
||||||
|
// don't boot to production if update failed
|
||||||
if (!success) {
|
if (!success) {
|
||||||
ESP_LOGE(TAG, "Error: %s", Update.errorString());
|
ESP_LOGE(TAG, "Error: %s", Update.errorString());
|
||||||
WiFi.disconnect(true);
|
RTC_runmode = RUNMODE_POWERCYCLE;
|
||||||
esp_restart();
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -192,6 +206,6 @@ void start_boot_menu(void) {
|
|||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
server.handleClient();
|
server.handleClient();
|
||||||
delay(1);
|
delay(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -51,7 +51,6 @@ So don't do it if you do not own a digital oscilloscope.
|
|||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
0 displayIRQ -> display refresh -> 40ms (DISPLAYREFRESH_MS)
|
0 displayIRQ -> display refresh -> 40ms (DISPLAYREFRESH_MS)
|
||||||
1 ppsIRQ -> pps clock irq -> 1sec
|
1 ppsIRQ -> pps clock irq -> 1sec
|
||||||
2 watchdog -> used in boot.cpp
|
|
||||||
3 MatrixDisplayIRQ -> matrix mux cycle -> 0,5ms (MATRIX_DISPLAY_SCAN_US)
|
3 MatrixDisplayIRQ -> matrix mux cycle -> 0,5ms (MATRIX_DISPLAY_SCAN_US)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user