STM32L1XX使用HAL_UART_Transmit_DMA發送串口資料

來源:互聯網
上載者:User

標籤:

使用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    {      <span style="color:#cc0000;">huart->State = HAL_UART_STATE_READY;</span>    }    HAL_UART_TxCpltCallback(huart);  }}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

STM32L1XX使用HAL_UART_Transmit_DMA發送串口資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.