nrf: add main macro support

This commit is contained in:
Dario Nieuwenhuis 2021-03-29 02:47:10 +02:00
parent af6d708c93
commit d5ab02792c
13 changed files with 173 additions and 264 deletions

View file

@ -8,10 +8,11 @@ edition = "2018"
syn = { version = "1.0.39", features = ["full", "extra-traits"] }
quote = "1.0.7"
darling = "0.10.2"
proc-macro2 = "1.0.24"
[lib]
proc-macro = true
[features]
stm32 = []
nrf = []
nrf = []

View file

@ -0,0 +1,62 @@
use darling::FromMeta;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::spanned::Spanned;
#[derive(Debug, FromMeta)]
pub enum HfclkSource {
Internal,
ExternalXtal,
}
impl Default for HfclkSource {
fn default() -> Self {
Self::Internal
}
}
#[derive(Debug, FromMeta)]
pub enum LfclkSource {
InternalRC,
Synthesized,
ExternalXtal,
ExternalLowSwing,
ExternalFullSwing,
}
impl Default for LfclkSource {
fn default() -> Self {
Self::InternalRC
}
}
#[derive(Debug, FromMeta)]
pub struct Args {
#[darling(default)]
pub hfclk_source: HfclkSource,
#[darling(default)]
pub lfclk_source: LfclkSource,
}
pub fn generate(args: Args) -> TokenStream {
let hfclk_source = format_ident!("{}", format!("{:?}", args.hfclk_source));
let lfclk_source = format_ident!("{}", format!("{:?}", args.lfclk_source));
quote!(
use embassy_nrf::{interrupt, peripherals, rtc};
let mut config = embassy_nrf::system::Config::default();
config.hfclk_source = embassy_nrf::system::HfclkSource::#hfclk_source;
config.lfclk_source = embassy_nrf::system::LfclkSource::#lfclk_source;
unsafe { embassy_nrf::system::configure(config) };
let mut rtc = rtc::RTC::new(unsafe { <peripherals::RTC1 as embassy::util::Steal>::steal() }, interrupt::take!(RTC1));
let rtc = unsafe { make_static(&mut rtc) };
rtc.start();
let mut alarm = rtc.alarm0();
unsafe { embassy::time::set_clock(rtc) };
let alarm = unsafe { make_static(&mut alarm) };
)
}

View file

