How to keep uart RX interrupt enabled forever using HAL library on a STM32F1

Gerard Vuyk picture Gerard Vuyk · Apr 16, 2016 · Viewed 14.3k times · Source

I see HAL provides the following function for receiving serial data with interrupts: HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
which basically setups rx interrupts and returns.
HAL_UART_RxCpltCallback() will be called once the amount of bytes received reaches Size.
HAL disables rx interrupts just before executing this callback

Problem:
The amount of incoming bytes will be variable (from 1 to ~100) and I need to put them into a ring buffer.
So I set Size to 1 and call HAL_UART_Receive_IT() again inside the callback to re-enable interrupts, and it works well if the board gets sent 1 or 2 bytes every now and then, but bytes are missed beyond that

My guess is they are missed because they arrive between interrupt disabling - enabling

Is there a way to keep interrupts running forever without overwriting HAL Cube generated code?
If not, what would be a clean way of overwriting the involved functions?
Is there something I'm missing that would avoid the need to find this solution?

Answer

Besnik B picture Besnik B · Feb 11, 2017

Try this

__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);