Merge pull request #123 from xoviat/cleanup

take clocks on peripheral take and add embassy::main in more places
This commit is contained in:
xoviat 2021-03-29 09:21:34 -05:00 committed by GitHub
commit 2bcd1aaebb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 62 deletions

View File

@ -122,7 +122,7 @@ macro_rules! std_peripherals {
impl Peripherals { impl Peripherals {
pub fn take() -> Option<(Peripherals, Clocks)> { pub fn take() -> Option<(Peripherals, Clocks)> {
match unsafe {GLOBAL_CLOCKS} { match unsafe {GLOBAL_CLOCKS.take()} {
Some(clocks) => { Some(clocks) => {
let dp = unsafe { pac::Peripherals::steal() }; let dp = unsafe { pac::Peripherals::steal() };
let peripherals = Peripherals { let peripherals = Peripherals {

View File

@ -11,6 +11,8 @@ pub struct Args {
pub sysclk: Option<u32>, pub sysclk: Option<u32>,
#[darling(default)] #[darling(default)]
pub pclk1: Option<u32>, pub pclk1: Option<u32>,
#[darling(default)]
pub require_pll48clk: bool,
} }
pub fn generate(args: Args) -> TokenStream { 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()) }; clock_cfg_args = quote! { #clock_cfg_args.pclk1(#mhz.mhz()) };
} }
if args.require_pll48clk {
clock_cfg_args = quote! { #clock_cfg_args.require_pll48clk() };
}
quote!( quote!(
use embassy_stm32::{rtc, interrupt, Peripherals, pac, hal::rcc::RccExt, hal::time::U32Ext}; use embassy_stm32::{rtc, interrupt, Peripherals, pac, hal::rcc::RccExt, hal::time::U32Ext};

View File

@ -11,7 +11,7 @@ use example_common::{panic, *};
use cortex_m::singleton; use cortex_m::singleton;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use embassy::executor::Executor; use embassy::executor::{Executor, Spawner};
use embassy::traits::uart::{Read, Write}; use embassy::traits::uart::{Read, Write};
use embassy::util::Forever; use embassy::util::Forever;
use embassy_stm32f4::interrupt; use embassy_stm32f4::interrupt;
@ -22,26 +22,19 @@ use stm32f4xx_hal::prelude::*;
use stm32f4xx_hal::serial::config::Config; use stm32f4xx_hal::serial::config::Config;
use stm32f4xx_hal::stm32; use stm32f4xx_hal::stm32;
#[embassy::task] #[embassy::main(use_hse = 16, sysclk = 48, pclk1 = 24)]
async fn run(dp: stm32::Peripherals, _cp: cortex_m::Peripherals) { 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| { dp.DBGMCU.cr.modify(|_, w| {
w.dbg_sleep().set_bit(); w.dbg_sleep().set_bit();
w.dbg_standby().set_bit(); w.dbg_standby().set_bit();
w.dbg_stop().set_bit() w.dbg_stop().set_bit()
}); });
dp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
// https://gist.github.com/thalesfragoso/a07340c5df6eee3b04c42fdc69ecdcb1 // https://gist.github.com/thalesfragoso/a07340c5df6eee3b04c42fdc69ecdcb1
let gpioa = dp.GPIOA.split(); 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 streams = StreamsTuple::new(dp.DMA2);
let _serial = unsafe { let _serial = unsafe {
@ -84,16 +77,3 @@ async fn run(dp: stm32::Peripherals, _cp: cortex_m::Peripherals) {
buf[5] = 0x01; buf[5] = 0x01;
serial.write(buf).await.unwrap(); 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)));
});
}

View File

@ -10,7 +10,7 @@ use example_common::*;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use defmt::panic; use defmt::panic;
use embassy::executor::Executor; use embassy::executor::{Executor, Spawner};
use embassy::interrupt::InterruptExt; use embassy::interrupt::InterruptExt;
use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; use embassy::io::{AsyncBufReadExt, AsyncWriteExt};
use embassy::time::{Duration, Timer}; 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(); static USB_BUS: Forever<UsbBusAllocator<UsbBus<USB>>> = Forever::new();
#[entry] #[embassy::main(use_hse = 25, sysclk = 48, require_pll48clk)]
fn main() -> ! { async fn main(spawner: Spawner) -> ! {
static mut EP_MEMORY: [u32; 1024] = [0; 1024]; static mut EP_MEMORY: [u32; 1024] = [0; 1024];
info!("Hello World!"); info!("Hello World!");
let p = unwrap!(pac::Peripherals::take()); let (p, clocks) = embassy_stm32::Peripherals::take().unwrap();
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 gpioa = p.GPIOA.split(); let gpioa = p.GPIOA.split();
let usb = USB { let usb = USB {
@ -138,9 +111,7 @@ fn main() -> ! {
}; };
// Rust analyzer isn't recognizing the static ref magic `cortex-m` does // Rust analyzer isn't recognizing the static ref magic `cortex-m` does
#[allow(unused_unsafe)] #[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| { spawner.spawn(run1(usb_bus)).unwrap();
unwrap!(spawner.spawn(run1(usb_bus)));
});
} }