STM32 UART always times out
Hello,
I am testing UART via a loopback test. This shows that the expected data is being sent on TX and received on RX, but the receive function always times out.
This post stated to clear the OVR and RXNE flags before calling the receive function, but that did not fix the issue.
My program gets stuck in the while loop while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)inside stm32l4xx_hal_uart.c, where Flag = RXNE. While RXNE is set before calling HAL_UART_Receive, it gets reset on the line if (huart->RxState == HAL_UART_STATE_READY), so the while loop never exits. I am not sure why this line resets RXNE.
I have tried different UART peripherals (USART1,2,3) as well as different pins. I also tried using interrupts with UART. Doing this did not fix the issue either.
Any possible solutions would be appreciated. Thanks.
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART3_UART_Init();
while (1)
{
uint8_t arr[3] = {2,4,6};
uint8_t buffer[3];
if(__HAL_UART_GET_FLAG(&huart3, UART_FLAG_RXNE)){
uint8_t temp = (uint8_t)(huart3.Instance->RDR);
}
HAL_UART_Transmit(&huart3, arr, sizeof(arr), HAL_MAX_DELAY);
if(__HAL_UART_GET_FLAG(&huart3, UART_FLAG_ORE)){
__HAL_UART_CLEAR_OREFLAG(&huart3);
}
HAL_UART_Receive(&huart3, buffer, sizeof(buffer), 100);
}
}
