Add some comments to uart example.

This commit is contained in:
Dario Nieuwenhuis 2021-01-01 23:04:18 +01:00
parent 20d3dc87f9
commit 396d7650ca

View File

@ -27,17 +27,26 @@ async fn run(mut uart: uarte::Uarte<pac::UARTE0>) {
uart.send(&buf).await; uart.send(&buf).await;
info!("wrote hello in uart!"); info!("wrote hello in uart!");
info!("reading...");
loop { loop {
let received = match select( info!("reading...");
uart.receive(&mut buf),
Timer::after(Duration::from_millis(10)), // `receive()` doesn't return until the buffer has been completely filled with
) // incoming data, which in this case is 8 bytes.
.await //
{ // This example shows how to use `select` to run an uart receive concurrently with a
// 1 second timer, effectively adding a timeout to the receive operation.
let recv_fut = uart.receive(&mut buf);
let timer_fut = Timer::after(Duration::from_millis(1000));
let received = match select(recv_fut, timer_fut).await {
// recv_fut completed first, so we've received `buf_len` bytes.
Either::Left((buf, _)) => buf, Either::Left((buf, _)) => buf,
Either::Right((_, read)) => { // timer_fut completed first. `select` gives us back the future that didn't complete, which
let (buf, n) = read.stop().await; // is `recv_fut` in this case, so we can do further stuff with it.
//
// The recv_fut would stop the uart read automatically when dropped. However, we want to know how
// many bytes have been received, so we have to "gracefully stop" it with `.stop()`.
Either::Right((_, recv_fut)) => {
let (buf, n) = recv_fut.stop().await;
&buf[..n] &buf[..n]
} }
}; };