【GD32H757Z海棠派开发板使用手册】第十讲 USART-中断串口收发实验

描述

GD32

10.1 实验内容

通过本实验主要学习以下内容:

  • 使用中断进行串口收发

10.2 实验原理

10.2.1 串口寄存器介绍

串口有几个非常重要的寄存器需要读者理解。

数据接收寄存器(USART_RDATA)

GD32

数据发送寄存器(USART_RDATA)

GD32

发送时,除了发送数据寄存器,还有一个移位寄存器,当数据写入数据寄存器中,移位寄存器空闲的情况下,数据从数据寄存器中转移到移位寄存器,移位寄存器按照低bit——高bit的顺序将数据移位到IO口上。

接收时,接收到的数据保存在数据接收寄存器中,CPU或DMA可以从该寄存器中读接收到的数据。

状态寄存器(USART_STAT  )

GD32

我们需要特别理解TBE、TC、RBNE、IDLE、OREE这几位。

  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”。

以上就是串口寄存器的介绍。本实验就是使用TBE中断和RBNE中断来实现中断收发数据,实验原理是RBNE中断用来接受数据,IDLE中断用于判断发送方数据结束,TBE中断用于发送数据。

10.3 硬件设计

本实验使用P1接口的PA9和PA10实现串口功能,硬件设计请见上一章。

10.4 代码解析

10.4.1 串口中断发送函数

在driver_uart.c中定义了串口中断发送函数:

C
Drv_Err driver_uart_int_transmit(typdef_uart_struct *uartx,uint8_t *pbuff,uint16_t length)
{
    __IO uint64_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.SendSuccess=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;
    
    usart_flag_clear(uartx->uart_x,USART_FLAG_TC);
    usart_interrupt_enable(uartx->uart_x,USART_INT_TBE);
    
    return DRV_SUCCESS;    
}

10.4.2 串口中断接受函数

在driver_uart.c中定义了串口中断接受函数:

C
Drv_Err driver_uart_int_receive(typdef_uart_struct *uartx,uint8_t *pbuff,uint16_t length)
{    
    __IO uint64_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;        
        } 
    }

    if(usart_flag_get(uartx->uart_x,USART_FLAG_ORERR))
    {
        usart_flag_clear(uartx->uart_x,USART_FLAG_ORERR);      
    }    
    
    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_flag_clear(uartx->uart_x,USART_FLAG_IDLE);
    
    usart_interrupt_enable(uartx->uart_x,USART_INT_RBNE);
    usart_interrupt_enable(uartx->uart_x,USART_INT_IDLE);
    
    return DRV_SUCCESS;    
}

10.4.3 main函数实现

以下为main函数代码:

C
int main(void)
{
    //延时、共用驱动部分初始化    
    driver_init(); 
    //初始化UART为中断模式,注册接受完成(IDLE)回调函数
    BOARD_UART.uart_mode_tx=MODE_INT;
    BOARD_UART.uart_mode_rx=MODE_INT;
    BOARD_UART.uart_idle_callback=user_receive_complete_callback;       
    bsp_uart_init(&BOARD_UART);

    bsp_led_init(&LED2);     
    bsp_led_init(&LED1); 
    bsp_led_on(&LED2);
    bsp_led_off(&LED1);    
    //使能UART中断    
    nvic_irq_enable(USART0_IRQn,2,0); 
    delay_ms(100);
    printf_log("uart interrupt mode sends and receives loopback packets of indefinite length.\r\n"); 
    //启动UART中断接受,最长100byte
    driver_uart_int_receive(&BOARD_UART,uart_rec_buff,100);
    
    while (1)
    {
        //查询到接受完成回调函数标志
        if(uart_receive_complete_flag==SET)
        {
            uart_receive_complete_flag=RESET;
            //启动中断方式发送刚接受到的数据
            driver_uart_int_transmit(&BOARD_UART,uart_send_buff,uart_receive_count);  
            printf_log("\r\n The received data is %s\r\n",uart_send_buff);  
            memset(uart_send_buff,0,100); 
        }
    }
}
 

本例程main函数首先进行了延时函数初始化,再初始化UART为中断模式,接着配置串口BOARD_UART,开启串口中断NVIC,这里使用到了IDLE中断,TBE中断和RBNE中断,然后配置串口D中断接受,最长100个字节,所以我们可以给串口发送100个字节以下长度的数据。在while(1)循环中循环查询uart_receive_complete_flag标志位,当该标志位为“SET”时,表示IDLE中断被触发,一帧数据接受完,最后将接收到的帧数据通过中断发送方式原封不动发送到串口上。

10.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);      
        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.SendSuccess=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);        
        uart_state=DRV_ERROR;
    }

    return uart_state;     

}

10.5 实验结果

使用串口调试助手发送一帧数据到MCU,MCU会将这帧数据回发到串口调试助手中。

GD32GD32

 

教程GD32 MCU方案商聚沃科技原创发布,了解更多GD32 MCU教程,关注聚沃科技官网

 

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分