wip (problem with Pin)

This commit is contained in:
Per Lindgren 2019-09-24 02:01:25 +02:00
parent 38306389ea
commit 99a45ff28e
8 changed files with 373 additions and 28 deletions

70
examples/generator.rs Normal file
View file

@ -0,0 +1,70 @@
//! examples/idle.rs
#![feature(generator_trait)]
#![feature(generators)]
#![feature(never_type)]
#![feature(type_alias_impl_trait)]
#![no_main]
#![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<Yield = (), Return = !>;
// static mut X: MaybeUninit<Foo> = 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 _;
#[rtfm::app(device = lm3s6965)]
const APP: () = {
#[init]
fn init(_: init::Context) {
hprintln!("init").unwrap();
rtfm::pend(Interrupt::GPIOA);
}
#[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();
debug::exit(debug::EXIT_SUCCESS);
loop {}
}
// #[task(binds = GPIOA, resources = [x])]
// in user code the return type will be `impl Generator<..>`
#[task(binds = GPIOA)]
fn foo1(ctx: foo1::Context) -> impl Generator<Yield = (), Return = !> {
move || loop {
// x.lock(|_| {});
hprintln!("foo1_1").unwrap();
yield;
hprintln!("foo1_2").unwrap();
yield;
}
}
};

172
gen.rs Normal file
View file

@ -0,0 +1,172 @@
#![feature(prelude_import)]
//! examples/idle.rs
#![feature(generator_trait)]
#![feature(generators)]
#![feature(never_type)]
#![feature(type_alias_impl_trait)]
#![no_main]
#![no_std]
#[prelude_import]
use core::prelude::v1::*;
#[macro_use]
extern crate core;
#[macro_use]
extern crate compiler_builtins;
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<Yield = (), Return = !>;
// static mut X: MaybeUninit<Foo> = 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 _;
#[allow(non_snake_case)]
fn init(_: init::Context) {
// Safe access to local `static mut` variable
// #[task(binds = GPIOA, resources = [x])]
// in user code the return type will be `impl Generator<..>`
// x.lock(|_| {});
::cortex_m_semihosting::export::hstdout_str("init\n").unwrap();
rtfm::pend(Interrupt::GPIOA);
}
#[allow(non_snake_case)]
fn idle(idle::Locals { X, .. }: idle::Locals, _: idle::Context) -> ! {
use rtfm::Mutex as _;
let _x: &'static mut u32 = X;
::cortex_m_semihosting::export::hstdout_str("idle\n").unwrap();
debug::exit(debug::EXIT_SUCCESS);
loop { }
}
type Generatorfoo1= impl : Generator<Yield = (), Return = !>;
static mut GENERATOR_FOO1: core::mem::MaybeUninit<Generatorfoo1> =
core::mem::MaybeUninit::uninit();
#[allow(non_snake_case)]
fn foo1(ctx: foo1::Context) -> Generatorfoo1 {
use rtfm::Mutex as _;
move ||
loop {
::cortex_m_semihosting::export::hstdout_str("foo1_1\n").unwrap();
yield;
::cortex_m_semihosting::export::hstdout_str("foo1_2\n").unwrap();
yield;
}
}
#[allow(non_snake_case)]
#[doc = "Initialization function"]
pub mod init {
#[doc = r" Execution context"]
pub struct Context {
#[doc = r" Core (Cortex-M) peripherals"]
pub core: rtfm::export::Peripherals,
}
impl Context<> {
#[inline(always)]
pub unsafe fn new(core: rtfm::export::Peripherals) -> Self {
Context{core,}
}
}
}
#[allow(non_snake_case)]
#[doc(hidden)]
pub struct idleLocals {
X: &'static mut u32,
}
impl idleLocals<> {
#[inline(always)]
unsafe fn new() -> Self {
static mut X: u32 = 0;
idleLocals{X: &mut X,}
}
}
#[allow(non_snake_case)]
#[doc = "Idle loop"]
pub mod idle {
#[doc(inline)]
pub use super::idleLocals as Locals;
#[doc = r" Execution context"]
pub struct Context {
}
impl Context<> {
#[inline(always)]
pub unsafe fn new(priority: &rtfm::export::Priority) -> Self {
Context{}
}
}
}
#[allow(non_snake_case)]
#[doc = "Hardware task"]
pub mod foo1 {
#[doc = r" Execution context"]
pub struct Context {
}
impl Context<> {
#[inline(always)]
pub unsafe fn new(priority: &rtfm::export::Priority) -> Self {
Context{}
}
}
}
#[doc = r" Implementation details"]
const APP: () =
{
#[doc =
r" Always include the device crate which contains the vector table"]
use lm3s6965 as _;
#[allow(non_snake_case)]
#[no_mangle]
unsafe fn GPIOA() {
const PRIORITY: u8 = 1u8;
rtfm::export::run(PRIORITY,
||
{
::cortex_m_semihosting::export::hstdout_str("here\n").unwrap();
core::pin::Pin::new(GENERATOR_FOO1.as_mut_ptr()).resume();
});
}
#[no_mangle]
unsafe extern "C" fn main() -> ! {
rtfm::export::interrupt::disable();
let mut core: rtfm::export::Peripherals =
core::mem::transmute(());
let _ = [(); ((1 << lm3s6965::NVIC_PRIO_BITS) - 1u8 as usize)];
core.NVIC.set_priority(lm3s6965::Interrupt::GPIOA,
rtfm::export::logical2hw(1u8,
lm3s6965::NVIC_PRIO_BITS));
rtfm::export::NVIC::unmask(lm3s6965::Interrupt::GPIOA);
let late = init(init::Context::new(core.into()));
const PRIORITY: u8 = 1u8;
unsafe {
GENERATOR_FOO1.as_mut_ptr().write(foo1(foo1::Context::new(&rtfm::export::Priority::new(PRIORITY))));
};
rtfm::export::interrupt::enable();
idle(idle::Locals::new(),
idle::Context::new(&rtfm::export::Priority::new(0)))
}
};

