uart_echo_example_main.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* UART Echo Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "driver/uart.h"
  11. #include "driver/gpio.h"
  12. #include "sdkconfig.h"
  13. #include "esp_log.h"
  14. /**
  15. * This is an example which echos any data it receives on configured UART back to the sender,
  16. * with hardware flow control turned off. It does not use UART driver event queue.
  17. *
  18. * - Port: configured UART
  19. * - Receive (Rx) buffer: on
  20. * - Transmit (Tx) buffer: off
  21. * - Flow control: off
  22. * - Event queue: off
  23. * - Pin assignment: see defines below (See Kconfig)
  24. */
  25. #define ECHO_TEST_TXD (CONFIG_EXAMPLE_UART_TXD)
  26. #define ECHO_TEST_RXD (CONFIG_EXAMPLE_UART_RXD)
  27. #define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
  28. #define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
  29. #define ECHO_UART_PORT_NUM (CONFIG_EXAMPLE_UART_PORT_NUM)
  30. #define ECHO_UART_BAUD_RATE (CONFIG_EXAMPLE_UART_BAUD_RATE)
  31. #define ECHO_TASK_STACK_SIZE (CONFIG_EXAMPLE_TASK_STACK_SIZE)
  32. static const char *TAG = "UART TEST";
  33. #define BUF_SIZE (1024)
  34. static void echo_task(void *arg)
  35. {
  36. /* Configure parameters of an UART driver,
  37. * communication pins and install the driver */
  38. uart_config_t uart_config = {
  39. .baud_rate = ECHO_UART_BAUD_RATE,
  40. .data_bits = UART_DATA_8_BITS,
  41. .parity = UART_PARITY_DISABLE,
  42. .stop_bits = UART_STOP_BITS_1,
  43. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  44. .source_clk = UART_SCLK_DEFAULT,
  45. };
  46. int intr_alloc_flags = 0;
  47. #if CONFIG_UART_ISR_IN_IRAM
  48. intr_alloc_flags = ESP_INTR_FLAG_IRAM;
  49. #endif
  50. ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
  51. ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &uart_config));
  52. ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS));
  53. // Configure a temporary buffer for the incoming data
  54. uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
  55. while (1) {
  56. // Read data from the UART
  57. int len = uart_read_bytes(ECHO_UART_PORT_NUM, data, (BUF_SIZE - 1), 20 / portTICK_PERIOD_MS);
  58. // Write data back to the UART
  59. uart_write_bytes(ECHO_UART_PORT_NUM, (const char *) data, len);
  60. if (len) {
  61. data[len] = '\0';
  62. ESP_LOGI(TAG, "Recv str: %s", (char *) data);
  63. }
  64. }
  65. }
  66. void app_main(void)
  67. {
  68. xTaskCreate(echo_task, "uart_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL);
  69. }