HAL_UART_Transmit_DMA的标准库版本函数是如何编写
时间: 2023-08-10 16:03:38 浏览: 95
标准例程-HAL库版本
HAL_UART_Transmit_DMA函数是HAL库中UART发送DMA数据的函数,其标准库版本的函数实现如下:
```c
HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
{
HAL_StatusTypeDef status = HAL_OK;
/* Check that a Tx process is not already ongoing */
if (huart->gState == HAL_UART_STATE_READY)
{
if ((pData == NULL) || (Size == 0U))
{
status = HAL_ERROR;
}
else
{
/* Process Locked */
__HAL_LOCK(huart);
huart->pTxBuffPtr = pData;
huart->TxXferSize = Size;
huart->TxXferCount = Size;
/* Computation of UART mask to apply to RDR register */
UART_MASK_COMPUTATION(huart);
/* Enable the UART Transmit data register empty Interrupt */
__HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
/* Enable the UART DMA requests */
huart->Instance->CR3 |= USART_CR3_DMAT;
/* Process Unlocked */
__HAL_UNLOCK(huart);
/* Enable the DMA Stream */
status = HAL_DMA_Start_IT(huart->hdmatx, (uint32_t)pData, (uint32_t)&huart->Instance->TDR, Size);
if (status == HAL_OK)
{
/* Change the UART state */
huart->gState = HAL_UART_STATE_BUSY_TX;
}
else
{
/* Disable the DMA transfer for transmit request by resetting the DMAT bit in the UART CR3 register */
huart->Instance->CR3 &= ~USART_CR3_DMAT;
/* Process Unlocked */
__HAL_UNLOCK(huart);
huart->gState = HAL_UART_STATE_READY;
}
}
}
else
{
status = HAL_BUSY;
}
/* Return function status */
return status;
}
```
该函数的主要作用是将数据通过DMA方式发送到UART外设上。该函数首先检查是否已经有发送进程正在运行,如果没有,则进一步检查pData和Size参数是否为空。然后,将pData指针和Size大小分别设置为UART_HandleTypeDef结构体中的成员变量pTxBuffPtr和TxXferSize,并将TxXferCount也设置为Size大小。接下来,使能UART的TXE中断和DMA传输请求,并启动DMA传输。最后,将状态设置为HAL_UART_STATE_BUSY_TX,表示发送进程正在运行。如果DMA传输启动失败,则将状态改回HAL_UART_STATE_READY,并返回错误状态HAL_ERROR。如果发送进程正在运行,则返回忙状态HAL_BUSY。
阅读全文