#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "esp_err.h"
#define ECHO_TEST_TXD (GPIO_NUM_43)
#define ECHO_TEST_RXD (GPIO_NUM_44)
#define UART_NUM (UART_NUM_1)
#define BUF_SIZE (1024)
void app_main(void)
{
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.rx_flow_ctrl_thresh = 122,
};
//Configure UART1 parameters
ESP_ERROR_CHECK(uart_param_config(UART_NUM, &uart_config));
//Set UART1 pins(TX: IO4, RX: I05)
uart_set_pin(UART_NUM, ECHO_TEST_TXD, ECHO_TEST_RXD, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
//Install UART driver (we don't need an event queue here)
//In this example we don't even use a buffer for sending data.
ESP_ERROR_CHECK(uart_driver_install(UART_NUM, BUF_SIZE * 2, 0, 0, NULL, 0));
uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
while (1) {
//Read data from UART
int len = uart_read_bytes(UART_NUM, data, BUF_SIZE, pdMS_TO_TICKS(20));
//Write data back to UART
uart_write_bytes(UART_NUM, (const char*) data, len);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}