STM32 uses the HAL Library to do the DMA sending and the interrupt receiving of the serial port __stm32

Source: Internet
Author: User
The Hal Library is too inflexible to use HAL Serial port interrupt receive

The HAL Library is too inflexible to use, too restrictive, and then, if you have time, you will use the registers and the HAL library to mix operations.
If the use of serial port interrupt reception, after receiving data, the library did a closed reception Rxneie, as follows
HAL Library DMA send

Currently, I am using DMA to send the data directly to the DMA, do not want to develop the interrupt to send DMA, however, the HAL Library to consider the more detailed, using the HAL Library's DMA send API, you will use the DMA handle in the relevant flag bit, these flags you open the interrupt, after the DMA send completed To do the corresponding cleanup, and, using the __hal_lock (), so that you use his library to send, the rest of the other processing you have to use his library; previously said, using the DMA sent by the library, do not open the interrupt, because the last is __hal_lock () state, resulting in the failure to continue execution, And with the DMA sent, but also flag a few signs for the busy state, if you do not open the interruption, you have to clear, in general, the use of it is very troublesome, for nothing more open an interruption.

The following code for the STM32F7 serial port 1 DMA send and Serial receiver interrupt program implementation code

/** ****************************************************************************** * @file driver_uart.c * @autho R Quentin.que * @version v.0 * @date 2017-12-14 * @brief configuration of all the bottom ports ************************************** * * * Includes----------------------------------------------------------- -------* * * #include "common_config.h" #include "stm32f7xx_hal.h" #include "driver_uart.h"/* Macros-------------------- ----------------------------------------------*///* Enumerations------------------------------------------------- -----------------*//////Type Definitions------------------------------------------------------------------/* #de Fine def_uart1_rx_buf_size 1 #define DEF_UART1_DMA_TX_BUF_SIZE/* Private variables-------------------------
--------------------------------*/static uart_handletypedef M_uart1handler;
Static Dma_handletypedef M_dmauart1handler; Static int8u Uart1rxbuf[def_uart1_rx_buf_size];
Static int8u uart1dmatxbuf[def_uart1_dma_tx_buf_size];

