rs485_example.c 4.0 KB

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