SmingHub / Sming

Sming - powerful open source framework simplifying the creation of embedded C++ applications.
https://sming.readthedocs.io
GNU Lesser General Public License v3.0
1.47k stars 349 forks source link

Switching to GPIO2 as TX on UART0 after initializing does not work. #2208

Closed iliis closed 3 years ago

iliis commented 3 years ago

I'm using a H801 LED driver board, which uses GPIO2 to transmit UART0 data. The following works:

Serial.begin(115200, SERIAL_8N1, SERIAL_FULL, 2);

While this does not:

Serial.begin(115200, SERIAL_8N1, SERIAL_FULL, 1); // Same as Serial.begin(115200);
Serial.setTx(2);

In both cases I receive the bootloader output (at 74480 baud), but only the first version produces anything when calling Serial.write().

I've tried both with the current develop (bdf1d31db839385a8e1075f901b9cd95d6fb538d) and master (4.2.0, 7bc3603e22ca91c36d0b6b1bcfa96b5801afa325) branches. Toolchain is https://github.com/SmingHub/SmingTools/releases/download/1.0/x86_64-linux-gnu.xtensa-lx106-elf-e6a192b.201211.tar.gz

mikee47 commented 3 years ago

@iliis Aha! You have found a bug in the uart driver. Try this patch to Sming/Arch/Esp8266/Components/driver/uart.cpp:

diff --git a/Sming/Arch/Esp8266/Components/driver/uart.cpp b/Sming/Arch/Esp8266/Components/driver/uart.cpp
index e6c50b94f..22114d381 100644
--- a/Sming/Arch/Esp8266/Components/driver/uart.cpp
+++ b/Sming/Arch/Esp8266/Components/driver/uart.cpp
@@ -910,9 +910,9 @@ void smg_uart_swap(smg_uart_t* uart, int tx_pin)
 bool smg_uart_set_tx(smg_uart_t* uart, int tx_pin)
 {
    if(uart != nullptr && uart->uart_nr == UART0 && smg_uart_tx_enabled(uart)) {
-       uart1_pin_restore(uart->tx_pin);
+       uart0_pin_restore(uart->tx_pin);
        uart->tx_pin = (tx_pin == 2) ? 2 : 1;
-       uart1_pin_select(uart->tx_pin);
+       uart0_pin_select(uart->tx_pin);
        return true;
    }
iliis commented 3 years ago

That was quick! And yes, it works :)