From 479ba94ef716a7c093f47b885444fdeb8a684d50 Mon Sep 17 00:00:00 2001 From: Pieter du Preez Date: Sun, 1 Oct 2017 13:02:06 +0200 Subject: [PATCH 1/2] Added an asynchronous UART example, using separate RX and TX tasks. Signed-off-by: krzychb --- .../peripherals/uart_async_rxtxtasks/Makefile | 8 ++ .../uart_async_rxtxtasks/README.md | 11 +++ .../uart_async_rxtxtasks/main/component.mk | 3 + .../main/uart_async_rxtxtasks_main.c | 74 +++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 examples/peripherals/uart_async_rxtxtasks/Makefile create mode 100644 examples/peripherals/uart_async_rxtxtasks/README.md create mode 100644 examples/peripherals/uart_async_rxtxtasks/main/component.mk create mode 100644 examples/peripherals/uart_async_rxtxtasks/main/uart_async_rxtxtasks_main.c diff --git a/examples/peripherals/uart_async_rxtxtasks/Makefile b/examples/peripherals/uart_async_rxtxtasks/Makefile new file mode 100644 index 000000000..250ca7316 --- /dev/null +++ b/examples/peripherals/uart_async_rxtxtasks/Makefile @@ -0,0 +1,8 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := uart_async_rxtxtasks + +include $(IDF_PATH)/make/project.mk diff --git a/examples/peripherals/uart_async_rxtxtasks/README.md b/examples/peripherals/uart_async_rxtxtasks/README.md new file mode 100644 index 000000000..6647a00e4 --- /dev/null +++ b/examples/peripherals/uart_async_rxtxtasks/README.md @@ -0,0 +1,11 @@ +UART asynchronous example, that uses separate RX and TX tasks +============================================================= + +Starts two FreeRTOS tasks: + - One task for transmitting 'Hello world' via the UART. + - One task for receiving from the UART. + +If you'd like to see your ESP32 receive something, simply short +TXD_PIN and RXD_PIN. By doing this data transmitted on TXD_PIN will +be received on RXD_PIN. See the definitions of TXD_PIN and RXD_PIN +in ./main/uart_async_rxtxtasks_main.c. diff --git a/examples/peripherals/uart_async_rxtxtasks/main/component.mk b/examples/peripherals/uart_async_rxtxtasks/main/component.mk new file mode 100644 index 000000000..44bd2b527 --- /dev/null +++ b/examples/peripherals/uart_async_rxtxtasks/main/component.mk @@ -0,0 +1,3 @@ +# +# Main Makefile. This is basically the same as a component makefile. +# diff --git a/examples/peripherals/uart_async_rxtxtasks/main/uart_async_rxtxtasks_main.c b/examples/peripherals/uart_async_rxtxtasks/main/uart_async_rxtxtasks_main.c new file mode 100644 index 000000000..044faafd4 --- /dev/null +++ b/examples/peripherals/uart_async_rxtxtasks/main/uart_async_rxtxtasks_main.c @@ -0,0 +1,74 @@ +/* UART asynchronous example, that uses separate RX and TX tasks + + 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 "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_system.h" +#include "esp_log.h" +#include "driver/uart.h" +#include "soc/uart_struct.h" +#include "string.h" + +static const int RX_BUF_SIZE = 1024; + +#define TXD_PIN (GPIO_NUM_4) +#define RXD_PIN (GPIO_NUM_5) + +void init() { + const 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 + }; + uart_param_config(UART_NUM_1, &uart_config); + uart_set_pin(UART_NUM_1, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); + // We won't use a buffer for sending data. + uart_driver_install(UART_NUM_1, RX_BUF_SIZE * 2, 0, 0, NULL, 0); +} + +int sendData(const char* logName, const char* data) +{ + const int len = strlen(data); + const int txBytes = uart_write_bytes(UART_NUM_1, data, len); + ESP_LOGI(logName, "Wrote %d bytes", txBytes); + return txBytes; +} + +static void tx_task() +{ + static const char *TX_TASK_TAG = "TX_TASK"; + esp_log_level_set(TX_TASK_TAG, ESP_LOG_INFO); + while (1) { + sendData(TX_TASK_TAG, "Hello world"); + vTaskDelay(2000 / portTICK_PERIOD_MS); + } +} + +static void rx_task() +{ + static const char *RX_TASK_TAG = "RX_TASK"; + esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO); + uint8_t* data = (uint8_t*) malloc(RX_BUF_SIZE+1); + while (1) { + const int rxBytes = uart_read_bytes(UART_NUM_1, data, RX_BUF_SIZE, 1000 / portTICK_RATE_MS); + if (rxBytes > 0) { + data[rxBytes] = 0; + ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data); + } + } + free(data); +} + +void app_main() +{ + init(); + xTaskCreate(rx_task, "uart_rx_task", 1024*2, NULL, configMAX_PRIORITIES, NULL); + xTaskCreate(tx_task, "uart_tx_task", 1024*2, NULL, configMAX_PRIORITIES-1, NULL); +} From 61dcade807d356349982cf4825199735e9a87010 Mon Sep 17 00:00:00 2001 From: krzychb Date: Thu, 19 Oct 2017 21:45:58 +0200 Subject: [PATCH 2/2] Print out in hex format in case non printable data are received --- examples/peripherals/uart_async_rxtxtasks/README.md | 10 ++++++++++ .../main/uart_async_rxtxtasks_main.c | 1 + 2 files changed, 11 insertions(+) diff --git a/examples/peripherals/uart_async_rxtxtasks/README.md b/examples/peripherals/uart_async_rxtxtasks/README.md index 6647a00e4..839d86bff 100644 --- a/examples/peripherals/uart_async_rxtxtasks/README.md +++ b/examples/peripherals/uart_async_rxtxtasks/README.md @@ -9,3 +9,13 @@ If you'd like to see your ESP32 receive something, simply short TXD_PIN and RXD_PIN. By doing this data transmitted on TXD_PIN will be received on RXD_PIN. See the definitions of TXD_PIN and RXD_PIN in ./main/uart_async_rxtxtasks_main.c. + +The output for such configuration will look as follows: + +``` +I (3261) TX_TASK: Wrote 11 bytes +I (4261) RX_TASK: Read 11 bytes: 'Hello world' +I (4261) RX_TASK: 0x3ffb821c 48 65 6c 6c 6f 20 77 6f 72 6c 64 |Hello world| +... +``` +The third line above prints received data in hex format, that comes handy to display non printable data bytes. \ No newline at end of file diff --git a/examples/peripherals/uart_async_rxtxtasks/main/uart_async_rxtxtasks_main.c b/examples/peripherals/uart_async_rxtxtasks/main/uart_async_rxtxtasks_main.c index 044faafd4..b3129f0ef 100644 --- a/examples/peripherals/uart_async_rxtxtasks/main/uart_async_rxtxtasks_main.c +++ b/examples/peripherals/uart_async_rxtxtasks/main/uart_async_rxtxtasks_main.c @@ -61,6 +61,7 @@ static void rx_task() if (rxBytes > 0) { data[rxBytes] = 0; ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data); + ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, data, rxBytes, ESP_LOG_INFO); } } free(data);