HAL_SPI_Transmit_IT源码
时间: 2023-10-06 07:09:29 浏览: 204
stm32 HAL库配置 1.8寸spiTFT液晶屏幕(源码)
5星 · 资源好评率100%
HAL_SPI_Transmit_IT 函数的源码如下:
```
HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
{
/* Check Direction parameter */
assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
/* Process Locked */
__HAL_LOCK(hspi);
if(hspi->State != HAL_SPI_STATE_READY)
{
__HAL_UNLOCK(hspi);
return HAL_BUSY;
}
if((pData == NULL ) || (Size == 0))
{
__HAL_UNLOCK(hspi);
return HAL_ERROR;
}
/* Prepare transfer parameters */
hspi->pTxBuffPtr = pData;
hspi->TxXferSize = Size;
hspi->TxXferCount = Size;
hspi->TxISR = SPI_TxISR_8BIT;
/* Reset CRC Calculation */
if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
{
__HAL_SPI_RESET_CRC(hspi);
}
/* Set the SPI Tansmit DMA transfer complete callback */
hspi->hdmatx->XferCpltCallback = SPI_DMAEndOfTx;
/* Set the DMA error callback */
hspi->hdmatx->XferErrorCallback = SPI_DMAError;
/* Enable the Tx DMA Stream */
HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, Size);
/* Enable Tx DMA Request */
SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
/* Enable the SPI Error Interrupt Bit */
__HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR));
/* Enable Tx DMA Request */
SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
/* Process Unlocked */
__HAL_UNLOCK(hspi);
/* Note : The SPI must be enabled after the DMA and interrupts configuration */
/* to avoid the risk of SPI interrupt handle execution before DMA */
/* interrupt handle */
/* Enable SPI peripheral */
__HAL_SPI_ENABLE(hspi);
return HAL_OK;
}
```
这个函数的作用是启动 SPI 的 DMA 发送模式,并且在传输完成后调用回调函数 `SPI_DMAEndOfTx`。函数首先对输入参数进行检查,然后准备传输参数,重置 CRC 计算,设置 DMA 回调和错误回调函数,启动 DMA 传输并启用 SPI 错误中断和 DMA 请求,最后启用 SPI 外设。
阅读全文