Closed andresv closed 10 months ago
I don't think we should add it, you can already do it with the available API.
while uart.read_ready()? {
let n = uart.fill_buf()?.len();
uart.consume(n);
}
Ahaa then there is just a bug in embassy https://github.com/embassy-rs/embassy/blob/main/embassy-stm32/src/usart/buffered.rs#L340 which does not allow to just call consume
with max buffer value.
Notice that it only modifies start and not the end: https://github.com/embassy-rs/embassy/blob/main/embassy-hal-internal/src/atomic_ring_buffer.rs#L338.
I'll move this discussion to embassy
.
does not allow to just call consume with max buffer value
you're only allowed to call consume
with at most fill_buf().len()
. You can't consume bytes you haven't received yet.
Then it is not that easy to do with async. Because in order to figure out how many bytes there are in internal buffer I have to call fill_buf().await
again. I cannot get the len otherwise.
loop {
match uart.fill_buf.await {
Ok(bytes) => {
match parser.parse(bytes) {
Ok(msg) => {
uart.consume(bytes.len());
return Some(msg);
}
Err(e) => {
// Let's just discard all possible data in the incoming buffer.
// This here returns immediately if there is some data in buffers,
// but if there isn't then it waits until something is received
// but then it already starts to receive data that I would like to handle in main match.
// Here I just wanted to be sure that everything that we already have is discarded
// and no new data is received.
if let Ok(bytes) = uart.fill_buf.await {
uart.consume(bytes.len());
}
}
}
}
Err(_) => {}
}
}
this is why I wrapped it in a while uart.read_ready()?
loop. If there's no data ready to be retrieved from the rx buffer, then don't do any fill_buf()
. If there's data, fill_buf()
will return just that, not waiting for more. So the loop is guaranteed to empty the buffer, and never block.
Let's say I have an akward protocol that uses time gaps between fixed length messages. Here it starts reading from wrong place and continues to do so.
Bunch of wrong bytes are feed into parser. In this situation I would like to discard all data that is in internal buffer and start over.
Currently there isn't any way to discard data so I propose addind
discard
method forBufRead
.