From 126471e2e008ffc469a571bbcf95acba09cbec80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sun, 1 Jul 2018 08:52:13 +0200 Subject: [PATCH] Serial debug working out of configuration --- code/espurna/config/general.h | 6 ++-- code/espurna/debug.ino | 66 ++++++++++++++++++++++++++++++----- code/espurna/espurna.ino | 19 +++++----- code/espurna/hardware.ino | 22 +++++++++++- code/espurna/ota.ino | 12 ++----- code/espurna/settings.ino | 9 ++--- code/espurna/system.ino | 22 +----------- code/espurna/web.ino | 12 ++----- 8 files changed, 103 insertions(+), 65 deletions(-) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index c49cd2b5..91667615 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -34,11 +34,11 @@ #endif #ifndef DEBUG_PORT -#define DEBUG_PORT Serial // Default debugging port +#define DEBUG_PORT Serial // Enable serial debug log #endif -#ifndef SERIAL_BAUDRATE -#define SERIAL_BAUDRATE 115200 // Default baudrate +#ifndef DEBUG_SERIAL_SPEED +#define DEBUG_SERIAL_SPEED 115200 // Default baudrate #endif #ifndef DEBUG_ADD_TIMESTAMP diff --git a/code/espurna/debug.ino b/code/espurna/debug.ino index df40b408..923a28ad 100644 --- a/code/espurna/debug.ino +++ b/code/espurna/debug.ino @@ -26,6 +26,11 @@ char _udp_syslog_header[40] = {0}; #endif #endif +#if DEBUG_SERIAL_SUPPORT +HardwareSerial & _dbg_port = DEBUG_PORT; +bool _dbg_serial_enabled = true; +#endif + void _debugSend(char * message) { bool pause = false; @@ -38,10 +43,12 @@ void _debugSend(char * message) { #endif #if DEBUG_SERIAL_SUPPORT - #if DEBUG_ADD_TIMESTAMP - DEBUG_PORT.printf(timestamp); - #endif - DEBUG_PORT.printf(message); + if (_dbg_serial_enabled) { + #if DEBUG_ADD_TIMESTAMP + _dbg_port.printf(timestamp); + #endif + _dbg_port.printf(message); + } #endif #if DEBUG_UDP_SUPPORT @@ -161,13 +168,56 @@ bool _debugKeyCheck(const char * key) { return (strncmp(key, "dbg", 3) == 0); } +int debugSerialAvailable() { + #if DEBUG_SERIAL_SUPPORT + if (_dbg_serial_enabled) { + return _dbg_port.available(); + } + #endif + return 0; +} + +int debugSerialRead() { + #if DEBUG_SERIAL_SUPPORT + if (_dbg_serial_enabled) { + return _dbg_port.read(); + } + #endif + return 0; +} + +void debugSerialWrite(uint8_t ch) { + #if DEBUG_SERIAL_SUPPORT + if (_dbg_serial_enabled) { + _dbg_port.write(ch); + } + #endif +} + void debugSetup() { #if DEBUG_SERIAL_SUPPORT - DEBUG_PORT.begin(SERIAL_BAUDRATE); - #if DEBUG_ESP_WIFI - DEBUG_PORT.setDebugOutput(true); - #endif + + _dbg_serial_enabled = getSetting("dbgSerial", 1).toInt() == 1; + + if (_dbg_serial_enabled) { + + unsigned char port = getSetting("dbgPort", 0).toInt(); + if (0 == port) { + _dbg_port = Serial; + } else { + _dbg_port = Serial1; + } + + unsigned long speed = getSetting("dbgSpeed", DEBUG_SERIAL_SPEED).toInt(); + _dbg_port.begin(speed); + + #if DEBUG_ESP_WIFI + _dbg_port.setDebugOutput(true); + #endif + + } + #endif settingsRegisterKeyCheck(_debugKeyCheck); diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index dba72f83..feda62d0 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -44,23 +44,26 @@ void setup() { // Basic modules, will always run // ------------------------------------------------------------------------- - // Serial debug - #if DEBUG_SUPPORT - debugSetup(); - #endif - // Init EEPROM eepromSetup(); - // Init Serial, SPIFFS and system check - systemSetup(); - // Init persistance and terminal features settingsSetup(); + // Serial debug + // Requires SETTINGS + #if DEBUG_SUPPORT + debugSetup(); + #endif + // Load board data + // Requires SETTINGS hardwareSetup(); + // Init Serial, SPIFFS and system check + // Requires EEPROM and DEBUG + systemSetup(); + // Show welcome message and system configuration info(); diff --git a/code/espurna/hardware.ino b/code/espurna/hardware.ino index c17056e3..ff60ec15 100644 --- a/code/espurna/hardware.ino +++ b/code/espurna/hardware.ino @@ -133,9 +133,12 @@ void _hardwareLoad() { setSetting("btnMode", 0, BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH); setSetting("btnRelay", 0, 0); - setSetting("dsGPIO", 0, 14); + setSetting("dhtEnabled", 1); setSetting("dhtGPIO", 0, 14); + setSetting("dsEnabled", 1); + setSetting("dsGPIO", 0, 14); + setSetting("ledGPIO", 0, 13); setSetting("ledLogic", 0, GPIO_LOGIC_INVERSE); @@ -856,6 +859,11 @@ void _hardwareLoad() { setSetting("btnRelay", 0, 0); setSetting("btnRelay", 1, 1); + // The ESPLive has an ADC MUX which needs to be enabled + setSetting("ledGPIO", 0, 16); + setSetting("ledLogic", 0, GPIO_LOGIC_DIRECT); + setSetting("ledMode", LED_MODE_ON); + setSetting("rlyGPIO", 0, 12); setSetting("rlyGPIO", 1, 13); setSetting("rlyType", 0, RELAY_TYPE_NORMAL); @@ -1814,9 +1822,21 @@ void _hardwareLoad() { } +void _hardwareSpecific() { + + // These devices use the hardware UART + // to communicate to secondary microcontrollers + #if defined(ITEAD_SONOFF_RFBRIDGE) || defined(ITEAD_SONOFF_DUAL) || defined(STM_RELAY) + Serial.begin(DEBUG_SERIAL_SPEED); + #endif + +} + + void hardwareSetup() { _hardwareMigrate(); if (getSetting("board", 1).toInt() != 1) { _hardwareLoad(); } + _hardwareSpecific(); } diff --git a/code/espurna/ota.ino b/code/espurna/ota.ino index 43f9fa35..ecfe6090 100644 --- a/code/espurna/ota.ino +++ b/code/espurna/ota.ino @@ -69,9 +69,7 @@ void _otaFrom(const char * host, unsigned int port, const char * url) { DEBUG_MSG_P(PSTR("[OTA] Success: %u bytes\n"), _ota_size); deferredReset(100, CUSTOM_RESET_OTA); } else { - #ifdef DEBUG_PORT - Update.printError(DEBUG_PORT); - #endif + DEBUG_MSG_P(PSTR("[OTA] Error #%u\n"), Update.getError()); eepromRotate(true); } @@ -99,9 +97,7 @@ void _otaFrom(const char * host, unsigned int port, const char * url) { Update.runAsync(true); if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)) { - #ifdef DEBUG_PORT - Update.printError(DEBUG_PORT); - #endif + DEBUG_MSG_P(PSTR("[OTA] Error #%u\n"), Update.getError()); } p = strstr((char *)data, "\r\n\r\n") + 4; @@ -111,9 +107,7 @@ void _otaFrom(const char * host, unsigned int port, const char * url) { if (!Update.hasError()) { if (Update.write((uint8_t *) p, len) != len) { - #ifdef DEBUG_PORT - Update.printError(DEBUG_PORT); - #endif + DEBUG_MSG_P(PSTR("[OTA] Error #%u\n"), Update.getError()); } } diff --git a/code/espurna/settings.ino b/code/espurna/settings.ino index aa6ac5fc..075a2cc3 100644 --- a/code/espurna/settings.ino +++ b/code/espurna/settings.ino @@ -530,14 +530,12 @@ void settingsRegisterKeyCheck(setting_key_check_callback_f callback) { void settingsSetup() { - EEPROMr.begin(SPI_FLASH_SEC_SIZE); - _serial.callback([](uint8_t ch) { #if TELNET_SUPPORT telnetWrite(ch); #endif #if DEBUG_SERIAL_SUPPORT - DEBUG_PORT.write(ch); + debugSerialWrite(ch); #endif }); @@ -575,12 +573,11 @@ void settingsLoop() { _settings_save = false; } - #if TERMINAL_SUPPORT #if DEBUG_SERIAL_SUPPORT - while (DEBUG_PORT.available()) { - _serial.inject(DEBUG_PORT.read()); + while (debugSerialAvailable()) { + _serial.inject(debugSerialRead()); } #endif diff --git a/code/espurna/system.ino b/code/espurna/system.ino index a71ae1ec..665a5bd5 100644 --- a/code/espurna/system.ino +++ b/code/espurna/system.ino @@ -60,7 +60,7 @@ void systemCheckLoop() { } } -#endif +#endif //SYSTEM_CHECK_ENABLED // ----------------------------------------------------------------------------- @@ -72,7 +72,6 @@ unsigned long systemLoopDelay() { return _loop_delay; } - unsigned long systemLoadAverage() { return _load_average; } @@ -132,22 +131,6 @@ void systemLoop() { } -void _systemSetupSpecificHardware() { - - //The ESPLive has an ADC MUX which needs to be configured. - #if defined(MANCAVEMADE_ESPLIVE) - pinMode(16, OUTPUT); - digitalWrite(16, HIGH); //Defualt CT input (pin B, solder jumper B) - #endif - - // These devices use the hardware UART - // to communicate to secondary microcontrollers - #if defined(ITEAD_SONOFF_RFBRIDGE) || defined(ITEAD_SONOFF_DUAL) || defined(STM_RELAY) - Serial.begin(SERIAL_BAUDRATE); - #endif - -} - void systemSetup() { #if SPIFFS_SUPPORT @@ -159,9 +142,6 @@ void systemSetup() { systemCheck(false); #endif - // Init device-specific hardware - _systemSetupSpecificHardware(); - // Cache loop delay value to speed things (recommended max 250ms) _loop_delay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str()); _loop_delay = constrain(_loop_delay, 0, 300); diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 011360b2..9582818c 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -300,18 +300,14 @@ void _onUpgradeData(AsyncWebServerRequest *request, String filename, size_t inde DEBUG_MSG_P(PSTR("[UPGRADE] Start: %s\n"), filename.c_str()); Update.runAsync(true); if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)) { - #ifdef DEBUG_PORT - Update.printError(DEBUG_PORT); - #endif + DEBUG_MSG_P(PSTR("[UPGRADE] Error #%u\n"), Update.getError()); } } if (!Update.hasError()) { if (Update.write(data, len) != len) { - #ifdef DEBUG_PORT - Update.printError(DEBUG_PORT); - #endif + DEBUG_MSG_P(PSTR("[UPGRADE] Error #%u\n"), Update.getError()); } } @@ -319,9 +315,7 @@ void _onUpgradeData(AsyncWebServerRequest *request, String filename, size_t inde if (Update.end(true)){ DEBUG_MSG_P(PSTR("[UPGRADE] Success: %u bytes\n"), index + len); } else { - #ifdef DEBUG_PORT - Update.printError(DEBUG_PORT); - #endif + DEBUG_MSG_P(PSTR("[UPGRADE] Error #%u\n"), Update.getError()); } } else { DEBUG_MSG_P(PSTR("[UPGRADE] Progress: %u bytes\r"), index + len);