Skip to content

Commit

Permalink
Allow to set uart pins for RP2040 and ESP32 platform
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Geramb committed Dec 25, 2023
1 parent a870dd8 commit ddd8284
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/esp32_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions src/esp32_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +40,8 @@ class Esp32Platform : public ArduinoPlatform
void commitToEeprom();
private:
WiFiUDP _udp;
int8_t _rxPin = -1;
int8_t _txPin = -1;
};

#endif
23 changes: 23 additions & 0 deletions src/rp2040_arduino_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<SerialUART*>(_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<SerialUART*>(_knxSerial);
Expand Down
2 changes: 2 additions & 0 deletions src/rp2040_arduino_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ddd8284

Please sign in to comment.