Stm32l1xx using HAL_UART_TRANSMIT_DMA to send serial data

Source: Internet
Author: User

Use STM32CUBEMX to generate initialization code.

Problem:

The HAL_UART_TRANSMIT_DMA function can only be called once, and the second time returns the state Hal_uart_state_busy 0x02.


Reason:

STM32L1XX_HAL_UART.C begins with a description

(##) 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)


Configure USARTX Interrupt priority, enable Nvic Usart interrupt handle (use DMA non-looping mode to detect the last byte sent complete)



The default USART1 global interrupt is not checked.


Or:

In the callback function that sends the end, restore the ready state of the UART.

void Hal_uart_txhalfcpltcallback (Uart_handletypedef *huart)
{
callback function
huart->state=hal_uart_state_ready;


}

The following is the code for Mbed-os, which The UART_DMATRANSMITCPLT function resets the UART state directly.


/** * @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_HandleTypeD  ef*) HDMA)->parent;    Huart->txxfercount = 0; /* Disable the DMA transfer for transmit request by setting the DMAT bit in the UART CR3 Register */huart->instance-    &GT;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)! = HA    L_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_sta    TE_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); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Stm32l1xx using HAL_UART_TRANSMIT_DMA to send serial data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.