LL库通过串口+DMA方式发送数据

来源:互联网 发布:为什么阿里云备案 编辑:程序博客网 时间:2024/06/01 18:56

在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到外设线上是有一段缓存区的。