[研电赛技术支持] 【GD32F303红枫派使用手册】第十六讲 USART-DMA串口收发实验

[复制链接]
 楼主| 聚沃科技 发表于 2024-6-15 09:46 | 显示全部楼层 |阅读模式
红枫派首图.png
16.1 实验内容
通过本实验主要学习以下内容:
串口DMA工作原理
使用DMA进行串口收发
16.2 实验原理
16.2.1 串口DMA工作原理
在前面ADC章节中,我们介绍了DMA的工作原理,这里就不多做介绍。从GD32F303用户手册中可以查到,各串口的TXRX分别对应DMA的不同通道,比如USART0TX对应DMA0的通道3,而RX对应DMA0的通道4
当需要使用DMA发送时,需要配置DMA工作为内存到外设的模式,DMA目标地址需要设置为串口的数据寄存器,当DMA使能后,一旦串口的TBE(发送空)标志位为1,则DMA自动从内存中搬运数据到串口数据寄存器中。
当需要使用DMA接受时,需要配置DMA工作为外设到内存的模式,DMA的源地址需要设置为串口的数据寄存器,当DMA使能,一旦串口收到一个字节数据,RBNE(接受非空)标志位为1,则DMA自动将数据寄存器中的数据搬运到内存中。
16.2.2 串口寄存器介绍
串口有几个非常重要的寄存器需要读者理解,这里单独用一个章节来介绍。
数据寄存器(USART_DATA
图片1.png
该寄存器虽然只有一个,但内部是映射为发送和接受两个寄存器。
发送时,除了发送数据寄存器,还有一个移位寄存器,当数据写入数据寄存器中,移位寄存器空闲的情况下,数据从数据寄存器中转移到移位寄存器,移位寄存器按照低bit——bit的顺序将数据移位到IO口上。
接收时,接收到的数据保存在数据寄存器中,CPUDMA可以从该寄存器中读接收到的数据。
状态寄存器0USART_STAT0  
图片2.png
我们需要特别理解TBETCRBNEIDLEOREE这几位。
1. TBE(发送空):这个位置“1”表示现在可以往数据寄存器中写数据了,当移位寄存器空闲时,写入到数据寄存器中的数据则会转移到移位寄存器中,串口开始对外发送数据;
2. TC(发送完成):发送数据时,当数据寄存器和移位寄存器都为空时,表示所有的数据都已经完成了,则TC“1”,所以当连续发数据时,最后一个字节从移位寄存器中发送完,TC才会置起。
3. RBNE(接受非空):当串口接受到一个字节数据,RBNE“1”,此时CPU可以去数据寄存器中取数据,当使用了DMA接受,DMA自动将数据寄存器中数据搬走,当数据寄存器数据被读走/搬走,RBNE位自动清“0”
4. IDLE(空闲):该标志位用于检测接受空闲,当串口接受最后一个字节后,再往后一个字节时间内,没有接受到新的数据,则该位置“1”
IDLE一般用于串口DMA接受中,DMA接受中,MCU无法知道发送方的数据个数,所以可以通过判断IDLE位(或IDLE中断)来判断发送方一帧数据发送结束了。
5. OREE(溢出错误):当RBNE置位的情况,又接收到一个字节数据,则OREE位置“1”
16.3 硬件设计
本实验使用DMA进行串口发送和接收,仍然使用USBUART接口,硬件设计见上一章。
16.4 代码解析
16.4.1 串口DMA发送函数
driver_uart.c中定义了串口DMA发送函数driver_uart_dma_transmit
  1. C
  2. Drv_Err driver_uart_dma_transmit(typdef_uart_struct *uartx,uint8_t *pbuff,uint16_t length)
  3. {
  4.     Drv_Err uart_state=DRV_ERROR;
  5.    
  6.     uint32_t timeout = driver_tick;   
  7.     while(uartx->uart_control.Com_Flag.Bits.SendState==1){
  8.         if((timeout+UART_TIMEOUT_MS) <= driver_tick) {              
  9.             uartx->uart_control.Com_Flag.Bits.SendState=0;
  10.             return DRV_ERROR;        
  11.         }
  12.     }      
  13.     uartx->uart_control.Com_Flag.Bits.SendSucess=0;
  14.     uartx->uart_control.Com_Flag.Bits.SendState=1;   
  15.     uartx->uart_control.p_Send=pbuff;
  16.     uartx->uart_control.SendSize=length;
  17.     uartx->uart_control.SendCount=0;   
  18.     uart_state=driver_dma_flag_wait_timeout(uartx->uart_tx_dma,DMA_FLAG_FTF,SET);     
  19.     usart_dma_transmit_config(uartx->uart_x,USART_DENT_DISABLE);   
  20.     driver_dma_start(uartx->uart_tx_dma,pbuff,length);   
  21.     usart_flag_clear(uartx->uart_x,USART_FLAG_TC);   
  22.     usart_dma_transmit_config(uartx->uart_x,USART_DENT_ENABLE);   
  23.     usart_interrupt_enable(uartx->uart_x,USART_INT_TC);
  24.     return uart_state;   
  25. }
16.4.2 串口DMA接收函数
driver_uart.c中定义了串口DMA接收函数driver_uart_dma_receive
  1. C
  2. Drv_Err driver_uart_dma_receive(typdef_uart_struct *uartx,uint8_t *pbuff,uint16_t length)
  3. {
  4.     Drv_Err uart_state=DRV_SUCCESS;
  5.     uint32_t timeout = driver_tick;
  6.     while(uartx->uart_control.Com_Flag.Bits.RecState==1){
  7.         if((timeout+UART_TIMEOUT_MS) <= driver_tick) {              
  8.             uartx->uart_control.Com_Flag.Bits.RecState=0;
  9.             return DRV_ERROR;        
  10.         }
  11.     }   
  12.     uartx->uart_control.Com_Flag.Bits.RecSuccess=0;
  13.     uartx->uart_control.Com_Flag.Bits.RecState=1;
  14.     uartx->uart_control.p_Rec=pbuff;
  15.     uartx->uart_control.RecSize=length;
  16.     uartx->uart_control.RecCount=0;      
  17.     usart_dma_receive_config(uartx->uart_x,USART_DENR_DISABLE);   
  18.     driver_dma_start(uartx->uart_rx_dma,pbuff,length);
  19.     USART_STAT0(uartx->uart_x);
  20.     usart_data_receive(uartx->uart_x);
  21.     usart_interrupt_flag_clear(uartx->uart_x,USART_INT_FLAG_IDLE);   
  22.     usart_interrupt_enable(uartx->uart_x,USART_INT_IDLE);        
  23.     usart_dma_receive_config(uartx->uart_x,USART_DENR_ENABLE);  
  24.     return uart_state;     
  25. }
16.4.3 main函数实现
以下为main函数代码:
  1. C
  2. int main(void)
  3. {
  4.     delay_init();

  5.     //初始化UART为DMA模式,注册接受完成(IDLE)回调函数
  6.     BOARD_UART.uart_mode_tx=MODE_DMA;
  7.     BOARD_UART.uart_mode_rx=MODE_DMA;
  8.     BOARD_UART.uart_idle_callback=user_receive_complete_callback;      
  9.     bsp_uart_init(&BOARD_UART);
  10.     nvic_irq_enable(USART0_IRQn,2,0);
  11.     delay_ms(1000);
  12.     printf("uart dma mode sends and receives loopback packets of indefinite length.\r\n");

  13.     //配置UART接受,最长100byte
  14.     driver_uart_dma_receive(&BOARD_UART,uart_rec_buff,100);
  15.    
  16.         while (1)
  17.         {
  18.         //查询到接受完成回调函数标志
  19.         if(uart_receive_complete_flag==SET)
  20.         {
  21.             uart_receive_complete_flag=RESET;
  22.             
  23.             //发送刚接受到的数据
  24.             driver_uart_dma_transmit(&BOARD_UART,uart_send_buff,uart_receive_count);            
  25.         }
  26.         }
  27. }
本例程main函数首先进行了延时函数初始化,再初始化UARTDMA模式,接着配置串口BOARD_UART,开启串口中断NVIC,这里使用到了IDLE中断,用来接受不定长数据,然后配置串口DMA接受,最长100个字节,所以我们可以给串口发送100个字节以下长度的数据。在while1)循环中循环查询uart_receive_complete_flag标志位,当该标志位为“SET”时,表示IDLE中断被触发,一帧数据接受完,最后将接收到的帧数据通过DMA发送方式原封不动发送到串口上。
16.4.4 中断函数
bsp_uart.c中定义了串口中断处理函数
  1. C
  2. void USART0_IRQHandler(void)
  3. {
  4.     driver_uart_int_handler(&BOARD_UART);
  5. }
