STM_ATEM/User/rs485_driver.c
zhoujie 28b4dc6af1 feat(系统): 新增4KHz采样率串口瓶颈分析及优化方案文档
- 新增《4KHz_UART_Bottleneck_Analysis.md》详细分析文档,识别阻塞式串口发送为主要瓶颈
- 新增《Code_Optimization_Summary.md》代码修改总结文档,记录优化实施细节
- 新增《Final_Solution_Explanation.md》最终方案说明文档,阐述中断+DMA非阻塞发送的最优方案

🐛 fix(串口驱动): 将RS485驱动改为DMA非阻塞发送

- 修改`User/rs485_driver.c`中的`RS485_SendData`函数,使用`HAL_UART_Transmit_DMA`替代阻塞式发送
- 启用`RS485_TxCpltCallback`回调函数,在DMA传输完成后自动切换回接收模式并清除忙标志
- 添加忙状态检查机制,防止上一次传输未完成时启动新传输

♻️ refactor(主循环): 优化定时器中断处理策略并启用串口输出

- 修改`Core/Src/main.c`中的定时器配置,将TIM2周期从999调整为124,提高中断频率至8KHz
- 简化`HAL_TIM_PeriodElapsedCallback`中断处理函数,取消循环处理多个数据包的逻辑
- 启用串口数据输出模式(`DATA_OUTPUT_MODE_UART=1`),禁用存储卡模式以降低负载

📝 docs(配置): 更新IOC配置文件和监控文件路径

- 更新`STM_ATEM_F405.ioc`中的TIM2配置,同步定时器周期修改
- 修改`User/system_monitor.h`中的监控状态文件路径,从"MONITOR.TXT"改为"LOG.TXT"
2026-02-07 14:04:36 +08:00

54 lines
1.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "rs485_driver.h"
#include <string.h>
static UART_HandleTypeDef *g_huart_485 = NULL;
static GPIO_TypeDef* g_de_re_port = NULL;
static uint16_t g_de_re_pin = 0;
volatile uint8_t g_rs485_tx_busy = 0;
void RS485_Init(UART_HandleTypeDef *huart, GPIO_TypeDef* de_re_port, uint16_t de_re_pin)
{
g_huart_485 = huart;
g_de_re_port = de_re_port;
g_de_re_pin = de_re_pin;
g_rs485_tx_busy = 0;
HAL_GPIO_WritePin(g_de_re_port, g_de_re_pin, GPIO_PIN_RESET); // 初始为接收模式
}
HAL_StatusTypeDef RS485_SendData(uint8_t *pData, uint16_t Size)
{
HAL_StatusTypeDef ret;
// 检查上一次传输是否完成
if (g_rs485_tx_busy)
{
return HAL_BUSY; // 上一次传输未完成,返回忙状态
}
g_rs485_tx_busy = 1; // 标记为忙状态
HAL_GPIO_WritePin(g_de_re_port, g_de_re_pin, GPIO_PIN_SET); // 设置为发送模式
// 使用DMA非阻塞发送
ret = HAL_UART_Transmit_DMA(g_huart_485, pData, Size);
if (ret != HAL_OK)
{
// 如果启动DMA失败需要清除忙标志并切换回接收模式
HAL_GPIO_WritePin(g_de_re_port, g_de_re_pin, GPIO_PIN_RESET);
g_rs485_tx_busy = 0;
}
return ret;
}
// UART DMA传输完成回调函数
void RS485_TxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart == g_huart_485)
{
// DMA传输完成后切换回接收模式
HAL_GPIO_WritePin(g_de_re_port, g_de_re_pin, GPIO_PIN_RESET);
g_rs485_tx_busy = 0; // 清除忙标志
}
}