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,
- https://docs.espressif.com/projects/esp ... munication
- https://github.com/espressif/esp-idf/bl ... ple_main.c
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
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.