Release:5.20灰测
This commit is contained in:
147
Core/User/Driver/drv_usart.c
Normal file
147
Core/User/Driver/drv_usart.c
Normal file
@@ -0,0 +1,147 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file user\hal\_hal_usart.c
|
||||
* @author luhuaishuai
|
||||
* @version v0.1
|
||||
* @date 2026-1-12
|
||||
* @brief Briefly describe the function of your function
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Includes -------------------------------------------------------------------*/
|
||||
#include "drv_usart.h"
|
||||
|
||||
/* variables ------------------------------------------------------------------*/
|
||||
uint8_t uart1_rx_buffer[UART1_RX_BUFFER_SIZE];
|
||||
uint8_t uart1_tx_buffer[UART1_TX_BUFFER_SIZE];
|
||||
|
||||
uint8_t uart3_rx_buffer[UART3_RX_BUFFER_SIZE];
|
||||
uint8_t uart3_tx_buffer[UART3_TX_BUFFER_SIZE];
|
||||
|
||||
static SemaphoreHandle_t air724_tx_mutex = NULL;
|
||||
|
||||
/* code -----------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief fputc:printf映射
|
||||
*
|
||||
* @note none
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @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
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @retval none
|
||||
*/
|
||||
void Air724_Message_Send(uint8_t *data, uint16_t len)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief drv_usart_init:所有串口初始化
|
||||
*
|
||||
* @note none
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @retval none
|
||||
*/
|
||||
|
||||
void drv_usart_init(void)
|
||||
{
|
||||
air724_tx_mutex = xSemaphoreCreateMutex();
|
||||
|
||||
/* 初始化串口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
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @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
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @retval none
|
||||
*/
|
||||
void air724_callback_fun()
|
||||
{
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
xQueueSendFromISR(Air724_Message_Queue, uart1_rx_buffer, &xHigherPriorityTaskWoken);
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
memset(uart1_rx_buffer, 0, UART1_RX_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user