driver_uart.c中定义了driver_uart_int_handler函数:
  1. C
  2. Drv_Err driver_uart_int_handler(typdef_uart_struct *uartx)
  3. {   
  4.     Drv_Err uart_state=DRV_SUCCESS;   
  5.     if(usart_interrupt_flag_get(uartx->uart_x,USART_INT_FLAG_RBNE)!=RESET)
  6.     {
  7.         if(uartx->uart_control.RecCount < uartx->uart_control.RecSize){
  8.             uartx->uart_control.p_Rec[uartx->uart_control.RecCount]=usart_data_receive(uartx->uart_x);
  9.             uartx->uart_control.RecCount++;            
  10.         }
  11.         else{
  12.             usart_data_receive(uartx->uart_x);
  13.             uart_state=DRV_ERROR;
  14.             //err 溢出
  15.         }
  16.         if(uartx->uart_rbne_callback!=NULL){
  17.             uartx->uart_rbne_callback(uartx);
  18.         }        
  19.         //callback
  20.         if(uartx->uart_control.RecCount == uartx->uart_control.RecSize){
  21.             uartx->uart_control.Com_Flag.Bits.RecSuccess=1;            
  22.             uartx->uart_control.Com_Flag.Bits.RecState=0;
  23.             uartx->uart_control.RecCount=0;            
  24.         }        
  25.     }      
  26.     if(usart_interrupt_flag_get(uartx->uart_x,USART_INT_FLAG_IDLE)!=RESET)
  27.     {
  28.         usart_interrupt_flag_clear(uartx->uart_x,USART_INT_FLAG_IDLE);
  29.         USART_STAT0(uartx->uart_x);
  30.         USART_DATA(uartx->uart_x);
  31.         
  32.         if( (uartx->uart_mode_rx==MODE_INT && uartx->uart_control.RecCount>0) \
  33.             ||(uartx->uart_mode_rx==MODE_DMA && dma_transfer_number_get(uartx->uart_rx_dma->dmax,uartx->uart_rx_dma->dma_chx)!=uartx->uart_control.RecSize))
  34.         {
  35.             uartx->uart_control.Com_Flag.Bits.RecSuccess=1;
  36.             uartx->uart_control.Com_Flag.Bits.RecState=0;              
  37.             
  38.             if(uartx->uart_mode_rx==MODE_DMA){
  39.                 uartx->uart_control.RecCount=uartx->uart_control.RecSize-dma_transfer_number_get(uartx->uart_rx_dma->dmax,uartx->uart_rx_dma->dma_chx);            
  40.             }
  41.             //callback            
  42.             if(uartx->uart_idle_callback!=NULL){
  43.                 uartx->uart_idle_callback(uartx);
  44.             }                                
  45.         }        
  46.             
  47.     }
  48.    
  49.     if(usart_interrupt_flag_get(uartx->uart_x,USART_INT_FLAG_TBE)!=RESET)
  50.     {
  51.         usart_data_transmit(uartx->uart_x,uartx->uart_control.p_Send[uartx->uart_control.SendCount]);
  52.         uartx->uart_control.SendCount++;
  53.         
  54.         if(uartx->uart_tbe_callback!=NULL){
  55.             uartx->uart_tbe_callback(uartx);
  56.         }
  57.         
  58.         if(uartx->uart_control.SendCount >= uartx->uart_control.SendSize)
  59.         {
  60.             uartx->uart_control.SendCount=0;            
  61.             usart_interrupt_disable(uartx->uart_x, USART_INT_TBE);
  62.             usart_interrupt_enable(uartx->uart_x, USART_INT_TC);
  63.         }
  64.     }

  65.     if(usart_interrupt_flag_get(uartx->uart_x,USART_INT_FLAG_TC)!=RESET)
  66.     {
  67.         usart_interrupt_disable(uartx->uart_x, USART_INT_TC);         
  68.         usart_flag_clear(uartx->uart_x,USART_FLAG_TC);  

  69.         if( !(uartx->uart_mode_rx==MODE_DMA && dma_transfer_number_get(uartx->uart_tx_dma->dmax,uartx->uart_tx_dma->dma_chx)!=0) )
  70.         {   
  71.             uartx->uart_control.Com_Flag.Bits.SendSucess=1;            
  72.             uartx->uart_control.Com_Flag.Bits.SendState=0;
  73.             
  74.             if(uartx->uart_tc_callback!=NULL){
  75.                 uartx->uart_tc_callback(uartx);
  76.             }         
  77.             
  78.             uartx->uart_control.SendCount=0;
  79.         }
  80.     }  

  81.     if(usart_flag_get(uartx->uart_x,USART_FLAG_ORERR)==SET)
  82.     {
  83.         usart_flag_clear(uartx->uart_x,USART_FLAG_ORERR);        
  84.         USART_STAT0(uartx->uart_x);
  85.         USART_DATA(uartx->uart_x);
  86.         uart_state=DRV_ERROR;
  87.     }

  88.     return uart_state;     

  89. }
16.5 实验结果
使用USB-TypeC线,连接电脑和板上USB to UART口后,使用串口调试助手发送一帧数据到MCUMCU会将这帧数据回发到串口调试助手中。
图片3.png
图片4.png

本教程由GD32 MCU方案商聚沃科技原创发布,了解更多GD32 MCU教程,关注聚沃科技官网,GD32MCU技术交流群:859440462

您需要登录后才可以回帖 登录 | 注册

本版积分规则

170

主题

190

帖子

13

粉丝
快速回复 在线客服 返回列表 返回顶部