你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32使用DMA发送串口数据

[复制链接]
STMCU小助手 发布时间:2021-7-20 13:45
01概述


上一篇文章《STM32使用DMA接收串口数据》讲解了如何使用DMA接收数据,使用DMA外设和串口外设,使用的中断是串口空闲中断。本篇文章主要讲解使用DMA发送数据,不会讲解基础的串口和DMA知识,直接上代码,如果有同学对DMA和串口都不熟悉,建议看一下上篇文章《STM32使用DMA接收串口数据》
使用DMA发送数据,首先我们要确认使用的串口有没有DMA。
我们使用USART1串口外设,从数据手册中可以查到,USART1的发送和接收都是支持DMA的,使用的是DMA2.
1.jpg
接下来就是撸代码的时刻了




02代码


DMA串口发送的代码是在上一篇文章DMA串口接收的基础上修改的。
  1. <font face="微软雅黑" size="3">void UART_Init(void)
  2. {
  3.   USART_InitTypeDef USART_InitStructure;
  4.   GPIO_InitTypeDef GPIO_InitStructure;
  5.   NVIC_InitTypeDef NVIC_InitStructure;

  6.   /* Enable GPIO clock */
  7.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  8.   /* Enable UART1 clock */
  9.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  10.   /* Connect PXx to USARTx_Tx*/
  11.   GPIO_PinAFConfig(GPIOA, 9, GPIO_AF_USART1);

  12.   /* Connect PXx to USARTx_Rx*/
  13.   GPIO_PinAFConfig(GPIOA, 10, GPIO_AF_USART1);

  14.   /* Configure USART Tx as alternate function  */
  15.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  16.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  17.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  18.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  19.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  20.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  21.   /* Configure USART Rx as alternate function  */
  22.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  23.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  24.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  25.   USART_InitStructure.USART_BaudRate = 115200;
  26.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  27.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  28.   USART_InitStructure.USART_Parity = USART_Parity_No;
  29.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  30.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  31.   /* USART configuration */
  32.   USART_Init(USART1, &USART_InitStructure);

  33.   USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);

  34.   /* Enable the USARTx Interrupt */
  35.   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  36.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
  37.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  38.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  39.   NVIC_Init(&NVIC_InitStructure);

  40.   /*使能串口DMA接收*/
  41.   USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
  42.   /*使能串口DMA发送*/
  43.   USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);

  44.   /* Enable USART */
  45.   USART_Cmd(USART1, ENABLE);
  46. }</font>
复制代码
在这里除了常规的串口配置,我们需要配置串口的DMA发送,和串口DMA接收一样的API函数,参数修改为USART_DMAReq_Tx即可。
串口DMA发送配置
  1. <font face="微软雅黑" size="3">void Uart_Send_DMA_Config(void)
  2. {
  3.   DMA_InitTypeDef  DMA_InitStructure;

  4.   /* Enable DMA clock */
  5.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);

  6.   /* Reset DMA Stream registers (for debug purpose) */
  7.   DMA_DeInit(DMA2_Stream7);

  8.   /* Check if the DMA Stream is disabled before enabling it.
  9.      Note that this step is useful when the same Stream is used multiple times:
  10.      enabled, then disabled then re-enabled... In this case, the DMA Stream disable
  11.      will be effective only at the end of the ongoing data transfer and it will
  12.      not be possible to re-configure it before making sure that the Enable bit
  13.      has been cleared by hardware. If the Stream is used only once, this step might
  14.      be bypassed. */
  15.   while (DMA_GetCmdStatus(DMA2_Stream7) != DISABLE)
  16.   {
  17.   }

  18.   /* Configure DMA Stream */
  19.   DMA_InitStructure.DMA_Channel = DMA_Channel_4;  //DMA请求发出通道
  20.   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;//配置外设地址
  21.   DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)UART_Buffer;//配置存储器地址
  22.   DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//传输方向配置
  23.   DMA_InitStructure.DMA_BufferSize = (uint32_t)UART_RX_LEN;//传输大小
  24.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址不变
  25.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//memory地址自增
  26.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设地址数据单位
  27.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//memory地址数据单位
  28.   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//DMA模式:正常模式
  29.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;//优先级:高
  30.   DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;//FIFO 模式不使能.         
  31.   DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;// FIFO 阈值选择
  32.   DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//存储器突发模式选择,可选单次模式、 4 节拍的增量突发模式、 8 节拍的增量突发模式或 16 节拍的增量突发模式。
  33.   DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外设突发模式选择,可选单次模式、 4 节拍的增量突发模式、 8 节拍的增量突发模式或 16 节拍的增量突发模式。
  34.   DMA_Init(DMA2_Stream7, &DMA_InitStructure);

  35.   /* DMA Stream enable */
  36. //  DMA_Cmd(DMA2_Stream7, ENABLE);
  37. }</font>
复制代码
这里也是常规的DMA配置流程,不明白的同学请看文章《STM32DMA详解》,这里值得注意的是,配置完成并没有使能DMA2_Stream7,使能了就会立即将UART_Buffer的数据发送出去。
其他代码处理
  1. <font face="微软雅黑" size="3">void USART1_IRQHandler(void)
  2. {
  3.   uint8_t temp;
  4.   if(USART_GetFlagStatus(USART1, USART_FLAG_IDLE) == SET)
  5.   {
  6.     DealWith_UartData();
  7. //    USART_ClearFlag(USART1, USART_FLAG_IDLE);
  8.     temp = USART1->SR;  
  9.     temp = USART1->DR; //清USART_IT_IDLE标志  
  10.   }
  11. }

  12. void DealWith_UartData()
  13. {
  14.   DMA_Cmd(DMA2_Stream2, DISABLE);
  15.   UART_Receive_flg = 1;
  16.   UART_Receive_len = UART_RX_LEN - DMA_GetCurrDataCounter(DMA2_Stream2);
  17.   UART_Buffer[UART_Receive_len] = 0;
  18.   DMA_SetCurrDataCounter(DMA2_Stream2,UART_RX_LEN);
  19.   DMA_ClearFlag(DMA2_Stream2, DMA_FLAG_TCIF2);
  20.   DMA_Cmd(DMA2_Stream2, ENABLE);
  21. }

  22. int main(void)
  23. {
  24.   UART_Receive_flg = 0;

  25.   Uart_Reveice_DMA_Config();
  26.   Uart_Send_DMA_Config();
  27.   UART_Init();

  28.   while (1)
  29.   {
  30.     if(UART_Receive_flg)
  31.     {
  32.       UART_Receive_flg = 0;
  33.       Uart_Send_DMA_Start();
  34.     }
  35.   }
  36. }</font>
复制代码
上面3个函数,简单逻辑就是,当串口使用DMA接收了一定量的数据,就会通过串口DMA发送出去,串口DMA发送的代码如下:
  1. <font face="微软雅黑" size="3">void Uart_Send_DMA_Start(void)
  2. {
  3.   DMA_SetCurrDataCounter(DMA2_Stream7,UART_Receive_len);
  4.   DMA_ClearFlag(DMA2_Stream7, DMA_FLAG_TCIF7);
  5.   /* DMA Stream enable */
  6.   DMA_Cmd(DMA2_Stream7, ENABLE);
  7. }</font>
复制代码


收藏 评论0 发布时间:2021-7-20 13:45

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版