已经完成了网络,DI口,RS485的数据透传,可通过RS485接收数据
This commit is contained in:
@ -31,7 +31,7 @@
|
||||
* 调试宏定义
|
||||
*============================================================================*/
|
||||
/* DEBUG_CMD_PARSER: 调试日志开关,置1时启用解析过程日志输出 */
|
||||
#define DEBUG_CMD_PARSER 1
|
||||
#define DEBUG_CMD_PARSER 0
|
||||
|
||||
#if DEBUG_CMD_PARSER
|
||||
/* 调试日志宏,带模块前缀"[CMD]"方便过滤 */
|
||||
|
||||
@ -27,6 +27,8 @@
|
||||
#include "cmd_parser.h"
|
||||
#include "uart2_print.h"
|
||||
#include "debug_log.h"
|
||||
#include "data_source.h"
|
||||
#include "uart3_protocol_discriminator.h"
|
||||
#include "uart3_protocol_discriminator.h"
|
||||
#include "uart3_passthrough.h"
|
||||
#include "uart3_smart_router_config.h"
|
||||
@ -36,7 +38,7 @@
|
||||
* 调试宏定义
|
||||
*============================================================================*/
|
||||
/* DEBUG_CMD_ROUTER: 调试日志开关,置1时启用路由过程日志输出 */
|
||||
#define DEBUG_CMD_ROUTER 1
|
||||
#define DEBUG_CMD_ROUTER 0
|
||||
|
||||
#if DEBUG_CMD_ROUTER
|
||||
/* 路由模块日志宏,带模块前缀"[ROUTER]"方便过滤 */
|
||||
@ -375,6 +377,7 @@ void CmdRouter_Task(void)
|
||||
*----------------------------------------------------------*/
|
||||
#if UART3_SMART_ROUTING_ENABLED
|
||||
{
|
||||
LOG_DEBUG("UART3", "Protocol state: %d", UART3_Protocol_GetState());
|
||||
uint8_t byte;
|
||||
|
||||
while (MultiUART_ReadByte(PORT_UART3, &byte) > 0) {
|
||||
@ -387,6 +390,7 @@ void CmdRouter_Task(void)
|
||||
break;
|
||||
|
||||
case ROUTE_PASSTHROUGH:
|
||||
// 透传数据暂时不加包头,由timeout处理时统一加
|
||||
Passthrough_PushByte(byte);
|
||||
LOG_DEBUG("UART3", "PTX byte: 0x%02X", byte);
|
||||
break;
|
||||
@ -397,11 +401,31 @@ void CmdRouter_Task(void)
|
||||
}
|
||||
|
||||
if (UART3_Protocol_CheckTimeout(current_tick)) {
|
||||
LOG_DEBUG("UART3", "CheckTimeout triggered");
|
||||
uint8_t buffer[128];
|
||||
uint16_t length;
|
||||
if (UART3_Protocol_GetPassthroughData(buffer, &length)) {
|
||||
Passthrough_PushBuffer(buffer, length);
|
||||
LOG_INFO("UART3", "PASSTHROUGH: %d bytes queued", (int)length);
|
||||
LOG_DEBUG("UART3", "Got passthrough data, length=%d", length);
|
||||
// 构造ASCII格式的RS485数据消息
|
||||
char msg[512]; // 足够容纳128字节数据 + 消息头
|
||||
int msg_len = snprintf(msg, sizeof(msg), "$RS485_DATA,");
|
||||
// 直接追加原始数据(假设ASCII可打印)
|
||||
memcpy(msg + msg_len, buffer, length);
|
||||
msg_len += length;
|
||||
msg[msg_len] = '\0'; // 确保字符串结束
|
||||
// 计算校验和(跳过'$')
|
||||
uint8_t cs = 0;
|
||||
for (int i = 1; i < msg_len; i++) {
|
||||
cs ^= (uint8_t)msg[i];
|
||||
}
|
||||
// 追加校验和和结束符
|
||||
int appended = snprintf(msg + msg_len, sizeof(msg) - msg_len, "*%02X\r\n", cs);
|
||||
LOG_DEBUG("UART3", "Constructed message: %s", msg);
|
||||
// 发送ASCII消息
|
||||
MultiUART_SendString(PORT_UART1, msg);
|
||||
LOG_INFO("UART3", "PASSTHROUGH: %d bytes sent as ASCII message", (int)length);
|
||||
} else {
|
||||
LOG_DEBUG("UART3", "No passthrough data");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -19,7 +19,7 @@
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "multi_uart_router.h"
|
||||
#define MAX_MODULES 16
|
||||
#define MODULE_NAME_LEN 16
|
||||
|
||||
@ -144,5 +144,6 @@ void DebugLog_Output(log_level_t level, const char *module, const char *fmt, ...
|
||||
|
||||
if (len > 0) {
|
||||
UART2_Print_Send((const uint8_t *)buffer, len);
|
||||
MultiUART_Send(PORT_UART3, (const uint8_t *)buffer, len); /* 增加:同时将日志发给 UART3 */
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,6 +23,7 @@
|
||||
#include "multi_uart_router.h"
|
||||
#include "main.h"
|
||||
#include <string.h>
|
||||
#include "data_source.h"
|
||||
|
||||
/*==============================================================================
|
||||
* 调试宏定义
|
||||
@ -196,7 +197,7 @@ static void send_di_event(uint8_t channel, uint8_t state)
|
||||
uint8_t cs;
|
||||
|
||||
/* 构造消息主体,channel+1将0-base转换为1-base的用户可见编号 */
|
||||
int len = snprintf(msg, sizeof(msg), "$DI_EVENT,%d,%d*", channel + 1, state);
|
||||
int len = snprintf(msg, sizeof(msg), "$DI_EVENT,%d,%d,%d*", channel + 1, state, SOURCE_DI);
|
||||
|
||||
/* 计算异或校验和,跳过'$'符号只对正文部分计算 */
|
||||
cs = calc_checksum(msg + 1, len - 1);
|
||||
|
||||
213
Core/Src/main.c
213
Core/Src/main.c
@ -19,6 +19,7 @@
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
#include "spi.h"
|
||||
#include "tim.h"
|
||||
#include "usart.h"
|
||||
#include "gpio.h"
|
||||
|
||||
@ -39,6 +40,15 @@
|
||||
#include "cmd_router.h"
|
||||
#include "debug_log.h"
|
||||
|
||||
/* W5500 Ethernet模块头文件 */
|
||||
#if USE_W5500
|
||||
#include "user_main.h"
|
||||
#include "wiz_platform.h"
|
||||
#include "wiz_interface.h"
|
||||
#include "wizchip_conf.h"
|
||||
#include "loopback.h"
|
||||
#endif
|
||||
extern void wiz_timer_handler(void);
|
||||
#if (RF433_MODE == RF433_MODE_TX) || (RF433_MODE == RF433_MODE_BOTH)
|
||||
#include "rf433_tx_app.h"
|
||||
#endif
|
||||
@ -64,12 +74,30 @@
|
||||
/* USER CODE END PM */
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PV */
|
||||
static uint8_t uart2_rx_byte = 0;
|
||||
static uint8_t uart3_rx_byte = 0;
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* === 433 模块的接收缓存 (UART1) === */
|
||||
static uint8_t u1_rx_buffer[256];
|
||||
static volatile uint16_t u1_rx_len = 0;
|
||||
static volatile uint32_t u1_last_rx_time = 0;
|
||||
|
||||
/* === 485 设备的接收缓存 (UART3) 增加这三行!=== */
|
||||
static uint8_t u3_rx_buffer[512];
|
||||
static volatile uint16_t u3_rx_len = 0;
|
||||
static volatile uint32_t u3_last_rx_time = 0;
|
||||
|
||||
/* W5500 variables */
|
||||
#if USE_W5500
|
||||
#define SOCKET_ID 0
|
||||
#define ETHERNET_BUF_MAX_SIZE 2048
|
||||
static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {0};
|
||||
static uint16_t local_port = 8000;
|
||||
#endif
|
||||
/* USER CODE END PV */
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
/* USER CODE BEGIN PFP */
|
||||
@ -109,31 +137,62 @@ int main(void)
|
||||
MX_USART1_UART_Init();
|
||||
MX_SPI2_Init();
|
||||
MX_USART2_UART_Init();
|
||||
|
||||
MX_USART3_UART_Init();
|
||||
|
||||
MX_TIM2_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
|
||||
HAL_TIM_Base_Start_IT(&htim2);
|
||||
/* 初始化应用层模块 */
|
||||
UART2_Print_Init();
|
||||
IO_Monitor_Init();
|
||||
CmdParser_Init();
|
||||
Relay_Init();
|
||||
|
||||
|
||||
/* 初始化多通信接口统一指令处理系统 */
|
||||
MultiUART_Init();
|
||||
CmdRouter_Init();
|
||||
DebugLog_Init();
|
||||
|
||||
|
||||
/* ============================================================== */
|
||||
/* 🚀 【核心修复】将全局日志门槛提高到 INFO 级别,屏蔽所有底层 DEBUG 噪音 */
|
||||
DebugLog_SetLevel(LOG_LEVEL_INFO);
|
||||
/* ============================================================== */
|
||||
|
||||
printf("\r\n[DEBUG] 1. 启动 TIM2 中断\r\n");
|
||||
HAL_TIM_Base_Start_IT(&htim2);
|
||||
|
||||
/* 启动UART2接收中断 */
|
||||
HAL_UART_Receive_IT(&huart2, &uart2_rx_byte, 1);
|
||||
|
||||
/* 启动UART3接收中断 - RS485接口 */
|
||||
#if USE_RS485
|
||||
HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1);
|
||||
#endif
|
||||
|
||||
/* 初始化RF433模块 - 使用默认配置 */
|
||||
rf433_init(NULL);
|
||||
|
||||
|
||||
|
||||
/* 启动UART1接收 - 使用rf433_hal中的临时变量 */
|
||||
HAL_UART_Receive_IT(&huart1, &rf433_uart_rx_tmp, 1);
|
||||
|
||||
#if USE_W5500
|
||||
printf("[DEBUG] 2. 进入 wizchip_initialize()\r\n");
|
||||
wizchip_initialize();
|
||||
printf("[DEBUG] 3. wizchip_initialize() 成功返回!\r\n");
|
||||
|
||||
printf("[DEBUG] 4. 进入 network_init()\r\n");
|
||||
network_init(ethernet_buf, &default_net_info);
|
||||
printf("[DEBUG] 5. network_init() 成功返回!\r\n");
|
||||
printf("wizchip UDP example started\r\n");
|
||||
#endif
|
||||
/* ======================================= */
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* 根据配置模式初始化TX/RX应用层 */
|
||||
#if (RF433_MODE == RF433_MODE_TX) || (RF433_MODE == RF433_MODE_BOTH)
|
||||
@ -154,24 +213,11 @@ int main(void)
|
||||
printf("System Clock: %d MHz\r\n", SystemCoreClock / 1000000);
|
||||
printf("========================================\r\n");
|
||||
|
||||
/* USER CODE END 2 */
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
|
||||
/* 应用层任务处理 */
|
||||
UART2_Print_Task();
|
||||
IO_Monitor_Task();
|
||||
CmdParser_Task();
|
||||
|
||||
/* 多通信接口统一指令处理系统任务 */
|
||||
MultiUART_Task();
|
||||
CmdRouter_Task();
|
||||
|
||||
|
||||
#if (RF433_MODE == RF433_MODE_TX) || (RF433_MODE == RF433_MODE_BOTH)
|
||||
/* TX任务 */
|
||||
@ -182,11 +228,74 @@ int main(void)
|
||||
/* RX任务 */
|
||||
rf433_rx_app_task();
|
||||
#endif
|
||||
|
||||
|
||||
/* W5500 UDP回环任务 */
|
||||
#if USE_W5500
|
||||
loopback_udps(SOCKET_ID, ethernet_buf, local_port);
|
||||
#endif
|
||||
|
||||
// /* 强行拉低 M0 和 M1,叫醒 433 模块处于透传模式! */
|
||||
// HAL_GPIO_WritePin(M0_GPIO_Port, M0_Pin, GPIO_PIN_RESET);
|
||||
// HAL_GPIO_WritePin(GPIOB, M1_Pin, GPIO_PIN_RESET);
|
||||
|
||||
// /* ⚠️ 致命修复:网络被注释后,单片机启动太快。
|
||||
// 433模块从休眠到唤醒至少需要几百毫秒。
|
||||
// 必须改成 1000 毫秒,给硬件足够的缓冲时间! */
|
||||
// HAL_Delay(1000);
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
/* ==========================================================
|
||||
第一部分:恢复系统的核心驱动引擎 (解决网络和端口不传数据的问题)
|
||||
========================================================== */
|
||||
UART2_Print_Task(); // 引擎:负责把串口2的缓存发出去
|
||||
MultiUART_Task(); // 引擎:负责网络分发、多串口异步发送 (极其重要!)
|
||||
IO_Monitor_Task();
|
||||
/* 如果你的网络指令需要解析,取消下面两行的注释 */
|
||||
// CmdRouter_Task();
|
||||
// CmdParser_Task();
|
||||
|
||||
/* ==========================================================
|
||||
第二部分:网络轮询任务
|
||||
========================================================== */
|
||||
#if USE_W5500
|
||||
loopback_udps(SOCKET_ID, ethernet_buf, local_port);
|
||||
#endif
|
||||
|
||||
/* ==========================================================
|
||||
第三部分:非阻塞无乱码透传 (解决485卡顿漏数据的问题)
|
||||
========================================================== */
|
||||
|
||||
/* 1. 从 433 收到无线数据 -> 发给 485 */
|
||||
if (u1_rx_len > 0 && (HAL_GetTick() - u1_last_rx_time > 20))
|
||||
{
|
||||
/* 改用 MultiUART_Send!它会把数据瞬间丢进发送队列,耗时0毫秒,绝不卡顿单片机! */
|
||||
MultiUART_Send(PORT_UART3, (uint8_t*)u1_rx_buffer, u1_rx_len);
|
||||
|
||||
/* 顺便发给串口2监控 */
|
||||
MultiUART_Send(PORT_UART2, (uint8_t*)u1_rx_buffer, u1_rx_len);
|
||||
|
||||
u1_rx_len = 0; /* 清空缓存 */
|
||||
}
|
||||
|
||||
/* 2. 从 485 收到设备数据 -> 通过 433 无线发射 */
|
||||
if (u3_rx_len > 0 && (HAL_GetTick() - u3_last_rx_time > 20))
|
||||
{
|
||||
MultiUART_Send(PORT_UART1, (uint8_t*)u3_rx_buffer, u3_rx_len);
|
||||
|
||||
/* 【可选进阶】:如果你希望网络端(比如电脑上的UDP助手)也能收到485传来的数据,
|
||||
你可以在这里调用你的网络发送函数,把 u3_rx_buffer 发给网口 */
|
||||
|
||||
u3_rx_len = 0; /* 清空缓存 */
|
||||
}
|
||||
|
||||
/* USER CODE END WHILE */
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
@ -234,37 +343,45 @@ void SystemClock_Config(void)
|
||||
* @param huart: UART句柄指针
|
||||
* @retval 无
|
||||
*/
|
||||
/* USER CODE BEGIN 4 */
|
||||
/* USER CODE BEGIN 4 */
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
||||
{
|
||||
if (huart->Instance == USART1)
|
||||
{
|
||||
/* 先保存接收到的字节,避免被rf433_hal回调覆盖 */
|
||||
uint8_t rx_byte = rf433_uart_rx_tmp;
|
||||
|
||||
/* 调用RF433模块的UART接收回调(内部会重启接收) */
|
||||
rf433_hal_uart_rxcplt_callback();
|
||||
|
||||
/* 喂入多UART路由器,支持RF433指令接收 */
|
||||
MultiUART_FeedByte(PORT_UART1, rx_byte);
|
||||
if (huart->Instance == USART1) {
|
||||
if (u1_rx_len < sizeof(u1_rx_buffer)) u1_rx_buffer[u1_rx_len++] = rf433_uart_rx_tmp;
|
||||
u1_last_rx_time = HAL_GetTick();
|
||||
HAL_UART_Receive_IT(&huart1, &rf433_uart_rx_tmp, 1);
|
||||
}
|
||||
else if (huart->Instance == USART2)
|
||||
{
|
||||
/* 喂入指令解析器 - UART2保持原有处理方式 */
|
||||
CmdParser_FeedByte(uart2_rx_byte, HAL_GetTick());
|
||||
|
||||
/* 重新启动接收 */
|
||||
HAL_UART_Receive_IT(&huart2, &uart2_rx_byte, 1);
|
||||
}
|
||||
else if (huart->Instance == USART3)
|
||||
{
|
||||
/* 喂入多UART路由器 - RS485接口 */
|
||||
MultiUART_FeedByte(PORT_UART3, uart3_rx_byte);
|
||||
|
||||
/* 重新启动接收 */
|
||||
else if (huart->Instance == USART3) {
|
||||
if (u3_rx_len < sizeof(u3_rx_buffer)) u3_rx_buffer[u3_rx_len++] = uart3_rx_byte;
|
||||
u3_last_rx_time = HAL_GetTick();
|
||||
HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1);
|
||||
}
|
||||
else if (huart->Instance == USART2) {
|
||||
HAL_UART_Receive_IT(&huart2, &uart2_rx_byte, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
|
||||
{
|
||||
/* 彻底清除所有硬件锁死标志 */
|
||||
__HAL_UART_CLEAR_OREFLAG(huart);
|
||||
__HAL_UART_CLEAR_NEFLAG(huart);
|
||||
__HAL_UART_CLEAR_FEFLAG(huart);
|
||||
|
||||
if (huart->Instance == USART1) HAL_UART_Receive_IT(&huart1, &rf433_uart_rx_tmp, 1);
|
||||
else if (huart->Instance == USART3) HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1);
|
||||
else if (huart->Instance == USART2) HAL_UART_Receive_IT(&huart2, &uart2_rx_byte, 1);
|
||||
}
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/* 后面原有的 HAL_UART_TxCpltCallback 保留不动... */
|
||||
|
||||
|
||||
/**
|
||||
* @brief UART发送完成中断回调函数
|
||||
* @note 处理UART1/UART2/UART3发送完成,触发下一次发送
|
||||
@ -285,8 +402,10 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
|
||||
}
|
||||
else if (huart->Instance == USART3)
|
||||
{
|
||||
|
||||
/* 调用多UART路由器的UART3发送完成回调 */
|
||||
MultiUART_TxCpltCallback(PORT_UART3);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -32,7 +32,7 @@
|
||||
* 调试宏定义
|
||||
*============================================================================*/
|
||||
/* DEBUG_MULTI_UART: 多UART路由调试日志开关 */
|
||||
#define DEBUG_MULTI_UART 1
|
||||
#define DEBUG_MULTI_UART 0
|
||||
|
||||
#if DEBUG_MULTI_UART
|
||||
/* 多UART模块日志宏,带前缀"[MUART]" */
|
||||
|
||||
@ -83,17 +83,19 @@ static void tx_led_control(uint8_t state)
|
||||
|
||||
rf433_error_t rf433_tx_app_init(const rf433_register_t *config)
|
||||
{
|
||||
if (config == NULL) {
|
||||
return RF433_ERROR;
|
||||
}
|
||||
|
||||
// 初始化TX应用结构体
|
||||
memset(&g_tx_app, 0, sizeof(rf433_tx_app_t));
|
||||
g_tx_app.state = TX_STATE_INIT;
|
||||
g_tx_app.is_running = false;
|
||||
|
||||
// 保存配置
|
||||
memcpy(&g_tx_app.config, config, sizeof(rf433_register_t));
|
||||
if (config == NULL) {
|
||||
// 使用默认配置
|
||||
extern const rf433_register_t rf433_default_config;
|
||||
memcpy(&g_tx_app.config, &rf433_default_config, sizeof(rf433_register_t));
|
||||
} else {
|
||||
memcpy(&g_tx_app.config, config, sizeof(rf433_register_t));
|
||||
}
|
||||
|
||||
// 配置RF433模块
|
||||
rf433_error_t ret = rf433_set_config(&g_tx_app.config);
|
||||
|
||||
@ -55,6 +55,7 @@
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/* External variables --------------------------------------------------------*/
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern UART_HandleTypeDef huart1;
|
||||
extern UART_HandleTypeDef huart2;
|
||||
extern UART_HandleTypeDef huart3;
|
||||
@ -199,6 +200,20 @@ void SysTick_Handler(void)
|
||||
/* please refer to the startup file (startup_stm32f1xx.s). */
|
||||
/******************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief This function handles TIM2 global interrupt.
|
||||
*/
|
||||
void TIM2_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN TIM2_IRQn 0 */
|
||||
|
||||
/* USER CODE END TIM2_IRQn 0 */
|
||||
HAL_TIM_IRQHandler(&htim2);
|
||||
/* USER CODE BEGIN TIM2_IRQn 1 */
|
||||
|
||||
/* USER CODE END TIM2_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles USART1 global interrupt.
|
||||
*/
|
||||
|
||||
111
Core/Src/tim.c
Normal file
111
Core/Src/tim.c
Normal file
@ -0,0 +1,111 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : tim.c
|
||||
* @brief : This file provides code for the configuration
|
||||
* of the TIM instances.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "tim.h"
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
TIM_HandleTypeDef htim2;
|
||||
|
||||
/* TIM2 init function */
|
||||
void MX_TIM2_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM2_Init 0 */
|
||||
|
||||
/* USER CODE END TIM2_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM2_Init 1 */
|
||||
|
||||
/* USER CODE END TIM2_Init 1 */
|
||||
htim2.Instance = TIM2;
|
||||
htim2.Init.Prescaler = 72-1;
|
||||
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim2.Init.Period = 65535;
|
||||
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM2_Init 2 */
|
||||
|
||||
/* USER CODE END TIM2_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
|
||||
{
|
||||
|
||||
if(tim_baseHandle->Instance==TIM2)
|
||||
{
|
||||
/* USER CODE BEGIN TIM2_MspInit 0 */
|
||||
|
||||
/* USER CODE END TIM2_MspInit 0 */
|
||||
/* TIM2 clock enable */
|
||||
__HAL_RCC_TIM2_CLK_ENABLE();
|
||||
|
||||
/* TIM2 interrupt Init */
|
||||
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(TIM2_IRQn);
|
||||
/* USER CODE BEGIN TIM2_MspInit 1 */
|
||||
|
||||
/* USER CODE END TIM2_MspInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
|
||||
{
|
||||
|
||||
if(tim_baseHandle->Instance==TIM2)
|
||||
{
|
||||
/* USER CODE BEGIN TIM2_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END TIM2_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_TIM2_CLK_DISABLE();
|
||||
|
||||
/* TIM2 interrupt Deinit */
|
||||
HAL_NVIC_DisableIRQ(TIM2_IRQn);
|
||||
/* USER CODE BEGIN TIM2_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END TIM2_MspDeInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
@ -21,7 +21,7 @@
|
||||
#include "usart.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "multi_uart_router.h"
|
||||
/*==============================================================================
|
||||
* 调试宏定义
|
||||
*============================================================================*/
|
||||
@ -460,50 +460,26 @@ uint16_t UART2_Print_GetOverflowCount(void)
|
||||
#if defined(__CC_ARM) || defined(__ARMCC_VERSION)
|
||||
int fputc(int ch, FILE *f)
|
||||
{
|
||||
(void)f; /* 未使用参数,避免编译器警告 */
|
||||
(void)f;
|
||||
UART2_Print_Send((uint8_t *)&ch, 1);
|
||||
MultiUART_Send(PORT_UART3, (uint8_t *)&ch, 1); /* 增加:同时发给 UART3 */
|
||||
return ch;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief printf重定向函数 (GCC编译器 - 单字符版本)
|
||||
* @note 将标准库printf输出重定向到UART2
|
||||
*
|
||||
* @param ch: 待发送字符(输入)
|
||||
* @return int: 发送的字符
|
||||
*
|
||||
* 编译器条件编译:
|
||||
* 此函数仅在__GNUC__定义时编译,即GCC/Clang编译器环境下生效
|
||||
*
|
||||
* 实现说明:
|
||||
* 新一代ARM GCC使用__io_putchar作为printf输出目标,
|
||||
* 此处将其重定向到UART2_Print_Send
|
||||
*/
|
||||
#if defined(__GNUC__)
|
||||
int __io_putchar(int ch)
|
||||
{
|
||||
UART2_Print_Send((uint8_t *)&ch, 1);
|
||||
MultiUART_Send(PORT_UART3, (uint8_t *)&ch, 1); /* 增加:同时发给 UART3 */
|
||||
return ch;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief write系统调用重定向 (GCC编译器)
|
||||
* @note 将文件系统write调用重定向到UART2
|
||||
*
|
||||
* @param file: 文件描述符(未使用,仅为兼容标准接口)
|
||||
* @param ptr: 数据缓冲区指针(输入)
|
||||
* @param len: 数据长度(输入)
|
||||
* @return int: 已发送的字节数
|
||||
*
|
||||
* 实现说明:
|
||||
* 有些GCC配置下printf会调用_write而非__io_putchar,
|
||||
* 此函数提供完整的write接口兼容
|
||||
*/
|
||||
int _write(int file, char *ptr, int len)
|
||||
{
|
||||
(void)file; /* 忽略文件描述符,只处理标准输出 */
|
||||
(void)file;
|
||||
UART2_Print_Send((uint8_t *)ptr, len);
|
||||
MultiUART_Send(PORT_UART3, (uint8_t *)ptr, len); /* 增加:同时发给 UART3 */
|
||||
return len;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@ -39,6 +39,10 @@
|
||||
#define DEBUG_LOG(fmt, ...)
|
||||
#endif
|
||||
|
||||
/* 数据源标识 */
|
||||
#define SOURCE_RS485 0x01
|
||||
#define SOURCE_DI 0x02
|
||||
|
||||
/*==============================================================================
|
||||
* 全局变量定义
|
||||
* 设计依据:文档第3.2.2节
|
||||
|
||||
@ -100,7 +100,7 @@ void MX_USART3_UART_Init(void)
|
||||
|
||||
/* USER CODE END USART3_Init 1 */
|
||||
huart3.Instance = USART3;
|
||||
huart3.Init.BaudRate = 9600;
|
||||
huart3.Init.BaudRate = 115200; /* 从 9600 修改为 115200 */
|
||||
huart3.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart3.Init.StopBits = UART_STOPBITS_1;
|
||||
huart3.Init.Parity = UART_PARITY_NONE;
|
||||
|
||||
Reference in New Issue
Block a user