HAL库——DMA(HAL_UART_Receive_DMA)

/**
* @briefReceives an amount of data in non blocking mode.
* @paramhuart: pointer to a UART_HandleTypeDef structure that contains
*the configuration information for the specified UART module.
* @param pData: Pointer to data buffer
* @param Size: Amount of data to be received
* @noteWhen the UART parity is enabled (PCE = 1) the data received contain the parity bit.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
{
uint32_t *tmp;

/* Check that a Rx process is not already ongoing */
if(huart->RxState == HAL_UART_STATE_READY)
{
if((pData =https://www.it610.com/article/= NULL) || (Size == 0U))
{
return HAL_ERROR;
}


/* Process Locked */
__HAL_LOCK(huart);


huart->pRxBuffPtr = pData;
huart->RxXferSize = Size;


huart->ErrorCode = HAL_UART_ERROR_NONE;
huart->RxState = HAL_UART_STATE_BUSY_RX;


/* Set the UART DMA transfer complete callback */
huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;


/* Set the UART DMA Half transfer complete callback */
huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;


/* Set the DMA error callback */
huart->hdmarx->XferErrorCallback = UART_DMAError;


/* Set the DMA abort callback */
huart->hdmarx->XferAbortCallback = NULL;


/* Enable the DMA channel */
tmp = (uint32_t*)&pData;
HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->DR, *(uint32_t*)tmp, Size);


/* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
__HAL_UART_CLEAR_OREFLAG(huart);


/* Process Unlocked */
__HAL_UNLOCK(huart);


/* Enable the UART Parity Error Interrupt */
SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);


/* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
SET_BIT(huart->Instance->CR3, USART_CR3_EIE);


/* Enable the DMA transfer for the receiver request by setting the DMAR bit
in the UART CR3 register */
SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);


return HAL_OK;
}
else
{
return HAL_BUSY;
}
}

    推荐阅读