Receive command UART polling mode using LL library
Hi everyone!
I've been trying to implement UART reception using LL library in polling mode. The code below is what handles the reception:
uint8_t LL_USART_Receive_Byte(USART_TypeDef *USARTx) {
// Wait for Read Data Register is not empty
while (!LL_USART_IsActiveFlag_RXNE(USARTx));
return LL_USART_ReceiveData8(USARTx);;
}
/**
* @brief Receive `len` amount of bytes on USARTx by calling LL_USART_Receive_Byte function.
* Usually, this function will be used to receive data in our programs.
* @param USARTx USART instance.
* @param buff Receive buffer.
* @param len Amount of bytes to receive
*/
void LL_USART_Receive(USART_TypeDef *USARTx, uint8_t *buff, uint32_t len) {
for (int i = 0; i < len; i++) {
buff[i] = LL_USART_Receive_Byte(USARTx);
}
}In order to test it, I have interconnected the UART6 Tx and UART1 Rx pins in the MCU and in the main program I simply send a message.
/* USER CODE BEGIN 2 */
uint8_t textTx[8] = "Hello!\r\n";
uint8_t textRx[8] = {};
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
LL_USART_Transmit(USART6, textTx, sizeof(textTx));
LL_USART_Receive(USART1, textRx, sizeof(textRx));
LL_mDelay(2000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */The transmit function is working as expected, but the problem is that it only receives the first character. I tested it in debug mode and checked Live Expressions and Variables.
The problem is because LL_USART_Receive_Byte() function is stuck in the while (!LL_USART_IsActiveFlag_RXNE(USARTx)) loop after receiving the first character. The first iteration of the for loop in LL_USART_Receive is fine but when receiving the second character is not working anymore.
I see that the problem is something related to the RXNE flag and maybe with Overrun flag as well as I read in the docs but I can't figure out how to read the whole message.
Hope someone can help me :)
