cancel
Showing results for 
Search instead for 
Did you mean: 

UART_DMA from STM32F3Cube

chenping
Associate II
Posted on April 17, 2015 at 18:28

Hello! This HAL_driver setting n functions look a bit different from the old Std_Periph_Driver...

The project works fine, but as I tried to set multiple UART, I failed. Does the structure of DMA needs to be redefined to add another for my other Uart? I use uart 2, and set the pin using the way exactly the same as uart 1 as predefined in the project.

And, does it must have the same callback function like Uart1 to set the Uart flag?

Below is my uart setting in msp.c  - the function of HAL_UART_MspInit

static DMA_HandleTypeDef hdma_tx2;

static DMA_HandleTypeDef hdma_rx2;

GPIO_InitTypeDef  GPIO_InitStruct;

  /*♯♯-1- Enable peripherals and GPIO Clocks♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

      /* Enable GPIO TX/RX clock */

 

      __GPIOD_CLK_ENABLE();

      /* Enable USARTx clock */

      __USART2_CLK_ENABLE();

      /* Enable DMA clock */

      __DMA2_CLK_ENABLE();

 /*♯♯-2- Configure peripheral GPIO ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

      /* UART TX GPIO pin configuration  */

      GPIO_InitStruct.Pin       = GPIO_PIN_5;

      GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;

      GPIO_InitStruct.Pull      = GPIO_PULLUP;

      GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;

      GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

      HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

      /* UART RX GPIO pin configuration  */

      GPIO_InitStruct.Pin = GPIO_PIN_6;

      GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

      HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

/******************DMA Setting****************/

/* Configure the DMA handler for Transmission process */

      hdma_tx2.Instance                 = USART2_TX_DMA_STREAM;

      hdma_tx2.Init.Direction           = DMA_MEMORY_TO_PERIPH;

      hdma_tx2.Init.PeriphInc           = DMA_PINC_DISABLE;

      hdma_tx2.Init.MemInc              = DMA_MINC_ENABLE;

      hdma_tx2.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;

      hdma_tx2.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;

      hdma_tx2.Init.Mode                = DMA_CIRCULAR;

      hdma_tx2.Init.Priority            = DMA_PRIORITY_LOW;

      HAL_DMA_Init(&hdma_tx2);

      /* Associate the initialized DMA handle to the UART handle */

      __HAL_LINKDMA(huart, hdmatx, hdma_tx2);

      /* Configure the DMA handler for reception process */

      hdma_rx2.Instance                 = USART2_RX_DMA_STREAM;

      hdma_rx2.Init.Direction           = DMA_PERIPH_TO_MEMORY;

      hdma_rx2.Init.PeriphInc           = DMA_PINC_DISABLE;

      hdma_rx2.Init.MemInc              = DMA_MINC_ENABLE;

      hdma_rx2.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;

      hdma_rx2.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;

      hdma_rx2.Init.Mode                = DMA_CIRCULAR;

      hdma_rx2.Init.Priority            = DMA_PRIORITY_HIGH;

      HAL_DMA_Init(&hdma_rx2);

      /* Associate the initialized DMA handle to the the UART handle */

      __HAL_LINKDMA(huart, hdmarx, hdma_rx2);

      /*♯♯-4- Configure the NVIC for DMA ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

      /* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */

      HAL_NVIC_SetPriority(USART2_DMA_TX_IRQn, 0, 1);

      HAL_NVIC_EnableIRQ(USART2_DMA_TX_IRQn);

      /* NVIC configuration for DMA transfer complete interrupt (USARTx_RX) */

      HAL_NVIC_SetPriority(USART2_DMA_RX_IRQn, 0, 0);

      HAL_NVIC_EnableIRQ(USART2_DMA_RX_IRQn);

In main.c , I called upon this function:

void UART2Init(){

    U2.Instance        = USART2;

    U2.Init.BaudRate   = 115200;

    U2.Init.WordLength = UART_WORDLENGTH_8B;

    U2.Init.StopBits   = UART_STOPBITS_1;

    U2.Init.Parity     = UART_PARITY_NONE;

    U2.Init.HwFlowCtl  = UART_HWCONTROL_NONE;

    U2.Init.Mode       = UART_MODE_TX_RX;

    HAL_UART_Init(&U2);

}

#clive1
1 REPLY 1
Posted on April 17, 2015 at 20:06

Sorry, not answering any HAL/Cube questions here.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..