#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "esp_log.h"
/**
* This is an example which echos any data it receives on configured UART back to the sender,
* with hardware flow control turned off. It does not use UART driver event queue.
*
* - Port: configured UART
* - Receive (Rx) buffer: on
* - Transmit (Tx) buffer: off
* - Flow control: off
* - Event queue: off
*/
static const char *TAG = "UART TEST";
#define UART_PORT UART_NUM_0
#define BUF_SIZE (1024)
void setup_uart(void);
void echo_task(void*);
void app_main(void)
{
xTaskCreate(echo_task, "uart_echo_task", 1024, NULL, 10, NULL);
}
void echo_task(void *arg)
{
const uint8_t uart_num = UART_PORT;
setup_uart();
// Configure a temporary buffer for the incoming data
uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
int len;
while (1) {
// Read data from the UART
len = uart_read_bytes(uart_num, data, (BUF_SIZE - 1), 20 / portTICK_PERIOD_MS);
if (len > 0) {
// Write data back to the UART
uart_write_bytes(uart_num, data, len);
//data[len] = '\0';
//ESP_LOGI(TAG, "Recv str: %s", (char *) data);
}
}
}
void setup_uart(void)
{
/* Configure parameters of an UART driver,
* communication pins and install the driver */
const uint8_t uart_num = UART_PORT;
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,
.source_clk = UART_SCLK_DEFAULT,
};
int intr_alloc_flags = 0;
#if CONFIG_UART_ISR_IN_IRAM
intr_alloc_flags = ESP_INTR_FLAG_IRAM;
#endif
ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
ESP_ERROR_CHECK(uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
}