pile_com_stm32/main/stm32/uart0_modbus_slave.c
2024-02-19 10:15:04 +08:00

134 lines
4.3 KiB
C

/* 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)
#define UART_READ_TOUT (50 / portTICK_PERIOD_MS)
#define LED1_GPIO_PIN 9
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);
extern uint16_t gWordVar[];
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;
}
}
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
//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));
// 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 uart0_modbus_slave_task(void *arg)
{
uart0_init();
// uint8_t count = 0;
while (1)
{
// ESP_LOGI("modbus_slave","enter while");
// Read data from UART
int txlen = 0;
int len = uart_read_bytes(UART_PORT_NUM, rxbuf, BUF_SIZE, UART_READ_TOUT);
/*uint32_t tick = */xTaskGetTickCount();
// Write data back to UART
if (len > 0)
{
ESP_LOGI("modbus_slave","len = %d",len);
// 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)
{
ESP_LOGI("modbus_slave","txlen = %d",txlen);
LED1_Toggle();
int uart_write_length = uart_write_bytes(UART_PORT_NUM, txbuf, txlen);
ESP_LOGI("modbus_slave","uart_write_length = %d",uart_write_length);
ESP_LOGI("modbus_slave","gWordVar[12] = 0x%04x",gWordVar[12]);
}
}
// 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)
{
xTaskCreate(uart0_modbus_slave_task, "uart0_modbus_slave_task", 4096, NULL, 10, NULL);
}