2023-07-20 10:17:11 +08:00
|
|
|
/* 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 <stdio.h>
|
|
|
|
#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"
|
|
|
|
|
|
|
|
static const char *TAG = "UART0";
|
|
|
|
|
|
|
|
#define BAUD_RATE (115200)
|
|
|
|
#define UART_PORT_NUM (0)
|
|
|
|
#define BUF_SIZE (256)
|
2023-11-15 22:06:47 +08:00
|
|
|
#define UART_READ_TOUT (50 / portTICK_RATE_MS)
|
2023-07-20 10:17:11 +08:00
|
|
|
|
2023-07-20 10:50:39 +08:00
|
|
|
#define LED1_GPIO_PIN 9
|
2023-07-20 10:17:11 +08:00
|
|
|
|
|
|
|
uint8_t txbuf[BUF_SIZE];
|
|
|
|
uint8_t rxbuf[BUF_SIZE];
|
|
|
|
extern int ModbusSlaveProcess(uint8_t *txbuf, uint8_t *rxbuf, uint16_t rxLen, int is_crc);
|
|
|
|
|
|
|
|
void uart0_init(void)
|
|
|
|
{
|
|
|
|
const int uart_num = UART_PORT_NUM;
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
// Set UART log level
|
|
|
|
// esp_log_level_set(TAG, ESP_LOG_NONE);
|
|
|
|
|
|
|
|
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_num, BUF_SIZE * 2, 0, 0, NULL, 0));
|
|
|
|
|
|
|
|
// Configure UART parameters
|
|
|
|
ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
|
|
|
|
ESP_LOGI(TAG, "UART set pins, mode and install driver.");
|
|
|
|
// Set UART pins as per KConfig settings
|
2023-11-15 22:06:47 +08:00
|
|
|
//ESP_ERROR_CHECK(uart_set_pin(uart_num, UART_TXD_PIN, UART_RXD_PIN, UART_RTS_PIN, UART_CTS_PIN));之前没解开注释
|
|
|
|
//ESP_ERROR_CHECK(uart_set_pin(uart_num, 11, 13, 12, -1));
|
2023-07-20 10:17:11 +08:00
|
|
|
// Set RS485 half duplex mode
|
|
|
|
ESP_ERROR_CHECK(uart_set_mode(uart_num, UART_MODE_RS485_HALF_DUPLEX));
|
|
|
|
// Set read timeout of UART TOUT feature
|
|
|
|
ESP_ERROR_CHECK(uart_set_rx_timeout(uart_num, UART_READ_TOUT));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LED1_Toggle(void)
|
|
|
|
{
|
|
|
|
static unsigned char flg = 1;
|
|
|
|
if (flg)
|
|
|
|
{
|
|
|
|
gpio_set_level(LED1_GPIO_PIN, 0);
|
|
|
|
flg = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gpio_set_level(LED1_GPIO_PIN, 1);
|
|
|
|
flg = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-15 22:06:47 +08:00
|
|
|
void uart0_modbus_slave_task(void *arg)
|
2023-07-20 10:17:11 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
// Read data from UART
|
|
|
|
int txlen = 0;
|
|
|
|
int len = uart_read_bytes(UART_PORT_NUM, rxbuf, BUF_SIZE, UART_READ_TOUT);
|
2023-11-15 22:06:47 +08:00
|
|
|
/*uint32_t tick = */xTaskGetTickCount();
|
2023-07-20 10:17:11 +08:00
|
|
|
// Write data back to UART
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
// uart_write_bytes(UART_PORT_NUM, rxbuf, len);
|
|
|
|
// ESP_LOGI(TAG, "uart_read_bytes len=%d", len);
|
|
|
|
txlen = ModbusSlaveProcess(txbuf, rxbuf, len, 1);
|
|
|
|
if (txlen > 0)
|
|
|
|
{
|
|
|
|
LED1_Toggle();
|
|
|
|
uart_write_bytes(UART_PORT_NUM, txbuf, txlen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if (tick - last_rxtick >= PACKET_READ_TICS)
|
|
|
|
// {
|
|
|
|
// com_poll_modbus_master_poll(0);
|
|
|
|
// // if (++mcu_watch_dog_cnt > mcu_watch_dog_timeout) //如果通讯中断会相互重启,计数器不起作用
|
|
|
|
// if (gpio_get_level(0)) // cpu 启动后会拉低GPIO0
|
|
|
|
// {
|
|
|
|
// mcu_watch_dog_cnt = 0;
|
|
|
|
// if (mcu_pwr_off_cnt < 2)
|
|
|
|
// {
|
|
|
|
// mcu_pwr_off_cnt++;
|
|
|
|
// gpio_set_level(12, 1); // 3v8 off
|
|
|
|
// vTaskDelay(3000 / portTICK_RATE_MS);
|
|
|
|
// gpio_set_level(12, 0); // 3v8 off
|
|
|
|
// mcu_watch_dog_timeout = 12000 / (portTICK_RATE_MS * PACKET_READ_TICS);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// last_rxtick = tick;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart0_modbus_slave_init(void)
|
|
|
|
{
|
|
|
|
uart0_init();
|
|
|
|
xTaskCreate(uart0_modbus_slave_task, "uart0_modbus_slave_task", 4096, NULL, 10, NULL);
|
|
|
|
}
|