@ -0,0 +1,51 @@
use darling::FromMeta;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::spanned::Spanned;
#[derive(Debug, FromMeta)]
pub struct Args {
#[darling(default)]
pub use_hse: Option<u32>,
#[darling(default)]
pub sysclk: Option<u32>,
#[darling(default)]
pub pclk1: Option<u32>,
}
pub fn generate(args: Args) -> TokenStream {
let mut clock_cfg_args = quote! {};
if args.use_hse.is_some() {
let mhz = args.use_hse.unwrap();
clock_cfg_args = quote! { #clock_cfg_args.use_hse(#mhz.mhz()) };
}
if args.sysclk.is_some() {
let mhz = args.sysclk.unwrap();
clock_cfg_args = quote! { #clock_cfg_args.sysclk(#mhz.mhz()) };
}
if args.pclk1.is_some() {
let mhz = args.pclk1.unwrap();
clock_cfg_args = quote! { #clock_cfg_args.pclk1(#mhz.mhz()) };
}
quote!(
use embassy_stm32::{rtc, interrupt, Peripherals, pac, hal::rcc::RccExt, hal::time::U32Ext};
let dp = pac::Peripherals::take().unwrap();
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr#clock_cfg_args.freeze();
unsafe { Peripherals::set_peripherals(clocks) };
let mut rtc = rtc::RTC::new(dp.TIM3, interrupt::take!(TIM3), clocks);
let rtc = unsafe { make_static(&mut rtc) };
rtc.start();
let mut alarm = rtc.alarm1();
unsafe { embassy::time::set_clock(rtc) };
let alarm = unsafe { make_static(&mut alarm) };
)
}

View file

@ -191,22 +191,21 @@ pub fn interrupt_take(item: TokenStream) -> TokenStream {
result.into()
}
#[derive(Debug, FromMeta)]
struct MainMacroArgs {
#[darling(default)]
use_hse: Option<u32>,
#[darling(default)]
sysclk: Option<u32>,
#[darling(default)]
pclk1: Option<u32>,
}
#[cfg(feature = "nrf")]
#[path = "chip/nrf.rs"]
mod chip;
#[cfg(feature = "stm32")]
#[path = "chip/stm32.rs"]
mod chip;
#[cfg(any(feature = "nrf", feature = "stm32"))]
#[proc_macro_attribute]
pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
let macro_args = syn::parse_macro_input!(args as syn::AttributeArgs);
let mut task_fn = syn::parse_macro_input!(item as syn::ItemFn);
let macro_args = match MainMacroArgs::from_list(&macro_args) {
let macro_args = match chip::Args::from_list(&macro_args) {
Ok(v) => v,
Err(e) => {
return TokenStream::from(e.write_errors());
@ -254,27 +253,9 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
let name = task_fn.sig.ident.clone();
let task_fn_body = task_fn.block.clone();
let mut clock_cfg_args = quote! {};
if macro_args.use_hse.is_some() {
let mhz = macro_args.use_hse.unwrap();
clock_cfg_args = quote! { #clock_cfg_args.use_hse(#mhz.mhz()) };
}
if macro_args.sysclk.is_some() {
let mhz = macro_args.sysclk.unwrap();
clock_cfg_args = quote! { #clock_cfg_args.sysclk(#mhz.mhz()) };
}
if macro_args.pclk1.is_some() {
let mhz = macro_args.pclk1.unwrap();
clock_cfg_args = quote! { #clock_cfg_args.pclk1(#mhz.mhz()) };
}
let chip_setup = chip::generate(macro_args);
let result = quote! {
static __embassy_rtc: embassy::util::Forever<embassy_stm32::rtc::RTC<embassy_stm32::pac::TIM3>> = embassy::util::Forever::new();
static __embassy_alarm: embassy::util::Forever<embassy_stm32::rtc::Alarm<embassy_stm32::pac::TIM3>> = embassy::util::Forever::new();
static __embassy_executor: embassy::util::Forever<embassy::executor::Executor> = embassy::util::Forever::new();
#[embassy::executor::task]
async fn __embassy_main(#args) {
#task_fn_body
@ -282,27 +263,20 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
#[cortex_m_rt::entry]
fn main() -> ! {
use embassy::executor::Executor;
use embassy_stm32::{rtc, interrupt, Peripherals, pac, hal::rcc::RccExt, hal::time::U32Ext};
unsafe fn make_static<T>(t: &mut T) -> &'static mut T {
::core::mem::transmute(t)
}
let dp = pac::Peripherals::take().unwrap();
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr#clock_cfg_args.freeze();
#chip_setup
unsafe { Peripherals::set_peripherals(clocks) };
let rtc = __embassy_rtc.put(rtc::RTC::new(dp.TIM3, interrupt::take!(TIM3), clocks));
rtc.start();
unsafe { embassy::time::set_clock(rtc) };
let alarm = __embassy_alarm.put(rtc.alarm1());
let executor = __embassy_executor.put(Executor::new());
let mut executor = ::embassy::executor::Executor::new();
let executor = unsafe { make_static(&mut executor) };
executor.set_alarm(alarm);
executor.run(|spawner| {
spawner.spawn(__embassy_main(spawner)).unwrap();
})
}
};
result.into()

View file

@ -8,19 +8,17 @@
#[path = "../example_common.rs"]
mod example_common;
use cortex_m_rt::entry;
use defmt::panic;
use embassy::executor::{task, Executor};
use embassy::executor::Spawner;
use embassy::io::{AsyncBufReadExt, AsyncWriteExt};
use embassy::util::{Forever, Steal};
use embassy_nrf::gpio::NoPin;
use embassy_nrf::{buffered_uarte::BufferedUarte, interrupt, peripherals, rtc, uarte, Peripherals};
use embassy_nrf::{buffered_uarte::BufferedUarte, interrupt, uarte, Peripherals};
use example_common::*;
use futures::pin_mut;
#[task]
async fn run() {
let p = unsafe { Peripherals::steal() };
#[embassy::main]
async fn main(spawner: Spawner) {
let p = Peripherals::take().unwrap();
let mut config = uarte::Config::default();
config.parity = uarte::Parity::EXCLUDED;
@ -70,27 +68,3 @@ async fn run() {
info!("write done");
}
}
static RTC: Forever<rtc::RTC<peripherals::RTC1>> = Forever::new();
static ALARM: Forever<rtc::Alarm<peripherals::RTC1>> = Forever::new();
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
let p = unwrap!(embassy_nrf::Peripherals::take());
unsafe { embassy_nrf::system::configure(Default::default()) };
let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1)));
rtc.start();
unsafe { embassy::time::set_clock(rtc) };
let alarm = ALARM.put(rtc.alarm0());
let executor = EXECUTOR.put(Executor::new());
executor.set_alarm(alarm);
executor.run(|spawner| {
unwrap!(spawner.spawn(run()));
});
}

View file

@ -10,13 +10,10 @@ mod example_common;
use example_common::*;
use core::task::Poll;
use cortex_m_rt::entry;
use defmt::panic;
use embassy::executor::{task, Executor};
use embassy::executor::{task, Spawner};
use embassy::time::{Duration, Instant, Timer};
use embassy::util::Forever;
use embassy_nrf::peripherals;
use embassy_nrf::{interrupt, rtc};
use embassy_nrf::interrupt;
#[task]
async fn run1() {
@ -42,27 +39,9 @@ async fn run3() {
.await;
}
static RTC: Forever<rtc::RTC<peripherals::RTC1>> = Forever::new();
static ALARM: Forever<rtc::Alarm<peripherals::RTC1>> = Forever::new();
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
let p = unwrap!(embassy_nrf::Peripherals::take());
unsafe { embassy_nrf::system::configure(Default::default()) };
let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1)));
rtc.start();
unsafe { embassy::time::set_clock(rtc) };
let alarm = ALARM.put(rtc.alarm0());
let executor = EXECUTOR.put(Executor::new());
executor.set_alarm(alarm);
executor.run(|spawner| {
unwrap!(spawner.spawn(run1()));
unwrap!(spawner.spawn(run2()));
unwrap!(spawner.spawn(run3()));
});
#[embassy::main]
async fn main(spawner: Spawner) {
unwrap!(spawner.spawn(run1()));
unwrap!(spawner.spawn(run2()));
unwrap!(spawner.spawn(run3()));
}

View file

@ -9,17 +9,14 @@
mod example_common;
use example_common::*;
use cortex_m_rt::entry;
use defmt::panic;
use embassy::executor::{task, Executor};
use embassy::util::Forever;
use embassy::executor::Spawner;
use embassy_nrf::gpio::{Input, Pull};
use embassy_nrf::gpiote::{self, InputChannel, InputChannelPolarity};
use embassy_nrf::{interrupt, Peripherals};
#[task]
async fn run() {
#[embassy::main]
async fn main(spawner: Spawner) {
let p = Peripherals::take().unwrap();
let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE));
@ -80,15 +77,3 @@ async fn run() {
futures::join!(button1, button2, button3, button4);
}
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(run()));
});
}

