cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble migrating UART USART DMA RX from SPL to HAL

Cristea.Catalin
Associate III
Posted on September 23, 2015 at 21:53

I have this SPL project on STM32F427 that I need to migrate to STM32F746.

I was hoping I can just program the F7 and it will run but that just shows how naive I am :)

Anyway, I wish there was an SPL for the F7 so I wouldn't have to do all this but it looks like there isn't so here I go:

I want the DMA to just move any received data into a circular buffer; NO interrupts.

I will deal with the data myself in the main loop.

Trouble is for some reason the DMA doesn't seem to do anything (I don't get any data in the circular buffer.

Help?

Apart from the code generated by the Cube I added one line that I thought would start the DMA and give it the buffer (this is called once):

HAL_DMA_Start (&hdma_usart3_rx, huart3.Instance->RDR, (uint32_t)&USART3_DMA_RX_Buff[0], USART3_DMA_RX_BUFFERSIZE);

I used the Cube to configure the USART3 DMA like this:

**********************************

  MX_DMA_Init();

  ...;

  MX_USART3_UART_Init();

**********************************

void MX_DMA_Init(void)

{

  /* DMA controller clock enable */

  __HAL_RCC_DMA1_CLK_ENABLE();

  /* DMA interrupt init */

  HAL_NVIC_SetPriority(DMA1_Stream1_IRQn, 0, 0);

  HAL_NVIC_EnableIRQ(DMA1_Stream1_IRQn);

}

**********************************

void MX_USART3_UART_Init(void)

{

  huart3.Instance = USART3;

  huart3.Init.BaudRate = 921600;

  huart3.Init.WordLength = UART_WORDLENGTH_8B;

  huart3.Init.StopBits = UART_STOPBITS_1;

  huart3.Init.Parity = UART_PARITY_NONE;

  huart3.Init.Mode = UART_MODE_TX_RX;

  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;

  huart3.Init.OverSampling = UART_OVERSAMPLING_16;

  huart3.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;

  huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

  HAL_UART_Init(&huart3);

}

**********************************

HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)

{

  /* Check the UART handle allocation */

  if(huart == NULL)

  {

    return HAL_ERROR;

  }

  if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)

  {

    /* Check the parameters */

    assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));

  }

  else

  {

    /* Check the parameters */

    assert_param(IS_UART_INSTANCE(huart->Instance));

  }

  if(huart->State == HAL_UART_STATE_RESET)

  {

    /* Allocate lock resource and initialize it */

    huart->Lock = HAL_UNLOCKED;

    /* Init the low level hardware : GPIO, CLOCK */

    HAL_UART_MspInit(huart);

  }

  huart->State = HAL_UART_STATE_BUSY;

  /* Disable the Peripheral */

  __HAL_UART_DISABLE(huart);

 

  /* Set the UART Communication parameters */

  if (UART_SetConfig(huart) == HAL_ERROR)

  {

    return HAL_ERROR;

  }

  if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)

  {

    UART_AdvFeatureConfig(huart);

  }

  /* In asynchronous mode, the following bits must be kept cleared:

  - LINEN and CLKEN bits in the USART_CR2 register,

  - SCEN, HDSEL and IREN  bits in the USART_CR3 register.*/

  huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);

  huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);

  __HAL_UART_ENABLE(huart);

  return (UART_CheckIdleState(huart));

}

**********************************

void HAL_UART_MspInit(UART_HandleTypeDef* huart)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  if(huart->Instance==USART3)

  {

    /* Peripheral clock enable */

    __USART3_CLK_ENABLE();

 

    /**USART3 GPIO Configuration    

    PB10     ------> USART3_TX

    PB11     ------> USART3_RX

    */

    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_PULLUP;

    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF7_USART3;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* Peripheral DMA init*/

    hdma_usart3_rx.Instance = DMA1_Stream1;

    hdma_usart3_rx.Init.Channel = DMA_CHANNEL_4;

    hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;

    hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;

    hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;

    hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;

    hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;

    hdma_usart3_rx.Init.Mode = DMA_CIRCULAR;

    hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;

    hdma_usart3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;

    hdma_usart3_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;

    hdma_usart3_rx.Init.MemBurst = DMA_MBURST_SINGLE;

    hdma_usart3_rx.Init.PeriphBurst = DMA_PBURST_SINGLE;

    HAL_DMA_Init(&hdma_usart3_rx);

    __HAL_LINKDMA(huart,hdmarx,hdma_usart3_rx);

  }

*************************************

#uart-usart-dma
11 REPLIES 11
Cristea.Catalin
Associate III
Posted on September 25, 2015 at 18:25

Thank you Jan; how could I have missed that :)

So would you be able to help me either by telling me what registers my HAL implementation missed, or giving me a minimal (but complete) implementation of my needs: get any chars coming through the serial port into a circular DMA buffer?

I'll deal with it once it's in the buffer.

Thanks and best,

Cat

Posted on September 26, 2015 at 14:40

> So would you be able to help me either by telling me what registers my HAL implementation missed, or giving me a minimal (but complete) implementation of my needs: get any chars coming through the serial port into a circular DMA buffer?

Able? Probably yes, why not. Willing? No, I don't have time to write it nor a 'F7xx hardware at hand.

JW