cancel
Showing results for 
Search instead for 
Did you mean: 

Continous UART/DMA receiver stops within STM32F0x

Youssouf Coulibaly
Associate II
Posted on September 04, 2017 at 09:16

Hello,

I had a problem with my UART interface on STM32F091RC. I have seen that others encountered the same, but I am not able to fix yet in my side.

I have UART Rx and Tx in DMA normal mode.

Tx function is working fine (with long running test). But my Rx is stopping just after some random times (10s and 5 min or  2 min). And it is not tiggered any other interrupt ! 

I am using the code generated by Cube Mx.

void MX_USART2_UART_Init(void)

{

huart2->Instance = USART3;

huart2->Init.BaudRate = 115200;

huart2->Init.WordLength = UART_WORDLENGTH_8B;

huart2->Init.StopBits = UART_STOPBITS_1;

huart2->Init.Parity = UART_PARITY_NONE;

huart2->Init.Mode = UART_MODE_TX_RX;

huart2->Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart2->Init.OverSampling = UART_OVERSAMPLING_16;

huart2->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

huart2->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

if (HAL_UART_Init(huart2) != HAL_OK)

{

//Error_Handler();

}

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)

{

GPIO_InitTypeDef GPIO_InitStruct;

if(uartHandle->Instance==USART3)

{

/* Peripheral clock enable */

__HAL_RCC_USART3_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();

/**USART2 GPIO Configuration

PA2 ------> USART2_TX

PA3 ------> USART2_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_FREQ_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF4_USART3;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* Peripheral DMA init*/

hdma_usart2_rx->Instance = DMA1_Channel1;

hdma_usart2_rx->Init.Direction = DMA_PERIPH_TO_MEMORY;

hdma_usart2_rx->Init.PeriphInc = DMA_PINC_DISABLE;

hdma_usart2_rx->Init.MemInc = DMA_MINC_ENABLE;

hdma_usart2_rx->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;

hdma_usart2_rx->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;

hdma_usart2_rx->Init.Mode = DMA_NORMAL;

hdma_usart2_rx->Init.Priority = DMA_PRIORITY_LOW;

if (HAL_DMA_Init(hdma_usart2_rx) != HAL_OK)

{

//Error_Handler();

}

__HAL_DMA1_REMAP(HAL_DMA1_CH1_USART3_RX);

uartHandle->hdmarx = hdma_usart2_rx;

hdma_usart2_rx->Parent = uartHandle;

__HAL_LINKDMA(uartHandle, hdmarx, *hdma_usart2_rx);

hdma_usart2_tx->Instance = DMA1_Channel4;

hdma_usart2_tx->Init.Direction = DMA_MEMORY_TO_PERIPH;

hdma_usart2_tx->Init.PeriphInc = DMA_PINC_DISABLE;

hdma_usart2_tx->Init.MemInc = DMA_MINC_ENABLE;

hdma_usart2_tx->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;

hdma_usart2_tx->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;

hdma_usart2_tx->Init.Mode = DMA_NORMAL;

hdma_usart2_tx->Init.Priority = DMA_PRIORITY_LOW;

if (HAL_DMA_Init(hdma_usart2_tx) != HAL_OK)

{

//Error_Handler();

}

__HAL_DMA1_REMAP(HAL_DMA1_CH4_USART3_TX);

uartHandle->hdmatx = hdma_usart2_tx;

hdma_usart2_tx->Parent = uartHandle;

__HAL_LINKDMA(uartHandle, hdmatx, *hdma_usart2_tx);

/* Peripheral interrupt init */

HAL_NVIC_SetPriority(USART3_8_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(USART3_8_IRQn);

}

}

void USART3_8_IRQHandler(void)

{

// If Overrun flag is still set, clear it

if (__HAL_UART_GET_FLAG(huart2, UART_FLAG_ORE) != RESET)

{

__HAL_UART_CLEAR_OREFLAG(huart2);

// Clear the buffer to prevent overrun

__HAL_UART_SEND_REQ(huart2, UART_RXDATA_FLUSH_REQUEST);

}

HAL_UART_IRQHandler(huart2);

}

I just added the overrun test here in cube mx base code. But even if I remove it still have the same issue on Rx function. 

Here all DMA channels are setting up.

void MX_DMA_Init(void)

{

/* DMA controller clock enable */

__HAL_RCC_DMA1_CLK_ENABLE();

/* DMA interrupt init */

/* DMA1_Ch1_IRQn interrupt configuration */

HAL_NVIC_SetPriority(DMA1_Ch1_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(DMA1_Ch1_IRQn);

/* DMA1_Ch2_3_DMA2_Ch1_2_IRQn interrupt configuration */

HAL_NVIC_SetPriority(DMA1_Ch2_3_DMA2_Ch1_2_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(DMA1_Ch2_3_DMA2_Ch1_2_IRQn);

/* DMA1_Ch4_7_DMA2_Ch3_5_IRQn interrupt configuration */

HAL_NVIC_SetPriority(DMA1_Ch4_7_DMA2_Ch3_5_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(DMA1_Ch4_7_DMA2_Ch3_5_IRQn);

}

Here are the IRQ handler functions.

void DMA1_Ch1_IRQHandler(void)

{

HAL_DMA_IRQHandler(hdma_usart2_rx);

}

void DMA1_Ch4_7_DMA2_Ch3_5_IRQHandler(void)

{

HAL_DMA_IRQHandler(hdma_usart2_tx);

}

Would it be possible if someone can tell me what I am doing wrong in this code ?

Since 2 days I debugged this stuff without success.

Thanks a lot for your support. 

1 REPLY 1
Youssouf Coulibaly
Associate II
Posted on September 07, 2017 at 09:04

Any help on this ?