Skip to content

feat(uart): Add function to invert hardware UART Tx line #11428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cores/esp32/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,10 @@ void HardwareSerial::setRxInvert(bool invert) {
uartSetRxInvert(_uart, invert);
}

void HardwareSerial::setTxInvert(bool invert) {
uartSetTxInvert(_uart, invert);
}

// negative Pin value will keep it unmodified
// can be called after or before begin()
bool HardwareSerial::setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin) {
Expand Down
1 change: 1 addition & 0 deletions cores/esp32/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ class HardwareSerial : public Stream {
void setDebugOutput(bool);

void setRxInvert(bool);
void setTxInvert(bool);

// Negative Pin Number will keep it unmodified, thus this function can set individual pins
// setPins() can be called after or before begin()
Expand Down
42 changes: 38 additions & 4 deletions cores/esp32/esp32-hal-uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -827,18 +827,23 @@ void uartSetRxInvert(uart_t *uart, bool invert) {
if (uart == NULL) {
return;
}
#if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32P4
// POTENTIAL ISSUE :: original code only set/reset rxd_inv bit
// IDF or LL set/reset the whole inv_mask!
// if (invert)
// ESP_ERROR_CHECK(uart_set_line_inverse(uart->num, UART_SIGNAL_RXD_INV));
// else
// ESP_ERROR_CHECK(uart_set_line_inverse(uart->num, UART_SIGNAL_INV_DISABLE));
log_e("uartSetRxInvert is not supported in ESP32C6, ESP32H2 and ESP32P4");
#else
// this implementation is better over IDF API because it only affects RXD
// this is supported in ESP32, ESP32-S2 and ESP32-C3
uart_dev_t *hw = UART_LL_GET_HW(uart->num);

#if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32P4
if (invert) {
hw->conf0_sync.rxd_inv = 1;
} else {
hw->conf0_sync.rxd_inv = 0;
}
#else
// this is supported in ESP32, ESP32-S2 and ESP32-C3
if (invert) {
hw->conf0.rxd_inv = 1;
} else {
Expand All @@ -847,6 +852,35 @@ void uartSetRxInvert(uart_t *uart, bool invert) {
#endif
}

void uartSetTxInvert(uart_t *uart, bool invert) {
if (uart == NULL) {
return;
}
// POTENTIAL ISSUE :: original code only set/reset txd_inv bit
// IDF or LL set/reset the whole inv_mask!
// if (invert)
// ESP_ERROR_CHECK(uart_set_line_inverse(uart->num, UART_SIGNAL_TXD_INV));
// else
// ESP_ERROR_CHECK(uart_set_line_inverse(uart->num, UART_SIGNAL_INV_DISABLE));
// this implementation is better over IDF API because it only affects TXD
uart_dev_t *hw = UART_LL_GET_HW(uart->num);

#if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32P4
if (invert) {
hw->conf0_sync.txd_inv = 1;
} else {
hw->conf0_sync.txd_inv = 0;
}
#else
// this is supported in ESP32, ESP32-S2 and ESP32-C3
if (invert) {
hw->conf0.txd_inv = 1;
} else {
hw->conf0.txd_inv = 0;
}
#endif
}

uint32_t uartAvailable(uart_t *uart) {

if (uart == NULL) {
Expand Down
1 change: 1 addition & 0 deletions cores/esp32/esp32-hal-uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ bool uartSetBaudRate(uart_t *uart, uint32_t baud_rate);
uint32_t uartGetBaudRate(uart_t *uart);

void uartSetRxInvert(uart_t *uart, bool invert);
void uartSetTxInvert(uart_t *uart, bool invert);
bool uartSetRxTimeout(uart_t *uart, uint8_t numSymbTimeout);
bool uartSetRxFIFOFull(uart_t *uart, uint8_t numBytesFIFOFull);
void uartSetFastReading(uart_t *uart);
Expand Down
8 changes: 8 additions & 0 deletions tests/validation/uart/uart.ino
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ void enabled_uart_calls_test(void) {
Serial1.setRxInvert(true);
Serial1.setRxInvert(false);

log_d("Checking if Serial 1 TX can be inverted while running");
Serial1.setTxInvert(true);
Serial1.setTxInvert(false);

Serial.println("Enabled UART calls test successful");
}

Expand Down Expand Up @@ -351,6 +355,10 @@ void disabled_uart_calls_test(void) {
Serial1.setRxInvert(true);
Serial1.setRxInvert(false);

log_d("Checking if Serial 1 TX can be inverted when stopped");
Serial1.setTxInvert(true);
Serial1.setTxInvert(false);

Serial.println("Disabled UART calls test successful");
}

Expand Down
Loading