Skip to main content
Associate
September 10, 2023
Solved

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

  • September 10, 2023
  • 5 replies
  • 5830 views

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?

This topic has been closed for replies.
Best answer by Issamos

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

5 replies

Issamos
Super User
September 10, 2023

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

darthanaAuthor
Associate
September 13, 2023

@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.

Issamos
IssamosBest answer
Super User
September 13, 2023

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

Tesla DeLorean
Guru
September 10, 2023

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
Pavel A.
September 13, 2023

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)