Merge pull request #755 from cyberman54/development

Development
This commit is contained in:
Verkehrsrot 2021-03-12 23:36:10 +01:00 committed by GitHub
commit c3d9163463
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 123 additions and 110 deletions

View File

@ -4,6 +4,73 @@
// Local logging tag // Local logging tag
static const char TAG[] = __FILE__; static const char TAG[] = __FILE__;
static hw_timer_t *wdTimer = NULL;
static WebServer server(80);
static TaskHandle_t RestartHandle;
static const char *loginMenu =
"<form name='loginForm'>"
"<table width='20%' bgcolor='A09F9F' align='center'>"
"<tr>"
"<td colspan=2>"
"<center><font size=4><b>Maintenance Menu</b></font></center>"
"<br>"
"</td>"
"<br>"
"<br>"
"</tr>"
"<tr>"
"<td><input type='submit' onclick='start(this.form)' "
"value='Start'></td>"
"</tr>"
"</table>"
"</form>"
"<script>"
"function start(form) {window.open('/serverIndex')}"
"</script>";
static const char *serverIndex =
"<script "
"src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/"
"jquery.min.js'></script>"
"<form method='POST' action='#' enctype='multipart/form-data' "
"id='upload_form'>"
"<input type='file' name='update'>"
"<input type='submit' value='Update'>"
"</form>"
"<div id='prg'>progress: 0%</div>"
"<script>"
"$('form').submit(function(e){"
"e.preventDefault();"
"var form = $('#upload_form')[0];"
"var data = new FormData(form);"
" $.ajax({"
"url: '/update',"
"type: 'POST',"
"data: data,"
"contentType: false,"
"processData:false,"
"xhr: function() {"
"var xhr = new window.XMLHttpRequest();"
"xhr.upload.addEventListener('progress', function(evt) {"
"if (evt.lengthComputable) {"
"var per = evt.loaded / evt.total;"
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
"}"
"}, false);"
"return xhr;"
"},"
"success:function(d, s) {"
"console.log('success!')"
"},"
"error: function (a, b, c) {"
"}"
"});"
"});"
"</script>";
void IRAM_ATTR watchdog() { xTaskResumeFromISR(RestartHandle); }
// start local web server with user interface for maintenance mode // start local web server with user interface for maintenance mode
// used for manually uploading a firmware file via wifi // used for manually uploading a firmware file via wifi
@ -21,76 +88,24 @@ void start_boot_menu(void) {
const char *ssid = WIFI_SSID; const char *ssid = WIFI_SSID;
const char *password = WIFI_PASS; const char *password = WIFI_PASS;
RTC_runmode = RUNMODE_NORMAL; // if watchdog fires, boot to production // set runmode normal makes watchdog booting to production if triggered
RTC_runmode = RUNMODE_NORMAL;
hw_timer_t *timer = NULL; // setup restart handle task for resetting ESP32, which is callable from ISR
timer = timerBegin(2, 80, true); // timer 2, div 80, countup // (because esp_restart() from ISR would trigger the ESP32 task watchdog)
timerAttachInterrupt(timer, &esp_restart, true); // apply watchdog reset xTaskCreate(
timerAlarmWrite(timer, BOOTDELAY * 1000000, false); // watchdog time in us [](void *p) {
timerAlarmEnable(timer); // enable watchdog vTaskSuspend(NULL);
ESP.restart();
},
"Restart", configMINIMAL_STACK_SIZE, NULL, (3 | portPRIVILEGE_BIT),
&RestartHandle);
WebServer server(80); // setup watchdog, based on esp32 timer2 interrupt
wdTimer = timerBegin(0, 80, true); // timer 0, div 80, countup
const char *loginMenu = timerAttachInterrupt(wdTimer, &watchdog, true); // callback for device reset
"<form name='loginForm'>" timerAlarmWrite(wdTimer, BOOTDELAY * 1000000, false); // set time in us
"<table width='20%' bgcolor='A09F9F' align='center'>" timerAlarmEnable(wdTimer); // enable watchdog
"<tr>"
"<td colspan=2>"
"<center><font size=4><b>Maintenance Menu</b></font></center>"
"<br>"
"</td>"
"<br>"
"<br>"
"</tr>"
"<tr>"
"<td><input type='submit' onclick='start(this.form)' "
"value='Start'></td>"
"</tr>"
"</table>"
"</form>"
"<script>"
"function start(form) {window.open('/serverIndex')}"
"</script>";
const char *serverIndex =
"<script "
"src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/"
"jquery.min.js'></script>"
"<form method='POST' action='#' enctype='multipart/form-data' "
"id='upload_form'>"
"<input type='file' name='update'>"
"<input type='submit' value='Update'>"
"</form>"
"<div id='prg'>progress: 0%</div>"
"<script>"
"$('form').submit(function(e){"
"e.preventDefault();"
"var form = $('#upload_form')[0];"
"var data = new FormData(form);"
" $.ajax({"
"url: '/update',"
"type: 'POST',"
"data: data,"
"contentType: false,"
"processData:false,"
"xhr: function() {"
"var xhr = new window.XMLHttpRequest();"
"xhr.upload.addEventListener('progress', function(evt) {"
"if (evt.lengthComputable) {"
"var per = evt.loaded / evt.total;"
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
"}"
"}, false);"
"return xhr;"
"},"
"success:function(d, s) {"
"console.log('success!')"
"},"
"error: function (a, b, c) {"
"}"
"});"
"});"
"</script>";
WiFi.disconnect(true); WiFi.disconnect(true);
WiFi.config(INADDR_NONE, INADDR_NONE, WiFi.config(INADDR_NONE, INADDR_NONE,
@ -111,75 +126,74 @@ 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();
switch (upload.status) { // did we get a file name?
if (upload.filename != NULL) {
case UPLOAD_FILE_START: switch (upload.status) {
// start file transfer
ESP_LOGI(TAG, "Uploading %s", upload.filename.c_str());
success = Update.begin();
break;
case UPLOAD_FILE_WRITE: case UPLOAD_FILE_START:
// flashing firmware to ESP // start file transfer
success = (Update.write(upload.buf, upload.currentSize) == ESP_LOGI(TAG, "Uploading %s", upload.filename.c_str());
upload.currentSize); success = Update.begin();
break; break;
case UPLOAD_FILE_END: case UPLOAD_FILE_WRITE:
success = Update.end(true); // true to set the size to the current // flashing firmware to ESP
if (success) success = (Update.write(upload.buf, upload.currentSize) ==
ESP_LOGI(TAG, "Upload finished, %u bytes written", upload.currentSize);
upload.totalSize); break;
else
ESP_LOGE(TAG, "Upload failed, status=%d", upload.status);
break;
case UPLOAD_FILE_ABORTED: case UPLOAD_FILE_END:
default: success = Update.end(true); // true to set the size to the current
break; if (success)
ESP_LOGI(TAG, "Upload finished, %u bytes written",
upload.totalSize);
else
ESP_LOGE(TAG, "Upload failed, status=%d", upload.status);
break;
} // switch case UPLOAD_FILE_ABORTED:
default:
break;
if (!success) { } // switch
ESP_LOGE(TAG, "Error: %s", Update.errorString());
WiFi.disconnect(true); // don't boot to production if update failed
esp_restart(); if (!success) {
ESP_LOGE(TAG, "Error: %s", Update.errorString());
RTC_runmode = RUNMODE_POWERCYCLE;
}
} }
}); });
@ -192,6 +206,6 @@ void start_boot_menu(void) {
while (1) { while (1) {
server.handleClient(); server.handleClient();
delay(1); delay(2);
} }
} }

View File

@ -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)