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.
This commit is contained in:
Bernd Giesecke 2019-09-07 20:23:55 +08:00
parent c9b68163d5
commit ec686e469e
3 changed files with 42 additions and 4 deletions

View file

@ -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() {

View file

@ -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;

View file

@ -3,7 +3,9 @@
#include <SPI.h>
//#include <Wire.h>
#ifndef ESP32
#include <SoftwareSerial.h>
#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.