#嵌入式设备 #rp2040 #运行时 #未来 #栈分配 #单线程 #执行器

嵌入式运行时-rp2040

适用于嵌入式设备的轻量级异步运行时,为rp2040提供了预定义的运行时钩子

3个版本 (重大更改)

0.3.0 2024年5月24日
0.2.0 2023年7月21日
0.1.2 2023年7月15日

#1569嵌入式开发

Download history 185/week @ 2024-05-21 7/week @ 2024-05-28 4/week @ 2024-06-04

每月151次下载

BSD-2-Clause OR MIT

23KB
184

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

嵌入式运行时-rp2040

这个crate提供了一个轻量级的异步运行时,针对嵌入式设备。因此,它提供了一个单线程执行器和栈分配的future盒子。这个crate基于wfe/sevrp2040注入了硬件实现。

示例

# 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");

依赖项

~600KB