UARTDMA How to work

Source: Internet
Author: User

First, initialize

1. Initialize the serial port, clock

Mx_usart1_uart_init ();

Serial clock initialized to internal clock

Periphclkinit.usart1clockselection = RCC_USART1CLKSOURCE_HSI;

Periphclkinit.usart2clockselection = RCC_USART2CLKSOURCE_HSI;

2. Initializing DMA, Port multiplexing

Hal_uart_mspinit ()

Open Idle Frame Interrupt

__hal_uart_enable_it (Uarthandle, Uart_it_idle);

Second, start receiving transmission

1. Start Data reception

/*Start DMA Receive transfer*/voidMx_uart_debugrxstart (uart_e Uart) {if(uart>= Uart_max) {return ;} if(!g_stmyuart[uart].bdebugrxstop) {return ;}  for(Uint8 i=0; i<uart_debug_buff_lever;i++)    {        if(!G_stmyuart[uart].bdebugrxavail[i]) {G_stmyuart[uart].bdebugrxstop=false; HAL_UART_RECEIVE_DMA (G_stmyuart[uart].huart, g_stmyuart[uart].cdebugbuffrx[i], uart_rx_buff_size -1); G_stmyuart[uart].ccurdebugrxbuff=i; G_stmyuart[uart].udebugrxcount[i]=0;  Break; }    }}

2. If the data is received, the DMA interrupt is triggered, when the DMA interrupt is triggered, the call transfer completes the interrupt, and if the free frame is received, the transfer completion interrupt is also called.

uint32_t temp;     = huart->instance->ISR;     = huart->instance->RDR;     if (HAL_OK! =hal_uart_dmastoprx (huart))    {        error_handler ();    }    G_stmyuart[uart].bdebugrxstop=true;     if (NULL! = huart->Hdmarx)    {        temp  = huart->hdmarx->instance->cndtr;        Mx_uart_debuggetdata (uart,temp);        Mx_uart_debugrxstart (Uart);    }     Else {Error_Handler ();}

Stop DMA transfer, set receive Stop flag, read data, restart data reception

Third, send DMA

1. Start the transfer

if (HAL_UART_TRANSMIT_DMA (G_stmyuart[uart_debug].huart                      , g_stmyuart[uart_debug].cdebugbufftx[i]                      = = Hal_ok)                    {                        G_stmyuart[uart_debug].ccurdebugtxbuff=i;                        G_stmyuart[uart_debug].udebugtxcount[i]=0;                    }

When the 2.DMA transfer is complete, open the serial port send complete interrupt,

void Hal_uart_txcpltcallback (Uart_handletypedef *huart) {    uart_e UART;      for (uart=uart_debug;uart<uart_max;uart++)    {        if(G_stmyuart[uart].huart = = Huart){break;    }    } if (Uart_max = = UART) {return  ;}    Hal_uart_dmastoptx (Huart);     if (G_stmyuart[uart].ccurdebugtxbuff <uart_debug_buff_lever)    {        G_stmyuart[uart].bdebugtxen[g_stmyuart[uart].ccurdebugtxbuff]=false;    }}

UARTDMA How to work

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.