rs485_example.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Uart Events 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 <string.h>
  9. #include <stdlib.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_system.h"
  13. #include "nvs_flash.h"
  14. #include "driver/uart.h"
  15. #include "freertos/queue.h"
  16. #include "esp_log.h"
  17. #include "soc/uart_struct.h"
  18. /**
  19. * This is a example example which echos any data it receives on UART back to the sender.
  20. *
  21. * - port: UART2
  22. * - rx buffer: on
  23. * - tx buffer: off
  24. * - flow control: off
  25. *
  26. * This example has been tested on a 3 node RS485 Serial Bus
  27. *
  28. */
  29. // Note: UART2 default pins IO16, IO17 do not work on ESP32-WROVER module
  30. // because these pins connected to PSRAM
  31. #define ECHO_TEST_TXD (23)
  32. #define ECHO_TEST_RXD (22)
  33. // RTS for RS485 Half-Duplex Mode manages DE/~RE
  34. #define ECHO_TEST_RTS (18)
  35. // CTS is not used in RS485 Half-Duplex Mode
  36. #define ECHO_TEST_CTS UART_PIN_NO_CHANGE
  37. #define BUF_SIZE (127)
  38. #define BAUD_RATE (115200)
  39. // Read packet timeout
  40. #define PACKET_READ_TICS (100 / portTICK_RATE_MS)
  41. #define ECHO_TASK_STACK_SIZE (2048)
  42. #define ECHO_TASK_PRIO (10)
  43. #define ECHO_UART_PORT (UART_NUM_2)
  44. static const char *TAG = "RS485_ECHO_APP";
  45. // An example of echo test with hardware flow control on UART
  46. static void echo_task()
  47. {
  48. const int uart_num = ECHO_UART_PORT;
  49. uart_config_t uart_config = {
  50. .baud_rate = BAUD_RATE,
  51. .data_bits = UART_DATA_8_BITS,
  52. .parity = UART_PARITY_DISABLE,
  53. .stop_bits = UART_STOP_BITS_1,
  54. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  55. .rx_flow_ctrl_thresh = 122,
  56. };
  57. // Set UART log level
  58. esp_log_level_set(TAG, ESP_LOG_INFO);
  59. ESP_LOGI(TAG, "Start RS485 application test and configure UART.");
  60. // Configure UART parameters
  61. uart_param_config(uart_num, &uart_config);
  62. ESP_LOGI(TAG, "UART set pins, mode and install driver.");
  63. // Set UART1 pins(TX: IO23, RX: I022, RTS: IO18, CTS: IO19)
  64. uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
  65. // Install UART driver (we don't need an event queue here)
  66. // In this example we don't even use a buffer for sending data.
  67. uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0);
  68. // Set RS485 half duplex mode
  69. uart_set_mode(uart_num, UART_MODE_RS485_HALF_DUPLEX);
  70. // Allocate buffers for UART
  71. uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
  72. ESP_LOGI(TAG, "UART start recieve loop.\r\n");
  73. uart_write_bytes(uart_num, "Start RS485 UART test.\r\n", 24);
  74. while(1) {
  75. //Read data from UART
  76. int len = uart_read_bytes(uart_num, data, BUF_SIZE, PACKET_READ_TICS);
  77. //Write data back to UART
  78. if (len > 0) {
  79. uart_write_bytes(uart_num, "\r\n", 2);
  80. char prefix[] = "RS485 Received: [";
  81. uart_write_bytes(uart_num, prefix, (sizeof(prefix) - 1));
  82. ESP_LOGI(TAG, "Received %u bytes:", len);
  83. printf("[ ");
  84. for (int i = 0; i < len; i++) {
  85. printf("0x%.2X ", (uint8_t)data[i]);
  86. uart_write_bytes(uart_num, (const char*)&data[i], 1);
  87. // Add a Newline character if you get a return charater from paste (Paste tests multibyte receipt/buffer)
  88. if (data[i] == '\r') {
  89. uart_write_bytes(uart_num, "\n", 1);
  90. }
  91. }
  92. printf("] \n");
  93. uart_write_bytes(uart_num, "]\r\n", 3);
  94. } else {
  95. // Echo a "." to show we are alive while we wait for input
  96. uart_write_bytes(uart_num, ".", 1);
  97. }
  98. }
  99. }
  100. void app_main()
  101. {
  102. //A uart read/write example without event queue;
  103. xTaskCreate(echo_task, "uart_echo_task", ECHO_TASK_STACK_SIZE, NULL, ECHO_TASK_PRIO, NULL);
  104. }