From 7097637c73c5c56d5665fc8296fd5d80f559a918 Mon Sep 17 00:00:00 2001 From: Bernd Giesecke Date: Sat, 7 Sep 2019 20:23:55 +0800 Subject: [PATCH] ESP32 - use HardwareSerial instead of SoftwareSerial ESP32 has no working SoftwareSerial. With a simple #ifdef ESP32 a hardware serial will be used instead. Minimum changes without influence on existing installations or usage. --- src/ISerial.cpp | 16 ++++++++++++++++ src/Module.cpp | 16 ++++++++++++++-- src/Module.h | 14 ++++++++++++-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/ISerial.cpp b/src/ISerial.cpp index 0ae9ca47..3734df21 100644 --- a/src/ISerial.cpp +++ b/src/ISerial.cpp @@ -9,7 +9,11 @@ void ISerial::begin(long speed) { } bool ISerial::listen() { +#ifndef ESP32 return(_mod->ModuleSerial->listen()); +#else + return true; +#endif } void ISerial::end() { @@ -17,15 +21,27 @@ void ISerial::end() { } bool ISerial::isListening() { +#ifndef ESP32 return(_mod->ModuleSerial->isListening()); +#else + return true; +#endif } bool ISerial::stopListening() { +#ifndef ESP32 return(_mod->ModuleSerial->stopListening()); +#else + return true; +#endif } bool ISerial::overflow() { +#ifndef ESP32 return(_mod->ModuleSerial->overflow()); +#else + return true; +#endif } int ISerial::peek() { diff --git a/src/Module.cpp b/src/Module.cpp index 5c238519..e14421ec 100644 --- a/src/Module.cpp +++ b/src/Module.cpp @@ -1,13 +1,17 @@ #include "Module.h" -Module::Module(int rx, int tx) { +Module::Module(int rx, int tx, int sernum) { _cs = -1; _rx = rx; _tx = tx; _int0 = -1; _int1 = -1; +#ifndef ESP32 ModuleSerial = new SoftwareSerial(_rx, _tx); +#else + ModuleSerial = new HardwareSerial(sernum); +#endif } Module::Module(int cs, int int0, int int1, SPIClass& spi, SPISettings spiSettings) { @@ -20,7 +24,7 @@ Module::Module(int cs, int int0, int int1, SPIClass& spi, SPISettings spiSetting _spiSettings = spiSettings; } -Module::Module(int cs, int int0, int int1, int rx, int tx, SPIClass& spi, SPISettings spiSettings) { +Module::Module(int cs, int int0, int int1, int rx, int tx, SPIClass& spi, SPISettings spiSettings, int sernum) { _cs = cs; _rx = rx; _tx = tx; @@ -29,7 +33,11 @@ Module::Module(int cs, int int0, int int1, int rx, int tx, SPIClass& spi, SPISet _spi = &spi; _spiSettings = spiSettings; +#ifndef ESP32 ModuleSerial = new SoftwareSerial(_rx, _tx); +#else + ModuleSerial = new HardwareSerial(sernum); +#endif } Module::Module(int cs, int int0, int int1, int int2, SPIClass& spi, SPISettings spiSettings) { @@ -51,7 +59,11 @@ void Module::init(uint8_t interface, uint8_t gpio) { _spi->begin(); break; case USE_UART: +#ifndef ESP32 ModuleSerial->begin(baudrate); +#else + ModuleSerial->begin(baudrate, _rx, _tx); +#endif break; case USE_I2C: break; diff --git a/src/Module.h b/src/Module.h index c596276f..ee440834 100644 --- a/src/Module.h +++ b/src/Module.h @@ -3,7 +3,9 @@ #include //#include +#ifndef ESP32 #include +#endif #include "TypeDef.h" @@ -22,8 +24,10 @@ class Module { \param tx Arduino pin to be used as Tx pin for SoftwareSerial communication. \param rx Arduino pin to be used as Rx pin for SoftwareSerial communication. + + \param sernum number of the HW serial to be used on ESP32. Defaults to 2. */ - Module(int tx, int rx); + Module(int tx, int rx, int sernum = 2); /*! \brief SPI-based module constructor. @@ -73,15 +77,21 @@ class Module { \param spi SPI interface to be used. Defaults to Arduino hardware SPI interface, can also use software SPI implementations. \param spiSettings SPI interface settings. Defaults to 2 MHz clock, MSB first, mode 0. + + \param sernum number of the HW serial to be used on ESP32. Defaults to 2. */ - Module(int cs, int int0, int int1, int rx, int tx, SPIClass& spi = SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0)); + Module(int cs, int int0, int int1, int rx, int tx, SPIClass& spi = SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0), int sernum = 2); // public member variables /*! \brief Internal SoftwareSerial instance. */ +#ifndef ESP32 SoftwareSerial* ModuleSerial; +#else + HardwareSerial* ModuleSerial; +#endif /*! \brief Baud rate of SoftwareSerial UART communication. Defaults to 9600 baud.