A UART! A UART! My kingdom for a UART! - Page 1
Forum section: electronics / general

author: HwAoRrDk
author_rank: Super Contributor
timestamp: January 06, 2023, 10:18:15 pm
content: I don't understand WTF Espressif's intentions were with giving the ESP32 three UARTs, but then - for the majority of usage scenarios - making all of them but oneunusable. You can't use UART1 because those pins are all taken by flash. You can't realistically dedicate UART0 to other purposes because you need that for programming and debug output. And for UART2, only RX/TX are usable on WROOM modules (not WROVER, because of PSRAM), with RTS/CTS also being nixed by flash.So, what's a guy to do if he has an ESP32-based project where essentiallyallthe pins are already in use, but would like to try and squeeze out the use of a UART for something like RS-485?I have two main questions:1. Could there be some way of sharing UART0? I am imagining perhaps a mux/switch/buffer that under control of another GPIO switches the RX/TX (and maybe RTS) between the programming/debug header and the RS-485 transceiver.2. If so, is it possible with the ESP-IDF framework to share UART0 in this manner? By default all log/trace 'console' output is directed there, but can you dynamically switch that off then back again?

author: tabemann
author_rank: Contributor
timestamp: January 06, 2023, 10:56:20 pm
content: At least on the RP2040, which also has a similar external QSPI flash arrangement, one can turn off QSPI flash, executing solely from SRAM, and use those pins designated for it for some other purpose.

author: woofy
author_rank: Frequent Contributor
timestamp: January 06, 2023, 11:21:27 pm
content: I thought the pins could be re-mapped;// Use GPIO2 and GPIO4 for Serial2Serial2.begin(115200, SERIAL_8N1, 2, 4);

author: Doctorandus_P
author_rank: Super Contributor
timestamp: January 06, 2023, 11:47:10 pm
content: It's quite common for microcontrollers to have many more functions than they have pins, and as far as I know the ESP32 is pretty constrained in the number of pins.Teensy 4 may be nice. I think it has 8 uart's. It also has wired ethernet which is much more reliable than that wireless rubbish. And you can also use the same cable to power from a remote power supply, so no messing with batteries. (100Mbit/s Ethernet uses only 2 wire pairs, the other two pairs are "free").If you go to chip families such as STM32, you can choose between 10 or so I/O pins to 140 pins or more and there have got to be some UsARTS among that sea of pins.

author: globoy
author_rank: Frequent Contributor
timestamp: January 06, 2023, 11:48:26 pm
content: Serial port pins can be remapped. As woofy shows for arduino and with uart_set_pin directly under the IDF.There's also RS-485 support:https://docs.espressif.com/projects/esp-idf/en/v5.0/esp32/api-reference/peripherals/uart.html

author: SiliconWizard
author_rank: Super Contributor
timestamp: January 07, 2023, 12:19:51 am
content: It's just that those MCUs have a ridiculously low number of GPIOs in general. But you get what you pay for.

author: HwAoRrDk
author_rank: Super Contributor
timestamp: January 07, 2023, 12:46:43 am
content: Quote from: globoy on January 06, 2023, 11:48:26 pmOh, they can, can they? I always thought that pin muxing on the ESP32 was just for general-purpose IO, and not available for most peripherals.Serves me right for not looking at the documentation before assuming.Although, it still doesn't help me much given that practically all pins are already in use.So I guess my original questions still stand.

author: Geoff-AU
author_rank: Frequent Contributor
timestamp: January 07, 2023, 05:22:44 am
content: ESP32 has a GPIO matrix for low speed IO and also an IO_MUX for high speed stuff, with more limited flexibility.I agree that Espressif's logic is a bit odd. The SPI program flash clobbers the pins labelled SD_DataX, so don't make the mistake of just wiring things up the logical way and expecting it to work. To get an SD card connected you need to use Function 4 of the IO MuxQuote from: HwAoRrDk on January 07, 2023, 12:46:43 amGenerally when you run out of IO you start hanging expanders off the SPI/I2C bus (either a GPIO expander or a UART or both, some UARTs also come with a few general IO)

author: feecat
author_rank: Newbie
timestamp: January 07, 2023, 06:15:56 am
content: You can use any gpio as RTS/CTS, IO have about 10MHz speed, just like this, GPIO14 is RTS.UART on esp32 will got delay when high baudrate, so i need directly write txfifo for minimum delay.Code:if (uart_length > 0){//we have message need sendGPIO.out_w1ts = (1 << GPIO_NUM_14);uart_ll_write_txfifo(&UART2, uart_buffer, uart_length);while (UART2.status.st_utx_out > 0){//FIXME: maybe infinity loop here}if (uart_length == 1) //if just one byte package, we need add a delayets_delay_us(8);GPIO.out_w1tc = (1 << GPIO_NUM_14);}

author: Psi
author_rank: Super Contributor
timestamp: January 07, 2023, 06:52:41 am
content: Quote from: HwAoRrDk on January 07, 2023, 12:46:43 amWhy would you want to remap general-purpose IO, there wouldn't be any point.Remapping IO is typically for peripherals so you can have more available peripherals than you have physicals pins. This makes the MCU useful to a wider range of people with different needs

author: HwAoRrDk
author_rank: Super Contributor
timestamp: January 07, 2023, 02:47:48 pm
content: Quote from: Geoff-AU on January 07, 2023, 05:22:44 amHmm, yes, I suppose external SPI/I2C UARTs might be a thing. Hadn't occurred to me as I assumed discrete UART ICs were archaic, or would only have parallel interfaces. I could do that - already have some other I2C and SPI devices. But having a quick look, they're all pretty expensive, and many are dual/quad which is unnecessary for my needs.Does anyone know of any SPI/I2C UARTs with integrated RS-485 transceiver? Could be worth the price if two functions reside in a single chip.

author: HwAoRrDk
author_rank: Super Contributor
timestamp: January 08, 2023, 12:30:47 pm
content: Quote from: HwAoRrDk on January 07, 2023, 02:47:48 pmHaha, I found one: the MaximMAX3140, an SPI UART with integrated RS485 transceiver. But the price! $10+ each!No thanks.

author: bidrohini
author_rank: Regular Contributor
timestamp: January 08, 2023, 02:07:03 pm
content: https://forum.arduino.cc/t/esp32-using-usart1/890225/4This thread says that it is possible to remap the USART1 pins of ESP32. I never tried that personally.

author: spostma
author_rank: Regular Contributor
timestamp: January 09, 2023, 11:02:50 pm
content: maybe these will suit your needs:SC16IS740_750_760 Single UART with I2C or SPI Interface and IrDA and RS85 supportSC16IS752_762 DUAL UART with I2C or SPI Interface and IrDA and RS85 supportlibraries:https://github.com/nopnop2002/esp-idf-sc16is750https://community.nxp.com/t5/Other-NXP-Products-Knowledge/SC16IS752-Bare-metal-example-project/ta-p/1105691https://github.com/agronaut/SC16IS752-micropython-librarymodule for experimenting:https://www.aliexpress.com/item/1005004682656027.htmlhttps://www.aliexpress.com/item/1005005076107445.html