cancel
Showing results for 
Search instead for 
Did you mean: 

serial.peek() and serial.flush() equivalent in STM32

darthana
Associate

I'm trying to port a UART driver from aurdino to STM32. Able to relate all interfaces for aurdino vs STM32 while can't able to map ones for the following

serial.peek()

serial.flush()

Does HAL have an direct interface as peek() to get the next byte of incoming serial data?

1 ACCEPTED SOLUTION

Accepted Solutions

I think you can't still got the data but you can send the DR value to a unit8_t and save it her than you can rewrite this value to the DR after reading.

Best regards.

II

View solution in original post

5 REPLIES 5
Issamos
Lead II

Hello @darthana 

For serial.flush(), I suggest you to use this:

  • HAL_UART_Transmit(&huart1, (uint8_t*)NULL, 0, HAL_MAX_DELAY);                        //that will not transmit any data but will send the data in wait.

For serial.peek(), I suggest you to use this:

  • Check if the UART_FLAG_RXNE is set or not.
  • If is set, read the data in the USARTx->DR.(usually USART2).

Best regards.

II

You'd have to create a buffer for the incoming data so that you could query it.

The STM32 doesn't hold a large number of bytes, and not in a way you can probe/query

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

@Issamos 

serial.peek() in arduino provides next byte but without clearing it. So when I use read() later, I can still get the data.

As per my understanding, reading USARTx->DR clears data as well as RXNE flag.

Pavel A.
Evangelist III

Yes,  reading USARTx->DR clears RXNE flag, so the data (DR content) becomes undefined. You need a software library that saves the new char and returns it later. See also getc & ungetc  (available with any decent stdio impl.)

As for flush, see __HAL_USART_SEND_REQ(USART_RXDATA_FLUSH_REQUEST)  

I think you can't still got the data but you can send the DR value to a unit8_t and save it her than you can rewrite this value to the DR after reading.

Best regards.

II