STM32: Receiving data via USART

Thierno Barry picture Thierno Barry · Jun 24, 2014 · Viewed 13.3k times · Source

I'm working on STM32 Discovery (F10x family), and I'm trying to send and receive data through USART1.

int uart_putc(int c, USART_TypeDef* USARTx)
{
    assert_param(IS_USART_123_PERIPH(USARTx));

    while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
    USARTx->DR =  (c & 0xff);
    return 0;
}

int uart_getc (USART_TypeDef* USARTx)
{
    assert_param(IS_USART_123_PERIPH(USARTx));

    while (USART_GetFlagStatus(USARTx, USART_FLAG_RXNE) == RESET);
    return  USARTx->DR & 0xff;
}

uart_putc works finely but uart_getc gets stucked in the while loop, it seems like the while condition is never true.
Does someone know what is wrong with this code?

Answer

atoK picture atoK · Jun 24, 2014

The USART_FLAG_RXNE flag stands for RX buffer Not Empty. If there's data in the RX buffer the flag is SET and not RESET.

That's why your code is stucked in the while loop.