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


    16.1 实验内容

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

    • 串口DMA工作原理
    • 使用DMA进行串口收发

    16.2 实验原理

    16.2.1 串口DMA工作原理

    在前面ADC章节中,我们介绍了DMA的工作原理,这里就不多做介绍。从GD32F303用户手册中可以查到,各串口的TX和RX分别对应DMA的不同通道,比如USART0的TX对应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口上。

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

    状态寄存器0(USART_STAT0  )

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

    1. TBE(发送空):这个位置“1”表示现在可以往数据寄存器中写数据了,当移位寄存器空闲时,写入到数据寄存器中的数据则会转移到移位寄存器中,串口开始对外发送数据;
    1. TC(发送完成):发送数据时,当数据寄存器和移位寄存器都为空时,表示所有的数据都已经完成了,则TC置“1”,所以当连续发数据时,最后一个字节从移位寄存器中发送完,TC才会置起。
    1. RBNE(接受非空):当串口接受到一个字节数据,RBNE置“1”,此时CPU可以去数据寄存器中取数据,当使用了DMA接受,DMA自动将数据寄存器中数据搬走,当数据寄存器数据被读走/搬走,RBNE位自动清“0”;
    1. IDLE(空闲):该标志位用于检测接受空闲,当串口接受最后一个字节后,再往后一个字节时间内,没有接受到新的数据,则该位置“1”;

    IDLE一般用于串口DMA接受中,DMA接受中,MCU无法知道发送方的数据个数,所以可以通过判断IDLE位(或IDLE中断)来判断发送方一帧数据发送结束了。

    1. OREE(溢出错误):当RBNE置位的情况,又接收到一个字节数据,则OREE位置“1”。

    16.3 硬件设计

    本实验使用DMA进行串口发送和接收,仍然使用USB转UART接口,硬件设计见上一章。

    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. uint32_t timeout = driver_tick;
    6. while(uartx->uart_control.Com_Flag.Bits.SendState==1){
    7. if((timeout+UART_TIMEOUT_MS) <= driver_tick) {
    8. uartx->uart_control.Com_Flag.Bits.SendState=0;
    9. return DRV_ERROR;
    10. }
    11. }
    12. uartx->uart_control.Com_Flag.Bits.SendSucess=0;
    13. uartx->uart_control.Com_Flag.Bits.SendState=1;
    14. uartx->uart_control.p_Send=pbuff;
    15. uartx->uart_control.SendSize=length;
    16. uartx->uart_control.SendCount=0;
    17. uart_state=driver_dma_flag_wait_timeout(uartx->uart_tx_dma,DMA_FLAG_FTF,SET);
    18. usart_dma_transmit_config(uartx->uart_x,USART_DENT_DISABLE);
    19. driver_dma_start(uartx->uart_tx_dma,pbuff,length);
    20. usart_flag_clear(uartx->uart_x,USART_FLAG_TC);
    21. usart_dma_transmit_config(uartx->uart_x,USART_DENT_ENABLE);
    22. usart_interrupt_enable(uartx->uart_x,USART_INT_TC);
    23. return uart_state;
    24. }

     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. while (1)
    16. {
    17. //查询到接受完成回调函数标志
    18. if(uart_receive_complete_flag==SET)
    19. {
    20. uart_receive_complete_flag=RESET;
    21. //发送刚接受到的数据
    22. driver_uart_dma_transmit(&BOARD_UART,uart_send_buff,uart_receive_count);
    23. }
    24. }
    25. }

     本例程main函数首先进行了延时函数初始化,再初始化UART为DMA模式,接着配置串口BOARD_UART,开启串口中断NVIC,这里使用到了IDLE中断,用来接受不定长数据,然后配置串口DMA接受,最长100个字节,所以我们可以给串口发送100个字节以下长度的数据。在while(1)循环中循环查询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. if( (uartx->uart_mode_rx==MODE_INT && uartx->uart_control.RecCount>0) \
    32. ||(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))
    33. {
    34. uartx->uart_control.Com_Flag.Bits.RecSuccess=1;
    35. uartx->uart_control.Com_Flag.Bits.RecState=0;
    36. if(uartx->uart_mode_rx==MODE_DMA){
    37. uartx->uart_control.RecCount=uartx->uart_control.RecSize-dma_transfer_number_get(uartx->uart_rx_dma->dmax,uartx->uart_rx_dma->dma_chx);
    38. }
    39. //callback
    40. if(uartx->uart_idle_callback!=NULL){
    41. uartx->uart_idle_callback(uartx);
    42. }
    43. }
    44. }
    45. if(usart_interrupt_flag_get(uartx->uart_x,USART_INT_FLAG_TBE)!=RESET)
    46. {
    47. usart_data_transmit(uartx->uart_x,uartx->uart_control.p_Send[uartx->uart_control.SendCount]);
    48. uartx->uart_control.SendCount++;
    49. if(uartx->uart_tbe_callback!=NULL){
    50. uartx->uart_tbe_callback(uartx);
    51. }
    52. if(uartx->uart_control.SendCount >= uartx->uart_control.SendSize)
    53. {
    54. uartx->uart_control.SendCount=0;
    55. usart_interrupt_disable(uartx->uart_x, USART_INT_TBE);
    56. usart_interrupt_enable(uartx->uart_x, USART_INT_TC);
    57. }
    58. }
    59. if(usart_interrupt_flag_get(uartx->uart_x,USART_INT_FLAG_TC)!=RESET)
    60. {
    61. usart_interrupt_disable(uartx->uart_x, USART_INT_TC);
    62. usart_flag_clear(uartx->uart_x,USART_FLAG_TC);
    63. if( !(uartx->uart_mode_rx==MODE_DMA && dma_transfer_number_get(uartx->uart_tx_dma->dmax,uartx->uart_tx_dma->dma_chx)!=0) )
    64. {
    65. uartx->uart_control.Com_Flag.Bits.SendSucess=1;
    66. uartx->uart_control.Com_Flag.Bits.SendState=0;
    67. if(uartx->uart_tc_callback!=NULL){
    68. uartx->uart_tc_callback(uartx);
    69. }
    70. uartx->uart_control.SendCount=0;
    71. }
    72. }
    73. if(usart_flag_get(uartx->uart_x,USART_FLAG_ORERR)==SET)
    74. {
    75. usart_flag_clear(uartx->uart_x,USART_FLAG_ORERR);
    76. USART_STAT0(uartx->uart_x);
    77. USART_DATA(uartx->uart_x);
    78. uart_state=DRV_ERROR;
    79. }
    80. return uart_state;
    81. }

    16.5 实验结果

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

     

    由聚沃科技原创,来源于【红枫派开发板】第十六讲 USART-DMA串口收发实验 - 苏州聚沃电子科技有限公司 (gd32bbs.com) 

  • 相关阅读:
    Python+大数据-知行教育(七)-学生出勤主题看板
    CanvasJS JavaScript 3.7.1 Crack
    vue使用AES加解密
    AL-实体抽取主动学习调研
    Google Cloud Spanner的实践经验
    Javaweb重要知识点总结(六)常见的前端框架
    爱心动画效果,教你如何更改颜色背景的爱心
    JavaScript快速入门
    如何使用netlify部署vue应用程序
    数据仓库
  • 原文地址:https://blog.csdn.net/weixin_45254847/article/details/139697881