RISC-V技术william hill官网
直播中

voidpbq

8年用户 131经验值
擅长:嵌入式技术,处理器/DSP
私信 关注
[经验]

【芯昇科技RISC-V生态开发板试用体验】3. uart读写

# 1 前言

本章介绍下如何使用uart的rx和tx功能,其中tx利用dma更为方便。

# 2 基础

参考文章:[STM32单片机串口空闲中断+DMA接收不定长数据](https://blog.csdn.net/qq_20222919/article/details/109091456)

uart一次只能收发一字节数据,所以发送相对简单点,直接用函数USART_SendData(),发送多个数据。
但是rx接收时需要等uart状态rx结束,读取到USART_FLAG_RXDNE状态可以收数据。
不过使用dma更为方便,使用usart目录下的DMA_polling示例。

简单介绍下dma:在内存中开辟一段数组空间,uart收到数据后将存入dma中,指针向后移动,直到填满后产生dma中断。
dma中断中可以配置清空等操作。

# 3 引脚介绍

整理了一下开发板的uart端口。
其中usart有三个,分别为usart1-3,uart有四个,分别为uart4-7。
usart相对uart多了时钟信号,用于同步。

其中uart4一般作为和电脑的串口通信,用于打印信息。printf函数显示log就通过uart4传输出来。

yjt.jpg

# 4 硬件

usart3和uart5-7,分别为PC10/11,PB13/14,PB0/1,PC4/5。

2.png

uart4根据威廉希尔官方网站 图来看是接到了PA2/3。推测PA2/3和PA13/12有关联,因为PA13/12是microusb数据线。

3.png

4.png

# 5 代码

主要参考两个示例,usart/printf是用于打印log,usart/dma_polling是用于dma接收数据。

printf示例:https://gitee.com/CMIOT-XinSheng ... amples/USART/Printf

示例源码路径:https://gitee.com/CMIOT-XinSheng ... s/USART/DMA_Polling

打印log使用的uart4,用到的io为PD0/1。
uart收发使用的uart5,用到io为PB13/14。


# 5.1 main.h
头文件定义uart引脚和dma信息。


  1. /*******************************************************************************
  2. *
  3. * COPYRIGHT(c) 2020, China Mobile IOT
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *        1. Redistributions of source code must retain the above copyright notice,
  8. *           this list of conditions and the following disclaimer.
  9. *        2. Redistributions in binary form must reproduce the above copyright notice,
  10. *           this list of conditions and the following disclaimer in the documentation
  11. *           and/or other materials provided with the distribution.
  12. *        3. Neither the name of China Mobile IOT nor the names of its contributors
  13. *           may be used to endorse or promote products derived from this software
  14. *           without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. *******************************************************************************/

  28. /**
  29. * [url=home.php?mod=space&uid=1455510]@file[/url] main.h
  30. * [url=home.php?mod=space&uid=40524]@author[/url] CMIOT Firmware Team
  31. * [url=home.php?mod=space&uid=644434]@version[/url] v1.0.0
  32. *
  33. * [url=home.php?mod=space&uid=855824]@copyright[/url] Copyright (c) 2020, CMIOT. All rights reserved.
  34. */

  35. #ifndef __MAIN_H__
  36. #define __MAIN_H__

  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif

  40. #include "nuclei_sdk_soc.h"

  41. /* USARTx configuration */
  42. #define _USART4_COM_

  43. #ifdef _USART4_COM_
  44. #define USARTx                  UART4
  45. #define USARTx_GPIO             GPIOD
  46. #define USARTx_CLK              RCC_APB1_PERIPH_UART4
  47. #define USARTx_GPIO_CLK                RCC_APB2_PERIPH_GPIOD
  48. #define USARTx_RxPin            GPIO_PIN_1
  49. #define USARTx_TxPin            GPIO_PIN_0

  50. #define GPIO_APBxClkCmd                RCC_EnableAPB2PeriphClk
  51. #define USART_APBxClkCmd        RCC_EnableAPB1PeriphClk
  52. #endif

  53. #define _USART5_USART6_

  54. #ifdef _USART5_USART6_
  55. #define USARTy                      UART5
  56. #define USARTy_GPIO                 GPIOB
  57. #define USARTy_CLK                  RCC_APB1_PERIPH_UART5
  58. #define USARTy_GPIO_CLK             RCC_APB2_PERIPH_GPIOB
  59. #define USARTy_TxPin                GPIO_PIN_13
  60. #define USARTy_RxPin                GPIO_PIN_14
  61. #define USARTy_APBxClkCmd           RCC_EnableAPB1PeriphClk
  62. #define USARTy_DMAx                 DMA1
  63. #define USARTy_DMAx_CLK             RCC_AHB_PERIPH_DMA1
  64. #define USARTy_DR_Base              (UART5_BASE + 0x04)
  65. #define USARTy_Tx_DMA_Channel       DMA1_CH1
  66. #define USARTy_Tx_DMA_FLAG          DMA1_FLAG_TC1
  67. #define USARTy_Rx_DMA_Channel       DMA1_CH8
  68. #define USARTy_Rx_DMA_FLAG          DMA1_FLAG_TC8
  69. #define USARTy_Tx_DMA_IRQn          DMA1_Channel1_IRQn
  70. #define USARTy_Tx_DMA_IRQHandler    DMA1_Channel1_IRQHandler
  71. #define USARTy_Tx_DMA_INT           DMA1_INT_TXC1

  72. #define USARTz                      UART6
  73. #define USARTz_GPIO                 GPIOB
  74. #define USARTz_CLK                  RCC_APB2_PERIPH_UART6
  75. #define USARTz_GPIO_CLK             RCC_APB2_PERIPH_GPIOB
  76. #define USARTz_TxPin                GPIO_PIN_0
  77. #define USARTz_RxPin                GPIO_PIN_1
  78. #define USARTz_APBxClkCmd           RCC_EnableAPB2PeriphClk
  79. #define USARTz_DMAx                 DMA2
  80. #define USARTz_DMAx_CLK             RCC_AHB_PERIPH_DMA2
  81. #define USARTz_DR_Base              (UART6_BASE + 0x04)
  82. #define USARTz_Tx_DMA_Channel       DMA2_CH2
  83. #define USARTz_Tx_DMA_FLAG          DMA2_FLAG_TC2
  84. #define USARTz_Rx_DMA_Channel       DMA2_CH1
  85. #define USARTz_Rx_DMA_FLAG          DMA2_FLAG_TC1
  86. #define USARTz_Rx_DMA_IRQn          DMA2_Channel1_IRQn
  87. #define USARTz_Rx_DMA_IRQHandler    DMA2_Channel1_IRQHandler
  88. #define USARTz_Rx_DMA_INT           DMA2_INT_TXC1
  89. #define USARTz_IRQn                       UART6_IRQn
  90. #endif

  91. #ifdef __cplusplus
  92. }
  93. #endif

  94. #endif /* __MAIN_H__ */