View file

@ -9,11 +9,9 @@
mod example_common;
use core::pin::Pin;
use cortex_m_rt::entry;
use defmt::panic;
use embassy::executor::{task, Executor};
use embassy::executor::Spawner;
use embassy::traits::gpio::{WaitForHigh, WaitForLow};
use embassy::util::Forever;
use embassy_nrf::gpio::{AnyPin, Input, Pin as _, Pull};
use embassy_nrf::gpiote::{self, PortInput};
use embassy_nrf::interrupt;
@ -29,8 +27,8 @@ async fn button(n: usize, mut pin: PortInput<'static, AnyPin>) {
}
}
#[task]
async fn run() {
#[embassy::main]
async fn main(spawner: Spawner) {
let p = Peripherals::take().unwrap();
let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE));
@ -53,15 +51,3 @@ async fn run() {
);
futures::join!(button1, button2, button3, button4);
}
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(run()));
});
}

View file

@ -7,23 +7,19 @@
#[path = "../example_common.rs"]
mod example_common;
use core::future::pending;
use example_common::*;
use cortex_m_rt::entry;
use core::future::pending;
use defmt::panic;
use embassy::executor::{task, Executor};
use embassy::util::Forever;
use embassy::executor::Spawner;
use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pull};
use embassy_nrf::gpiote::{self, InputChannel, InputChannelPolarity};
use embassy_nrf::ppi::Ppi;
use embassy_nrf::{interrupt, Peripherals};
use gpiote::{OutputChannel, OutputChannelPolarity};
#[task]
async fn run() {
#[embassy::main]
async fn main(spawner: Spawner) {
let p = Peripherals::take().unwrap();
let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE));
@ -94,18 +90,7 @@ async fn run() {
info!("Press button 2 to turn on LED 1");
info!("Press button 3 to turn off LED 1");
info!("Press button 4 to toggle LEDs 1 and 2");
// Block forever so the above drivers don't get dropped
pending::<()>().await;
}
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(run()));
});
}

View file

@ -8,11 +8,9 @@
#[path = "../example_common.rs"]
mod example_common;
use cortex_m_rt::entry;
use defmt::{assert_eq, panic};
use embassy::executor::{task, Executor};
use embassy::executor::Spawner;
use embassy::traits::flash::Flash;
use embassy::util::Forever;
use embassy_nrf::Peripherals;
use embassy_nrf::{interrupt, qspi};
use example_common::*;
@ -25,8 +23,8 @@ const PAGE_SIZE: usize = 4096;
#[repr(C, align(4))]
struct AlignedBuf([u8; 4096]);
#[task]
async fn run() {
#[embassy::main]
async fn main(spawner: Spawner) {
let p = Peripherals::take().unwrap();
let csn = p.P0_17;
@ -96,15 +94,3 @@ async fn run() {
info!("done!")
}
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(run()));
});
}

