diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index 6f8691c3..9b9db0f0 100644 --- a/embassy-stm32/Cargo.toml +++ b/embassy-stm32/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" resolver = "2" [dependencies] -embassy = { version = "0.1.0", path = "../embassy", features = ["time-tick-32768hz"] } +embassy = { version = "0.1.0", path = "../embassy" } embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["stm32"] } embassy-hal-common = {version = "0.1.0", path = "../embassy-hal-common" } embassy-traits = {version = "0.1.0", path = "../embassy-traits" } @@ -44,6 +44,12 @@ sdmmc-rs = ["embedded-sdmmc"] net = ["embassy-net", "vcell"] memory-x = ["stm32-metapac/memory-x"] +# Features starting with `_` are for internal use only. They're not intended +# to be enabled by other crates, and are not covered by semver guarantees. +_time-driver = ["embassy/time-tick-32768hz"] +time-driver-tim2 = ["_time-driver"] +time-driver-tim3 = ["_time-driver"] + # Reexport stm32-metapac at `embassy_stm32::pac`. # This is unstable because semver-minor (non-breaking) releases of embassy-stm32 may major-bump (breaking) the stm32-metapac version. # If this is an issue for you, you're encouraged to directly depend on a fixed version of the PAC. diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index 28bfcbaa..8a213b68 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs @@ -20,6 +20,7 @@ pub mod time; pub mod dma; pub mod gpio; pub mod rcc; +#[cfg(feature = "_time-driver")] mod time_driver; // Sometimes-present hardware @@ -88,6 +89,7 @@ pub fn init(config: Config) -> Peripherals { rcc::init(config.rcc); // must be after rcc init + #[cfg(feature = "_time-driver")] time_driver::init(); } diff --git a/embassy-stm32/src/time_driver.rs b/embassy-stm32/src/time_driver.rs index 16c871e8..226e5e39 100644 --- a/embassy-stm32/src/time_driver.rs +++ b/embassy-stm32/src/time_driver.rs @@ -17,8 +17,23 @@ use crate::rcc::sealed::RccPeripheral; use self::sealed::Instance as _; const ALARM_COUNT: usize = 3; + +#[cfg(feature = "time-driver-tim2")] +type T = peripherals::TIM2; +#[cfg(feature = "time-driver-tim3")] type T = peripherals::TIM3; +#[cfg(feature = "time-driver-tim2")] +#[interrupt] +fn TIM2() { + STATE.on_interrupt() +} +#[cfg(feature = "time-driver-tim3")] +#[interrupt] +fn TIM3() { + STATE.on_interrupt() +} + // Clock timekeeping works with something we call "periods", which are time intervals // of 2^15 ticks. The Clock counter value is 16 bits, so one "overflow cycle" is 2 periods. // @@ -275,11 +290,6 @@ impl Driver for RtcDriver { } } -#[interrupt] -fn TIM3() { - STATE.on_interrupt() -} - pub(crate) fn init() { STATE.init() } diff --git a/examples/stm32f0/Cargo.toml b/examples/stm32f0/Cargo.toml index cc2c3f2b..c5730de8 100644 --- a/examples/stm32f0/Cargo.toml +++ b/examples/stm32f0/Cargo.toml @@ -15,7 +15,7 @@ defmt-rtt = "0.2.0" panic-probe = { version = "0.2.0" } rtt-target = { version = "0.3", features = ["cortex-m"] } embassy = { path = "../../embassy", features = ["defmt"] } -embassy-stm32 = { path = "../../embassy-stm32", features = ["defmt", "stm32f030f4"] } +embassy-stm32 = { path = "../../embassy-stm32", features = ["defmt", "stm32f030f4", "time-driver-tim3"] } [features] default = [ diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index 84b1e30e..c254573f 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml @@ -19,7 +19,7 @@ defmt-error = [] [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } -embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32f429zi", "unstable-pac", "memory-x"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-tim2"] } embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } defmt = "0.2.0" diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml index 0868be75..c7761ee8 100644 --- a/examples/stm32h7/Cargo.toml +++ b/examples/stm32h7/Cargo.toml @@ -19,7 +19,7 @@ defmt-error = [] [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } -embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32h743zi", "net", "memory-x"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32h743zi", "net", "memory-x", "time-driver-tim2"] } embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } embassy-net = { path = "../../embassy-net", default-features = false, features = ["defmt-debug", "defmt", "tcp", "medium-ethernet", "pool-16"] } stm32-metapac = { path = "../../stm32-metapac", features = ["stm32h743zi"] } diff --git a/examples/stm32l0/Cargo.toml b/examples/stm32l0/Cargo.toml index 47d23d08..6c594df6 100644 --- a/examples/stm32l0/Cargo.toml +++ b/examples/stm32l0/Cargo.toml @@ -19,7 +19,7 @@ defmt-error = [] [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } -embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32l072cz"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32l072cz", "time-driver-tim3"] } embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } defmt = "0.2.0" diff --git a/examples/stm32l4/Cargo.toml b/examples/stm32l4/Cargo.toml index 8c620767..3f6b3467 100644 --- a/examples/stm32l4/Cargo.toml +++ b/examples/stm32l4/Cargo.toml @@ -19,7 +19,7 @@ defmt-error = [] [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } -embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "unstable-pac", "stm32l4s5vi"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "unstable-pac", "stm32l4s5vi", "time-driver-tim2"] } embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } defmt = "0.2.0" diff --git a/examples/stm32wb55/Cargo.toml b/examples/stm32wb55/Cargo.toml index 4d6f7789..ee2432cb 100644 --- a/examples/stm32wb55/Cargo.toml +++ b/examples/stm32wb55/Cargo.toml @@ -19,7 +19,7 @@ defmt-error = [] [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } -embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32wb55cc"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32wb55cc", "time-driver-tim2"] } embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } defmt = "0.2.0" diff --git a/examples/stm32wb55/src/bin/blinky.rs b/examples/stm32wb55/src/bin/blinky.rs index 2eba361a..5ffef9b3 100644 --- a/examples/stm32wb55/src/bin/blinky.rs +++ b/examples/stm32wb55/src/bin/blinky.rs @@ -6,27 +6,29 @@ #[path = "../example_common.rs"] mod example_common; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_stm32::dbgmcu::Dbgmcu; use embassy_stm32::gpio::{Level, Output, Speed}; +use embassy_stm32::Peripherals; use embedded_hal::digital::v2::OutputPin; use example_common::*; -use cortex_m_rt::entry; - -#[entry] -fn main() -> ! { +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { info!("Hello World!"); - let p = embassy_stm32::init(Default::default()); + unsafe { Dbgmcu::enable_all() }; let mut led = Output::new(p.PB0, Level::High, Speed::Low); loop { info!("high"); led.set_high().unwrap(); - cortex_m::asm::delay(10_000_000); + Timer::after(Duration::from_millis(500)).await; info!("low"); led.set_low().unwrap(); - cortex_m::asm::delay(10_000_000); + Timer::after(Duration::from_millis(500)).await; } }