# 5.2 main.c

实现uart测试功能。

测试1:dma的tx传输
测试2:uart传输字符串
测试3:dma的rx接收


  1. /*******************************************************************************
  2. *
  3. * COPYRIGHT(c) 2020, China Mobile IOT
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *    1. Redistributions of source code must retain the above copyright notice,
  8. *       this list of conditions and the following disclaimer.
  9. *    2. Redistributions in binary form must reproduce the above copyright notice,
  10. *       this list of conditions and the following disclaimer in the documentation
  11. *       and/or other materials provided with the distribution.
  12. *    3. Neither the name of China Mobile IOT nor the names of its contributors
  13. *       may be used to endorse or promote products derived from this software
  14. *       without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. *******************************************************************************/

  28. /**
  29. * @file main.c
  30. * @author CMIOT Firmware Team
  31. * @version v1.0.0
  32. *
  33. * @copyright Copyright (c) 2020, CMIOT. All rights reserved.
  34. */

  35. #include
  36. #include "main.h"

  37. /** @addtogroup CM32M4xxR_StdPeriph_Examples
  38. * @{
  39. */

  40. /** @addtogroup USART_DMA_Polling
  41. * @{
  42. */

  43. typedef enum
  44. {
  45.     FAILED = 0,
  46.     PASSED = !FAILED
  47. } TestStatus;

  48. #define TxBufferSize1 (countof(TxBuffer1) - 1)
  49. #define TxBufferSize2 (countof(TxBuffer2) - 1)

  50. #define countof(a) (sizeof(a) / sizeof(*(a)))

  51. USART_InitType USART_InitStructure;
  52. uint8_t TxBuffer1[] = "USART DMA Polling: USARTy -> USARTz using DMA";
  53. uint8_t TxBuffer2[] = "USART DMA Polling: USARTz -> USARTy using DMA";
  54. uint8_t RxBuffer1[TxBufferSize2];
  55. uint8_t RxBuffer2[TxBufferSize1];
  56. volatile TestStatus TransferStatus1 = FAILED;
  57. volatile TestStatus TransferStatus2 = FAILED;

  58. void RCC_Configuration(void);
  59. void GPIO_Configuration(void);
  60. void DMA_Configuration(void);

  61. // youkai add
  62. void func_init_uart4();
  63. void print_dma();

  64. bool status_wait_uart = false;
  65. #define UART_STATUS_RX true
  66. #define UART_STATUS_TX false

  67. void print_uart_wait();
  68. void print_uart_wait_end();

  69. void reset_dma_rx();
  70. void reset_dma_tx();
  71. void myUart_rx(uint8_t uart_id,bool rx_tx);
  72. void myUart_tx(uint8_t uart_id,uint8_t * data,uint8_t len_send);
  73. void myUart_senddata(uint8_t uart_id, uint8_t * data,uint8_t len_send);

  74. void myDMA_tx(uint8_t uart_id);

  75. void delay(vu32 nCount);

  76. /**
  77. * [url=home.php?mod=space&uid=2666770]@Brief[/url]  Main program
  78. */
  79. int main(void)
  80. {
  81.     func_init_uart4();

  82.     printf("start initrn");

  83.     printf("-------test1: dma tx---------rn");

  84.     /* System Clocks Configuration */
  85.     RCC_Configuration();

  86.     /* Configure the GPIO ports */
  87.     GPIO_Configuration();

  88.     /* Configure the DMA */
  89.     DMA_Configuration();

  90.     /* USARTy and USARTz configuration ------------------------------------------------------*/
  91.     USART_InitStructure.BaudRate            = 115200;
  92.     USART_InitStructure.WordLength          = USART_WL_8B;
  93.     USART_InitStructure.StopBits            = USART_STPB_1;
  94.     USART_InitStructure.Parity              = USART_PE_NO;
  95.     USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
  96.     USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;

  97.     /* Configure USARTy and USARTz */
  98.     USART_Init(USARTy, &USART_InitStructure);
  99.     USART_Init(USARTz, &USART_InitStructure);

  100. //    strcpy(TxBuffer1,"hihihihihihihihihihihihihihihihihihihihihihih");
  101. //    printf("TxBuffer1 = [%s]rn",(char*)TxBuffer1);

  102.     /* Enable USARTy DMA Rx and TX request */
  103.     USART_EnableDMA(USARTy, USART_DMAREQ_RX | USART_DMAREQ_TX, ENABLE);

  104.     /* Enable USARTz DMA Rx and TX request */
  105.     USART_EnableDMA(USARTz, USART_DMAREQ_RX | USART_DMAREQ_TX, ENABLE);

  106.     /* Enable USARTy TX DMA1 Channel */
  107.    DMA_EnableChannel(USARTy_Tx_DMA_Channel, ENABLE);

  108.     /* Enable USARTy RX DMA1 Channel */
  109.     DMA_EnableChannel(USARTy_Rx_DMA_Channel, ENABLE);

  110.     /* Enable USARTz TX DMA1 Channel */
  111.     DMA_EnableChannel(USARTz_Tx_DMA_Channel, ENABLE);

  112.     /* Enable USARTz RX DMA1 Channel */
  113.     DMA_EnableChannel(USARTz_Rx_DMA_Channel, ENABLE);

  114.     /* Enable the USARTy and USARTz */
  115.     USART_Enable(USARTy, ENABLE);
  116.     USART_Enable(USARTz, ENABLE);

  117.     // wait until dma send data ok.
  118.     while(DMA_GetFlagStatus(DMA1_FLAG_TC1,USARTy_DMAx)==RESET)
  119.     {
  120.     }

  121.     printf("rn-------test2: uart tx---------rn");
  122.     delay(1000);

  123.     printf("dma tx over.rn");

  124. //    USART_SendData(USARTy,'n');
  125. //    delay();
  126.     char temp_rx[]="hihihi";
  127.     myUart_tx(5,temp_rx,4);
  128. //    reset_dma_rx();

  129.     printf("rn-------test3: uart rx---------rn");
  130.     int count = 0;
  131.     while (1)
  132.     {
  133.         printf("count = %drn",count++);

  134.         myUart_rx(5,UART_STATUS_RX);

  135.     }
  136. }

  137. /**
  138. * @brief  Configures the different system clocks.
  139. */
  140. void RCC_Configuration(void)
  141. {
  142.     /* DMA clock enable */
  143.     RCC_EnableAHBPeriphClk(USARTy_DMAx_CLK, ENABLE);
  144.     RCC_EnableAHBPeriphClk(USARTz_DMAx_CLK, ENABLE);

  145.     /* Enable GPIO clock */
  146.     RCC_EnableAPB2PeriphClk(USARTy_GPIO_CLK | USARTz_GPIO_CLK | RCC_APB2_PERIPH_AFIO, ENABLE);

  147.     /* Enable USARTy and USARTz Clock */
  148.     USARTy_APBxClkCmd(USARTy_CLK, ENABLE);
  149.     USARTz_APBxClkCmd(USARTz_CLK, ENABLE);
  150. }

  151. /**
  152. * @brief  Configures the different GPIO ports.
  153. */
  154. void GPIO_Configuration(void)
  155. {
  156.     GPIO_InitType GPIO_InitStructure;
  157.     GPIO_ConfigPinRemap(GPIO_RMP_USART1,ENABLE);

  158.     /* Configure USARTy Rx as input floating */
  159.     GPIO_InitStructure.Pin       = USARTy_RxPin;
  160.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  161.     GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);

  162.     /* Configure USARTz Rx as input floating */
  163.     GPIO_InitStructure.Pin = USARTz_RxPin;
  164.     GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);

  165.     /* Configure USARTy Tx as alternate function push-pull */
  166.     GPIO_InitStructure.Pin        = USARTy_TxPin;
  167.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  168.     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
  169.     GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);

  170.     /* Configure USARTz Tx as alternate function push-pull */
  171.     GPIO_InitStructure.Pin = USARTz_TxPin;
  172.     GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);

  173.     GPIO_ConfigPinRemap(GPIO_RMP1_UART5, ENABLE);
  174.     GPIO_ConfigPinRemap(GPIO_RMP3_UART6, ENABLE);
  175. }

  176. /**
  177. * @brief  Configures the DMA.
  178. */
  179. void DMA_Configuration(void)
  180. {
  181.     DMA_InitType DMA_InitStructure;

  182.     /* USARTy TX DMA1 Channel (triggered by USARTy Tx event) Config */
  183.     DMA_DeInit(USARTy_Tx_DMA_Channel);
  184.     DMA_InitStructure.PeriphAddr     = USARTy_DR_Base;
  185.     DMA_InitStructure.MemAddr        = (uint32_t)TxBuffer1;
  186.     DMA_InitStructure.Direction      = DMA_DIR_PERIPH_DST;
  187.     DMA_InitStructure.BufSize        = TxBufferSize1;
  188.     DMA_InitStructure.PeriphInc      = DMA_PERIPH_INC_DISABLE;
  189.     DMA_InitStructure.DMA_MemoryInc  = DMA_MEM_INC_ENABLE;
  190.     DMA_InitStructure.PeriphDataSize = DMA_PERIPH_DATA_SIZE_BYTE;
  191.     DMA_InitStructure.MemDataSize    = DMA_MEMORY_DATA_SIZE_BYTE;
  192.     DMA_InitStructure.CircularMode   = DMA_MODE_NORMAL;
  193.     DMA_InitStructure.Priority       = DMA_PRIORITY_VERY_HIGH;
  194.     DMA_InitStructure.Mem2Mem        = DMA_M2M_DISABLE;
  195.     DMA_Init(USARTy_Tx_DMA_Channel, &DMA_InitStructure);

  196.     /* USARTy RX DMA1 Channel (triggered by USARTy Rx event) Config */
  197.     DMA_DeInit(USARTy_Rx_DMA_Channel);
  198.     DMA_InitStructure.PeriphAddr = USARTy_DR_Base;
  199.     DMA_InitStructure.MemAddr    = (uint32_t)RxBuffer1;
  200.     DMA_InitStructure.Direction  = DMA_DIR_PERIPH_SRC;
  201.     DMA_InitStructure.BufSize    = TxBufferSize2;
  202.     DMA_Init(USARTy_Rx_DMA_Channel, &DMA_InitStructure);

  203.     /* USARTz TX DMA1 Channel (triggered by USARTz Tx event) Config */
  204.     DMA_DeInit(USARTz_Tx_DMA_Channel);
  205.     DMA_InitStructure.PeriphAddr = USARTz_DR_Base;
  206.     DMA_InitStructure.MemAddr    = (uint32_t)TxBuffer2;
  207.     DMA_InitStructure.Direction  = DMA_DIR_PERIPH_DST;
  208.     DMA_InitStructure.BufSize    = TxBufferSize2;
  209.     DMA_Init(USARTz_Tx_DMA_Channel, &DMA_InitStructure);

  210.     /* USARTz RX DMA1 Channel (triggered by USARTz Rx event) Config */
  211.     DMA_DeInit(USARTz_Rx_DMA_Channel);
  212.     DMA_InitStructure.PeriphAddr = USARTz_DR_Base;
  213.     DMA_InitStructure.MemAddr    = (uint32_t)RxBuffer2;
  214.     DMA_InitStructure.Direction  = DMA_DIR_PERIPH_SRC;
  215.     DMA_InitStructure.BufSize    = TxBufferSize1;
  216.     DMA_Init(USARTz_Rx_DMA_Channel, &DMA_InitStructure);
  217. }



  218. void func_init_uart4()
  219. {
  220.     //RCC_Configuration
  221.     /* Enable GPIO clock */
  222.     GPIO_APBxClkCmd(USARTx_GPIO_CLK | RCC_APB2_PERIPH_AFIO, ENABLE);

  223.     /* Enable USARTy and USARTz Clock */
  224.     USART_APBxClkCmd(USARTx_CLK, ENABLE);

  225.     //GPIO_Configuration
  226.     GPIO_InitType GPIO_InitStructure;
  227.     GPIO_ConfigPinRemap(GPIO_RMP_USART1,ENABLE);

  228.     /* Configure USARTx Tx as alternate function push-pull */
  229.     GPIO_InitStructure.Pin        = USARTx_TxPin;
  230.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  231.     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
  232.     GPIO_Init(USARTx_GPIO, &GPIO_InitStructure);

  233.     /* Configure USARTx Rx as input floating */
  234.     GPIO_InitStructure.Pin       = USARTx_RxPin;
  235.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  236.     GPIO_Init(USARTx_GPIO, &GPIO_InitStructure);

  237.     GPIO_ConfigPinRemap(GPIO_RMP3_UART4, ENABLE);

  238.     //uart
  239.     /* USARTy and USARTz configuration ------------------------------------------------------*/
  240.     USART_InitStructure.BaudRate            = 115200;
  241.     USART_InitStructure.WordLength          = USART_WL_8B;
  242.     USART_InitStructure.StopBits            = USART_STPB_1;
  243.     USART_InitStructure.Parity              = USART_PE_NO;
  244.     USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
  245.     USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;

  246.     /* Configure USARTx */
  247.     USART_Init(USARTx, &USART_InitStructure);

  248.     /* Enable the USARTx */
  249.     USART_Enable(USARTx, ENABLE);

  250. }


  251. void print_dma()
  252. {
  253. //    printf("TxBuffer1 = [%s]rn",(char*)TxBuffer1);
  254. //    printf("TxBuffer2 = [%s]rn",(char*)TxBuffer2);
  255.     printf("RxBuffer1 = [%s]rn",(char*)RxBuffer1);
  256. //    printf("RxBuffer2 = [%s]rn",(char*)RxBuffer2);
  257.     printf("rn");
  258. }



  259. void print_uart_wait()
  260. {
  261.     if(status_wait_uart == true)
  262.     {
  263.         status_wait_uart = false;
  264.         printf("uart transfer waiting......rn");
  265.     }
  266. }

  267. void print_uart_wait_end()
  268. {
  269.     status_wait_uart = true;
  270.     printf("end uart wait.rn");
  271. }

  272. void myDMA_tx(uint8_t uart_id)
  273. {

  274.     // choose dma
  275.     DMA_Module* dma =  USARTy_Tx_DMA_Channel;
  276.     uint32_t dma_flag = DMA1_FLAG_TC1;
  277.     switch(uart_id)
  278.     {
  279.     case 5:
  280.             dma =  USARTy_Tx_DMA_Channel;
  281.             dma_flag= DMA1_FLAG_TC1;
  282.             break;
  283.     case 6:
  284.             dma =  USARTz_Tx_DMA_Channel;
  285.             dma_flag = DMA2_FLAG_TC8;
  286.             break;
  287.     default:
  288.             printf("use default uart5rn");
  289.             dma =  USARTy_Tx_DMA_Channel;
  290.             dma_flag = DMA1_FLAG_TC1;
  291.             break;
  292.     }

  293.     printf("sendrn");
  294.     // tx : send uart data to other dev
  295.     while (DMA_GetFlagStatus(dma_flag, dma) == RESET)
  296.     {
  297.         print_uart_wait();
  298.     }
  299.     print_uart_wait_end();



  300. }

  301. void myUart_senddata(uint8_t uart_id,uint8_t* data, uint8_t len_send)
  302. {
  303.     uint8_t * temp_value = data;
  304.     uint8_t temp_len = len_send;

  305.     printf("len_send = %d, strvalue = [%s]rn",temp_len,(char * )data);

  306.     // choose uart
  307.     USART_Module* uart =  USARTy;
  308.     switch(uart_id)
  309.     {
  310.     case 5:
  311.             uart = USARTy;
  312.             break;
  313.     case 6:
  314.             uart = USARTz;
  315.             break;
  316.     default:
  317.             printf("use default uart5rn");
  318.             uart = USARTy;
  319.             break;
  320.     }

  321.     int i=0;
  322.     while(temp_len > i)
  323.     {
  324. //        printf("i = %d, value = %02xrn",i,*temp_value);
  325.         USART_SendData(uart,*temp_value);

  326.         // wait until data has send
  327.         while(USART_GetFlagStatus(uart,USART_FLAG_TXDE)==RESET)
  328.         {

  329.         }

  330.         i++;
  331.         temp_value++;
  332.     }
  333.     return;
  334. }

  335. void myUart_tx(uint8_t uart_id,uint8_t * data,uint8_t len_send)
  336. {
  337.     int len_str = strlen(data);

  338.     bool str_data = len_send>=len_str;

  339.     // if send overside strlen, just send strlen
  340.     int len = str_data? len_str:len_send;
  341.     printf("strlen = %d, send len = %d, send>strlen = %srn",len_str,len_send,
  342.             str_data?"true":"false");

  343.     if(str_data == true)
  344.     {
  345.         printf("send data len overside str_len.rn");
  346.     }
  347.     else
  348.     {
  349.         printf("send data len = %d.rn",len_send);
  350.     }

  351.     myUart_senddata(uart_id,data,len);
  352. }

  353. void myUart_rx(uint8_t uart_id,bool rx_tx)
  354. {
  355.     USART_Module * uart = USARTy;
  356.     DMA_Module * dma = USARTy_Rx_DMA_Channel;

  357.     switch(uart_id)
  358.     {
  359.     case 5:
  360.             uart = USARTy;
  361.             dma = USARTy_Rx_DMA_Channel;
  362.             break;
  363.     case 6:
  364.             uart = USARTz;
  365.             dma = USARTz_Rx_DMA_Channel;
  366.             break;
  367.     default:
  368.             printf("use default uart5rn");
  369.             uart = USARTy;
  370.             break;
  371.     }

  372. //    printf("receivern");
  373.     // rx : get uart data from other dev
  374.     /* Wait until USARTy get idlef */
  375.     while (USART_GetFlagStatus(uart, USART_FLAG_IDLEF) == RESET)
  376.     {
  377.         print_uart_wait();
  378.     }
  379.     print_uart_wait_end();

  380.     printf("uart is idlefrn");

  381.     int dma_last = DMA_GetCurrDataCounter(USARTy_Rx_DMA_Channel);
  382.     printf("dma get count =%d rn",dma_last);

  383.     printf("has recv data len = %drn",TxBufferSize2-dma_last);

  384.     reset_dma_rx();
  385. }

  386. void reset_dma_rx()
  387. {
  388.     print_dma();

  389.     memset(RxBuffer1,0x00,sizeof(RxBuffer1));
  390.     DMA_EnableChannel(USARTy_Rx_DMA_Channel, DISABLE);

  391.     DMA_SetCurrDataCounter(USARTy_Rx_DMA_Channel,TxBufferSize1);

  392.     DMA_EnableChannel(USARTy_Rx_DMA_Channel, ENABLE);

  393.     // clear idlef flag
  394.     USART_ReceiveData(USARTy);
  395. }

  396. void reset_dma_tx()
  397. {
  398.     print_dma();

  399.     memset(TxBuffer1,0x00,sizeof(TxBuffer1));
  400.     DMA_EnableChannel(USARTy_Rx_DMA_Channel, DISABLE);

  401.     DMA_SetCurrDataCounter(USARTy_Rx_DMA_Channel,TxBufferSize1);

  402.     DMA_EnableChannel(USARTy_Rx_DMA_Channel, ENABLE);

  403.     // clear idlef flag
  404.     USART_ReceiveData(USARTy);
  405. }

  406. /**
  407. * @brief  Delay function.
  408. */
  409. void delay(vu32 nCount)
  410. {
  411.     vu32 index = 0;
  412.     for (index = (34000 * nCount); index != 0; index--)
  413.     {
  414.     }
  415. }


# 6 结果


rx_结果.png

可以看到,串口接收工具
1. 收到dma传输的字符串
2. 收到通过uart函数传输的字符串
3. 向uart5发出不定长度的数据,log中显示出来获取的数据及长度。

# 7 使用函数

USART_SendData:uart传输数据tx
USART_GetFlagStatus:获取uart状态,根据传入的uart和flag,返回set表示该flag已经设置,则跳出while循环

DMA_GetCurrDataCounter:获取dma剩余空间长度,用于计算当前收到了多少数据。

DMA_EnableChannel:开关dma的channel,两个dma,每个有8channel。
DMA_SetCurrDataCounter:设置当前dma的指针。
PS:收到过一次后需要开关dma,然后重新设置当前数据数。

此外,由于判断dma收到数据状态,通过判断usart的idle,空闲中断。
当判定到uart收到空闲中断,则表示uart数据传输完成。不过该状态清空需要使用到receive函数。
USART_ReceiveData:uart接收数据rx。

# 8 小结

本章介绍了结合uart及dma实现数据的收发,后续再结合串口屏实现功能。



更多回帖

发帖
×
20
完善资料,
赚取积分