serial.peek() and serial.flush() equivalent in STM32
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-10 8:05 AM - edited ‎2023-09-13 1:09 AM
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?
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-13 1:39 AM - edited ‎2023-09-13 1:41 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-10 8:52 AM - edited ‎2023-09-10 9:04 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-10 3:05 PM
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
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-13 1:07 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-13 1:30 AM - edited ‎2023-09-13 1:41 AM
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-13 1:39 AM - edited ‎2023-09-13 1:41 AM
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
