STM32: Receiving data via USART

13,865

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.

Share:
13,865

Related videos on Youtube

Thierno Barry
Author by

Thierno Barry

Updated on June 04, 2022

Comments

  • Thierno Barry
    Thierno Barry almost 2 years

    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?

    • Étienne
      Étienne almost 10 years
      Why don't you simply use the STM32 standard peripheral library?
  • Thierno Barry
    Thierno Barry almost 10 years
    Thanks for your answer,
  • bunkerdive
    bunkerdive over 9 years
    The only reason while(a==FALSE) would be an infinite loop....is because a=FALSE. Isn't that all you're saying here?
  • Tarick Welling
    Tarick Welling over 4 years
    Please format your answer with code tags and proper spelling. It is quite hard to read now
  • Linosa
    Linosa over 4 years
    I have edited my post recently and i think that i have suggested some methods to found out the problem that are based on my experience, I think they are correct, if anybody that down voted this post, thinks it is wrong please share it here, because down voting without telling the reason could be misleading for someone searching for problem solution and right answer.