#embedded-devices #run-time #tiny #nrf52840 #async #events #wake

embedded-runtime-nrf52840

A tiny async runtime for embedded devices, with predefined runtime hooks for the nrf52840

2 unstable releases

0.3.0 May 24, 2024
0.2.0 Aug 23, 2023

#2001 in Embedded development

Download history 137/week @ 2024-05-25 6/week @ 2024-06-01 1/week @ 2024-06-08 4/week @ 2024-06-29 13/week @ 2024-07-06

89 downloads per month

BSD-2-Clause OR MIT

23KB
184 lines

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

embedded-runtime-nrf52840

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 nrf52840, based upon wfe/sev.

Example

# use core::{
#     future::Future,
#     pin::Pin,
#     task::{Poll, Context}
# };
#
# /// Blocks until an event occurs (may wake spuriously)
# #[no_mangle]
# #[allow(non_snake_case)]
# pub fn _runtime_waitforevent_TBFzxdKN() {
#     // No-op
# }
# 
# /// Raises an event
# #[no_mangle]
# #[allow(non_snake_case)]
# pub fn _runtime_sendevent_3YSaPmB7() {
#     // No-op
# }
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");

Dependencies

~600KB