From cf0c818138210fb6f3c9fe16512b340e24f864f0 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Fri, 1 Oct 2021 09:09:30 +0200 Subject: [PATCH] Cleanup ESP specific details --- CommandStation-EX.ino | 17 +++++++++++++---- DCCEX.h | 2 ++ DCCEXParser.cpp | 5 ++++- DCCTimer.cpp | 23 +++++++++++------------ DCCWaveform.cpp | 10 ++++++---- MotorDriver.cpp | 8 ++++---- MotorDriver.h | 17 ++++++++++------- WifiESP.cpp | 35 +++++++++++++++++++---------------- WifiESP.h | 2 +- defines.h | 12 ++++++++++-- freeMemory.cpp | 5 ++--- 11 files changed, 82 insertions(+), 54 deletions(-) diff --git a/CommandStation-EX.ino b/CommandStation-EX.ino index 2b89708..71c1d67 100644 --- a/CommandStation-EX.ino +++ b/CommandStation-EX.ino @@ -59,7 +59,9 @@ void setup() // Responsibility 1: Start the usb connection for diagnostics // This is normally Serial but uses SerialUSB on a SAMD processor Serial.begin(115200); +#ifdef ESP_DEBUG Serial.setDebugOutput(true); +#endif DIAG(F("License GPLv3 fsf.org (c) dcc-ex.com")); @@ -73,9 +75,12 @@ void setup() // Start the WiFi interface on a MEGA, Uno cannot currently handle WiFi // Start Ethernet if it exists #if WIFI_ON +#ifndef ESP_FAMILY WifiInterface::setup(WIFI_SERIAL_LINK_SPEED, F(WIFI_SSID), F(WIFI_PASSWORD), F(WIFI_HOSTNAME), IP_PORT, WIFI_CHANNEL); +#else + WifiESP::setup(WIFI_SSID, WIFI_PASSWORD, WIFI_HOSTNAME, IP_PORT, WIFI_CHANNEL); +#endif #endif // WIFI_ON - WifiESP::setup(WIFI_SSID, WIFI_PASSWORD, WIFI_HOSTNAME, 2560, 1); #if ETHERNET_ON EthernetInterface::setup(); #endif // ETHERNET_ON @@ -117,9 +122,12 @@ void loop() // Responsibility 3: Optionally handle any incoming WiFi traffic #if WIFI_ON +#ifndef ESP_FAMILY WifiInterface::loop(); -#endif +#else WifiESP::loop(); +#endif +#endif //WIFI_ON #if ETHERNET_ON EthernetInterface::loop(); #endif @@ -136,12 +144,13 @@ void loop() // Report any decrease in memory (will automatically trigger on first call) static int ramLowWatermark = __INT_MAX__; // replaced on first loop -/* +#ifdef ESP_FAMILY + updateMinimumFreeMemory(128); +#endif int freeNow = minimumFreeMemory(); if (freeNow < ramLowWatermark) { ramLowWatermark = freeNow; LCD(2,F("Free RAM=%5db"), ramLowWatermark); } - */ } diff --git a/DCCEX.h b/DCCEX.h index f6668a6..1c2c522 100644 --- a/DCCEX.h +++ b/DCCEX.h @@ -31,7 +31,9 @@ #include "DCCEXParser.h" #include "version.h" #include "WifiInterface.h" +#ifdef ESP_FAMILY #include "WifiESP.h" +#endif #if ETHERNET_ON == true #include "EthernetInterface.h" #endif diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index 8591844..2bbcc5b 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -17,6 +17,7 @@ * You should have received a copy of the GNU General Public License * along with CommandStation. If not, see . */ +#include "defines.h" #include "StringFormatter.h" #include "DCCEXParser.h" #include "DCC.h" @@ -30,7 +31,9 @@ #include "EEStore.h" #include "DIAG.h" -//#include +#ifndef ESP_FAMILY +#include +#endif // These keywords are used in the <1> command. The number is what you get if you use the keyword as a parameter. // To discover new keyword numbers , use the <$ YOURKEYWORD> command diff --git a/DCCTimer.cpp b/DCCTimer.cpp index 5228501..063a6ae 100644 --- a/DCCTimer.cpp +++ b/DCCTimer.cpp @@ -147,27 +147,26 @@ void DCCTimer::read(uint8_t word, uint8_t *mac, uint8_t offset) { #endif #elif defined(ARDUINO_ARCH_ESP8266) -// ESP8266 !!!!!!!!!!!!!!!!!!!!! + void DCCTimer::begin(INTERRUPT_CALLBACK callback) { interruptHandler=callback; timer1_disable(); -// ETS_FRC_TIMER1_INTR_ATTACH(NULL, NULL); -// ETS_FRC_TIMER1_NMI_INTR_ATTACH(interruptHandler); + + // There seem to be differnt ways to attach interrupt handler + // ETS_FRC_TIMER1_INTR_ATTACH(NULL, NULL); + // ETS_FRC_TIMER1_NMI_INTR_ATTACH(interruptHandler); + // Let us choose the one from the API timer1_attachInterrupt(interruptHandler); + + // not exactly sure of order: timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP); timer1_write(CLOCK_CYCLES); -/* - noInterrupts(); - timer1_attachInterrupt(interruptHandler); - timer1_write(CLOCK_CYCLES); - timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP); - interrupts(); -*/ } -IRAM_ATTR bool DCCTimer::isPWMPin(byte pin) { +// We do not support to use PWM to make the Waveform on ESP +bool IRAM_ATTR DCCTimer::isPWMPin(byte pin) { return false; } -void ICACHE_RAM_ATTR DCCTimer::setPWM(byte pin, bool high) { +void IRAM_ATTR DCCTimer::setPWM(byte pin, bool high) { } diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index 3e7e98f..df599de 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -20,6 +20,7 @@ #pragma GCC optimize ("-O3") #include +#include "defines.h" #include "DCCWaveform.h" #include "DCCTimer.h" #include "DIAG.h" @@ -53,7 +54,6 @@ void DCCWaveform::begin(MotorDriver * mainDriver, MotorDriver * progDriver) { DCCTimer::begin(DCCWaveform::interruptHandler); } -#define SLOW_ANALOG_READ #ifdef SLOW_ANALOG_READ // Flag to hold if we need to run ack checking in loop static bool ackflag = 0; @@ -213,7 +213,7 @@ const bool DCCWaveform::signalTransform[]={ /* WAVE_LOW_0 -> */ LOW, /* WAVE_PENDING (should not happen) -> */ LOW}; -void ICACHE_RAM_ATTR DCCWaveform::interrupt2() { +void IRAM_ATTR DCCWaveform::interrupt2() { // calculate the next bit to be sent: // set state WAVE_MID_1 for a 1=bit // or WAVE_HIGH_0 for a 0 bit. @@ -223,7 +223,9 @@ void ICACHE_RAM_ATTR DCCWaveform::interrupt2() { remainingPreambles--; // Update free memory diagnostic as we don't have anything else to do this time. // Allow for checkAck and its called functions using 22 bytes more. -// might break ESP8266 updateMinimumFreeMemory(22); +#ifndef ESP_FAMILY + updateMinimumFreeMemory(22); +#endif return; } @@ -322,7 +324,7 @@ byte DCCWaveform::getAck() { return(0); // pending set off but not detected means no ACK. } -void ICACHE_RAM_ATTR DCCWaveform::checkAck() { +void IRAM_ATTR DCCWaveform::checkAck() { // This function operates in interrupt() time so must be fast and can't DIAG if (sentResetsSincePacket > 6) { //ACK timeout ackCheckDuration=millis()-ackCheckStart; diff --git a/MotorDriver.cpp b/MotorDriver.cpp index c6c37cf..8bcb03c 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -104,8 +104,8 @@ void MotorDriver::setBrake(bool on) { if (on ^ invertBrake) setHIGH(fastBrakePin); else setLOW(fastBrakePin); } -/* -IRAM_ATTR void MotorDriver::setSignal( bool high) { + +void IRAM_ATTR MotorDriver::setSignal( bool high) { if (usePWM) { DCCTimer::setPWM(signalPin,high); } @@ -120,7 +120,7 @@ IRAM_ATTR void MotorDriver::setSignal( bool high) { } } } -*/ + #if defined(ARDUINO_TEENSY32) || defined(ARDUINO_TEENSY35)|| defined(ARDUINO_TEENSY36) volatile unsigned int overflow_count=0; #endif @@ -179,5 +179,5 @@ void MotorDriver::getFastPin(const FSH* type,int pin, bool input, FASTPIN & res result.inout = portOutputRegister(port); result.maskHIGH = digitalPinToBitMask(pin); result.maskLOW = ~result.maskHIGH; - DIAG(F(" port=0x%x, inoutpin=0x%x, isinput=%d, mask=0x%x"),port, result.inout,input,result.maskHIGH); + // DIAG(F(" port=0x%x, inoutpin=0x%x, isinput=%d, mask=0x%x"),port, result.inout,input,result.maskHIGH); } diff --git a/MotorDriver.h b/MotorDriver.h index 06c19b1..a020ef3 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -52,17 +52,20 @@ class MotorDriver { MotorDriver(byte power_pin, byte signal_pin, byte signal_pin2, int8_t brake_pin, byte current_pin, float senseFactor, unsigned int tripMilliamps, byte faultPin); virtual void setPower( bool on); - virtual void setSignal( bool high) { - if (high) { + void setSignal( bool high);/* { + if (usePWM) { + DCCTimer::setPWM(signalPin,high); + } + + if (high) { setHIGH(fastSignalPin); if (dualSignal) setLOW(fastSignalPin2); - } - else { + } + else { setLOW(fastSignalPin); if (dualSignal) setHIGH(fastSignalPin2); - } - }; - + } + };*/ virtual void setBrake( bool on); virtual int getCurrentRaw(); virtual unsigned int raw2mA( int raw); diff --git a/WifiESP.cpp b/WifiESP.cpp index 8948e3d..d949679 100644 --- a/WifiESP.cpp +++ b/WifiESP.cpp @@ -17,6 +17,8 @@ along with CommandStation. If not, see . */ +#include "defines.h" +#ifdef ESP_FAMILY #include #include #include @@ -92,7 +94,7 @@ static void deleteClient(AsyncClient* client) { } } static void handleDisconnect(void* arg, AsyncClient* client) { - DIAG(F("client %s disconnected"), client->remoteIP().toString().c_str()); + DIAG(F("Client disconnected")); deleteClient(client); } @@ -103,11 +105,11 @@ static void handleTimeOut(void* arg, AsyncClient* client, uint32_t time) { static void handleNewClient(void* arg, AsyncClient* client) { - DIAG(F("New client has been connected to server, ip: %s"), client->remoteIP().toString().c_str()); + DIAG(F("New client %s"), client->remoteIP().toString().c_str()); // add to list clients.push_back(client); - + // register events client->onData(&handleData, NULL); client->onError(&handleError, NULL); @@ -118,7 +120,7 @@ static void handleNewClient(void* arg, AsyncClient* client) { /* Things one _might_ want to do: Disable soft watchdog: ESP.wdtDisable() - Enable soft watchdog: ESP.wdtEnable(X) ignores the value of X and enables it for fixed + Enable soft watchdog: ESP.wdtEnable(X) ignores the value of X and enables it for fixed time at least in version 3.0.2 of the esp8266 package. Internet says: @@ -140,30 +142,29 @@ bool WifiESP::setup(const char *wifiESSID, const char *hostname, int port, const byte channel) { - DIAG(F("START")); - // connects to access point + // We are server and should not sleep wifi_set_sleep_type(NONE_SLEEP_T); + // connects to access point WiFi.mode(WIFI_STA); WiFi.setAutoReconnect(true); - DIAG(F("BEGIN")); WiFi.begin(wifiESSID, wifiPassword); - DIAG(F("STATUS")); while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(500); } - - DIAG(F("SERVER")); + if (WiFi.status() == WL_CONNECTED) + DIAG(F("Wifi IP %s"),WiFi.localIP().toString().c_str()); + else { + DIAG(F("Wifi fail")); + // no idea to go on + return false; + } server = new AsyncServer(port); // start listening on tcp port - DIAG(F("CLIENT")); server->onClient(&handleNewClient, server); - DIAG(F("SBEGIN")); - server->begin(); - - DIAG(F("ENDSETUP")); + DIAG(F("Server up port %d"),port); return true; } @@ -213,11 +214,13 @@ void WifiESP::loop() { } } } - +#ifdef ESP_DEBUG static unsigned long last = 0; if (millis() - last > 60000) { last = millis(); DIAG(F("+")); } +#endif ESP.wdtFeed(); } +#endif //ESP_FAMILY diff --git a/WifiESP.h b/WifiESP.h index 971ccc7..47de4ed 100644 --- a/WifiESP.h +++ b/WifiESP.h @@ -1,6 +1,6 @@ /* * © 2021, Harald Barth. - * + * * This file is part of CommandStation-EX * * This is free software: you can redistribute it and/or modify diff --git a/defines.h b/defines.h index b018c54..afb18e8 100644 --- a/defines.h +++ b/defines.h @@ -1,5 +1,5 @@ /* - © 2020, Harald Barth. + © 2020,2021 Harald Barth. This file is part of CommandStation-EX @@ -18,12 +18,20 @@ */ +//////////////////////////////////////////////////////////////////////////////// +// +#if defined (ARDUINO_ARCH_ESP8266) +#define ESP_FAMILY +//#define ESP_DEBUG +#define SLOW_ANALOG_READ +#endif + //////////////////////////////////////////////////////////////////////////////// // // WIFI_ON: All prereqs for running with WIFI are met // Note: WIFI_CHANNEL may not exist in early config.h files so is added here if needed. -#if ENABLE_WIFI && (defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_SAMD_ZERO) || defined(TEENSYDUINO)) +#if ENABLE_WIFI && (defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_SAMD_ZERO) || defined(TEENSYDUINO) || defined (ESP_FAMILY)) #define WIFI_ON true #ifndef WIFI_CHANNEL #define WIFI_CHANNEL 1 diff --git a/freeMemory.cpp b/freeMemory.cpp index af7bd16..f2dc29d 100644 --- a/freeMemory.cpp +++ b/freeMemory.cpp @@ -28,7 +28,7 @@ extern "C" char* sbrk(int); extern char *__brkval; extern char *__malloc_heap_start; #elif defined(ARDUINO_ARCH_ESP8266) -// fine as well +// supported but nothing needed here #else #error Unsupported board type #endif @@ -116,9 +116,8 @@ int minimumFreeMemory() { // So even if all of the heap is freed, the reported minimum free // memory will not increase. // -void ICACHE_RAM_ATTR updateMinimumFreeMemory(unsigned char extraBytes) { +void updateMinimumFreeMemory(unsigned char extraBytes) { int spare = freeMemory()-extraBytes; if (spare < 0) spare = 0; if (spare < minimum_free_memory) minimum_free_memory = spare; } -