2026-03-31 15:46:04 +08:00
|
|
|
|
/**
|
|
|
|
|
|
******************************************************************************
|
2026-05-21 12:19:01 +08:00
|
|
|
|
* @file User\Driver\drv_usart.c
|
|
|
|
|
|
* @author 路淮
|
2026-03-31 15:46:04 +08:00
|
|
|
|
* @version v0.1
|
2026-05-21 12:19:01 +08:00
|
|
|
|
* @date 2026-05-21
|
|
|
|
|
|
* @brief 串口驱动
|
2026-03-31 15:46:04 +08:00
|
|
|
|
******************************************************************************
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Includes -------------------------------------------------------------------*/
|
2026-05-21 10:01:28 +08:00
|
|
|
|
#include "drv_usart.h"
|
2026-03-31 15:46:04 +08:00
|
|
|
|
|
|
|
|
|
|
/* variables ------------------------------------------------------------------*/
|
2026-05-21 12:19:01 +08:00
|
|
|
|
uint8_t uart1_rx_buffer[UART1_RX_BUFFER_SIZE];// 4G模块接收缓冲区
|
|
|
|
|
|
uint8_t uart1_tx_buffer[UART1_TX_BUFFER_SIZE];// 4G模块发送缓冲区
|
2026-03-31 15:46:04 +08:00
|
|
|
|
|
2026-05-21 12:19:01 +08:00
|
|
|
|
uint8_t uart3_rx_buffer[UART3_RX_BUFFER_SIZE];// RS485模块接收缓冲区
|
|
|
|
|
|
uint8_t uart3_tx_buffer[UART3_TX_BUFFER_SIZE];// RS485模块发送缓冲区
|
|
|
|
|
|
|
|
|
|
|
|
static SemaphoreHandle_t air724_tx_mutex = NULL;// 4G模块发送互斥信号量
|
2026-03-31 15:46:04 +08:00
|
|
|
|
|
2026-04-30 17:16:01 +08:00
|
|
|
|
|
2026-03-31 15:46:04 +08:00
|
|
|
|
/* code -----------------------------------------------------------------------*/
|
2026-05-21 12:19:01 +08:00
|
|
|
|
|
2026-03-31 15:46:04 +08:00
|
|
|
|
/**
|
2026-05-21 12:19:01 +08:00
|
|
|
|
* @brief fputc:printf映射函数
|
2026-03-31 15:46:04 +08:00
|
|
|
|
* @note none
|
2026-05-21 12:19:01 +08:00
|
|
|
|
* @param ch:字符
|
|
|
|
|
|
* @param f:文件指针
|
2026-03-31 15:46:04 +08:00
|
|
|
|
* @retval none
|
|
|
|
|
|
*/
|
|
|
|
|
|
int fputc(int ch, FILE *f)
|
|
|
|
|
|
{
|
|
|
|
|
|
HAL_UART_Transmit(&huart6, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
|
|
|
|
|
|
return ch;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief Air724_Message_Send:4G数据发送
|
|
|
|
|
|
* @note none
|
2026-05-21 12:19:01 +08:00
|
|
|
|
* @param data:数据指针
|
|
|
|
|
|
* @param len:数据长度
|
2026-03-31 15:46:04 +08:00
|
|
|
|
* @retval none
|
|
|
|
|
|
*/
|
|
|
|
|
|
void Air724_Message_Send(uint8_t *data, uint16_t len)
|
|
|
|
|
|
{
|
2026-04-30 17:16:01 +08:00
|
|
|
|
if (air724_tx_mutex != NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (xSemaphoreTake(air724_tx_mutex, portMAX_DELAY) == pdTRUE)
|
|
|
|
|
|
{
|
|
|
|
|
|
HAL_UART_Transmit(&huart1, data, len, HAL_MAX_DELAY);
|
|
|
|
|
|
xSemaphoreGive(air724_tx_mutex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
HAL_UART_Transmit(&huart1, data, len, HAL_MAX_DELAY);
|
|
|
|
|
|
}
|
2026-03-31 15:46:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-21 12:19:01 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief Rs485_Message_Send:RS485数据发送
|
|
|
|
|
|
* @note none
|
|
|
|
|
|
* @param data:数据指针
|
|
|
|
|
|
* @param len:数据长度
|
|
|
|
|
|
* @retval none
|
|
|
|
|
|
*/
|
2026-03-31 15:46:04 +08:00
|
|
|
|
void Rs485_Message_Send(uint8_t *data, uint16_t len)
|
|
|
|
|
|
{
|
|
|
|
|
|
RS485_EN(1);
|
|
|
|
|
|
HAL_UART_Transmit(&huart3, data, len, HAL_MAX_DELAY);
|
|
|
|
|
|
RS485_EN(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-05-21 10:01:28 +08:00
|
|
|
|
* @brief drv_usart_init:所有串口初始化
|
2026-03-31 15:46:04 +08:00
|
|
|
|
* @note none
|
|
|
|
|
|
* @param none
|
|
|
|
|
|
* @retval none
|
|
|
|
|
|
*/
|
2026-05-21 10:01:28 +08:00
|
|
|
|
void drv_usart_init(void)
|
2026-03-31 15:46:04 +08:00
|
|
|
|
{
|
2026-04-30 17:16:01 +08:00
|
|
|
|
air724_tx_mutex = xSemaphoreCreateMutex();
|
2026-03-31 15:46:04 +08:00
|
|
|
|
|
|
|
|
|
|
/* 初始化串口1 */
|
|
|
|
|
|
Air724_Message_Queue_Init(); // 初始化4G数据接收队列
|
|
|
|
|
|
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
|
|
|
|
|
|
HAL_UART_Receive_DMA(&huart1, uart1_rx_buffer, UART1_RX_BUFFER_SIZE);
|
|
|
|
|
|
|
|
|
|
|
|
/* 初始化串口3 */
|
|
|
|
|
|
RS485_Message_Queue_Init(); // 初始化RS485数据接收队列
|
|
|
|
|
|
__HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
|
|
|
|
|
|
HAL_UART_Receive_DMA(&huart3, uart3_rx_buffer, UART3_RX_BUFFER_SIZE);
|
|
|
|
|
|
|
|
|
|
|
|
RS485_EN(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief HAL_UARTEx_RxEventCallback:串口接收完成中断处理函数
|
|
|
|
|
|
* @note none
|
2026-05-21 12:19:01 +08:00
|
|
|
|
* @param huart:串口句柄
|
|
|
|
|
|
* @param Size:接收数据大小
|
2026-03-31 15:46:04 +08:00
|
|
|
|
* @retval none
|
|
|
|
|
|
*/
|
|
|
|
|
|
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (huart->Instance == USART1)
|
|
|
|
|
|
{
|
|
|
|
|
|
SCB_InvalidateDCache_by_Addr((uint32_t *)uart1_rx_buffer, UART1_RX_BUFFER_SIZE);
|
|
|
|
|
|
air724_callback_fun();
|
|
|
|
|
|
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, (uint8_t *)uart1_rx_buffer, UART1_RX_BUFFER_SIZE);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (huart->Instance == USART3)
|
|
|
|
|
|
{
|
|
|
|
|
|
SCB_InvalidateDCache_by_Addr((uint32_t *)uart3_rx_buffer, UART3_RX_BUFFER_SIZE);
|
|
|
|
|
|
rs485_callback_fun();
|
|
|
|
|
|
HAL_UARTEx_ReceiveToIdle_DMA(&huart3, (uint8_t *)uart3_rx_buffer, UART3_RX_BUFFER_SIZE);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief air724_callback_fun:4G数据接收回调函数
|
|
|
|
|
|
* @note none
|
2026-05-21 12:19:01 +08:00
|
|
|
|
* @param huart:串口句柄
|
|
|
|
|
|
* @param Size:接收数据大小
|
2026-03-31 15:46:04 +08:00
|
|
|
|
* @retval none
|
|
|
|
|
|
*/
|
|
|
|
|
|
void air724_callback_fun()
|
|
|
|
|
|
{
|
|
|
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
|
|
xQueueSendFromISR(Air724_Message_Queue, uart1_rx_buffer, &xHigherPriorityTaskWoken);
|
|
|
|
|
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
2026-05-06 15:22:21 +08:00
|
|
|
|
memset(uart1_rx_buffer, 0, UART1_RX_BUFFER_SIZE);
|
2026-03-31 15:46:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-21 12:19:01 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief rs485_callback_fun:RS485数据接收回调函数
|
|
|
|
|
|
* @note none
|
|
|
|
|
|
* @param huart:串口句柄
|
|
|
|
|
|
* @param Size:接收数据大小
|
|
|
|
|
|
* @retval none
|
|
|
|
|
|
*/
|
2026-03-31 15:46:04 +08:00
|
|
|
|
void rs485_callback_fun()
|
|
|
|
|
|
{
|
|
|
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
|
|
xQueueSendFromISR(RS485_Message_Queue, uart3_rx_buffer, &xHigherPriorityTaskWoken);
|
|
|
|
|
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
|
|
|
|
|
memset(uart3_rx_buffer, 0, UART3_RX_BUFFER_SIZE);
|
|
|
|
|
|
}
|