From e7957af62738c1786b8f13ab82f60ddf26471775 Mon Sep 17 00:00:00 2001 From: Maik Hofmann Date: Mon, 1 Mar 2021 18:28:33 +0100 Subject: [PATCH] Bugfixing, move ISR code to IRAM, store crashdump --- HCPBridgeISR/platformio.ini | 1 + HCPBridgeISR/src/crc.c | 2 +- HCPBridgeISR/src/hciemulator.c | 35 ++++++++++---------------- HCPBridgeISR/src/hciemulator.h | 5 +++- HCPBridgeISR/src/main.cpp | 46 +++++++++++++++++++++------------- 5 files changed, 47 insertions(+), 42 deletions(-) diff --git a/HCPBridgeISR/platformio.ini b/HCPBridgeISR/platformio.ini index 2dd88b2..887c077 100644 --- a/HCPBridgeISR/platformio.ini +++ b/HCPBridgeISR/platformio.ini @@ -16,3 +16,4 @@ lib_deps = ottowinter/ESPAsyncWebServer-esphome@^1.2.7 bblanchon/ArduinoJson@^6.17.2 ayushsharma82/AsyncElegantOTA@^2.2.5 + krzychb/EspSaveCrash@^1.2.0 diff --git a/HCPBridgeISR/src/crc.c b/HCPBridgeISR/src/crc.c index 0a2a204..fe7df63 100644 --- a/HCPBridgeISR/src/crc.c +++ b/HCPBridgeISR/src/crc.c @@ -20,7 +20,7 @@ * #define readUInt16(arr, index) word(arr[index], arr[index + 1]) * #define readCRC(arr, length) word(arr[(length - MODBUS_CRC_LENGTH) + 1], arr[length - MODBUS_CRC_LENGTH]) */ -uint16_t calculateCRC(uint8_t *buffer, int length) +uint16_t ICACHE_RAM_ATTR calculateCRC(uint8_t *buffer, int length) { int i, j; uint16_t crc = 0xFFFF; diff --git a/HCPBridgeISR/src/hciemulator.c b/HCPBridgeISR/src/hciemulator.c index 68a4f75..943d8e3 100644 --- a/HCPBridgeISR/src/hciemulator.c +++ b/HCPBridgeISR/src/hciemulator.c @@ -1,6 +1,7 @@ /* - * modified/reduced to provide HCP Modbus low level functions - * + * ISR Code to emulate the modbus rtu protocol for hoermann HCP2 + * + * code based on https://github.com/juhovh/esp-uart * The MIT License (MIT) * * Copyright (c) 2016 Juho Vähä-Herttua (juhovh@iki.fi) @@ -136,7 +137,7 @@ SHCIState* getHCIState(){ const unsigned char ResponseTemplate_Fcn17_Cmd03_L08 []= {0x02,0x17,0x10,0x3E,0x00,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x74,0x1B}; const unsigned char ResponseTemplate_Fcn17_Cmd04_L02 []= {0x02,0x17,0x04,0x0F,0x00,0x04,0xFD,0x0A,0x72}; -static void processDeviceStatusFrame(){ +static void ICACHE_RAM_ATTR processDeviceStatusFrame(){ if(rxlen==0x11){ unsigned char counter = rxbuffer[11]; unsigned char cmd = rxbuffer[12]; @@ -267,7 +268,7 @@ static void processDeviceStatusFrame(){ } const unsigned char ResponseTemplate_Fcn17_Cmd02_L05 []= {0x02,0x17,0x0a,0x00,0x00,0x02,0x05,0x04,0x30,0x10,0xff,0xa8,0x45,0x0e,0xdf}; -static void processDeviceBusScanFrame(){ +static void ICACHE_RAM_ATTR processDeviceBusScanFrame(){ // 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 //0013: 02 17 9C B9 00 05 9C 41 00 03 06 00 02 00 00 01 02 f8 35 //res=> 02 17 0a 00 00 02 05 04 30 10 ff a8 45 0e df @@ -282,7 +283,7 @@ static void processDeviceBusScanFrame(){ //Log(LL_INFO,"Busscan received"); } -static void processBroadcastStatusFrame(){ +static void ICACHE_RAM_ATTR processBroadcastStatusFrame(){ //001B: 00 10 9D 31 00 09 12 64 00 00 00 40 60 00 00 00 00 00 00 00 00 00 01 00 00 CA 22 bool hasStateChanged = false; CHECKCHANGEDSET(state.lampOn,rxbuffer[20] == 0x14,hasStateChanged); @@ -293,8 +294,7 @@ static void processBroadcastStatusFrame(){ CHECKCHANGEDSET(state.valid, true,hasStateChanged); } -static bool processFrame(){ - +static bool ICACHE_RAM_ATTR processFrame(){ switch (rxlen) { case 0x1b: @@ -355,8 +355,7 @@ static bool processFrame(){ } - -static void processMessage (){ +static void ICACHE_RAM_ATTR processMessage (){ // check frame, process frame if(statemachine!= WAITING && lastStateTime+2000 +#include "EspSaveCrash.h" #include #include #include @@ -29,6 +30,9 @@ #define ESP8266_GPIO5 5 // Optocoupler input. #define LED_PIN ESP8266_GPIO2 +EspSaveCrash SaveCrash; +// the buffer to put the Crash log to +char *_debugOutputBuffer; // webserver on port 80 AsyncWebServer server(80); @@ -70,6 +74,8 @@ void setup(){ // Hörmann HCP2 based on modbus rtu @57.6kB 8E1 uart0_open(57600,UART_FLAGS_8E1); + _debugOutputBuffer = (char *) calloc(2048, sizeof(char)); + //setup wifi WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); @@ -89,7 +95,7 @@ void setup(){ SHCIState *doorstate = getHCIState(); AsyncResponseStream *response = request->beginResponseStream("application/json"); DynamicJsonDocument root(1024); - root["valid"] = doorstate->valid; + root["valid"] = doorstate->valid && (getMessageAge()/1000)<3000; root["doorstate"] = doorstate->doorState; root["doorposition"] = doorstate->doorCurrentPosition; root["doortarget"] = doorstate->doorTargetPosition; @@ -129,29 +135,33 @@ void setup(){ request->send(200, "text/plain", "OK"); }); - server.on("/sysinfo", HTTP_GET, [] (AsyncWebServerRequest *request) { - String freemem; - String ResetReason; - freemem = ESP.getFreeHeap(); + server.on("/crashinfo", HTTP_GET, [] (AsyncWebServerRequest *request){ + strcpy(_debugOutputBuffer, ""); + SaveCrash.print(_debugOutputBuffer,2048); + if (request->hasParam("clear")) { + SaveCrash.clear(); + } + request->send(200, "text/plain", _debugOutputBuffer); + }); + + server.on("/sysinfo", HTTP_GET, [] (AsyncWebServerRequest *request) { + char buffer[150]; rst_info* rinfo = ESP.getResetInfoPtr(); - //JSONencoder["uptimed"] = Day2; - //JSONencoder["uptimeh"] = Hour2; - //JSONencoder["uptimem"] = Minute2; - //JSONencoder["uptimes"] = Second2; - AsyncResponseStream *response = request->beginResponseStream("application/json"); DynamicJsonDocument root(1024); - root["freemem"] = freemem; - ResetReason += String(rinfo->reason); - ResetReason += String(" - "); - ResetReason += String(ESP.getResetReason().c_str()); - root["hostname"] = String(WiFi.hostname()); - root["dsdd"] = WiFi.localIP().toString(); + root["freemem"] = ESP.getFreeHeap(); + root["hostname"] = WiFi.hostname(); + root["ip"] = WiFi.localIP().toString(); root["ssid"] = String(ssid); - root["wifistatus"] = String(WiFi.status()); - root["resetreason"] =ResetReason; + root["wifistatus"] = WiFi.status(); + root["resetreason"] =ESP.getResetReason(); root["errors"] = rinfo->exccause; + + //The address of the last crash is printed, which is used to + sprintf(buffer, "epc1=0x%08x, epc2=0x%08x, epc3=0x%08x, excvaddr=0x%08x, depc=0x%08x, exccause=0x%x, reason=0x%x", + rinfo->epc1, rinfo->epc2, rinfo->epc3, rinfo->excvaddr, rinfo->depc, rinfo->exccause, rinfo->reason); + root["rstinfo"] = buffer; serializeJson(root,*response); request->send(response);