/* UART Echo Example 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 #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/uart.h" #include "driver/gpio.h" #include "sdkconfig.h" #include "esp_log.h" #include "uart.h" /** * This is an example which echos any data it receives on configured UART back to the sender, * with hardware flow control turned off. It does not use UART driver event queue. * * - Port: configured UART * - Receive (Rx) buffer: on * - Transmit (Tx) buffer: off * - Flow control: off * - Event queue: off * - Pin assignment: see defines below (See Kconfig) */ #define UART_TXD_PIN 11 #define UART_RXD_PIN 13 #define UART_RTS_PIN 12 #define UART_CTS_PIN (-1) static const char *TAG = "UART1"; #define BAUD_RATE (9600) #define UART_PORT_NUM (1) #define RX_BUF_SIZE (256) #define TX_BUF_SIZE (256) #define UART_READ_TOUT (5 / portTICK_PERIOD_MS) static void uart1_rs485_init(void) { uart_config_t uart_config = { .baud_rate = BAUD_RATE, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, .rx_flow_ctrl_thresh = 122, .source_clk = UART_SCLK_APB, }; ESP_LOGI(TAG, "Start RS485 application test and configure UART."); // Install UART driver (we don't need an event queue here) // In this example we don't even use a buffer for sending data. ESP_ERROR_CHECK(uart_driver_install(UART_PORT_NUM, RX_BUF_SIZE, TX_BUF_SIZE, 0, NULL, 0)); // Configure UART parameters ESP_ERROR_CHECK(uart_param_config(UART_PORT_NUM, &uart_config)); ESP_LOGI(TAG, "UART set pins, mode and install driver."); // Set UART pins as per KConfig settings ESP_ERROR_CHECK(uart_set_pin(UART_PORT_NUM, UART_TXD_PIN, UART_RXD_PIN, UART_RTS_PIN, UART_CTS_PIN)); // Set RS485 half duplex mode ESP_ERROR_CHECK(uart_set_mode(UART_PORT_NUM, UART_MODE_RS485_HALF_DUPLEX)); // Set read timeout of UART TOUT feature ESP_ERROR_CHECK(uart_set_rx_timeout(UART_PORT_NUM, UART_READ_TOUT)); } static void uart0_com_poll_task(void *arg) { uint32_t last_rxtick = 0; uint8_t rx_data[RX_BUF_SIZE]; uint8_t tx_data[RX_BUF_SIZE]; while (1) { // Read data from UART int len = uart_read_bytes(UART_PORT_NUM, rx_data, RX_BUF_SIZE, UART_READ_TOUT); uint32_t tick = xTaskGetTickCount(); // Write data back to UART if (len > 0) { // ESP_LOGI(TAG, "uart_read_bytes len=%d", len); int err = com_poll_modbus_master_on_revice(0, data, len); if (err == 0) { } } if (tick - last_rxtick >= UART_READ_TOUT) { com_poll_modbus_master_poll(0); // if (++mcu_watch_dog_cnt > mcu_watch_dog_timeout) //如果通讯中断会相互重启,计数器不起作用 } } vTaskDelete(NULL); } void uart1_task_init(void) { ESP_LOGI(TAG, "uart1_com_poll_task"); // A uart read/write example without event queue; xTaskCreate(uart0_com_poll_task, "uart0_com_poll_task", 1024, NULL, 9, NULL); }