From 0f1aaaf28fed8582371ffb3bde5595b2e2b1b753 Mon Sep 17 00:00:00 2001 From: Scott Mabin Date: Fri, 22 Jul 2022 11:15:15 +0100 Subject: [PATCH] cfg away the #[interrupt] macro when not using vectoring --- esp-hal-common/Cargo.toml | 2 +- esp-hal-procmacros/Cargo.toml | 3 +- esp-hal-procmacros/src/lib.rs | 13 +++-- esp32-hal/test.rs | 96 ----------------------------------- 4 files changed, 11 insertions(+), 103 deletions(-) delete mode 100644 esp32-hal/test.rs diff --git a/esp-hal-common/Cargo.toml b/esp-hal-common/Cargo.toml index 1605fe011ce..5aa98a1d46c 100644 --- a/esp-hal-common/Cargo.toml +++ b/esp-hal-common/Cargo.toml @@ -65,4 +65,4 @@ smartled = ["smart-leds-trait"] # Implement the `embedded-hal==1.0.0-alpha.x` traits eh1 = ["embedded-hal-1"] # To use vectored interrupts (calling the handlers defined in the PAC) -vectored = [] +vectored = ["procmacros/interrupt"] diff --git a/esp-hal-procmacros/Cargo.toml b/esp-hal-procmacros/Cargo.toml index e6cab554aa0..c13d8e65212 100644 --- a/esp-hal-procmacros/Cargo.toml +++ b/esp-hal-procmacros/Cargo.toml @@ -23,4 +23,5 @@ proc-macro-error = "1.0.4" [features] rtc_slow = [] xtensa = [] -riscv = [] \ No newline at end of file +riscv = [] +interrupt = [] \ No newline at end of file diff --git a/esp-hal-procmacros/src/lib.rs b/esp-hal-procmacros/src/lib.rs index 7b5766b5443..73438a3e543 100644 --- a/esp-hal-procmacros/src/lib.rs +++ b/esp-hal-procmacros/src/lib.rs @@ -1,16 +1,13 @@ -use std::iter; - use darling::FromMeta; use proc_macro::{self, Span, TokenStream}; use proc_macro_error::{abort, proc_macro_error}; use quote::quote; +#[cfg(feature = "interrupt")] use syn::{ parse, - parse_macro_input, spanned::Spanned, AttrStyle, Attribute, - AttributeArgs, Ident, ItemFn, Meta::Path, @@ -18,6 +15,7 @@ use syn::{ Type, Visibility, }; +use syn::{parse_macro_input, AttributeArgs}; #[derive(Debug, Default, FromMeta)] #[darling(default)] @@ -111,6 +109,7 @@ pub fn ram(args: TokenStream, input: TokenStream) -> TokenStream { /// When specified between braces (`#[interrupt(example)]`) that interrupt will /// be used and the function can have an arbitrary name. Otherwise the name of /// the function must be the name of the interrupt. +#[cfg(feature = "interrupt")] #[proc_macro_attribute] pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream { let mut f: ItemFn = syn::parse(input).expect("`#[interrupt]` must be applied to a function"); @@ -179,7 +178,7 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream { &format!("__esp_hal_internal_{}", f.sig.ident), proc_macro2::Span::call_site(), ); - f.block.stmts.extend(iter::once( + f.block.stmts.extend(std::iter::once( syn::parse2(quote! {{ // Check that this interrupt actually exists crate::pac::Interrupt::#ident_s; @@ -227,10 +226,12 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream { .into() } +#[cfg(feature = "interrupt")] enum WhiteListCaller { Interrupt, } +#[cfg(feature = "interrupt")] fn check_attr_whitelist(attrs: &[Attribute], caller: WhiteListCaller) -> Result<(), TokenStream> { let whitelist = &[ "doc", @@ -267,10 +268,12 @@ fn check_attr_whitelist(attrs: &[Attribute], caller: WhiteListCaller) -> Result< } /// Returns `true` if `attr.path` matches `name` +#[cfg(feature = "interrupt")] fn eq(attr: &Attribute, name: &str) -> bool { attr.style == AttrStyle::Outer && attr.path.is_ident(name) } +#[cfg(feature = "interrupt")] fn extract_cfgs(attrs: Vec) -> (Vec, Vec) { let mut cfgs = vec![]; let mut not_cfgs = vec![]; diff --git a/esp32-hal/test.rs b/esp32-hal/test.rs deleted file mode 100644 index efe312cfd8b..00000000000 --- a/esp32-hal/test.rs +++ /dev/null @@ -1,96 +0,0 @@ -#![feature(prelude_import)] -#![no_std] -#![no_main] -#[prelude_import] -use core::prelude::rust_2021::*; -#[macro_use] -extern crate core; -#[macro_use] -extern crate compiler_builtins; -use core::cell::RefCell; -use esp32_hal::{ - clock::ClockControl, - gpio::{Gpio0, IO}, - gpio_types::{Event, Input, Pin, PullDown}, - interrupt, - macros::ram, - pac::{self, Peripherals}, - prelude::*, - timer::TimerGroup, - Cpu, Delay, RtcCntl, -}; -use panic_halt as _; -use xtensa_lx::mutex::{Mutex, SpinLockMutex}; -use xtensa_lx_rt::entry; -static mut BUTTON: SpinLockMutex>>>> = - SpinLockMutex::new(RefCell::new(None)); -#[doc(hidden)] -#[export_name = "main"] -pub unsafe extern "C" fn __xtensa_lx_rt_main_trampoline() { - __xtensa_lx_rt_main() -} -#[inline(always)] -fn __xtensa_lx_rt_main() -> ! { - let peripherals = Peripherals::take().unwrap(); - let system = peripherals.DPORT.split(); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); - let mut wdt = timer_group0.wdt; - let mut rtc_cntl = RtcCntl::new(peripherals.RTC_CNTL); - wdt.disable(); - rtc_cntl.set_wdt_global_enable(false); - let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - let mut led = io.pins.gpio15.into_push_pull_output(); - let mut button = io.pins.gpio0.into_pull_down_input(); - button.listen(Event::FallingEdge); - unsafe { - (&BUTTON).lock(|data| (*data).replace(Some(button))); - } - interrupt::vectored::enable_with_priority( - Cpu::ProCpu, - pac::Interrupt::GPIO, - interrupt::vectored::Priority::Priority2, - ) - .unwrap(); - led.set_high().unwrap(); - let mut delay = Delay::new(&clocks); - loop { - led.toggle().unwrap(); - delay.delay_ms(500u32); - } -} -#[link_section = ".rwtext"] -#[inline(never)] -#[doc(hidden)] -#[export_name = "GPIO"] -pub unsafe extern "C" fn __esp_hal_internal_GPIO_trampoline( - context: &mut xtensa_lx_rt::exception::Context, -) { - __esp_hal_internal_GPIO() -} -#[inline(always)] -#[link_section = ".rwtext"] -#[inline(never)] -fn __esp_hal_internal_GPIO() { - unsafe { - { - use core::fmt::Write; - ::esp_println::Printer - .write_fmt(::core::fmt::Arguments::new_v1( - &["GPIO Interrupt with priority ", "\n"], - &[::core::fmt::ArgumentV1::new_display( - &xtensa_lx::interrupt::get_level(), - )], - )) - .ok(); - }; - (&BUTTON).lock(|data| { - let mut button = data.borrow_mut(); - let button = button.as_mut().unwrap(); - button.clear_interrupt(); - }); - } - { - crate::pac::Interrupt::GPIO; - } -}