HAL驱动的串口编程陷阱
STM32远程升级(基于串口本地升级与WiFi通信远程升级)
串口是嵌入式开发中最常前的外设设备,既可以用作不同单片机之间的通信,也可以用作在STM32 MCU和PC机之间的通信,STM32F407的串口功能非常强大,可以接红外,可以接流控,也可以接SIM卡接口,但我这里只介绍我们最常用的UART通信的一点调试经验,以STM32F407为例,对其它STM32芯片也适用,希望对大家有所帮助,如有错误不当之处欢迎大家联系指正。
一、串口的三种工作方式
操作串口一般有两种方式:查询和中断;STM32还支持第三种DMA方式。
(1)查询:串口程序不断地循环查询标志,看看当前有没有数据要它传送或接收。如果有的话进行相应的写操作和读操作进行传送或接收数据。
(2)中断:平时串口只要打开中断即可。如果发现有一个中断来,则意味着有数据需要接收(接收中断)或数据已经发送完成(发送中断)。
(3)DMA方式,设置好DMA工作方式,由DMA来自动接收或发送数据。
一般来说,查询方式的效率是比较低的,并且由于STM32的UART硬件上没有FIFO,如果程序功能比较多,查询不及时的话很容易出现数据丢失的现象, 故实际项目中这种方式用的并不多。
中断方式的话我们可以分别设置接收中断和发送中断,当串口有数据需要接收时才进入中断程序进行读读操,这种方式占用CPU资源比较少,实际项目中比较常用,但需要注意中断程序不要太复杂使执行时间太长,如果执行时间超过一个字符的时间的话也会出现数据丢失的现象,这个波特率比较高的串口编程中比较容易出现,可以考虑用循环BUF方法,在中断程序中只负责实时地接收实数数和发送时的填数(写发送寄存器),其它操作放在中断外处理。
STM32还提供了第三种DMA方式用来支持高速地串口传输。这种方式只要设置好接收和发送缓冲位置,可以由DMA来自动接收和发送数据,这可以最小化占用CPU时间。
二、串口的使用步骤
(1)中断方式
基本步骤是初试化时钟,脚位、波特率设置、安装中断服务程序、开中断等,参考代码如下:
- void uart_init(void)
- {
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* Enable GPIO clock */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
-
- /* Enable USART clock */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
-
- /* Connect USART pins to AF7 */
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
-
- /* Configure USART Tx and Rx as alternate function push-pull */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /* USARTx configuration ----------------------------------------------------*/
- /* USARTx configured as follow:
- - BaudRate = 3750000 baud
- - Maximum BaudRate that can be achieved when using the Oversampling by 8
- is: (USART APB Clock / 8)
- Example:
- - (USART3 APB1 Clock / 8) = (30 MHz / 8) = 3750000 baud
- - (USART1 APB2 Clock / 8) = (60 MHz / 8) = 7500000 baud
- - Maximum BaudRate that can be achieved when using the Oversampling by 16
- is: (USART APB Clock / 16)
- Example: (USART3 APB1 Clock / 16) = (30 MHz / 16) = 1875000 baud
- Example: (USART1 APB2 Clock / 16) = (60 MHz / 16) = 3750000 baud
- - Word Length = 8 Bits
- - one Stop Bit
- - No parity
- - Hardware flow control disabled (RTS and CTS signals)
- - Receive and transmit enabled
- */
- USART_InitStructure.USART_BaudRate = 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART3, &USART_InitStructure);
-
- /* NVIC configuration */
- /* Configure the Priority Group to 2 bits */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- /* Enable the USARTx Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- /* Enable USART */
- USART_Cmd(USART3, ENABLE);
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
- }
复制代码
中断服务程序如下:
- void USART3_IRQHandler(void)
- {
- unsigned char ch;
- if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
- {
- /* Read one byte from the receive data register */
- ch = (USART_ReceiveData(USART3));
- printf("in[%c].\r\n",ch);
- }
- }
复制代码
直接把接收到的字符打印出来。
(2)DMA方式
基本步骤同中断方式,额外需要DMA的初始化配置,参考代码如下:
- void uart_init(void)
- {
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- DMA_InitTypeDef DMA_InitStruct;
-
- /* Enable GPIO clock */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
-
- /* Enable USART clock */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
-
- /* Connect USART pins to AF7 */
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
-
- /* Configure USART Tx and Rx as alternate function push-pull */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /* USARTx configuration ----------------------------------------------------*/
- /* USARTx configured as follow:
- - BaudRate = 3750000 baud
- - Maximum BaudRate that can be achieved when using the Oversampling by 8
- is: (USART APB Clock / 8)
- Example:
- - (USART3 APB1 Clock / 8) = (30 MHz / 8) = 3750000 baud
- - (USART1 APB2 Clock / 8) = (60 MHz / 8) = 7500000 baud
- - Maximum BaudRate that can be achieved when using the Oversampling by 16
- is: (USART APB Clock / 16)
- Example: (USART3 APB1 Clock / 16) = (30 MHz / 16) = 1875000 baud
- Example: (USART1 APB2 Clock / 16) = (60 MHz / 16) = 3750000 baud
- - Word Length = 8 Bits
- - one Stop Bit
- - No parity
- - Hardware flow control disabled (RTS and CTS signals)
- - Receive and transmit enabled
- */
- USART_InitStructure.USART_BaudRate = 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART3, &USART_InitStructure);
- /* DMA_Configuration */
- DMA_DeInit(DMA1_Stream1);
-
- DMA_InitStruct.DMA_Channel = DMA_Channel_4;
- DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)&USART3->DR; //source buf
- DMA_InitStruct.DMA_Memory0BaseAddr = (u8)pdata; //target buf
- DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
- DMA_InitStruct.DMA_BufferSize = lenght; //BuffSize;
- DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
- DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
- DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
- DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; //DMA_Mode_Normal;
- DMA_InitStruct.DMA_Priority = DMA_Priority_High;
- DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;
- DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
- DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
- DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
- DMA_Init(DMA1_Stream1, &DMA_InitStruct);
- /* NVIC configuration */
- /* Configure the Priority Group to 2 bits */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- /* Enable the USARTx Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- /* Open DMA interrupt*/
- DMA_ITConfig(DMA1_Stream1, DMA_IT_TC, ENABLE);
- DMA_Cmd(DMA1_Stream1, ENABLE);
- USART_Cmd(USART3, ENABLE);
- USART_DMACmd(USART3,USART_DMAReq_Rx,ENABLE);
- }
复制代码
DMA中断服务程序如下:
- void DMA1_Stream1_IRQHandler(void) //UART3_RX
- {
- static short i;
- //When a Transfer Complete
- if(SET == DMA_GetITStatus(DMA1_Stream1, DMA_IT_TCIF1))
- {
- DMA_ClearITPendingBit(DMA1_Stream1, DMA_IT_TCIF1);
- i++;
- }
- }
复制代码
上面程序只配了DMA接收,发送类似。
三、实现DMX512协议
DMX512 协议是美国剧场技术协会( United States Institute for Theater Technology, USITT) 制定的数字多路复用协议, 其制定的初衷是为了使舞台、剧场等地所使用的众多的调光器和控制器能相互兼容。虽然它不是一个行业或国家标准, 但是由于它的简单性和实用性, 自从出台以来, 得到了世界各地生产商和使用者普遍承认,这个协议在LED控制方面应用很广泛,利用STM32 USART可以高速传输的特性,我们很容易用STM32来实现DMX512协议。
(1)数据的格式及传输
DMX512 协议规定数据以数据包的形式通过异步通讯的方式进行传输。每个数据包由若干数据帧组成, 每帧数据包括1 位低电平起始位、8 位数据位和2 位高电平停止位。DMX 协议要求数据传输的波特率为250kb/s, 亦即每位的传输时间为4us, 每帧数据的传输时间为44us, 它支持多达512 帧数据传输, 每帧数据与相应的控制支路相对应。数据包的传送要符合一定的格式和时序要求。为了使接收器能够分辨出第一帧数据, 每一个数据包以一个不短于88us 的低电平信号为起始信号, 即所谓的“Break”信号, 接收器接收到“Break”信号就准备接受随后而来的数据帧; 紧接着“Break”信号之后是不短于8us 的高电平信号M. a. b ( Mark after Break) ; 之后就是数据帧。在DMX512 协议中, M. a. b 之后的第一帧数据被称
为“Star-t code”, 在协议中规定其为零, 但在实际应用中可以由生产厂家自己确定其具体的值, 以传递特殊消息。“Star-t code”标明其后面的数据是8 位控制信号数据帧。数据帧之间可以有时间间隔, 也可以没有; 同样, 数据包之间可以有时间间隔, 也可以没有。DMX512 协议规定“Break”信号、M. a. b 信号的最短时间, 并规定“Break”信号、M. a. b 信号、
数据帧之间及数据包之间的时间间隔的最大值不得超过1s, 否则做出错处理, 但是DMX512 协议并未对出错处理做任何规定。为了严格实现DMX512 数据的时序要求,“Break”和M. a. b信号我们可以用定时器来实现。
具体的UART配置如下:
- USART_InitStructure.USART_BaudRate = 250000;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_2;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART1, &USART_InitStructure);
复制代码
发送DMX512信号过程如下,先把UART的TX脚配置为普通的GPIO并输出低电平,然后启动定时器计时88us, 时器到后把TX脚置为高电平并计时8us, 时器到了后在配为UART模式用DMA方式把数据发出。
DMX512信号的接收是个难点,一般直接配为UART接收就行,不需要在UART模式和GPIO模式间切换,但需要在接收过程中检查接收到“Break”信号时的状态是有帧错误出现,并且接收数据全为零,这样的话可以确认已经收到“Break”信号,随后数据正常DMA接收就行了。
|
1. 使用已经调试好的例程,特别是参考官方例程包。
2. 使用KEIL的RTE配置串口,很好用。
3. 使用CubeMX配置串口,也很好用。