Files
platform-espressif32/examples/espidf-peripherals-uart/src/uart_echo_example_main.c
T

83 lines
2.7 KiB
C
Raw Normal View History

2018-05-05 20:34:19 +03:00
/* UART Echo Example
2017-02-19 21:23:59 +02:00
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
2020-03-05 11:18:07 +02:00
#include "driver/gpio.h"
2023-01-04 20:05:31 +02:00
#include "sdkconfig.h"
#include "esp_log.h"
2017-02-19 21:23:59 +02:00
/**
2023-01-04 20:05:31 +02:00
* This is an example which echos any data it receives on configured UART back to the sender,
2018-05-05 20:34:19 +03:00
* with hardware flow control turned off. It does not use UART driver event queue.
2017-02-19 21:23:59 +02:00
*
2023-01-04 20:05:31 +02:00
* - Port: configured UART
2018-05-05 20:34:19 +03:00
* - Receive (Rx) buffer: on
* - Transmit (Tx) buffer: off
* - Flow control: off
* - Event queue: off
2023-01-04 20:05:31 +02:00
* - Pin assignment: see defines below (See Kconfig)
2017-02-19 21:23:59 +02:00
*/
2023-01-04 20:05:31 +02:00
#define ECHO_TEST_TXD (CONFIG_EXAMPLE_UART_TXD)
#define ECHO_TEST_RXD (CONFIG_EXAMPLE_UART_RXD)
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
#define ECHO_UART_PORT_NUM (CONFIG_EXAMPLE_UART_PORT_NUM)
#define ECHO_UART_BAUD_RATE (CONFIG_EXAMPLE_UART_BAUD_RATE)
#define ECHO_TASK_STACK_SIZE (CONFIG_EXAMPLE_TASK_STACK_SIZE)
static const char *TAG = "UART TEST";
2017-02-19 21:23:59 +02:00
2018-05-05 20:34:19 +03:00
#define BUF_SIZE (1024)
2017-02-19 21:23:59 +02:00
2020-09-01 21:48:00 +03:00
static void echo_task(void *arg)
2017-02-19 21:23:59 +02:00
{
2018-05-05 20:34:19 +03:00
/* Configure parameters of an UART driver,
* communication pins and install the driver */
2017-02-19 21:23:59 +02:00
uart_config_t uart_config = {
2023-01-04 20:05:31 +02:00
.baud_rate = ECHO_UART_BAUD_RATE,
2017-02-19 21:23:59 +02:00
.data_bits = UART_DATA_8_BITS,
2018-05-05 20:34:19 +03:00
.parity = UART_PARITY_DISABLE,
2017-02-19 21:23:59 +02:00
.stop_bits = UART_STOP_BITS_1,
2020-09-01 21:48:00 +03:00
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
2023-01-04 20:05:31 +02:00
.source_clk = UART_SCLK_DEFAULT,
2017-02-19 21:23:59 +02:00
};
2023-01-04 20:05:31 +02:00
int intr_alloc_flags = 0;
#if CONFIG_UART_ISR_IN_IRAM
intr_alloc_flags = ESP_INTR_FLAG_IRAM;
#endif
ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, 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));
2018-05-05 20:34:19 +03:00
// Configure a temporary buffer for the incoming data
uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
while (1) {
// Read data from the UART
2023-01-04 20:05:31 +02:00
int len = uart_read_bytes(ECHO_UART_PORT_NUM, data, (BUF_SIZE - 1), 20 / portTICK_PERIOD_MS);
2018-05-05 20:34:19 +03:00
// Write data back to the UART
2023-01-04 20:05:31 +02:00
uart_write_bytes(ECHO_UART_PORT_NUM, (const char *) data, len);
if (len) {
data[len] = '\0';
ESP_LOGI(TAG, "Recv str: %s", (char *) data);
}
2017-02-19 21:23:59 +02:00
}
}
2020-09-01 21:48:00 +03:00
void app_main(void)
2017-02-19 21:23:59 +02:00
{
2023-01-04 20:05:31 +02:00
xTaskCreate(echo_task, "uart_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL);
2018-05-05 20:34:19 +03:00
}