RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1090197
Accepted
Артём
Артём
Asked:2020-03-03 21:22:47 +0000 UTC2020-03-03 21:22:47 +0000 UTC 2020-03-03 21:22:47 +0000 UTC

STM32 HAL_UART_Transmit_DMA 功能不工作

  • 772

Stone STM32F103C8,在CubeMX中创建工程,设置标准-USART2为异步模式,激活其全局中断,创建DMA通道:创建 DMA 通道

我生成一个项目并将以下代码添加到while中:

  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
      HAL_UART_Transmit_DMA(&huart2, buff, 100);
      HAL_Delay(100);
  }

我编译,闪存和......什么都没有!Tx 腿一直很高,没有传输的迹象。同时,HAL_UART_Transmit_IT 函数工作正常,具有类似设置的相同代码在 STM32F407VG (Discovery) 上工作。尝试了其他石头F103C8,尝试在不同的地方调用HAL_UART_Abort,HAL_DMA_Abort - 没有成功。


我注意到,在调用 HAL_UART_Transmit_DMA 之后,立即调用了 DMA 中断处理程序,其中 HAL 发出传输完成的判断,这意味着什么?HAL 坏了?


PS这里是USART-a和DMA初始化代码

static void MX_USART2_UART_Init(void)
{

  /* USER CODE BEGIN USART2_Init 0 */

  /* USER CODE END USART2_Init 0 */

  /* USER CODE BEGIN USART2_Init 1 */

  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 100;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_2;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */

  /* USER CODE END USART2_Init 2 */

}

/** 
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void) 
{

  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();

  /* DMA interrupt init */
  /* DMA1_Channel7_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn);

}
stm32
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Roman Ozhegov
    2020-03-03T21:59:35Z2020-03-03T21:59:35Z

    DMA 未在您的代码中初始化。下面是我的一段代码:

    void StandardUart::initialize()
    {
    
        ...
        // инициализируем сам Uart
        mHUart->Init.BaudRate = UART_BAUD_RATE;
        mHUart->Init.WordLength = UART_WORDLENGTH_8B;
        mHUart->Init.StopBits = UART_STOPBITS_1;
        mHUart->Init.Parity = UART_PARITY_NONE;
        mHUart->Init.Mode = UART_MODE_TX_RX;
        mHUart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
        mHUart->Init.OverSampling = UART_OVERSAMPLING_16;
    
        //NOTE: Покамест оставил без дополнительной инициаизации
        // DMA initialization
        /* DMA controller clock enable */
        __HAL_RCC_DMA1_CLK_ENABLE();
    
        /*##-3- Configure the DMA ##################################################*/
        /* Configure the DMA handler for Transmission process */
        static DMA_HandleTypeDef hdma_tx; //никуда не должен деться
        hdma_tx.Instance                 = DMA1_Channel4;
        hdma_tx.Init.Direction           = DMA_MEMORY_TO_PERIPH;
        hdma_tx.Init.PeriphInc           = DMA_PINC_DISABLE;
        hdma_tx.Init.MemInc              = DMA_MINC_ENABLE;
        hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_tx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
        hdma_tx.Init.Mode                = DMA_NORMAL;
        hdma_tx.Init.Priority            = DMA_PRIORITY_LOW;
    
        HAL_DMA_Init(&hdma_tx);
    
        /* Associate the initialized DMA handle to the UART handle */
        __HAL_LINKDMA(mHUart, hdmatx, hdma_tx);
    
        /* Configure the DMA handler for reception process */
        static DMA_HandleTypeDef hdma_rx;
        hdma_rx.Instance                 = DMA1_Channel5;
        hdma_rx.Init.Direction           = DMA_PERIPH_TO_MEMORY;
        hdma_rx.Init.PeriphInc           = DMA_PINC_DISABLE;
        hdma_rx.Init.MemInc              = DMA_MINC_ENABLE;
        hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_rx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
        hdma_rx.Init.Mode                = DMA_CIRCULAR;
        hdma_rx.Init.Priority            = DMA_PRIORITY_HIGH;
    
        HAL_DMA_Init(&hdma_rx);
        /* Associate the initialized DMA handle to the the UART handle */
        __HAL_LINKDMA(mHUart, hdmarx, hdma_rx);
    
    
        /* DMA interrupt init */
        /* DMA1_Channel4_IRQn interrupt configuration */
        HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);
        /* DMA1_Channel5_IRQn interrupt configuration */
        HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
    
        // UART initialization
        // подключаем тактирование самого USART
        IRQn_Type Irq = USART1_IRQn;
        if (mHUart->Instance == USART2){
            __HAL_RCC_USART2_CLK_ENABLE();
            Irq = USART2_IRQn;
        }
        if (mHUart->Instance == USART3){
            __HAL_RCC_USART3_CLK_ENABLE();
            Irq = USART3_IRQn;
        }
    
        HAL_UART_DeInit(mHUart);
        HAL_UART_Init(mHUart);
    
        /* Peripheral interrupt init */
        HAL_NVIC_SetPriority(Irq, 0, 0);
        HAL_NVIC_EnableIRQ(Irq);
    
        HAL_UART_Receive_DMA(mHUart,
                             mRxBuffer,
                             MaxBufferSize);
    } 
    

    所见:

    • 没有 DMA 初始化。HAL_DMA_Init(&hdma_tx)
    • 没有与 UART 的 DMA 链接__HAL_LINKDMA(mHUart, hdmatx, hdma_tx)
    • 在代码中我没有看到包含 UART 时钟__HAL_RCC_USART2_CLK_ENABLE();
    • 2

相关问题

  • 启用流 2 后会自发写入 DMA_S2CR 寄存器

  • 如何正确使用晶体管?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5