2017-09-27 16:03:22 -04:00
|
|
|
/* UART Echo Example
|
2017-03-22 00:36:11 -04: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"
|
|
|
|
|
|
|
|
/**
|
2017-09-27 16:03:22 -04:00
|
|
|
* This is an example which echos any data it receives on UART1 back to the sender,
|
|
|
|
* with hardware flow control turned off. It does not use UART driver event queue.
|
2017-03-22 00:36:11 -04:00
|
|
|
*
|
2017-09-27 16:03:22 -04:00
|
|
|
* - Port: UART1
|
|
|
|
* - Receive (Rx) buffer: on
|
|
|
|
* - Transmit (Tx) buffer: off
|
|
|
|
* - Flow control: off
|
|
|
|
* - Event queue: off
|
|
|
|
* - Pin assignment: see defines below
|
2017-03-22 00:36:11 -04:00
|
|
|
*/
|
|
|
|
|
2017-09-27 16:03:22 -04:00
|
|
|
#define ECHO_TEST_TXD (GPIO_NUM_4)
|
|
|
|
#define ECHO_TEST_RXD (GPIO_NUM_5)
|
|
|
|
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
|
|
|
|
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
|
2017-03-22 00:36:11 -04:00
|
|
|
|
|
|
|
#define BUF_SIZE (1024)
|
|
|
|
|
|
|
|
static void echo_task()
|
|
|
|
{
|
2017-09-27 16:03:22 -04:00
|
|
|
/* Configure parameters of an UART driver,
|
|
|
|
* communication pins and install the driver */
|
2017-03-22 00:36:11 -04:00
|
|
|
uart_config_t uart_config = {
|
|
|
|
.baud_rate = 115200,
|
|
|
|
.data_bits = UART_DATA_8_BITS,
|
2017-09-27 16:03:22 -04:00
|
|
|
.parity = UART_PARITY_DISABLE,
|
2017-03-22 00:36:11 -04:00
|
|
|
.stop_bits = UART_STOP_BITS_1,
|
2017-09-27 16:03:22 -04:00
|
|
|
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
|
2017-03-22 00:36:11 -04:00
|
|
|
};
|
2017-09-27 16:03:22 -04:00
|
|
|
uart_param_config(UART_NUM_1, &uart_config);
|
|
|
|
uart_set_pin(UART_NUM_1, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
|
|
|
|
uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);
|
|
|
|
|
|
|
|
// Configure a temporary buffer for the incoming data
|
|
|
|
uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// Read data from the UART
|
|
|
|
int len = uart_read_bytes(UART_NUM_1, data, BUF_SIZE, 20 / portTICK_RATE_MS);
|
|
|
|
// Write data back to the UART
|
|
|
|
uart_write_bytes(UART_NUM_1, (const char *) data, len);
|
2017-03-22 00:36:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_main()
|
|
|
|
{
|
|
|
|
xTaskCreate(echo_task, "uart_echo_task", 1024, NULL, 10, NULL);
|
|
|
|
}
|