打印
[研电赛技术支持]

【GD32F303红枫派使用手册】第十六讲 USART-DMA串口收发实验

[复制链接]
166|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主

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
该寄存器虽然只有一个,但内部是映射为发送和接受两个寄存器。
发送时,除了发送数据寄存器,还有一个移位寄存器,当数据写入数据寄存器中,移位寄存器空闲的情况下,数据从数据寄存器中转移到移位寄存器,移位寄存器按照低bit——bit的顺序将数据移位到IO口上。
接收时,接收到的数据保存在数据寄存器中,CPUDMA可以从该寄存器中读接收到的数据。
状态寄存器0USART_STAT0  
我们需要特别理解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
C
Drv_Err driver_uart_dma_transmit(typdef_uart_struct *uartx,uint8_t *pbuff,uint16_t length)
{
    Drv_Err uart_state=DRV_ERROR;
   
    uint32_t timeout = driver_tick;   
    while(uartx->uart_control.Com_Flag.Bits.SendState==1){
        if((timeout+UART_TIMEOUT_MS) <= driver_tick) {              
            uartx->uart_control.Com_Flag.Bits.SendState=0;
            return DRV_ERROR;        
        }
    }      
    uartx->uart_control.Com_Flag.Bits.SendSucess=0;
    uartx->uart_control.Com_Flag.Bits.SendState=1;   
    uartx->uart_control.p_Send=pbuff;
    uartx->uart_control.SendSize=length;
    uartx->uart_control.SendCount=0;   
    uart_state=driver_dma_flag_wait_timeout(uartx->uart_tx_dma,DMA_FLAG_FTF,SET);     
    usart_dma_transmit_config(uartx->uart_x,USART_DENT_DISABLE);   
    driver_dma_start(uartx->uart_tx_dma,pbuff,length);   
    usart_flag_clear(uartx->uart_x,USART_FLAG_TC);   
    usart_dma_transmit_config(uartx->uart_x,USART_DENT_ENABLE);   
    usart_interrupt_enable(uartx->uart_x,USART_INT_TC);
    return uart_state;   
}
16.4.2 串口DMA接收函数
driver_uart.c中定义了串口DMA接收函数driver_uart_dma_receive
C
Drv_Err driver_uart_dma_receive(typdef_uart_struct *uartx,uint8_t *pbuff,uint16_t length)
{
    Drv_Err uart_state=DRV_SUCCESS;
    uint32_t timeout = driver_tick;
    while(uartx->uart_control.Com_Flag.Bits.RecState==1){
        if((timeout+UART_TIMEOUT_MS) <= driver_tick) {              
            uartx->uart_control.Com_Flag.Bits.RecState=0;
            return DRV_ERROR;        
        }
    }   
    uartx->uart_control.Com_Flag.Bits.RecSuccess=0;
    uartx->uart_control.Com_Flag.Bits.RecState=1;
    uartx->uart_control.p_Rec=pbuff;
    uartx->uart_control.RecSize=length;
    uartx->uart_control.RecCount=0;      
    usart_dma_receive_config(uartx->uart_x,USART_DENR_DISABLE);   
    driver_dma_start(uartx->uart_rx_dma,pbuff,length);
    USART_STAT0(uartx->uart_x);
    usart_data_receive(uartx->uart_x);
    usart_interrupt_flag_clear(uartx->uart_x,USART_INT_FLAG_IDLE);   
    usart_interrupt_enable(uartx->uart_x,USART_INT_IDLE);        
    usart_dma_receive_config(uartx->uart_x,USART_DENR_ENABLE);  
    return uart_state;     
}
16.4.3 main函数实现
以下为main函数代码:
C
int main(void)
{
    delay_init();

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

    //配置UART接受,最长100byte
    driver_uart_dma_receive(&BOARD_UART,uart_rec_buff,100);
   
        while (1)
        {
        //查询到接受完成回调函数标志
        if(uart_receive_complete_flag==SET)
        {
            uart_receive_complete_flag=RESET;
            
            //发送刚接受到的数据
            driver_uart_dma_transmit(&BOARD_UART,uart_send_buff,uart_receive_count);            
        }
        }
}
本例程main函数首先进行了延时函数初始化,再初始化UARTDMA模式,接着配置串口BOARD_UART,开启串口中断NVIC,这里使用到了IDLE中断,用来接受不定长数据,然后配置串口DMA接受,最长100个字节,所以我们可以给串口发送100个字节以下长度的数据。在while1)循环中循环查询uart_receive_complete_flag标志位,当该标志位为“SET”时,表示IDLE中断被触发,一帧数据接受完,最后将接收到的帧数据通过DMA发送方式原封不动发送到串口上。
16.4.4 中断函数
bsp_uart.c中定义了串口中断处理函数
C
void USART0_IRQHandler(void)
{
    driver_uart_int_handler(&BOARD_UART);
}
driver_uart.c中定义了driver_uart_int_handler函数:
C
Drv_Err driver_uart_int_handler(typdef_uart_struct *uartx)
{   
    Drv_Err uart_state=DRV_SUCCESS;   
    if(usart_interrupt_flag_get(uartx->uart_x,USART_INT_FLAG_RBNE)!=RESET)
    {
        if(uartx->uart_control.RecCount < uartx->uart_control.RecSize){
            uartx->uart_control.p_Rec[uartx->uart_control.RecCount]=usart_data_receive(uartx->uart_x);
            uartx->uart_control.RecCount++;            
        }
        else{
            usart_data_receive(uartx->uart_x);
            uart_state=DRV_ERROR;
            //err 溢出
        }
        if(uartx->uart_rbne_callback!=NULL){
            uartx->uart_rbne_callback(uartx);
        }        
        //callback
        if(uartx->uart_control.RecCount == uartx->uart_control.RecSize){
            uartx->uart_control.Com_Flag.Bits.RecSuccess=1;            
            uartx->uart_control.Com_Flag.Bits.RecState=0;
            uartx->uart_control.RecCount=0;            
        }        
    }      
    if(usart_interrupt_flag_get(uartx->uart_x,USART_INT_FLAG_IDLE)!=RESET)
    {
        usart_interrupt_flag_clear(uartx->uart_x,USART_INT_FLAG_IDLE);
        USART_STAT0(uartx->uart_x);
        USART_DATA(uartx->uart_x);
        
        if( (uartx->uart_mode_rx==MODE_INT && uartx->uart_control.RecCount>0) \
            ||(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))
        {
            uartx->uart_control.Com_Flag.Bits.RecSuccess=1;
            uartx->uart_control.Com_Flag.Bits.RecState=0;              
            
            if(uartx->uart_mode_rx==MODE_DMA){
                uartx->uart_control.RecCount=uartx->uart_control.RecSize-dma_transfer_number_get(uartx->uart_rx_dma->dmax,uartx->uart_rx_dma->dma_chx);            
            }
            //callback            
            if(uartx->uart_idle_callback!=NULL){
                uartx->uart_idle_callback(uartx);
            }                                
        }        
            
    }
   
    if(usart_interrupt_flag_get(uartx->uart_x,USART_INT_FLAG_TBE)!=RESET)
    {
        usart_data_transmit(uartx->uart_x,uartx->uart_control.p_Send[uartx->uart_control.SendCount]);
        uartx->uart_control.SendCount++;
        
        if(uartx->uart_tbe_callback!=NULL){
            uartx->uart_tbe_callback(uartx);
        }
        
        if(uartx->uart_control.SendCount >= uartx->uart_control.SendSize)
        {
            uartx->uart_control.SendCount=0;            
            usart_interrupt_disable(uartx->uart_x, USART_INT_TBE);
            usart_interrupt_enable(uartx->uart_x, USART_INT_TC);
        }
    }

    if(usart_interrupt_flag_get(uartx->uart_x,USART_INT_FLAG_TC)!=RESET)
    {
        usart_interrupt_disable(uartx->uart_x, USART_INT_TC);         
        usart_flag_clear(uartx->uart_x,USART_FLAG_TC);  

        if( !(uartx->uart_mode_rx==MODE_DMA && dma_transfer_number_get(uartx->uart_tx_dma->dmax,uartx->uart_tx_dma->dma_chx)!=0) )
        {   
            uartx->uart_control.Com_Flag.Bits.SendSucess=1;            
            uartx->uart_control.Com_Flag.Bits.SendState=0;
            
            if(uartx->uart_tc_callback!=NULL){
                uartx->uart_tc_callback(uartx);
            }         
            
            uartx->uart_control.SendCount=0;
        }
    }  

    if(usart_flag_get(uartx->uart_x,USART_FLAG_ORERR)==SET)
    {
        usart_flag_clear(uartx->uart_x,USART_FLAG_ORERR);        
        USART_STAT0(uartx->uart_x);
        USART_DATA(uartx->uart_x);
        uart_state=DRV_ERROR;
    }

    return uart_state;     

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

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

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

65

主题

85

帖子

2

粉丝