STM32L1XX使用HAL_UART_Transmit_DMA发送串口数据

使用STM32CubeMX生成初始化代码。

问题:

HAL_UART_Transmit_DMA函数只能调用一次,第二次就返回状态HAL_UART_STATE_BUSY 0x02。


原因:

stm32l1xx_hal_uart.c开头有描述

        (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
             and HAL_UART_Receive_DMA() APIs):
            (+++) Declare a DMA handle structure for the Tx/Rx channel.
            (+++) Enable the DMAx interface clock.
            (+++) Configure the declared DMA handle structure with the required 
                  Tx/Rx parameters.                
            (+++) Configure the DMA Tx/Rx channel.
            (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
            (+++) Configure the priority and enable the NVIC for the transfer complete 
                  interrupt on the DMA Tx/Rx channel.
            (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
                  (used for last byte sending completion detection in DMA non circular mode)


配置USARTx中断优先级,启用NVIC USART中断句柄(使用DMA非循环模式时,用来检测最后一个字节发送完毕)



默认 USART1的全局中断未Checked。


或者:

在发送结束的回调函数中,恢复uart的Ready状态。

void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
{
//回调函数
huart->State=HAL_UART_STATE_READY; 


}

下面附的是mbed-os的代码,它的UART_DMATransmitCplt函数直接复位Uart的状态了。


/**
  * @brief DMA UART transmit process complete callback 
  * @param hdma: DMA handle
  * @retval None
  */
01523 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)     
{
  UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  huart->TxXferCount = 0;
  
  /* Disable the DMA transfer for transmit request by setting the DMAT bit
  in the UART CR3 register */
  huart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT);
  
  /* Wait for UART TC Flag */
  if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, HAL_UART_TXDMA_TIMEOUTVALUE) != HAL_OK)
  {
    /* Timeout Occured */ 
    huart->State = HAL_UART_STATE_TIMEOUT;
    HAL_UART_ErrorCallback(huart);
  }
  else
  {
    /* No Timeout */
    /* Check if a receive process is ongoing or not */
    if(huart->State == HAL_UART_STATE_BUSY_TX_RX) 
    {
      huart->State = HAL_UART_STATE_BUSY_RX;
    }
    else
    {
      huart->State = HAL_UART_STATE_READY;
    }
    HAL_UART_TxCpltCallback(huart);
  }
}


  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

容沁风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值