ESP32, UART2 uart_write_bytes, no serial output

shenanigami
Posts: 6
Joined: Sat Dec 16, 2023 10:52 am

ESP32, UART2 uart_write_bytes, no serial output

Postby shenanigami » Sun Mar 24, 2024 10:18 am

Hi

I'm using ESP32-WROOM-32U. I'm new to ESP-IDF and so decided to learn how to write a simple uart echo program.

I used the following resources,
to write a program below

Code: Select all

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

static const char *TAG = "[ECHO] ";

// use only 1 core
// CONFIG_FREERTOS_UNICORE enabled

#define ECHO_TEST_TXD GPIO_NUM_17
#define ECHO_TEST_RXD GPIO_NUM_16
// no flow control
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)

#define ECHO_UART_PORT_NUM      UART_NUM_2
#define ECHO_UART_BAUD_RATE     115200
#define ECHO_TASK_STACK_SIZE    2*1024

#define BUF_SIZE 256


static void echo_task(void *arg)
{
    /* Configure parameters of an UART driver,
     * communication pins and install the driver */
    uart_config_t uart_config = {
        .baud_rate = ECHO_UART_BAUD_RATE,
        .data_bits = UART_DATA_8_BITS,
        .parity    = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_DEFAULT,
    };
    int intr_alloc_flags = 0;

#if CONFIG_UART_ISR_IN_IRAM
    intr_alloc_flags = ESP_INTR_FLAG_IRAM;
#endif

	QueueHandle_t uart_queue;
    ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE, BUF_SIZE, 10, &uart_queue, intr_alloc_flags));
    ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &uart_config));
    ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS));

	char* test_str = "test\n\r";


    while (1) {
        uart_write_bytes(ECHO_UART_PORT_NUM, (const char *) test_str, strlen(test_str));

    }

}

void app_main(void)
{

    xTaskCreate(echo_task, "uart_echo_task", ECHO_TASK_STACK_SIZE, NULL, 1, NULL);

}

I started out with simply sending uart data from ESP32 (no uart_read_bytes; no echo yet). The program worked fine for UART_NUM_0 and the corresponding TX (GPIO 1) and RX (GPIO 3) pins. When I would use a serial communication program, like minicom,

Code: Select all

sudo minicom -D /dev/ttyUSB0
(same settings as in the program: baud rate is 115200 by default, no HW/SW flow control, etc.) the test_str got output as expected.

However, when I decided to try UART_NUM_2, with TX (17) and RX (16), no test_str was output.

I'm not sure what is wrong with the program. I can only suspect that the program is fine, but maybe when I'm running sudo minicom -D /dev/ttyUSB0 or the ESP-IDF monitor command (aka idf.py -p /dev/ttyUSB0 monitor), I'm still accessing UART0 output, and so should use something else to access UART2?

I have ESP32 connected via USB only.

ESP_Sprite
Posts: 9052
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32, UART2 uart_write_bytes, no serial output

Postby ESP_Sprite » Mon Mar 25, 2024 1:55 am

Just to check: you also re-wired your USB-to-serial thing to connect to the new GPIOs?

shenanigami
Posts: 6
Joined: Sat Dec 16, 2023 10:52 am

Re: ESP32, UART2 uart_write_bytes, no serial output

Postby shenanigami » Mon Mar 25, 2024 11:27 am

No. Is that done on config level?

When I looked for UART configs in menuconfig,

Component Config > Driver Configurations > UART Configuration

I only saw "Place UART ISR function into IRAM" config option.

ESP_Sprite
Posts: 9052
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32, UART2 uart_write_bytes, no serial output

Postby ESP_Sprite » Mon Mar 25, 2024 1:16 pm

Well, if you have a devboard, the USB-to-serial chip that is n that is likely hardwired to GPIO1 and GPIO3. In other words: if you have serial communication on GPIO16 and 17, the chip won't have any connections to those pins and won't see the serial output.

shenanigami
Posts: 6
Joined: Sat Dec 16, 2023 10:52 am

Re: ESP32, UART2 uart_write_bytes, no serial output

Postby shenanigami » Mon Mar 25, 2024 5:17 pm

So, in theory, if I connect my ESP32 board's GPIO16 (RX) and GPIO17 (TX) to my PC (while keeping its USB-to-UART0 bridge connection intact), using FTDI USB-to-TTL adapter, like so

adapter -> ESP32
============
TX -> GPIO16
RX -> GPIO17
3.3V -> 3.3V
GND -> GND

and opening its corresponding ttyUSB1 (for example) minicom session on my pc, then there should be serial output involving my test_str? (Spoiler: I don't see any serial output)

ESP_Sprite
Posts: 9052
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32, UART2 uart_write_bytes, no serial output

Postby ESP_Sprite » Tue Mar 26, 2024 2:21 am

Yes, that should happen. (Although you should *not* connect the 3.3V line, as it's an output on both the devboard and FTDI board side.)

Can you sanity-check your setup by connecting the Tx and Rx of the FTDI board together (disconnect them from the ESP32) and typing something on the minicom connected to /dev/ttyUSB1? You should see on screen what you're typing. If not, you have the wrong device node or something else is amiss.

shenanigami
Posts: 6
Joined: Sat Dec 16, 2023 10:52 am

Re: ESP32, UART2 uart_write_bytes, no serial output

Postby shenanigami » Tue Mar 26, 2024 7:21 pm

It works! Thank you so much :D

The sanity test for FTDI adapter was successful. And this time, when connecting the adapter I only connected RX and TX to ESP32.

The 3.3V -> 3.3V connection was the culprit (based off of previous setup).

Who is online

Users browsing this forum: No registered users and 185 guests