diff --git a/DCC.h b/DCC.h index b473673..715a38d 100644 --- a/DCC.h +++ b/DCC.h @@ -193,6 +193,8 @@ private: #define ARDUINO_TYPE "TEENSY41" #elif defined(ARDUINO_ARCH_ESP8266) #define ARDUINO_TYPE "ESP8266" +#elif defined(ARDUINO_ARCH_ESP32) +#define ARDUINO_TYPE "ESP32" #else #error CANNOT COMPILE - DCC++ EX ONLY WORKS WITH AN ARDUINO UNO, NANO 328, OR ARDUINO MEGA 1280/2560 #endif diff --git a/DCCEX.h b/DCCEX.h index 1c2c522..8ff2f51 100644 --- a/DCCEX.h +++ b/DCCEX.h @@ -30,9 +30,12 @@ #include "DIAG.h" #include "DCCEXParser.h" #include "version.h" +#if defined(ARDUINO_ARCH_ESP8266) +#include "WifiESP8266.h" +#elif defined(ARDUINO_ARCH_ESP32) +#include "WifiESP32.h" +#else #include "WifiInterface.h" -#ifdef ESP_FAMILY -#include "WifiESP.h" #endif #if ETHERNET_ON == true #include "EthernetInterface.h" diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index 2bbcc5b..c8d7fde 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -789,8 +789,12 @@ bool DCCEXParser::parseD(Print *stream, int16_t params, int16_t p[]) case HASH_KEYWORD_RESET: { - wdt_enable( WDTO_15MS); // set Arduino watchdog timer for 15ms - delay(50); // wait for the prescaller time to expire +#ifndef ESP_FAMILY + wdt_enable( WDTO_15MS); // set Arduino watchdog timer for 15ms + delay(50); // wait for the prescaler time to expire +#else + /* XXX do right thing to reboot */ +#endif break; // and if we didnt restart } diff --git a/DCCTimer.h b/DCCTimer.h index b518701..d421828 100644 --- a/DCCTimer.h +++ b/DCCTimer.h @@ -37,4 +37,5 @@ class DCCTimer { private: }; +extern portMUX_TYPE timerMux; #endif diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index 33f7392..439e781 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -255,7 +255,8 @@ void IRAM_ATTR DCCWaveform::interrupt2() { transmitRepeats--; } else if (packetPending) { - // Copy pending packet to transmit packet + portENTER_CRITICAL(&timerMux); + // Copy pending packet to transmit packet // a fixed length memcpy is faster than a variable length loop for these small lengths // for (int b = 0; b < pendingLength; b++) transmitPacket[b] = pendingPacket[b]; memcpy( transmitPacket, pendingPacket, sizeof(pendingPacket)); @@ -264,6 +265,7 @@ void IRAM_ATTR DCCWaveform::interrupt2() { transmitRepeats = pendingRepeats; packetPending = false; sentResetsSincePacket=0; + portEXIT_CRITICAL(&timerMux); } else { // Fortunately reset and idle packets are the same length @@ -282,7 +284,7 @@ void IRAM_ATTR DCCWaveform::interrupt2() { void DCCWaveform::schedulePacket(const byte buffer[], byte byteCount, byte repeats) { if (byteCount > MAX_PACKET_SIZE) return; // allow for chksum while (packetPending); - + portENTER_CRITICAL(&timerMux); byte checksum = 0; for (byte b = 0; b < byteCount; b++) { checksum ^= buffer[b]; @@ -294,6 +296,7 @@ void DCCWaveform::schedulePacket(const byte buffer[], byte byteCount, byte repea pendingRepeats = repeats; packetPending = true; sentResetsSincePacket=0; + portEXIT_CRITICAL(&timerMux); } // Operations applicable to PROG track ONLY. @@ -330,7 +333,7 @@ byte DCCWaveform::getAck() { } void IRAM_ATTR DCCWaveform::checkAck() { - // This function operates in interrupt() time so must be fast and can't DIAG + // This function operates in interrupt() time (not on ESP) so must be fast and can't DIAG if (sentResetsSincePacket > 6) { //ACK timeout ackCheckDuration=millis()-ackCheckStart; ackPending = false; diff --git a/MotorDriver.h b/MotorDriver.h index a020ef3..9193828 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -18,6 +18,7 @@ */ #ifndef MotorDriver_h #define MotorDriver_h +#include "defines.h" #include "FSH.h" // Virtualised Motor shield 1-track hardware Interface @@ -26,7 +27,7 @@ #define UNUSED_PIN 127 // inside int8_t #endif -#if defined(__IMXRT1062__) || defined (ARDUINO_ARCH_ESP8266) +#if defined(__IMXRT1062__) || defined(ESP_FAMILY) typedef uint32_t PORTTYPE; struct FASTPIN { volatile uint32_t *inout; diff --git a/WifiESP32.cpp b/WifiESP32.cpp new file mode 100644 index 0000000..93ec702 --- /dev/null +++ b/WifiESP32.cpp @@ -0,0 +1,39 @@ +/* + © 2021, Harald Barth. + + This file is part of CommandStation-EX + + This is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + It is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with CommandStation. If not, see . +*/ + +#include "defines.h" +#if defined(ARDUINO_ARCH_ESP32) +#include +#include "WifiESP32.h" +#include "DIAG.h" +#include "RingStream.h" +#include "CommandDistributor.h" + +bool WifiESP::setup(const char *SSid, + const char *password, + const char *hostname, + int port, + const byte channel) { + return false; +} + +void WifiESP::loop() { + +} +#endif //ESP32 diff --git a/WifiESP.h b/WifiESP32.h similarity index 89% rename from WifiESP.h rename to WifiESP32.h index 47de4ed..100e393 100644 --- a/WifiESP.h +++ b/WifiESP32.h @@ -17,8 +17,9 @@ * along with CommandStation. If not, see . */ -#ifndef WifiESP_h -#define WifiESP_h +#if defined(ARDUINO_ARCH_ESP32) +#ifndef WifiESP32_h +#define WifiESP32_h #include "FSH.h" @@ -34,4 +35,5 @@ public: static void loop(); private: }; -#endif +#endif //WifiESP8266_h +#endif //ESP8266 diff --git a/WifiESP.cpp b/WifiESP8266.cpp similarity index 99% rename from WifiESP.cpp rename to WifiESP8266.cpp index 2c6f169..7b3dec5 100644 --- a/WifiESP.cpp +++ b/WifiESP8266.cpp @@ -18,13 +18,13 @@ */ #include "defines.h" -#ifdef ESP_FAMILY +#if defined(ARDUINO_ARCH_ESP8266) #include #include #include #include -#include "WifiESP.h" +#include "WifiESP8266.h" #include "DIAG.h" #include "RingStream.h" #include "CommandDistributor.h" diff --git a/WifiESP8266.h b/WifiESP8266.h new file mode 100644 index 0000000..6fd71dd --- /dev/null +++ b/WifiESP8266.h @@ -0,0 +1,39 @@ +/* + * © 2021, Harald Barth. + * + * This file is part of CommandStation-EX + * + * This is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * It is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CommandStation. If not, see . + */ + +#if defined(ARDUINO_ARCH_ESP8266) +#ifndef WifiESP8266_h +#define WifiESP8266_h + +#include "FSH.h" + +class WifiESP +{ + +public: + static bool setup(const char *wifiESSID, + const char *wifiPassword, + const char *hostname, + const int port, + const byte channel); + static void loop(); +private: +}; +#endif //WifiESP8266_h +#endif //ESP8266 diff --git a/WifiInterface.cpp b/WifiInterface.cpp index bd7b7f2..cb40a79 100644 --- a/WifiInterface.cpp +++ b/WifiInterface.cpp @@ -17,9 +17,10 @@ You should have received a copy of the GNU General Public License along with CommandStation. If not, see . */ +#include "WifiInterface.h" /* config.h included there */ +#ifndef ESP_FAMILY #ifndef ARDUINO_AVR_UNO_WIFI_REV2 // This code is NOT compiled on a unoWifiRev2 processor which uses a different architecture -#include "WifiInterface.h" /* config.h included there */ #include #include "DIAG.h" #include "StringFormatter.h" @@ -370,4 +371,5 @@ void WifiInterface::loop() { } } -#endif +#endif //ARDUINO_AVR_UNO_WIFI_REV2 +#endif //ESP_FAMILY diff --git a/WifiInterface.h b/WifiInterface.h index 19f8a3a..d674bee 100644 --- a/WifiInterface.h +++ b/WifiInterface.h @@ -19,6 +19,8 @@ */ #ifndef WifiInterface_h #define WifiInterface_h +#include "defines.h" +#ifndef ESP_FAMILY #include "FSH.h" #include "DCCEXParser.h" #include @@ -50,4 +52,5 @@ private: static bool checkForOK(const unsigned int timeout, const FSH *waitfor, bool echo, bool escapeEcho = true); static bool connected; }; -#endif +#endif //ESP_FAMILY +#endif diff --git a/defines.h b/defines.h index a79ddca..7ad98fe 100644 --- a/defines.h +++ b/defines.h @@ -20,7 +20,7 @@ //////////////////////////////////////////////////////////////////////////////// // -#if defined (ARDUINO_ARCH_ESP8266) +#if defined(ARDUINO_ARCH_ESP8266) #define ESP_FAMILY //#define ESP_DEBUG #define SLOW_ANALOG_READ @@ -28,7 +28,7 @@ //////////////////////////////////////////////////////////////////////////////// // -#if defined (ARDUINO_ARCH_ESP32) +#if defined(ARDUINO_ARCH_ESP32) #define ESP_FAMILY #define SLOW_ANALOG_READ #else @@ -42,7 +42,7 @@ // 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) || defined (ESP_FAMILY)) +#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 f2dc29d..4758f83 100644 --- a/freeMemory.cpp +++ b/freeMemory.cpp @@ -1,5 +1,5 @@ /* - * © 2020, Harald Barth + * © 2020,2021 Harald Barth * © 2021, Neil McKechnie * * This file is part of Asbelos DCC-EX @@ -27,7 +27,7 @@ extern "C" char* sbrk(int); #elif defined(__AVR__) extern char *__brkval; extern char *__malloc_heap_start; -#elif defined(ARDUINO_ARCH_ESP8266) +#elif defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) // supported but nothing needed here #else #error Unsupported board type @@ -36,7 +36,7 @@ extern char *__malloc_heap_start; static volatile int minimum_free_memory = __INT_MAX__; -#if !defined(__IMXRT1062__) && !defined(ARDUINO_ARCH_ESP8266) +#if !defined(__IMXRT1062__) && !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) static inline int freeMemory() { char top; #if defined(__arm__) @@ -57,8 +57,8 @@ int minimumFreeMemory() { return retval; } -#elif defined(ARDUINO_ARCH_ESP8266) -// ESP8266 +#elif defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) +// ESP8266 and ESP32 static inline int freeMemory() { return ESP.getFreeHeap(); }