Merge pull request #123 from xoviat/cleanup
take clocks on peripheral take and add embassy::main in more places
This commit is contained in:
commit
2bcd1aaebb
@ -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 {
|
||||
|
@ -11,6 +11,8 @@ pub struct Args {
|
||||
pub sysclk: Option<u32>,
|
||||
#[darling(default)]
|
||||
pub pclk1: Option<u32>,
|
||||
#[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};
|
||||
|
||||
|
@ -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<Executor> = 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)));
|
||||
});
|
||||
}
|
||||
|
@ -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<UsbBus<USB>>) {
|
||||
}
|
||||
}
|
||||
|
||||
static RTC: Forever<rtc::RTC<pac::TIM2>> = Forever::new();
|
||||
static ALARM: Forever<rtc::Alarm<pac::TIM2>> = Forever::new();
|
||||
static EXECUTOR: Forever<Executor> = Forever::new();
|
||||
static USB_BUS: Forever<UsbBusAllocator<UsbBus<USB>>> = 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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user