天天看點

LL庫通過序列槽+DMA方式發送資料

在RM0038(L151的手冊)中的Chapter 27.3.13 Continuous communication using DMA章節有配置的方式和順序:

Transmission using DMA:

DMA mode can be enabled for transmission by setting DMAT bit in the USART_CR3 register.

這個是使用了API:LL_USART_EnableDMAReq_TX來實作的。

按照該手冊描述配置順序為:

1.Write the USART_DR register address in the DMA control register to configure it as the destination of the transfer. The data will be moved to this address from memory after each TXE event.

2. Write the memory address in the DMA control register to configure it as the source of the transfer. The data will be loaded into the USART_DR register from this memory area after each TXE event.

3. Configure the total number of bytes to be transferred to the DMA control register.

4. Configure the channel priority in the DMA register

5. Configure DMA interrupt generation after half/ full transfer as required by the application.

6. Clear the TC bit in the SR register by writing 0 to it.

7. Activate the channel in the DMA register.

按照描述,我們初始化DMA和USART1(使用Stm32CubeMX預設生成的工程,屏蔽了DMA的中斷)後:

1. 配置外設和memory的位址(LL_DMA_SetPeriphAddress和LL_DMA_SetMemoryAddress)

2.設定DMA傳輸數量LL_DMA_SetDataLength

3. 清除TC标志(LL_USART_ClearFlag_TC ) 該步驟很重要,如果沒有清除,一旦使能TC中斷,就會執行中斷操作

4. 使能TCIE中斷(LL_USART_EnableIT_TC)

5. 使能DMA(LL_DMA_EnableChannel)

6. 發送DMA傳輸請求(LL_USART_EnableDMAReq_TX)

USART1_IRQHandler中斷裡面:

1.檢測TC标志(LL_USART_IsActiveFlag_TC)

2. 關閉TC中斷(LL_USART_DisableIT_TC) 防止反複進入TC中斷

3. 關閉DMA通道(LL_DMA_DisableChannel)

總結:一定要先清除标志位,然後再使能該标志位的中斷

另外,發送完成中斷,應該使用Uart的TC标志,而不應該是DMA的發送完成标志TCIF。

前者,表示資料全部傳輸到了uart的輸出線上(所有的Data+1個空閑byte時間)

後者,僅僅表示資料全部傳給了Uart的DR。DR到外設線上是有一段緩存區的。

LL庫通過序列槽+DMA方式發送資料

繼續閱讀