static Uart_receive_callback pfunuart1callback = NULL; /* Private Function Prototypes-----------------------------------------------* */******************************** * @brief Initialize serial port 1 * @param baudrate: Baud rate * @retval NONE ************************ /static void Driver_uart1init (int32u baudrate) {m_uart1handler.in
    stance = USART1;
    M_Uart1Handler.Init.BaudRate = baudrate;
    M_UART1HANDLER.INIT.HWFLOWCTL = Uart_hwcontrol_none;
    M_Uart1Handler.Init.Mode = Uart_mode_tx_rx;
    m_Uart1Handler.Init.Parity = Uart_parity_none;
    M_Uart1Handler.Init.StopBits = Uart_stopbits_1;

    M_Uart1Handler.Init.WordLength = uart_wordlength_8b; Hal_uart_init (&m_uart1handler);  /* Serial port Initialization/hal_uart_receive_it (&m_uart1handler, int8u *) uart1rxbuf, def_uart1_rx_buf_size); /* Open Interrupt, set receive BUF and size * */********* @brief Set serial 1 receive interrupt * @param Revice_ca Llback: Receive callback function * @retval NONE ************************************************************************************* */static void Driver_uart1setrevicecallback (Uart_receive_callback revice_callback) {pfunuart1callback = Revice_c
Allback; }/*************************************************************************************** * @brief DRIVER_ Uart1senddata * @param serial 1 Send * @retval NONE *************************************************************************
    /static BOOLEAN Driver_uart1senddata (int8u* pdata, int16u len) {int16u Real_len = 0; Real_len = len > def_uart1_dma_tx_buf_size?  Def_uart1_dma_tx_buf_size:len;
    /* Force interception cannot exceed the defined cache size * * memcpy (UART1DMATXBUF, Pdata,real_len); while (0!= __hal_dma_get_counter (&m_dmauart1handler))/* Wait to send completion/{} do {}while (HAL_OK!= Hal_u Art_transmit_dMA (&m_uart1handler, Uart1dmatxbuf, Real_len));
return TRUE; }/*************************************************************************************** * @brief HAL_UART_ Mspinit will be called by Hal_uart_init () * @param serial handle * @retval NONE *************************************************************
    /void Hal_uart_mspinit (Uart_handletypedef *huart) {gpio_inittypedef gpio_initure;
        if (USART1 = = huart->instance) {__hal_rcc_gpioa_clk_enable ();

        __hal_rcc_usart1_clk_enable ();

        __hal_rcc_dma2_clk_enable ();         
        Gpio_initure.pin=gpio_pin_9;   
        GPIO_INITURE.MODE=GPIO_MODE_AF_PP;           
        Gpio_initure.pull=gpio_pullup;      
        Gpio_initure.speed=gpio_speed_fast;  
        Gpio_initure.alternate=gpio_af7_usart1; 

        Hal_gpio_init (gpioa,&gpio_initure);        
        gpio_initure.pin=gpio_pin_10;

    Hal_gpio_init (gpioa,&gpio_initure);
  /* Peripheral DMA init*/  M_dmauart1handler.instance = DMA2_STREAM7;
    M_DmaUart1Handler.Init.Channel = Dma_channel_4;
    M_DmaUart1Handler.Init.Direction = Dma_memory_to_periph;
    M_DMAUART1HANDLER.INIT.PERIPHINC = dma_pinc_disable;
    M_DMAUART1HANDLER.INIT.MEMINC = dma_minc_enable;
    M_DmaUart1Handler.Init.PeriphDataAlignment = Dma_pdataalign_byte;
    M_DmaUart1Handler.Init.MemDataAlignment = Dma_mdataalign_byte;
    M_DmaUart1Handler.Init.Mode = Dma_normal;     

        m_DmaUart1Handler.Init.Priority = Dma_priority_low;
    Hal_dma_deinit (&m_dmauart1handler);

    Hal_dma_init (&m_dmauart1handler); 

        __HAL_LINKDMA (Huart,hdmatx,m_dmauart1handler);
        Hal_nvic_setpriority (DMA2_STREAM7_IRQN, 3, 0);  

        HAL_NVIC_ENABLEIRQ (DMA2_STREAM7_IRQN);
        HAL_NVIC_ENABLEIRQ (USART1_IRQN);           
    Hal_nvic_setpriority (usart1_irqn,3,0); }/******************************************************************************** * @brief HAL_UART_ Rxcpltcallback into the break after the HAL LibraryWill call this function * @param serial handle * @retval NONE ******************************************************************************** */void Hal_uart_rxcpltcallback (Uart_handletypedef *huart) {if (USART1 = = huart->instance) {if (NULL
        != pfunuart1callback) {pfunuart1callback (uart1rxbuf[0]);//* Send received data through callback/do  {}while (Hal_ok!= hal_uart_receive_it (&m_uart1handler, (int8u *) Uart1rxbuf, def_uart1_rx_buf_size));   /* Open Interrupt, set receive BUF and Size/}/******************************************************************************* * @brief Usart1_irqhandler Serial 1 Interrupt function * @param serial handle * @retval NONE **************************************************************

/void Usart1_irqhandler (void) {Hal_uart_irqhandler (&m_uart1handler); }/******************************************************************************* * @brief DMA2_Stream7_ Irqhandler serial 1DMA Send interrupt * @param none * @retval None * * */void Dma2_stream7_irqhandler (void) 

{Hal_dma_irqhandler (&m_dmauart1handler); /*********************************************************************** * @brief Initialize serial port * @param port: Serial number baudrate: Baud rate Prx_fun: Receive callback function * @retval TRUE: Success FALSE: Failed ********************************************************************** * * BOOLEAN driver_uartinit (int8u port, int32u baudrate, Uart_receive_callback prx_fun) {switch (port) {CA
            Se driver_uart_port1:driver_uart1init (baudrate);
            Driver_uart1setrevicecallback (Prx_fun);

        Break
    Default:return FALSE;
return TRUE; /*********************************************************************** * @brief serial port Send * @param port: Serial number pdata: Data start Address len: Length * @retval TRUE: Success FALSE: Failed ***********************************************************************/BOOLEAN Driver_uartwrite (int8u port, int8u* pdata, int16u len) {switch (port) {case Driver_uart_port1:return Driver_uart1senddata (p
        data, Len);
    Default:return FALSE;

 }

}

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.