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
Posted on September 23, 2015 at 22:38

&huart3.Instance->RDR

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Cristea.Catalin
Associate III
Posted on September 24, 2015 at 00:40

Thank you Clive, but I'm not sure what you're trying to say.

I could probably read it from there but what if more bytes come before I have a chance?

I need my data to be moved by DMA to ''USART3_DMA_RX_Buff'' so I can deal with it in the main loop.

Cristea.Catalin
Associate III
Posted on September 24, 2015 at 00:40

Changed to:

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

I think you're right; thank you; but it seems it's not enough.

Cristea.Catalin
Associate III
Posted on September 24, 2015 at 00:48

I think I get it; I'll try; thanks.

Posted on September 24, 2015 at 01:20

I'm not sure what you're trying to say.

The Address of the register the DMA is acting on vs the arbitrary content of the register itself at the instant the function is called.

ST's casting the address as a (uint32_t) rather than a (void *) does make it hard for the compiler to flag the mismatch.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Cristea.Catalin
Associate III
Posted on September 24, 2015 at 17:12

Yes, I relied on the compiler to warn me if I did the wrong thing which was wrong of me :)

But it still doesn't work.

Any other ideas?

Thanks

Posted on September 24, 2015 at 17:37

Sorry not with the HAL, migrating to other solutions here.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Cristea.Catalin
Associate III
Posted on September 25, 2015 at 00:22

Is there anything (not HAL) else for the STM32F7?

Posted on September 25, 2015 at 10:16

Yes - the old fashioned direct register access.

JW