Skip to main content
arun rajajinagar
Associate II
July 12, 2019
Question

Code get stuck at

  • July 12, 2019
  • 1 reply
  • 2109 views

Hi,

I am using STM32L072 LoRa discovery kit and enabled UART with DMA. Some times my code gets stuck in HAL_UART_Receive_DMA( ....) function at the line 

SET_BIT(huart->Instance->CR3, USART_CR3_EIE);

This is called at the begining of the code. It wont recover at all until i reset the MCU for some number of times.

Kindly let me know how shall I resolve this issue.

This topic has been closed for replies.

1 reply

MSipo
Senior II
September 25, 2023

Probably this happens because one error flag is set when you call  HAL_UART_Receive_DMA(). You must clear the flags first.

Try this:

 

 uint32_t primask_bit;
 primask_bit = __get_PRIMASK();
 // HAL_UART_Receive_DMA() will enable the Error interrupt. If any of the ORE, FE, PE flags are set there will be an
 // interrupt.
 __disable_irq();

 // Clear the flags that could cause an interrupt when HAL_UART_Receive_DMA() enables the error interrupt
 huart1.Instance->ICR = USART_ICR_FECF | // Framing Error Clear Flag
 USART_ICR_NCF | // Noise detected Clear Flag
 USART_ICR_ORECF; // OverRun Error Clear Flag

 // Enable DMA to receive all bytes into a circular buffer
 HAL_UART_Receive_DMA(&huart1, (uint8_t *)uartCircularRxBuffer, sizeof(uartCircularRxBuffer));

 // Turn off Error Interrupt Enable. This will disable ORE, FE, PE interrupts
 huart1.Instance->CR3 &= ~USART_CR3_EIE;

 // Re-enable the interrupts: restore previous priority mask
 __set_PRIMASK(primask_bit);