diff --git a/embassy-extras/src/macros.rs b/embassy-extras/src/macros.rs index e57b89ec7..5ce14a76d 100644 --- a/embassy-extras/src/macros.rs +++ b/embassy-extras/src/macros.rs @@ -122,7 +122,7 @@ macro_rules! std_peripherals { impl Peripherals { pub fn take() -> Option<(Peripherals, Clocks)> { - match unsafe {GLOBAL_CLOCKS} { + match unsafe {GLOBAL_CLOCKS.take()} { Some(clocks) => { let dp = unsafe { pac::Peripherals::steal() }; let peripherals = Peripherals { diff --git a/embassy-macros/src/chip/stm32.rs b/embassy-macros/src/chip/stm32.rs index 01c6a9cfd..e94c21a97 100644 --- a/embassy-macros/src/chip/stm32.rs +++ b/embassy-macros/src/chip/stm32.rs @@ -11,6 +11,8 @@ pub struct Args { pub sysclk: Option, #[darling(default)] pub pclk1: Option, + #[darling(default)] + pub require_pll48clk: bool, } pub fn generate(args: Args) -> TokenStream { @@ -30,6 +32,10 @@ pub fn generate(args: Args) -> TokenStream { clock_cfg_args = quote! { #clock_cfg_args.pclk1(#mhz.mhz()) }; } + if args.require_pll48clk { + clock_cfg_args = quote! { #clock_cfg_args.require_pll48clk() }; + } + quote!( use embassy_stm32::{rtc, interrupt, Peripherals, pac, hal::rcc::RccExt, hal::time::U32Ext}; diff --git a/embassy-stm32f4-examples/src/bin/serial.rs b/embassy-stm32f4-examples/src/bin/serial.rs index 925fd68ff..1717cadb9 100644 --- a/embassy-stm32f4-examples/src/bin/serial.rs +++ b/embassy-stm32f4-examples/src/bin/serial.rs @@ -11,7 +11,7 @@ use example_common::{panic, *}; use cortex_m::singleton; use cortex_m_rt::entry; -use embassy::executor::Executor; +use embassy::executor::{Executor, Spawner}; use embassy::traits::uart::{Read, Write}; use embassy::util::Forever; use embassy_stm32f4::interrupt; @@ -22,26 +22,19 @@ use stm32f4xx_hal::prelude::*; use stm32f4xx_hal::serial::config::Config; use stm32f4xx_hal::stm32; -#[embassy::task] -async fn run(dp: stm32::Peripherals, _cp: cortex_m::Peripherals) { +#[embassy::main(use_hse = 16, sysclk = 48, pclk1 = 24)] +async fn main(spawner: Spawner) { + let (dp, clocks) = embassy_stm32::Peripherals::take().unwrap(); + let cp = cortex_m::peripheral::Peripherals::take().unwrap(); + dp.DBGMCU.cr.modify(|_, w| { w.dbg_sleep().set_bit(); w.dbg_standby().set_bit(); w.dbg_stop().set_bit() }); - dp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled()); // https://gist.github.com/thalesfragoso/a07340c5df6eee3b04c42fdc69ecdcb1 let gpioa = dp.GPIOA.split(); - let rcc = dp.RCC.constrain(); - - let clocks = rcc - .cfgr - .use_hse(16.mhz()) - .sysclk(48.mhz()) - .pclk1(24.mhz()) - .freeze(); - let streams = StreamsTuple::new(dp.DMA2); let _serial = unsafe { @@ -84,16 +77,3 @@ async fn run(dp: stm32::Peripherals, _cp: cortex_m::Peripherals) { buf[5] = 0x01; serial.write(buf).await.unwrap(); } - -static EXECUTOR: Forever = Forever::new(); - -#[entry] -fn main() -> ! { - let dp = stm32::Peripherals::take().unwrap(); - let cp = cortex_m::peripheral::Peripherals::take().unwrap(); - - let executor = EXECUTOR.put(Executor::new()); - executor.run(|spawner| { - unwrap!(spawner.spawn(run(dp, cp))); - }); -} diff --git a/embassy-stm32f4-examples/src/bin/usb_serial.rs b/embassy-stm32f4-examples/src/bin/usb_serial.rs index 6d3b2cc14..669d187d4 100644 --- a/embassy-stm32f4-examples/src/bin/usb_serial.rs +++ b/embassy-stm32f4-examples/src/bin/usb_serial.rs @@ -10,7 +10,7 @@ use example_common::*; use cortex_m_rt::entry; use defmt::panic; -use embassy::executor::Executor; +use embassy::executor::{Executor, Spawner}; use embassy::interrupt::InterruptExt; use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; use embassy::time::{Duration, Timer}; @@ -90,42 +90,15 @@ async fn run1(bus: &'static mut UsbBusAllocator>) { } } -static RTC: Forever> = Forever::new(); -static ALARM: Forever> = Forever::new(); -static EXECUTOR: Forever = Forever::new(); static USB_BUS: Forever>> = Forever::new(); -#[entry] -fn main() -> ! { +#[embassy::main(use_hse = 25, sysclk = 48, require_pll48clk)] +async fn main(spawner: Spawner) -> ! { static mut EP_MEMORY: [u32; 1024] = [0; 1024]; info!("Hello World!"); - let p = unwrap!(pac::Peripherals::take()); - - p.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled()); - let rcc = p.RCC.constrain(); - let clocks = rcc - .cfgr - .use_hse(25.mhz()) - .sysclk(48.mhz()) - .require_pll48clk() - .freeze(); - - p.DBGMCU.cr.modify(|_, w| { - w.dbg_sleep().set_bit(); - w.dbg_standby().set_bit(); - w.dbg_stop().set_bit() - }); - - let rtc = RTC.put(rtc::RTC::new(p.TIM2, interrupt::take!(TIM2), clocks)); - rtc.start(); - - unsafe { embassy::time::set_clock(rtc) }; - - let alarm = ALARM.put(rtc.alarm1()); - let executor = EXECUTOR.put(Executor::new()); - executor.set_alarm(alarm); + let (p, clocks) = embassy_stm32::Peripherals::take().unwrap(); let gpioa = p.GPIOA.split(); let usb = USB { @@ -138,9 +111,7 @@ fn main() -> ! { }; // Rust analyzer isn't recognizing the static ref magic `cortex-m` does #[allow(unused_unsafe)] - let usb_bus = USB_BUS.put(UsbBus::new(usb, unsafe { EP_MEMORY })); + let usb_bus = USB_BUS.put(UsbBus::new(usb, unsafe { &mut EP_MEMORY })); - executor.run(move |spawner| { - unwrap!(spawner.spawn(run1(usb_bus))); - }); + spawner.spawn(run1(usb_bus)).unwrap(); }