View file

@ -39,7 +39,8 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 {
let (const_app_init, root_init, user_init, call_init) = let (const_app_init, root_init, user_init, call_init) =
init::codegen(core, app, analysis, extra); init::codegen(core, app, analysis, extra);
let (const_app_post_init, post_init_stmts) = post_init::codegen(core, analysis, extra); let (const_app_post_init, post_init_stmts) =
post_init::codegen(core, &app, analysis, extra);
let (const_app_idle, root_idle, user_idle, call_idle) = let (const_app_idle, root_idle, user_idle, call_idle) =
idle::codegen(core, app, analysis, extra); idle::codegen(core, app, analysis, extra);

View file

@ -29,6 +29,9 @@ pub fn codegen(
let mut user_tasks = vec![]; let mut user_tasks = vec![];
for (name, task) in &app.hardware_tasks { for (name, task) in &app.hardware_tasks {
// per: debug
// println!("// -- name -- {:?}", name);
// println!("// -- task -- {:?}", task);
let core = task.args.core; let core = task.args.core;
let cfg_core = util::cfg_core(core, app.args.cores); let cfg_core = util::cfg_core(core, app.args.cores);
@ -57,6 +60,28 @@ pub fn codegen(
let priority = task.args.priority; let priority = task.args.priority;
let section = util::link_section("text", core); let section = util::link_section("text", core);
if task.is_generator {
let gen_static = util::gen_static_ident(&name);
let gen_type = util::gen_type_ident(&name);
const_app.push(quote!(
#[allow(non_snake_case)]
#[no_mangle]
#section
#cfg_core
unsafe fn #symbol() {
const PRIORITY: u8 = #priority;
#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();
});
}
));
} else {
const_app.push(quote!( const_app.push(quote!(
#[allow(non_snake_case)] #[allow(non_snake_case)]
#[no_mangle] #[no_mangle]
@ -75,6 +100,7 @@ pub fn codegen(
}); });
} }
)); ));
}
let mut needs_lt = false; let mut needs_lt = false;
@ -116,6 +142,40 @@ pub fn codegen(
let section = util::link_section("text", core); let section = util::link_section("text", core);
// XXX shouldn't this have a cfg_core? // XXX shouldn't this have a cfg_core?
let locals_pat = locals_pat.iter(); let locals_pat = locals_pat.iter();
if task.is_generator {
// exapmle expansion
// type GeneratorFoo = impl Generator<Yield = (), Return = !>;
// static mut GENERATOR_FOO: MaybeUninit<GeneratorFoo> = MaybeUninit::uninit();
// #[allow(non_snake_case)]
// fn foo(ctx: foo::Context) -> GeneratorFoo {
// use rtfm::Mutex as _;
// move || loop { yield }
// }
let gen_type = util::gen_type_ident(&name);
let gen_static = util::gen_static_ident(&name);
user_tasks.push(quote!(
type #gen_type = impl Generator<Yield = (), Return = !>;
));
user_tasks.push(quote!(
static mut #gen_static : core::mem::MaybeUninit<#gen_type> = core::mem::MaybeUninit::uninit();
));
user_tasks.push(quote!(
#(#attrs)*
#[allow(non_snake_case)]
#section
fn #name(#(#locals_pat,)* #context: #name::Context) -> #gen_type {
use rtfm::Mutex as _;
#(#stmts)*
}
));
} else {
// generate ordinary task
user_tasks.push(quote!( user_tasks.push(quote!(
#(#attrs)* #(#attrs)*
#[allow(non_snake_case)] #[allow(non_snake_case)]
@ -127,6 +187,7 @@ pub fn codegen(
} }
)); ));
} }
}
(const_app, root, user_tasks) (const_app, root, user_tasks)
} }

