STM_ATEM/User/rs485_driver.c
zhoujie fee2e96eaa feat(system): 优化系统性能并简化监控模块
- 新增VSCode编辑器配置,启用保存时自动格式化
- 新增DMA2_Stream5和USART1中断处理函数,优化SPI传输性能
- 移除TIM1相关配置和性能监控模块,简化系统架构
- 优化GPIO配置,将ADC_SYNC和PA8引脚合并配置
- 简化系统监控模块,仅保留采样计数和数据溢出统计
- 优化SPI配置,使用16位数据宽度并提高DMA优先级
- 提高USART波特率(USART1:2Mbps, USART3:921600bps)
- 优化LTC2508驱动,增加初始化状态检查和缓冲区大小
- 修正数据存储模块,使用校正数据包而非校正结果结构体
- 优化RS485驱动,使用中断传输并添加传输完成回调
- 更新IOC配置文件,移除TIM1并调整中断优先级配置
- 新增系统监控简化说明文档,详细记录架构变更
2026-02-06 22:45:25 +08:00

59 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); // 设置为发送模式
ret = HAL_UART_Transmit_IT(g_huart_485, pData, Size);
// 注意:不能在这里立即切换回接收模式!
// DMA传输是非阻塞的需要在传输完成回调中切换
if (ret != HAL_OK)
{
// 如果启动DMA失败需要清除忙标志并切换回接收模式
HAL_GPIO_WritePin(g_de_re_port, g_de_re_pin, GPIO_PIN_RESET);
g_rs485_tx_busy = 0;
}
// 等待数据传输完成
while(g_rs485_tx_busy)
{
;
}
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;
}
}