From ddd82845782e020b103e4cba7998b58f723b66e9 Mon Sep 17 00:00:00 2001 From: Michael Geramb Date: Mon, 25 Dec 2023 19:39:05 +0100 Subject: [PATCH] Allow to set uart pins for RP2040 and ESP32 platform --- src/esp32_platform.cpp | 25 +++++++++++++++++++++++++ src/esp32_platform.h | 6 ++++++ src/rp2040_arduino_platform.cpp | 23 +++++++++++++++++++++++ src/rp2040_arduino_platform.h | 2 ++ 4 files changed, 56 insertions(+) diff --git a/src/esp32_platform.cpp b/src/esp32_platform.cpp index ada8fd3d..a8c985ba 100644 --- a/src/esp32_platform.cpp +++ b/src/esp32_platform.cpp @@ -10,17 +10,42 @@ #define KNX_SERIAL Serial1 #endif +#ifndef KNX_UART_RX_PIN +#define KNX_UART_RX_PIN -1 +#endif + +#ifndef KNX_UART_TX_PIN +#define KNX_UART_TX_PIN -1 +#endif + Esp32Platform::Esp32Platform() #ifndef KNX_NO_DEFAULT_UART : ArduinoPlatform(&KNX_SERIAL) #endif { +#ifndef KNX_NO_DEFAULT_UART + knxUartPins(KNX_UART_RX_PIN, KNX_UART_TX_PIN); +#endif } Esp32Platform::Esp32Platform(HardwareSerial* s) : ArduinoPlatform(s) { } +void Esp32Platform::knxUartPins(int8_t rxPin, int8_t txPin) +{ + _rxPin = rxPin; + _txPin = txPin; +} + +// ESP specific uart handling with pins +void Esp32Platform::setupUart() +{ + _knxSerial->begin(19200, SERIAL_8E1, _rxPin, _txPin); + while (!_knxSerial) + ; +} + uint32_t Esp32Platform::currentIpAddress() { return WiFi.localIP(); diff --git a/src/esp32_platform.h b/src/esp32_platform.h index 0193efe4..318291cb 100644 --- a/src/esp32_platform.h +++ b/src/esp32_platform.h @@ -10,6 +10,10 @@ class Esp32Platform : public ArduinoPlatform Esp32Platform(); Esp32Platform(HardwareSerial* s); + // uart + void knxUartPins(int8_t rxPin, int8_t txPin); + void setupUart() override; + // ip stuff uint32_t currentIpAddress() override; uint32_t currentSubnetMask() override; @@ -36,6 +40,8 @@ class Esp32Platform : public ArduinoPlatform void commitToEeprom(); private: WiFiUDP _udp; + int8_t _rxPin = -1; + int8_t _txPin = -1; }; #endif diff --git a/src/rp2040_arduino_platform.cpp b/src/rp2040_arduino_platform.cpp index 44eb8c4b..8de0c66e 100644 --- a/src/rp2040_arduino_platform.cpp +++ b/src/rp2040_arduino_platform.cpp @@ -49,11 +49,22 @@ A RAM-buffered Flash can be use by defining USE_RP2040_LARGE_EEPROM_EMULATION #define KNX_SERIAL Serial1 #endif +#ifndef KNX_UART_RX_PIN +#define KNX_UART_RX_PIN UART_PIN_NOT_DEFINED +#endif + +#ifndef KNX_UART_TX_PIN +#define KNX_UART_TX_PIN UART_PIN_NOT_DEFINED +#endif + RP2040ArduinoPlatform::RP2040ArduinoPlatform() #ifndef KNX_NO_DEFAULT_UART : ArduinoPlatform(&KNX_SERIAL) #endif { + #ifndef KNX_NO_DEFAULT_UART + knxUartPins(KNX_UART_RX_PIN, KNX_UART_TX_PIN); + #endif #ifndef USE_RP2040_EEPROM_EMULATION _memoryType = Flash; #endif @@ -66,6 +77,18 @@ RP2040ArduinoPlatform::RP2040ArduinoPlatform( HardwareSerial* s) : ArduinoPlatfo #endif } +void RP2040ArduinoPlatform::knxUartPins(pin_size_t rxPin, pin_size_t txPin) +{ + SerialUART* serial = dynamic_cast(_knxSerial); + if(serial) + { + if (rxPin != UART_PIN_NOT_DEFINED) + serial->setRX(rxPin); + if (txPin != UART_PIN_NOT_DEFINED) + serial->setTX(txPin); + } +} + void RP2040ArduinoPlatform::setupUart() { SerialUART* serial = dynamic_cast(_knxSerial); diff --git a/src/rp2040_arduino_platform.h b/src/rp2040_arduino_platform.h index 15e6dcb7..836f8644 100644 --- a/src/rp2040_arduino_platform.h +++ b/src/rp2040_arduino_platform.h @@ -22,6 +22,8 @@ class RP2040ArduinoPlatform : public ArduinoPlatform RP2040ArduinoPlatform(); RP2040ArduinoPlatform( HardwareSerial* s); + // uart + void knxUartPins(pin_size_t rxPin, pin_size_t txPin); void setupUart(); // unique serial number