2019-07-12 02:52 AM
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.
2023-09-25 06:28 AM
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);