From d0f33add0a354da612a1543bdcd74418feedbaf2 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 23 Feb 2019 21:48:47 +0100 Subject: [PATCH 1/8] add `binds` argument to the `interrupt` and `exception` attributes --- macros/src/codegen.rs | 24 ++++++++---- macros/src/syntax.rs | 91 ++++++++++++++++++++++++++++++++----------- 2 files changed, 85 insertions(+), 30 deletions(-) diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index 83e5026670..117bce8cf6 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -469,6 +469,11 @@ fn post_init(ctxt: &Context, app: &App, analysis: &Analysis) -> proc_macro2::Tok let device = &app.args.device; let nvic_prio_bits = quote!(#device::NVIC_PRIO_BITS); for (name, exception) in &app.exceptions { + let name = if let Some(ref binds) = exception.args.binds { + binds + } else { + name + }; let priority = exception.args.priority; exprs.push(quote!(assert!(#priority <= (1 << #nvic_prio_bits)))); exprs.push(quote!(p.SCB.set_priority( @@ -1082,9 +1087,10 @@ fn exceptions(ctxt: &mut Context, app: &App, analysis: &Analysis) -> Vec Vec proc_macro2::Toke let device = &app.args.device; let nvic_prio_bits = quote!(#device::NVIC_PRIO_BITS); for (name, interrupt) in &app.interrupts { + let name = if let Some(ref binds) = interrupt.args.binds { + binds + } else { + name + }; let priority = interrupt.args.priority; exprs.push(quote!(p.NVIC.enable(#device::Interrupt::#name);)); exprs.push(quote!(assert!(#priority <= (1 << #nvic_prio_bits));)); diff --git a/macros/src/syntax.rs b/macros/src/syntax.rs index 9771ea922e..ee03b2f5a3 100644 --- a/macros/src/syntax.rs +++ b/macros/src/syntax.rs @@ -699,6 +699,29 @@ impl Init { } } +/// Union of `TaskArgs`, `ExceptionArgs` and `InterruptArgs` +pub struct Args { + pub binds: Option, + pub capacity: Option, + pub priority: u8, + pub resources: Idents, + pub schedule: Idents, + pub spawn: Idents, +} + +impl Default for Args { + fn default() -> Self { + Args { + binds: None, + capacity: None, + priority: 1, + resources: Idents::new(), + schedule: Idents::new(), + spawn: Idents::new(), + } + } +} + pub struct Exception { pub args: ExceptionArgs, pub attrs: Vec, @@ -708,6 +731,7 @@ pub struct Exception { } pub struct ExceptionArgs { + pub binds: Option, pub priority: u8, pub resources: Idents, pub schedule: Idents, @@ -716,8 +740,9 @@ pub struct ExceptionArgs { impl Parse for ExceptionArgs { fn parse(input: ParseStream<'_>) -> parse::Result { - parse_args(input, false).map( - |TaskArgs { + parse_args(input, /* binds */ true, /* capacity */ false).map( + |Args { + binds, priority, resources, schedule, @@ -725,6 +750,7 @@ impl Parse for ExceptionArgs { .. }| { ExceptionArgs { + binds, priority, resources, schedule, @@ -755,7 +781,7 @@ impl Exception { } let span = item.ident.span(); - match &*item.ident.to_string() { + match &*args.binds.as_ref().unwrap_or(&item.ident).to_string() { "MemoryManagement" | "BusFault" | "UsageFault" | "SecureFault" | "SVCall" | "DebugMonitor" | "PendSV" => {} // OK "SysTick" => { @@ -893,30 +919,40 @@ pub struct TaskArgs { pub schedule: Idents, } -impl Default for TaskArgs { - fn default() -> Self { - TaskArgs { - capacity: None, - priority: 1, - resources: Idents::new(), - schedule: Idents::new(), - spawn: Idents::new(), - } - } -} - impl Parse for TaskArgs { fn parse(input: ParseStream<'_>) -> parse::Result { - parse_args(input, true) + parse_args(input, /* binds */ false, /* capacity */ true).map( + |Args { + capacity, + priority, + resources, + schedule, + spawn, + .. + }| { + TaskArgs { + capacity, + priority, + resources, + schedule, + spawn, + } + }, + ) } } -// Parser shared by TaskArgs and ExceptionArgs / InterruptArgs -fn parse_args(input: ParseStream<'_>, accept_capacity: bool) -> parse::Result { +// Parser shared by ExceptionArgs, InterruptArgs and TaskArgs +fn parse_args( + input: ParseStream<'_>, + accepts_binds: bool, + accepts_capacity: bool, +) -> parse::Result { if input.is_empty() { - return Ok(TaskArgs::default()); + return Ok(Args::default()); } + let mut binds = None; let mut capacity = None; let mut priority = None; let mut resources = None; @@ -936,7 +972,13 @@ fn parse_args(input: ParseStream<'_>, accept_capacity: bool) -> parse::Result { + "binds" if accepts_binds => { + // #ident + let ident = content.parse()?; + + binds = Some(ident); + } + "capacity" if accepts_capacity => { if capacity.is_some() { return Err(parse::Error::new( ident.span(), @@ -1052,7 +1094,11 @@ fn parse_args(input: ParseStream<'_>, accept_capacity: bool) -> parse::Result { return Err(parse::Error::new( ident.span(), - "expected one of: priority, resources, schedule or spawn", + format!( + "expected one of: {}{}priority, resources, schedule or spawn", + if accepts_binds { "binds, " } else { "" }, + if accepts_capacity { "capacity, " } else { "" }, + ), )); } } @@ -1065,7 +1111,8 @@ fn parse_args(input: ParseStream<'_>, accept_capacity: bool) -> parse::Result Date: Sat, 23 Feb 2019 21:50:52 +0100 Subject: [PATCH 2/8] add some tests --- tests/cfail/used-free-interrupt-2.rs | 21 +++++++++++++++++++++ tests/cpass/binds.rs | 25 +++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 tests/cfail/used-free-interrupt-2.rs create mode 100644 tests/cpass/binds.rs diff --git a/tests/cfail/used-free-interrupt-2.rs b/tests/cfail/used-free-interrupt-2.rs new file mode 100644 index 0000000000..f9aab78efd --- /dev/null +++ b/tests/cfail/used-free-interrupt-2.rs @@ -0,0 +1,21 @@ +#![no_main] +#![no_std] + +extern crate lm3s6965; +extern crate panic_halt; +extern crate rtfm; + +use rtfm::app; + +#[app(device = lm3s6965)] +const APP: () = { + #[init] + fn init() {} + + #[interrupt(binds = UART0)] + fn foo() {} //~ ERROR free interrupts (`extern { .. }`) can't be used as interrupt handlers + + extern "C" { + fn UART0(); + } +}; diff --git a/tests/cpass/binds.rs b/tests/cpass/binds.rs new file mode 100644 index 0000000000..361f08fc93 --- /dev/null +++ b/tests/cpass/binds.rs @@ -0,0 +1,25 @@ +//! Check that `binds` works as advertised +#![no_main] +#![no_std] + +extern crate lm3s6965; +extern crate panic_halt; +extern crate rtfm; + +use rtfm::app; + +#[app(device = lm3s6965)] +const APP: () = { + #[init] + fn init() {} + + #[exception(binds = SVCall)] + fn foo() {} + + #[interrupt(binds = UART0)] + fn bar() {} +}; + +fn foo_trampoline(_: foo::Context) {} + +fn bar_trampoline(_: bar::Context) {} From 72f0cc505addf00d493b764418e4207f53434152 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 23 Feb 2019 21:54:56 +0100 Subject: [PATCH 3/8] make cfail test actually fail --- macros/src/check.rs | 12 +++++++++--- tests/cfail/used-free-interrupt-2.rs | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/macros/src/check.rs b/macros/src/check.rs index 464e280ac1..ab86461612 100644 --- a/macros/src/check.rs +++ b/macros/src/check.rs @@ -106,10 +106,16 @@ pub fn app(app: &App) -> parse::Result<()> { } // Check that free interrupts are not being used - for int in app.interrupts.keys() { - if app.free_interrupts.contains_key(int) { + for (name, interrupt) in &app.interrupts { + let name = if let Some(ref binds) = interrupt.args.binds { + binds + } else { + name + }; + + if app.free_interrupts.contains_key(name) { return Err(parse::Error::new( - int.span(), + name.span(), "free interrupts (`extern { .. }`) can't be used as interrupt handlers", )); } diff --git a/tests/cfail/used-free-interrupt-2.rs b/tests/cfail/used-free-interrupt-2.rs index f9aab78efd..616d308d0d 100644 --- a/tests/cfail/used-free-interrupt-2.rs +++ b/tests/cfail/used-free-interrupt-2.rs @@ -12,8 +12,8 @@ const APP: () = { #[init] fn init() {} - #[interrupt(binds = UART0)] - fn foo() {} //~ ERROR free interrupts (`extern { .. }`) can't be used as interrupt handlers + #[interrupt(binds = UART0)] //~ ERROR free interrupts (`extern { .. }`) can't be used as interrupt handlers + fn foo() {} extern "C" { fn UART0(); From a23380828071af69ec0362aae1b30c6e09b511f0 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 23 Feb 2019 21:56:05 +0100 Subject: [PATCH 4/8] fix warnings in cpass test --- tests/cpass/binds.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/cpass/binds.rs b/tests/cpass/binds.rs index 361f08fc93..7cb91741fd 100644 --- a/tests/cpass/binds.rs +++ b/tests/cpass/binds.rs @@ -20,6 +20,8 @@ const APP: () = { fn bar() {} }; +#[allow(dead_code)] fn foo_trampoline(_: foo::Context) {} +#[allow(dead_code)] fn bar_trampoline(_: bar::Context) {} From 11f795aaf69dbd7d185bbf0136ae555b53768538 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 23 Feb 2019 22:20:30 +0100 Subject: [PATCH 5/8] add `binds` example and make it work --- ci/expected/binds.run | 4 ++++ ci/script.sh | 1 + examples/binds.rs | 48 +++++++++++++++++++++++++++++++++++++++++++ macros/src/codegen.rs | 4 ++-- 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 ci/expected/binds.run create mode 100644 examples/binds.rs diff --git a/ci/expected/binds.run b/ci/expected/binds.run new file mode 100644 index 0000000000..f84cff0157 --- /dev/null +++ b/ci/expected/binds.run @@ -0,0 +1,4 @@ +init +foo called 1 time +idle +foo called 2 times diff --git a/ci/script.sh b/ci/script.sh index 5cc79fbbce..7cda1e5e66 100644 --- a/ci/script.sh +++ b/ci/script.sh @@ -93,6 +93,7 @@ main() { idle init interrupt + binds resource lock diff --git a/examples/binds.rs b/examples/binds.rs new file mode 100644 index 0000000000..a8b386fb7e --- /dev/null +++ b/examples/binds.rs @@ -0,0 +1,48 @@ +//! examples/binds.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +extern crate panic_semihosting; + +use cortex_m_semihosting::{debug, hprintln}; +use lm3s6965::Interrupt; +use rtfm::app; + +// `examples/interrupt.rs` rewritten to use `binds` +#[app(device = lm3s6965)] +const APP: () = { + #[init] + fn init() { + rtfm::pend(Interrupt::UART0); + + hprintln!("init").unwrap(); + } + + #[idle] + fn idle() -> ! { + hprintln!("idle").unwrap(); + + rtfm::pend(Interrupt::UART0); + + debug::exit(debug::EXIT_SUCCESS); + + loop {} + } + + #[interrupt(binds = UART0)] + fn foo() { + static mut TIMES: u32 = 0; + + *TIMES += 1; + + hprintln!( + "foo called {} time{}", + *TIMES, + if *TIMES > 1 { "s" } else { "" } + ) + .unwrap(); + } +}; diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index 117bce8cf6..0e25e8a7a6 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -1128,7 +1128,7 @@ fn exceptions(ctxt: &mut Context, app: &App, analysis: &Analysis) -> Vec Date: Sat, 23 Feb 2019 22:21:30 +0100 Subject: [PATCH 6/8] document the `binds` feature cc @burrbull --- book/en/src/by-example/tips.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/book/en/src/by-example/tips.md b/book/en/src/by-example/tips.md index 8f71599a06..c0bfc56e14 100644 --- a/book/en/src/by-example/tips.md +++ b/book/en/src/by-example/tips.md @@ -75,3 +75,19 @@ $ cargo nm --example ramfunc --release | grep ' foo::' ``` console $ cargo nm --example ramfunc --release | grep ' bar::' {{#include ../../../../ci/expected/ramfunc.grep.bar}}``` + +## `binds` + +**NOTE**: Requires RTFM ~0.4.2 + +You can give hardware tasks more task-like names using the `binds` argument: you +name the function as you wish and specify the name of the interrupt / exception +in the `binds` argument. Types like `Spawn` will be placed in a module named +after the function, not the interrupt / exception. Example below: + +``` rust +{{#include ../../../../examples/binds.rs}} +``` +``` console +$ cargo run --example binds +{{#include ../../../../ci/expected/binds.run}}``` From 2fd6ae69d1c79635896b643b1094b3805d1ec6c2 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 23 Feb 2019 22:38:10 +0100 Subject: [PATCH 7/8] `binds` can only appear once in the argument list --- macros/src/syntax.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/macros/src/syntax.rs b/macros/src/syntax.rs index ee03b2f5a3..23981d98d0 100644 --- a/macros/src/syntax.rs +++ b/macros/src/syntax.rs @@ -973,6 +973,13 @@ fn parse_args( let ident_s = ident.to_string(); match &*ident_s { "binds" if accepts_binds => { + if binds.is_some() { + return Err(parse::Error::new( + ident.span(), + "argument appears more than once", + )); + } + // #ident let ident = content.parse()?; From 8eccef7d9cda8a60594b86d31b656a3680d1ca16 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Tue, 26 Feb 2019 23:25:16 +0100 Subject: [PATCH 8/8] refactor: make `binds` harder to misuse --- macros/src/check.rs | 8 ++------ macros/src/codegen.rs | 20 ++++++-------------- macros/src/syntax.rs | 9 ++++++++- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/macros/src/check.rs b/macros/src/check.rs index ab86461612..4adc2c1756 100644 --- a/macros/src/check.rs +++ b/macros/src/check.rs @@ -106,12 +106,8 @@ pub fn app(app: &App) -> parse::Result<()> { } // Check that free interrupts are not being used - for (name, interrupt) in &app.interrupts { - let name = if let Some(ref binds) = interrupt.args.binds { - binds - } else { - name - }; + for (handler, interrupt) in &app.interrupts { + let name = interrupt.args.binds(handler); if app.free_interrupts.contains_key(name) { return Err(parse::Error::new( diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index 0e25e8a7a6..1d201c0866 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -468,12 +468,8 @@ fn post_init(ctxt: &Context, app: &App, analysis: &Analysis) -> proc_macro2::Tok // the device into compile errors let device = &app.args.device; let nvic_prio_bits = quote!(#device::NVIC_PRIO_BITS); - for (name, exception) in &app.exceptions { - let name = if let Some(ref binds) = exception.args.binds { - binds - } else { - name - }; + for (handler, exception) in &app.exceptions { + let name = exception.args.binds(handler); let priority = exception.args.priority; exprs.push(quote!(assert!(#priority <= (1 << #nvic_prio_bits)))); exprs.push(quote!(p.SCB.set_priority( @@ -1128,7 +1124,7 @@ fn exceptions(ctxt: &mut Context, app: &App, analysis: &Analysis) -> Vec proc_macro2::Toke // the device into compile errors let device = &app.args.device; let nvic_prio_bits = quote!(#device::NVIC_PRIO_BITS); - for (name, interrupt) in &app.interrupts { - let name = if let Some(ref binds) = interrupt.args.binds { - binds - } else { - name - }; + for (handler, interrupt) in &app.interrupts { + let name = interrupt.args.binds(handler); let priority = interrupt.args.priority; exprs.push(quote!(p.NVIC.enable(#device::Interrupt::#name);)); exprs.push(quote!(assert!(#priority <= (1 << #nvic_prio_bits));)); diff --git a/macros/src/syntax.rs b/macros/src/syntax.rs index 23981d98d0..7f87f6339e 100644 --- a/macros/src/syntax.rs +++ b/macros/src/syntax.rs @@ -731,13 +731,20 @@ pub struct Exception { } pub struct ExceptionArgs { - pub binds: Option, + binds: Option, pub priority: u8, pub resources: Idents, pub schedule: Idents, pub spawn: Idents, } +impl ExceptionArgs { + /// Returns the name of the exception / interrupt this handler binds to + pub fn binds<'a>(&'a self, handler: &'a Ident) -> &'a Ident { + self.binds.as_ref().unwrap_or(handler) + } +} + impl Parse for ExceptionArgs { fn parse(input: ParseStream<'_>) -> parse::Result { parse_args(input, /* binds */ true, /* capacity */ false).map(