From 06e6e9dc9dea33d18df968d7399aaeda65c1ef40 Mon Sep 17 00:00:00 2001 From: Per Date: Tue, 24 Sep 2019 17:20:15 +0200 Subject: [PATCH] wip, problem with static Context --- Cargo.toml | 3 ++ examples/generator.rs | 55 ++++++++++++---------------- macros/src/codegen/hardware_tasks.rs | 18 ++++++--- macros/src/codegen/module.rs | 12 +++++- macros/src/codegen/post_init.rs | 19 +++++++++- 5 files changed, 66 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d8b7563e2e..446d98a306 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,9 @@ readme = "README.md" repository = "https://github.com/rtfm-rs/cortex-m-rtfm" version = "0.5.0-beta.1" +[patch.crates-io] +rtfm-syntax = { path = '../rtfm-syntax' } + [lib] name = "rtfm" diff --git a/examples/generator.rs b/examples/generator.rs index bc51bc9c94..a6293c3881 100644 --- a/examples/generator.rs +++ b/examples/generator.rs @@ -7,34 +7,18 @@ #![no_std] use core::ops::Generator; -// use core::mem::MaybeUninit; - -// // #[task(binds = EXTI0, resources = [x])] -// // fn bar(cx: bar::Context) { -// // let x = cx.resources.x; -// // } - -// // expansion -// type Foo = impl Generator; - -// static mut X: MaybeUninit = MaybeUninit::uninit(); - -// fn main() { -// // init(); -// unsafe { -// X.as_mut_ptr().write(foo()); -// } -// // idle(); -// } - -// #![deny(unsafe_code)] - use cortex_m_semihosting::{debug, hprintln}; use lm3s6965::Interrupt; use panic_semihosting as _; +use rtfm::{Exclusive, Mutex}; #[rtfm::app(device = lm3s6965)] const APP: () = { + struct Resources { + #[init(0)] + shared: u32, + } + #[init] fn init(_: init::Context) { hprintln!("init").unwrap(); @@ -43,28 +27,35 @@ const APP: () = { #[idle] fn idle(_: idle::Context) -> ! { - static mut X: u32 = 0; - - // Safe access to local `static mut` variable - let _x: &'static mut u32 = X; hprintln!("idle").unwrap(); + rtfm::pend(Interrupt::GPIOA); + rtfm::pend(Interrupt::GPIOA); debug::exit(debug::EXIT_SUCCESS); loop {} } - // #[task(binds = GPIOA, resources = [x])] - // in user code the return type will be `impl Generator<..>` - #[task(binds = GPIOA)] + #[task(binds = GPIOA, resources = [shared])] + // fn foo1(ctx: &'static mut foo1::Context) -> impl Generator { fn foo1(ctx: foo1::Context) -> impl Generator { + let mut local = 0; + //let shared_res = Exclusive(ctx.resources.shared); + // static shared_res : &mut u32 = ctx.resources.shared; move || loop { - // x.lock(|_| {}); - hprintln!("foo1_1").unwrap(); + hprintln!("foo1_1 {}", local).unwrap(); + local += 1; + // shared_res.lock(|s| hprintln!("s {}", s)); + // *shared_res += 1; + // *ctx.resources.shared += 1; yield; - hprintln!("foo1_2").unwrap(); + hprintln!("foo1_2 {}", local).unwrap(); + local += 1; yield; } } + + // #[task(binds = GPIOB, resources = [shared], priority = 2)] + // fn foo2(ctx: foo2::Context) {} }; diff --git a/macros/src/codegen/hardware_tasks.rs b/macros/src/codegen/hardware_tasks.rs index e62330718c..119ef2c992 100644 --- a/macros/src/codegen/hardware_tasks.rs +++ b/macros/src/codegen/hardware_tasks.rs @@ -74,10 +74,12 @@ pub fn codegen( #let_instant rtfm::export::run(PRIORITY, || { - hprintln!("here").unwrap(); - // core::pin::Pin::new(crate::#gen_static.assume_init()).resume(); - // let stat = &mut core::mem::transmute::<_,#gen_type>(crate::#gen_static); - core::pin::Pin::new(#gen_static.as_mut_ptr()).resume(); + // TODO: Remove trace + hprintln!("interrupt dispatch").unwrap(); + // TODO: possible compiler bug + // core::pin::Pin::new(#gen_static.as_mut_ptr()).resume(); + let mut g: &mut dyn Generator = &mut *#gen_static.as_mut_ptr(); + core::pin::Pin::new_unchecked(&mut *g).resume(); }); } )); @@ -129,8 +131,12 @@ pub fn codegen( // `${task}Locals` let mut locals_pat = None; if !task.locals.is_empty() { - let (struct_, pat) = - locals::codegen(Context::HardwareTask(name), &task.locals, core, app); + let (struct_, pat) = locals::codegen( + Context::HardwareTask(name), + &task.locals, + core, + app, + ); root.push(struct_); locals_pat = Some(pat); diff --git a/macros/src/codegen/module.rs b/macros/src/codegen/module.rs index 5f077a2200..67eae0a0b3 100644 --- a/macros/src/codegen/module.rs +++ b/macros/src/codegen/module.rs @@ -4,7 +4,12 @@ use rtfm_syntax::{ast::App, Context}; use crate::{check::Extra, codegen::util}; -pub fn codegen(ctxt: Context, resources_tick: bool, app: &App, extra: &Extra) -> TokenStream2 { +pub fn codegen( + ctxt: Context, + resources_tick: bool, + app: &App, + extra: &Extra, +) -> TokenStream2 { let mut items = vec![]; let mut fields = vec![]; let mut values = vec![]; @@ -184,7 +189,9 @@ pub fn codegen(ctxt: Context, resources_tick: bool, app: &App, extra: &Extra) -> } )); - values.push(quote!(spawn: Spawn { _not_send: core::marker::PhantomData })); + values.push( + quote!(spawn: Spawn { _not_send: core::marker::PhantomData }), + ); } else { lt = Some(quote!('a)); @@ -319,6 +326,7 @@ pub fn codegen(ctxt: Context, resources_tick: bool, app: &App, extra: &Extra) -> #[doc = #doc] #cfg_core pub mod #name { + fn plepps() {} #(#items)* } ) diff --git a/macros/src/codegen/post_init.rs b/macros/src/codegen/post_init.rs index e3e3b20e01..bee481dbc5 100644 --- a/macros/src/codegen/post_init.rs +++ b/macros/src/codegen/post_init.rs @@ -161,10 +161,27 @@ pub fn codegen( let gen_static = util::gen_static_ident(&name); let priority = task.args.priority; + // #[doc(inline)] + // pub use super::foo1Resources as Resources; + // #[doc = r" Execution context"] + // pub struct Context<'a> { + // #[doc = r" Resources this task has access to"] + // pub resources: Resources<'a>, + // } + // impl<'a> Context<'a> { + // #[inline(always)] + // pub unsafe fn new( + // priority: &'a rtfm::export::Priority, + // ) -> Self { + // Context { + // resources: Resources::new(priority), + // } + // } + // } stmts.push(quote!( const PRIORITY: u8 = #priority; - unsafe { + static CTX: #name::Context = #name::Context::new(&rtfm::export::Priority::new(PRIORITY)); #gen_static.as_mut_ptr().write( #name(#name::Context::new(&rtfm::export::Priority::new(PRIORITY))) );