Crate embedded_runtime_rp2040

source ·
Expand description

License BSD-2-Clause License MIT AppVeyor CI docs.rs crates.io Download numbers dependency status

§embedded-runtime-rp2040

This crate provides a tiny async runtime, targeted at embedded devices. Therefore, it provides a single-threaded executor as well as a stack-allocated box to box futures. This crate injects a hardware implementation for the rp2040, based upon wfe/sev.

§Example

use embedded_runtime::run;

/// A countdown future that resolves to pending until the poll-countdown becomes zero
struct CountdownFuture {
    /// The current countdown value
    countdown: usize
}
impl CountdownFuture {
    /// Creates a new countdown future
    pub const fn new(countdown: usize) -> Self {
        Self { countdown }
    }
}
impl Future for CountdownFuture {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        // Decrement the value if we are still pending
        if self.countdown > 0 {
            // Print the countdown
            println!("{}!", self.countdown);

            // Decrement the future, wake the executor and return pending
            *self = Self::new(self.countdown - 1);
            cx.waker().wake_by_ref();
            return Poll::Pending;
        }

        // Return ready
        println!("Liftoff 🚀");
        Poll::Ready(())
    }
}

// This creates a new runtime and executes the given futures in an async context
run!(async {
    CountdownFuture::new(3).await;
    CountdownFuture::new(7).await;
}).expect("failed to perform countdowns");

Modules§

  • Futures to interrupt the current async context to give timeslices to other pending tasks
  • A runtime related error type
  • The executor for this runtime

Macros§

  • Creates a new error
  • Creates an executor and executes the given futures

Structs§

  • A tiny, single-threaded async executor suitable for embedded runtimes

Functions§

  • A function that can be polled once before it becomes ready; useful to suspend the current context and up a timeslice to the runtime/other pending futures
  • A function that can be awaited once before it returns; useful to cooperatively give up a timeslice to the runtime/other pending futures