Using UART0 and UART2 concurrently

S23_IDS
Posts: 1
Joined: Mon May 20, 2024 9:29 am

Using UART0 and UART2 concurrently

Postby S23_IDS » Thu Feb 27, 2025 1:21 pm

Greetings

I am using the ESP32-WROOM-32E NodeMCU board and I am using UART2 to read and write to another device. I am also getting logs using the UART0 pins. My configuration for UART2 is as follows:
  1. uart_port_t uart_tx_panel = UART_NUM_2;
  2.  
  3. static void init(void) {
  4.     const uart_config_t uart_config = {
  5.         .baud_rate = 19200,
  6.         .data_bits = UART_DATA_8_BITS,
  7.         .parity = UART_PARITY_DISABLE,
  8.         .stop_bits = UART_STOP_BITS_1,
  9.         .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  10.         .source_clk = UART_SCLK_DEFAULT,
  11.     };
  12.     // We won't use a buffer for sending data.
  13.     uart_driver_install(uart_tx_panel, RX_BUF_SIZE * 2, 0, 0, NULL, 0);
  14.     uart_param_config(uart_tx_panel, &uart_config);
  15.     uart_set_pin(uart_tx_panel, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
  16.  
  17.  
  18. }
I am using FreeRTOS and I never receive anything on UART2. I haven't configured anything regarding UART0 as I just use the ESP_LOG functions. Could there be some issue in using UART2?

teletypeguy
Posts: 10
Joined: Sat Feb 22, 2025 7:21 pm

Re: Using UART0 and UART2 concurrently

Postby teletypeguy » Fri Feb 28, 2025 3:39 am

Well, you set all four pins to "not-used" when you used:
UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE
(I think uart_pin_no_change is ambiguous and should something like uart_pin_not_used or something, but it is what it is)

I leave uart-0 system messages on the usb port (which I only use for flashing and testing), but re-purpose the uart pins. I also use uart1 and uart2 streaming at 230400:
  1. // TTY_UART is USB-serial port 0 --  console/debug at 115200-baud:
  2. #define     TTY_UART_PORT                   UART_NUM_0
  3. #define     TTY_UART_BAUD                   (115200)
  4. #define     TTY_UART_RXBUF_SIZE             (160)                   // hw fifo fills this via driver; must be > 128
  5. #define     TTY_UART_TXBUF_SIZE             (256)                   //
  6. #define     TTY_UART_TX_PIN                 UART_PIN_NO_CHANGE      // (for usb, no pin change)
  7. #define     TTY_UART_RX_PIN                 UART_PIN_NO_CHANGE      // (for usb, no pin change)
  8.  
  9. // ADC_UART is serial port 1 -- ADC interface at 230400-baud:
  10. #define     ADC_UART_PORT                   UART_NUM_1
  11. #define     ADC_UART_BAUD                   (230400)
  12. #define     ADC_UART_RXBUF_SIZE             (160)                   // hw fifo fills this via driver; must be > 128
  13. #define     ADC_UART_TXBUF_SIZE             (256)
  14. #define     ADC_UART_TX_PIN                 (GPIO_NUM_3)
  15. #define     ADC_UART_RX_PIN                 (GPIO_NUM_8)
  16. ////#define  SEND_STR_TO_ADC(s)  uart_write_bytes(ADC_UART_PORT, (const char *)(s), strlen((const char *)(s)))
  17.  
  18. // HOST_UART is serial port 2 -- PC interface at 230400-baud:
  19. #define     HOST_UART_PORT                  UART_NUM_2
  20. #define     HOST_UART_BAUD                  (230400)
  21. #define     HOST_UART_RXBUF_SIZE            (160)                   // hw fifo fills this via driver; must be > 128
  22. #define     HOST_UART_TXBUF_SIZE            (256)
  23. #define     HOST_UART_TX_PIN                (GPIO_NUM_16)
  24. #define     HOST_UART_RX_PIN                (GPIO_NUM_15)
  25. ////#define SEND_TO_HOST(s  uart_write_bytes(HOST_UART_PORT, (const char *)(s), strlen((const char *)(s)))

I have init funcs like this:
  1. //------------------------------------------------------------------------------
  2. /**
  3.     Inits HOST_UART and starts ISR
  4.  
  5.     @param      void *parameter
  6.     @return     void
  7. */
  8.  
  9. void host_uart_init_task(void *parameter)
  10. {
  11. #if defined (HOST_UART_PORT)
  12.     while (1)
  13.     {
  14.         // --- install UART driver/isr and set UART configuration:
  15.         //          Note: UART ISR handler will be attached to the same CPU core on which this function is running.
  16.         //
  17.         // params for uart_driver_install(...)
  18.         //      UART port number
  19.         //      RX ring buffer size -- should be greater than UART_HW_FIFO_LEN(uart_num) (128 currently)
  20.         //      TX ring buffer size -- should be either zero or greater than UART_HW_FIFO_LEN(uart_num) (128 currently)
  21.         //              (if zero, driver will not use buf and will block task until all data sent)
  22.         //      UART event queue size/depth
  23.         //      UART event queue handle (new queue handle is written here to provide access to UART events)
  24.         //                              (if set to NULL, driver will not use an event queue)
  25.         //      Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h.
  26.         //              (do not set ESP_INTR_FLAG_IRAM here since the driver's ISR handler is not located in IRAM)
  27.         ESP_ERROR_CHECK(uart_driver_install(HOST_UART_PORT, HOST_UART_RXBUF_SIZE, HOST_UART_TXBUF_SIZE, 0, NULL, 0));
  28.  
  29.         // config the port:
  30.         uart_config_t uart_config =
  31.         {
  32.             .baud_rate = HOST_UART_BAUD,
  33.             .data_bits = UART_DATA_8_BITS,
  34.             .parity    = UART_PARITY_DISABLE,
  35.             .stop_bits = UART_STOP_BITS_1,
  36.             .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  37.             .source_clk = UART_SCLK_DEFAULT,
  38.             .rx_flow_ctrl_thresh = 122,
  39.         };
  40.         ESP_ERROR_CHECK(uart_param_config(HOST_UART_PORT, &uart_config));
  41.  
  42.         ESP_ERROR_CHECK(uart_set_pin(HOST_UART_PORT, HOST_UART_TX_PIN, HOST_UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
  43.         ESP_ERROR_CHECK(uart_flush(HOST_UART_PORT));
  44.  
  45.         // and kill this task:
  46.         vTaskDelete(NULL);
  47.     }
  48. #endif
  49. }

From app_main I start the uarts (uart0 on core 0 with other system stuff, and others I put on core 1):
  1.     g_log(TAG, "----- Init StreamBuffers, ADC_UART, and HOST_UART");
  2.     init_streambuffers();
  3.     xTaskCreatePinnedToCore(adc_uart_init_task, "adc_uart_init_task", STACK_SIZE_UART_INIT_TASK, NULL, PRIORITY_FROM_ADCUART_TASK, NULL, CORE_ADC_UART_TASK);
  4.     vTaskDelay(pdMS_TO_TICKS(50));
  5.     xTaskCreatePinnedToCore(host_uart_init_task, "host_uart_init_task", STACK_SIZE_UART_INIT_TASK, NULL, PRIORITY_FROM_HOSTUART_TASK, NULL, CORE_HOST_UART_TASK);
  6.     vTaskDelay(pdMS_TO_TICKS(50));

Who is online

Users browsing this forum: No registered users and 90 guests