View file

@ -1,11 +1,13 @@
use proc_macro2::TokenStream as TokenStream2; use proc_macro2::TokenStream as TokenStream2;
use quote::quote; use quote::quote;
use rtfm_syntax::ast::App;
use crate::{analyze::Analysis, check::Extra, codegen::util}; use crate::{analyze::Analysis, check::Extra, codegen::util};
/// Generates code that runs after `#[init]` returns /// Generates code that runs after `#[init]` returns
pub fn codegen( pub fn codegen(
core: u8, core: u8,
app: &App,
analysis: &Analysis, analysis: &Analysis,
extra: &Extra, extra: &Extra,
) -> (Vec<TokenStream2>, Vec<TokenStream2>) { ) -> (Vec<TokenStream2>, Vec<TokenStream2>) {
@ -148,6 +150,29 @@ pub fn codegen(
} }
} }
// initialize generators
for (name, task) in &app.hardware_tasks {
if task.is_generator {
// example expansion
// const PRIORITY: u8 = 1;
// unsafe {
// GeneratorFoo.as_mut_ptr().write(foo(foo::Context::new(PRIORITY)));
// }
let gen_static = util::gen_static_ident(&name);
let priority = task.args.priority;
stmts.push(quote!(
const PRIORITY: u8 = #priority;
unsafe {
#gen_static.as_mut_ptr().write(
#name(#name::Context::new(&rtfm::export::Priority::new(PRIORITY)))
);
};
));
}
}
// enable the interrupts -- this completes the `init`-ialization phase // enable the interrupts -- this completes the `init`-ialization phase
stmts.push(quote!(rtfm::export::interrupt::enable();)); stmts.push(quote!(rtfm::export::interrupt::enable();));

View file

@ -323,3 +323,17 @@ pub fn suffixed(name: &str, core: u8) -> Ident {
pub fn tq_ident(core: Core) -> Ident { pub fn tq_ident(core: Core) -> Ident {
Ident::new(&format!("TQ{}", core), Span::call_site()) Ident::new(&format!("TQ{}", core), Span::call_site())
} }
/// Generates the generator type
/// TODO: Should be CamelCase
pub fn gen_type_ident(name: &Ident) -> Ident {
Ident::new(&format!("Generator{}", name), Span::call_site())
}
/// Generates the generator type
pub fn gen_static_ident(name: &Ident) -> Ident {
Ident::new(
&format!("GENERATOR_{}", name).to_uppercase(),
Span::call_site(),
)
}

View file

@ -1,4 +1,5 @@
#![deny(warnings)] // Per : remove comment
// #![deny(warnings)]
extern crate proc_macro; extern crate proc_macro;

View file

@ -39,7 +39,8 @@
#![deny(missing_docs)] #![deny(missing_docs)]
#![deny(rust_2018_compatibility)] #![deny(rust_2018_compatibility)]
#![deny(rust_2018_idioms)] #![deny(rust_2018_idioms)]
#![deny(warnings)] // Per : remove comment
// #![deny(warnings)]
#![no_std] #![no_std]
use core::ops::Sub; use core::ops::Sub;