Skip to content

Commit f3ac90d

Browse files
committed
feat(HardwareSerial): Add function to invert Hardware UART's Tx line alongside existing Rx functionality
Simply clone existing Rx functionality for Tx. The goal here is to allow granular control over both lines but avoid overloading HardwareSerial::begin() to change the boolean 'invert' parameter to a bitmask type.
1 parent f3ae2a6 commit f3ac90d

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

cores/esp32/HardwareSerial.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,10 @@ void HardwareSerial::setRxInvert(bool invert) {
578578
uartSetRxInvert(_uart, invert);
579579
}
580580

581+
void HardwareSerial::setTxInvert(bool invert) {
582+
uartSetTxInvert(_uart, invert);
583+
}
584+
581585
// negative Pin value will keep it unmodified
582586
// can be called after or before begin()
583587
bool HardwareSerial::setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin) {

cores/esp32/HardwareSerial.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ class HardwareSerial : public Stream {
349349
void setDebugOutput(bool);
350350

351351
void setRxInvert(bool);
352+
void setTxInvert(bool);
352353

353354
// Negative Pin Number will keep it unmodified, thus this function can set individual pins
354355
// setPins() can be called after or before begin()

cores/esp32/esp32-hal-uart.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,30 @@ void uartSetRxInvert(uart_t *uart, bool invert) {
847847
#endif
848848
}
849849

850+
void uartSetTxInvert(uart_t *uart, bool invert) {
851+
if (uart == NULL) {
852+
return;
853+
}
854+
#if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32P4
855+
// POTENTIAL ISSUE :: original code only set/reset txd_inv bit
856+
// IDF or LL set/reset the whole inv_mask!
857+
// if (invert)
858+
// ESP_ERROR_CHECK(uart_set_line_inverse(uart->num, UART_SIGNAL_TXD_INV));
859+
// else
860+
// ESP_ERROR_CHECK(uart_set_line_inverse(uart->num, UART_SIGNAL_INV_DISABLE));
861+
log_e("uartSetTxInvert is not supported in ESP32C6, ESP32H2 and ESP32P4");
862+
#else
863+
// this implementation is better over IDF API because it only affects TXD
864+
// this is supported in ESP32, ESP32-S2 and ESP32-C3
865+
uart_dev_t *hw = UART_LL_GET_HW(uart->num);
866+
if (invert) {
867+
hw->conf0.txd_inv = 1;
868+
} else {
869+
hw->conf0.txd_inv = 0;
870+
}
871+
#endif
872+
}
873+
850874
uint32_t uartAvailable(uart_t *uart) {
851875

852876
if (uart == NULL) {

cores/esp32/esp32-hal-uart.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ bool uartSetBaudRate(uart_t *uart, uint32_t baud_rate);
6262
uint32_t uartGetBaudRate(uart_t *uart);
6363

6464
void uartSetRxInvert(uart_t *uart, bool invert);
65+
void uartSetTxInvert(uart_t *uart, bool invert);
6566
bool uartSetRxTimeout(uart_t *uart, uint8_t numSymbTimeout);
6667
bool uartSetRxFIFOFull(uart_t *uart, uint8_t numBytesFIFOFull);
6768
void uartSetFastReading(uart_t *uart);

tests/validation/uart/uart.ino

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ void enabled_uart_calls_test(void) {
276276
Serial1.setRxInvert(true);
277277
Serial1.setRxInvert(false);
278278

279+
log_d("Checking if Serial 1 TX can be inverted while running");
280+
Serial1.setTxInvert(true);
281+
Serial1.setTxInvert(false);
282+
279283
Serial.println("Enabled UART calls test successful");
280284
}
281285

@@ -351,6 +355,10 @@ void disabled_uart_calls_test(void) {
351355
Serial1.setRxInvert(true);
352356
Serial1.setRxInvert(false);
353357

358+
log_d("Checking if Serial 1 TX can be inverted when stopped");
359+
Serial1.setTxInvert(true);
360+
Serial1.setTxInvert(false);
361+
354362
Serial.println("Disabled UART calls test successful");
355363
}
356364

0 commit comments

Comments
 (0)