View file

@ -8,21 +8,19 @@
#[path = "../example_common.rs"]
mod example_common;
use cortex_m_rt::entry;
use defmt::panic;
use embassy::executor::{task, Executor};
use embassy::util::Forever;
use embassy::executor::Spawner;
use embassy::util::Steal;
use embassy_nrf::gpio::{Level, Output, OutputDrive};
use embassy_nrf::{interrupt, rtc, spim};
use embassy_nrf::{peripherals, Peripherals};
use embassy_nrf::Peripherals;
use embassy_nrf::{interrupt, spim};
use embassy_traits::spi::FullDuplex;
use embedded_hal::digital::v2::*;
use example_common::*;
use futures::pin_mut;
#[task]
async fn run() {
#[embassy::main]
async fn main(spawner: Spawner) {
info!("running!");
let p = unsafe { Peripherals::steal() };
@ -84,29 +82,3 @@ async fn run() {
info!("erevid: {=[?]}", rx);
}
static RTC: Forever<rtc::RTC<peripherals::RTC1>> = Forever::new();
static ALARM: Forever<rtc::Alarm<peripherals::RTC1>> = Forever::new();
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
let p = unwrap!(embassy_nrf::Peripherals::take());
unsafe { embassy_nrf::system::configure(Default::default()) };
let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1)));
rtc.start();
unsafe { embassy::time::set_clock(rtc) };
unsafe { embassy::time::set_clock(rtc) };
let alarm = ALARM.put(rtc.alarm0());
let executor = EXECUTOR.put(Executor::new());
executor.set_alarm(alarm);
executor.run(|spawner| {
unwrap!(spawner.spawn(run()));
});
}

View file

@ -9,12 +9,9 @@
mod example_common;
use example_common::*;
use cortex_m_rt::entry;
use defmt::panic;
use embassy::executor::{task, Executor};
use embassy::executor::{task, Spawner};
use embassy::time::{Duration, Timer};
use embassy::util::Forever;
use embassy_nrf::{interrupt, peripherals, rtc};
#[task]
async fn run1() {
@ -32,26 +29,8 @@ async fn run2() {
}
}
static RTC: Forever<rtc::RTC<peripherals::RTC1>> = Forever::new();
static ALARM: Forever<rtc::Alarm<peripherals::RTC1>> = Forever::new();
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
let p = unwrap!(embassy_nrf::Peripherals::take());
unsafe { embassy_nrf::system::configure(Default::default()) };
let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1)));
rtc.start();
unsafe { embassy::time::set_clock(rtc) };
let alarm = ALARM.put(rtc.alarm0());
let executor = EXECUTOR.put(Executor::new());
executor.set_alarm(alarm);
executor.run(|spawner| {
unwrap!(spawner.spawn(run1()));
unwrap!(spawner.spawn(run2()));
});
#[embassy::main]
async fn main(spawner: Spawner) {
unwrap!(spawner.spawn(run1()));
unwrap!(spawner.spawn(run2()));
}

View file

@ -9,17 +9,16 @@
mod example_common;
use example_common::*;
use cortex_m_rt::entry;
use defmt::panic;
use embassy::executor::{task, Executor};
use embassy::executor::Spawner;
use embassy::traits::uart::{Read, Write};
use embassy::util::{Forever, Steal};
use embassy::util::Steal;
use embassy_nrf::gpio::NoPin;
use embassy_nrf::{interrupt, peripherals, rtc, uarte, Peripherals};
use embassy_nrf::{interrupt, uarte, Peripherals};
use futures::pin_mut;
#[task]
async fn run() {
#[embassy::main]
async fn main(spawner: Spawner) {
let p = unsafe { Peripherals::steal() };
let mut config = uarte::Config::default();
@ -74,27 +73,3 @@ async fn run() {
*/
}
}
static RTC: Forever<rtc::RTC<peripherals::RTC1>> = Forever::new();
static ALARM: Forever<rtc::Alarm<peripherals::RTC1>> = Forever::new();
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
let p = unwrap!(embassy_nrf::Peripherals::take());
unsafe { embassy_nrf::system::configure(Default::default()) };
let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1)));
rtc.start();
unsafe { embassy::time::set_clock(rtc) };
let alarm = ALARM.put(rtc.alarm0());
let executor = EXECUTOR.put(Executor::new());
executor.set_alarm(alarm);
executor.run(|spawner| {
unwrap!(spawner.spawn(run()));
});
}