cancel
Showing results for 
Search instead for 
Did you mean: 

UART Read Non Blocking STM32F217?

Larry Martin
Associate II
Posted on November 26, 2017 at 16:27

I am working with the UART_Hyperterminal_IT example in STM32Cube_FW_F2_V1.7.0.  My micro is STM32F217.

My calls to HAL_UART_Receive_IT() are blocking.  That is probably the expected behavior, but I would prefer to:

1. have the call return immediately with some bad status if no data is available

or

2. use some other call that checks data available in the UART before I call Read.

Is that possible?

2 REPLIES 2
Posted on November 26, 2017 at 20:58

Not supposed to block,

You can read the USART->SR to check for RXNE, might actually be a macro for that, __HAL_USART_GET_FLAG() ?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on November 26, 2017 at 21:47

Thanks, Clive, that did it!

HAL_UART_Receive_IT() does seem to block.  Here is my main loop:

int loopCount = 0;

GlFeigInit(&UartHandle);

while (1)

{

   loopCount += 1;

   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, (loopCount & 0x0001) ?

      GPIO_PIN_SET : GPIO_PIN_RESET);

   GlFeigTick(&UartHandle);

}

and here is the top of GlFeigTick():

void GlFeigTick(UART_HandleTypeDef * pUartHandle_p)

{

   uint8_t ch;

   if (HAL_UART_GetState(pUartHandle_p) == HAL_UART_STATE_READY)

   {

      HAL_StatusTypeDef rc = HAL_TIMEOUT;

      rc = HAL_UART_Receive_IT(pUartHandle_p, &ch, 1); //BLOCKS!!!!!

      if (rc == HAL_OK)

      {

If I comment out the call to HAL_UART_Receive_IT, B1 toggles at about 1 MHz.  If I uncomment that call, B1 changes state when I send a byte to USART2.  Checking RXNE in the UART before trying to read was the fix:

void GlFeigTick(UART_HandleTypeDef * pUartHandle_p)

{

   uint8_t ch;

   if (HAL_UART_GetState(pUartHandle_p) == HAL_UART_STATE_READY)

   {

      HAL_StatusTypeDef rc = HAL_TIMEOUT;

      int notEmpty = __HAL_USART_GET_FLAG(pUartHandle_p, USART_FLAG_RXNE);

      if (notEmpty)

      {

         rc = HAL_UART_Receive_IT(pUartHandle_p, &ch, 1); //BLOCKS!!!!!

         }

      if (rc == HAL_OK)

      {

This code toggles B1, i.e. it does not block, but when I send a byte it gets processed under HAL_OK.

Best Regards,

Larry