From 03ecc91d552328f625c3ed13088291272123784f Mon Sep 17 00:00:00 2001 From: xoviat Date: Fri, 19 Mar 2021 15:26:20 -0500 Subject: [PATCH] stm32: consolidate functionality into new pkg --- Cargo.toml | 1 + embassy-stm32/Cargo.toml | 46 +++++ {embassy-stm32f4 => embassy-stm32}/src/fmt.rs | 0 .../src/interrupt.rs | 102 +++++++++++ embassy-stm32/src/lib.rs | 35 ++++ embassy-stm32f4/Cargo.toml | 36 ++-- embassy-stm32f4/src/lib.rs | 7 +- embassy-stm32l0/Cargo.toml | 7 +- embassy-stm32l0/src/fmt.rs | 119 ------------- embassy-stm32l0/src/interrupt.rs | 162 ------------------ embassy-stm32l0/src/lib.rs | 7 +- 11 files changed, 208 insertions(+), 314 deletions(-) create mode 100644 embassy-stm32/Cargo.toml rename {embassy-stm32f4 => embassy-stm32}/src/fmt.rs (100%) rename {embassy-stm32f4 => embassy-stm32}/src/interrupt.rs (91%) create mode 100644 embassy-stm32/src/lib.rs delete mode 100644 embassy-stm32l0/src/fmt.rs delete mode 100644 embassy-stm32l0/src/interrupt.rs diff --git a/Cargo.toml b/Cargo.toml index 31fe2647..d9fbeaf4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = [ "embassy", "embassy-traits", "embassy-nrf", + "embassy-stm32", "embassy-stm32f4", "embassy-stm32l0", "embassy-nrf-examples", diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml new file mode 100644 index 00000000..6701ff8f --- /dev/null +++ b/embassy-stm32/Cargo.toml @@ -0,0 +1,46 @@ +[package] +name = "embassy-stm32" +version = "0.1.0" +authors = ["Dario Nieuwenhuis "] +edition = "2018" + +[features] +defmt-trace = [ ] +defmt-debug = [ ] +defmt-info = [ ] +defmt-warn = [ ] +defmt-error = [ ] + +stm32f401 = ["stm32f4xx-hal/stm32f401"] +stm32f405 = ["stm32f4xx-hal/stm32f405"] +stm32f407 = ["stm32f4xx-hal/stm32f407"] +stm32f410 = ["stm32f4xx-hal/stm32f410"] +stm32f411 = ["stm32f4xx-hal/stm32f411"] +stm32f412 = ["stm32f4xx-hal/stm32f412"] +stm32f413 = ["stm32f4xx-hal/stm32f413"] +stm32f415 = ["stm32f4xx-hal/stm32f405"] +stm32f417 = ["stm32f4xx-hal/stm32f407"] +stm32f423 = ["stm32f4xx-hal/stm32f413"] +stm32f427 = ["stm32f4xx-hal/stm32f427"] +stm32f429 = ["stm32f4xx-hal/stm32f429"] +stm32f437 = ["stm32f4xx-hal/stm32f427"] +stm32f439 = ["stm32f4xx-hal/stm32f429"] +stm32f446 = ["stm32f4xx-hal/stm32f446"] +stm32f469 = ["stm32f4xx-hal/stm32f469"] +stm32f479 = ["stm32f4xx-hal/stm32f469"] + +stm32l0x1 = ["stm32l0xx-hal/stm32l0x1"] +stm32l0x2 = ["stm32l0xx-hal/stm32l0x2"] +stm32l0x3 = ["stm32l0xx-hal/stm32l0x3"] + +[dependencies] +embassy = { version = "0.1.0", path = "../embassy" } + +defmt = { version = "0.2.0", optional = true } +log = { version = "0.4.11", optional = true } +cortex-m-rt = "0.6.13" +cortex-m = "0.7.1" +embedded-hal = { version = "0.2.4" } +embedded-dma = { version = "0.1.2" } +stm32f4xx-hal = { version = "0.8.3", features = ["rt", "can"], git = "https://github.com/stm32-rs/stm32f4xx-hal.git", optional = true } +stm32l0xx-hal = { version = "0.7.0", features = ["rt"], git = "https://github.com/stm32-rs/stm32l0xx-hal.git", optional = true } \ No newline at end of file diff --git a/embassy-stm32f4/src/fmt.rs b/embassy-stm32/src/fmt.rs similarity index 100% rename from embassy-stm32f4/src/fmt.rs rename to embassy-stm32/src/fmt.rs diff --git a/embassy-stm32f4/src/interrupt.rs b/embassy-stm32/src/interrupt.rs similarity index 91% rename from embassy-stm32f4/src/interrupt.rs rename to embassy-stm32/src/interrupt.rs index 402aee58..5ad7ef8e 100644 --- a/embassy-stm32f4/src/interrupt.rs +++ b/embassy-stm32/src/interrupt.rs @@ -1022,4 +1022,106 @@ mod irqs { declare!(DSIHOST); } +#[cfg(feature = "stm32l0x1")] +mod irqs { + use super::*; + declare!(WWDG); + declare!(PVD); + declare!(RTC); + declare!(FLASH); + declare!(RCC); + declare!(EXTI0_1); + declare!(EXTI2_3); + declare!(EXTI4_15); + declare!(DMA1_CHANNEL1); + declare!(DMA1_CHANNEL2_3); + declare!(DMA1_CHANNEL4_7); + declare!(ADC_COMP); + declare!(LPTIM1); + declare!(USART4_USART5); + declare!(TIM2); + declare!(TIM3); + declare!(TIM6); + declare!(TIM7); + declare!(TIM21); + declare!(I2C3); + declare!(TIM22); + declare!(I2C1); + declare!(I2C2); + declare!(SPI1); + declare!(SPI2); + declare!(USART1); + declare!(USART2); + declare!(AES_RNG_LPUART1); +} + +#[cfg(feature = "stm32l0x2")] +mod irqs { + use super::*; + declare!(WWDG); + declare!(PVD); + declare!(RTC); + declare!(RCC); + declare!(EXTI0_1); + declare!(EXTI2_3); + declare!(EXTI4_15); + declare!(TSC); + declare!(DMA1_CHANNEL1); + declare!(DMA1_CHANNEL2_3); + declare!(DMA1_CHANNEL4_7); + declare!(ADC_COMP); + declare!(LPTIM1); + declare!(USART4_USART5); + declare!(TIM2); + declare!(TIM3); + declare!(TIM6_DAC); + declare!(TIM7); + declare!(TIM21); + declare!(I2C3); + declare!(TIM22); + declare!(I2C1); + declare!(I2C2); + declare!(SPI1); + declare!(SPI2); + declare!(USART1); + declare!(USART2); + declare!(AES_RNG_LPUART1); + declare!(USB); +} + +#[cfg(feature = "stm32l0x3")] +mod irqs { + use super::*; + declare!(WWDG); + declare!(PVD); + declare!(RTC); + declare!(RCC); + declare!(EXTI0_1); + declare!(EXTI2_3); + declare!(EXTI4_15); + declare!(TSC); + declare!(DMA1_CHANNEL1); + declare!(DMA1_CHANNEL2_3); + declare!(DMA1_CHANNEL4_7); + declare!(ADC_COMP); + declare!(LPTIM1); + declare!(USART4_USART5); + declare!(TIM2); + declare!(TIM3); + declare!(TIM6_DAC); + declare!(TIM7); + declare!(TIM21); + declare!(I2C3); + declare!(TIM22); + declare!(I2C1); + declare!(I2C2); + declare!(SPI1); + declare!(SPI2); + declare!(USART1); + declare!(USART2); + declare!(AES_RNG_LPUART1); + declare!(LCD); + declare!(USB); +} + pub use irqs::*; diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs new file mode 100644 index 00000000..a1f40b2c --- /dev/null +++ b/embassy-stm32/src/lib.rs @@ -0,0 +1,35 @@ +#![no_std] +#![feature(generic_associated_types)] +#![feature(asm)] +#![feature(min_type_alias_impl_trait)] +#![feature(impl_trait_in_bindings)] +#![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] + +#[cfg(any( + feature = "stm32f401", + feature = "stm32f405", + feature = "stm32f407", + feature = "stm32f410", + feature = "stm32f411", + feature = "stm32f412", + feature = "stm32f413", + feature = "stm32f415", + feature = "stm32f417", + feature = "stm32f423", + feature = "stm32f427", + feature = "stm32f429", + feature = "stm32f437", + feature = "stm32f439", + feature = "stm32f446", + feature = "stm32f469", + feature = "stm32f479", +))] +pub use {stm32f4xx_hal as hal, stm32f4xx_hal::stm32 as pac}; + +#[cfg(any(feature = "stm32l0x1", feature = "stm32l0x2", feature = "stm32l0x3",))] +pub use {stm32l0xx_hal as hal, stm32l0xx_hal::pac}; + +pub mod fmt; + +pub mod interrupt; diff --git a/embassy-stm32f4/Cargo.toml b/embassy-stm32f4/Cargo.toml index 935b4a2b..ae3273d6 100644 --- a/embassy-stm32f4/Cargo.toml +++ b/embassy-stm32f4/Cargo.toml @@ -11,27 +11,27 @@ defmt-info = [ ] defmt-warn = [ ] defmt-error = [ ] -stm32f401 = ["stm32f4xx-hal/stm32f401"] -stm32f405 = ["stm32f4xx-hal/stm32f405"] -stm32f407 = ["stm32f4xx-hal/stm32f407"] -stm32f410 = ["stm32f4xx-hal/stm32f410"] -stm32f411 = ["stm32f4xx-hal/stm32f411"] -stm32f412 = ["stm32f4xx-hal/stm32f412"] -stm32f413 = ["stm32f4xx-hal/stm32f413"] -stm32f415 = ["stm32f4xx-hal/stm32f405"] -stm32f417 = ["stm32f4xx-hal/stm32f407"] -stm32f423 = ["stm32f4xx-hal/stm32f413"] -stm32f427 = ["stm32f4xx-hal/stm32f427"] -stm32f429 = ["stm32f4xx-hal/stm32f429"] -stm32f437 = ["stm32f4xx-hal/stm32f427"] -stm32f439 = ["stm32f4xx-hal/stm32f429"] -stm32f446 = ["stm32f4xx-hal/stm32f446"] -stm32f469 = ["stm32f4xx-hal/stm32f469"] -stm32f479 = ["stm32f4xx-hal/stm32f469"] +stm32f401 = ["stm32f4xx-hal/stm32f401", "embassy-stm32/stm32f401"] +stm32f405 = ["stm32f4xx-hal/stm32f405", "embassy-stm32/stm32f405"] +stm32f407 = ["stm32f4xx-hal/stm32f407", "embassy-stm32/stm32f407"] +stm32f410 = ["stm32f4xx-hal/stm32f410", "embassy-stm32/stm32f410"] +stm32f411 = ["stm32f4xx-hal/stm32f411", "embassy-stm32/stm32f411"] +stm32f412 = ["stm32f4xx-hal/stm32f412", "embassy-stm32/stm32f412"] +stm32f413 = ["stm32f4xx-hal/stm32f413", "embassy-stm32/stm32f413"] +stm32f415 = ["stm32f4xx-hal/stm32f405", "embassy-stm32/stm32f415"] +stm32f417 = ["stm32f4xx-hal/stm32f407", "embassy-stm32/stm32f417"] +stm32f423 = ["stm32f4xx-hal/stm32f413", "embassy-stm32/stm32f423"] +stm32f427 = ["stm32f4xx-hal/stm32f427", "embassy-stm32/stm32f427"] +stm32f429 = ["stm32f4xx-hal/stm32f429", "embassy-stm32/stm32f429"] +stm32f437 = ["stm32f4xx-hal/stm32f427", "embassy-stm32/stm32f437"] +stm32f439 = ["stm32f4xx-hal/stm32f429", "embassy-stm32/stm32f439"] +stm32f446 = ["stm32f4xx-hal/stm32f446", "embassy-stm32/stm32f446"] +stm32f469 = ["stm32f4xx-hal/stm32f469", "embassy-stm32/stm32f469"] +stm32f479 = ["stm32f4xx-hal/stm32f469", "embassy-stm32/stm32f479"] [dependencies] embassy = { version = "0.1.0", path = "../embassy" } - +embassy-stm32 = { version = "0.1.0", path = "../embassy-stm32" } defmt = { version = "0.2.0", optional = true } log = { version = "0.4.11", optional = true } cortex-m-rt = "0.6.13" diff --git a/embassy-stm32f4/src/lib.rs b/embassy-stm32f4/src/lib.rs index 82d26d95..0d490525 100644 --- a/embassy-stm32f4/src/lib.rs +++ b/embassy-stm32f4/src/lib.rs @@ -307,16 +307,11 @@ compile_error!( "Multile chip features activated. You must activate exactly one of the following features: " ); -pub use stm32f4xx_hal as hal; -pub use stm32f4xx_hal::stm32 as pac; - -// This mod MUST go first, so that the others see its macros. -pub(crate) mod fmt; +pub use embassy_stm32::{fmt, hal, interrupt, pac}; #[cfg(not(any(feature = "stm32f401", feature = "stm32f410", feature = "stm32f411",)))] pub mod can; pub mod exti; -pub mod interrupt; #[cfg(not(feature = "stm32f410"))] pub mod qei; pub mod rtc; diff --git a/embassy-stm32l0/Cargo.toml b/embassy-stm32l0/Cargo.toml index c74d1407..8926763d 100644 --- a/embassy-stm32l0/Cargo.toml +++ b/embassy-stm32l0/Cargo.toml @@ -11,12 +11,13 @@ defmt-info = [ ] defmt-warn = [ ] defmt-error = [ ] -stm32l0x1 = ["stm32l0xx-hal/stm32l0x1"] -stm32l0x2 = ["stm32l0xx-hal/stm32l0x2"] -stm32l0x3 = ["stm32l0xx-hal/stm32l0x3"] +stm32l0x1 = ["stm32l0xx-hal/stm32l0x1", "embassy-stm32/stm32l0x1"] +stm32l0x2 = ["stm32l0xx-hal/stm32l0x2", "embassy-stm32/stm32l0x2"] +stm32l0x3 = ["stm32l0xx-hal/stm32l0x3", "embassy-stm32/stm32l0x3"] [dependencies] embassy = { version = "0.1.0", path = "../embassy" } +embassy-stm32 = { version = "0.1.0", path = "../embassy-stm32" } defmt = { version = "0.2.0", optional = true } futures = { version = "0.3.5", default-features = false, features = [ "cfg-target-has-atomic", "unstable" ] } log = { version = "0.4.11", optional = true } diff --git a/embassy-stm32l0/src/fmt.rs b/embassy-stm32l0/src/fmt.rs deleted file mode 100644 index 1be1057a..00000000 --- a/embassy-stm32l0/src/fmt.rs +++ /dev/null @@ -1,119 +0,0 @@ -#![macro_use] -#![allow(clippy::module_inception)] - -#[cfg(all(feature = "defmt", feature = "log"))] -compile_error!("You may not enable both `defmt` and `log` features."); - -pub use fmt::*; - -#[cfg(feature = "defmt")] -mod fmt { - pub use defmt::{ - assert, assert_eq, assert_ne, debug, debug_assert, debug_assert_eq, debug_assert_ne, error, - info, panic, todo, trace, unreachable, unwrap, warn, - }; -} - -#[cfg(feature = "log")] -mod fmt { - pub use core::{ - assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo, - unreachable, - }; - pub use log::{debug, error, info, trace, warn}; -} - -#[cfg(not(any(feature = "defmt", feature = "log")))] -mod fmt { - #![macro_use] - - pub use core::{ - assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo, - unreachable, - }; - - #[macro_export] - macro_rules! trace { - ($($msg:expr),+ $(,)?) => { - () - }; - } - - #[macro_export] - macro_rules! debug { - ($($msg:expr),+ $(,)?) => { - () - }; - } - - #[macro_export] - macro_rules! info { - ($($msg:expr),+ $(,)?) => { - () - }; - } - - #[macro_export] - macro_rules! warn { - ($($msg:expr),+ $(,)?) => { - () - }; - } - - #[macro_export] - macro_rules! error { - ($($msg:expr),+ $(,)?) => { - () - }; - } -} - -#[cfg(not(feature = "defmt"))] -#[macro_export] -macro_rules! unwrap { - ($arg:expr) => { - match $crate::fmt::Try::into_result($arg) { - ::core::result::Result::Ok(t) => t, - ::core::result::Result::Err(e) => { - ::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e); - } - } - }; - ($arg:expr, $($msg:expr),+ $(,)? ) => { - match $crate::fmt::Try::into_result($arg) { - ::core::result::Result::Ok(t) => t, - ::core::result::Result::Err(e) => { - ::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e); - } - } - } -} - -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub struct NoneError; - -pub trait Try { - type Ok; - type Error; - fn into_result(self) -> Result; -} - -impl Try for Option { - type Ok = T; - type Error = NoneError; - - #[inline] - fn into_result(self) -> Result { - self.ok_or(NoneError) - } -} - -impl Try for Result { - type Ok = T; - type Error = E; - - #[inline] - fn into_result(self) -> Self { - self - } -} diff --git a/embassy-stm32l0/src/interrupt.rs b/embassy-stm32l0/src/interrupt.rs deleted file mode 100644 index 499f3f27..00000000 --- a/embassy-stm32l0/src/interrupt.rs +++ /dev/null @@ -1,162 +0,0 @@ -//! Interrupt management -use crate::pac::NVIC_PRIO_BITS; - -// Re-exports -pub use cortex_m::interrupt::{CriticalSection, Mutex}; -pub use embassy::interrupt::{declare, take, Interrupt}; - -#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] -#[cfg_attr(feature = "defmt", derive(defmt::Format))] -#[repr(u8)] -pub enum Priority { - Level0 = 0, - Level1 = 1, - Level2 = 2, - Level3 = 3, - Level4 = 4, - Level5 = 5, - Level6 = 6, - Level7 = 7, - Level8 = 8, - Level9 = 9, - Level10 = 10, - Level11 = 11, - Level12 = 12, - Level13 = 13, - Level14 = 14, - Level15 = 15, -} - -impl From for Priority { - fn from(priority: u8) -> Self { - match priority >> (8 - NVIC_PRIO_BITS) { - 0 => Self::Level0, - 1 => Self::Level1, - 2 => Self::Level2, - 3 => Self::Level3, - 4 => Self::Level4, - 5 => Self::Level5, - 6 => Self::Level6, - 7 => Self::Level7, - 8 => Self::Level8, - 9 => Self::Level9, - 10 => Self::Level10, - 11 => Self::Level11, - 12 => Self::Level12, - 13 => Self::Level13, - 14 => Self::Level14, - 15 => Self::Level15, - _ => unreachable!(), - } - } -} - -impl From for u8 { - fn from(p: Priority) -> Self { - (p as u8) << (8 - NVIC_PRIO_BITS) - } -} - -#[cfg(feature = "stm32l0x1")] -mod irqs { - use super::*; - declare!(WWDG); - declare!(PVD); - declare!(RTC); - declare!(FLASH); - declare!(RCC); - declare!(EXTI0_1); - declare!(EXTI2_3); - declare!(EXTI4_15); - declare!(DMA1_CHANNEL1); - declare!(DMA1_CHANNEL2_3); - declare!(DMA1_CHANNEL4_7); - declare!(ADC_COMP); - declare!(LPTIM1); - declare!(USART4_USART5); - declare!(TIM2); - declare!(TIM3); - declare!(TIM6); - declare!(TIM7); - declare!(TIM21); - declare!(I2C3); - declare!(TIM22); - declare!(I2C1); - declare!(I2C2); - declare!(SPI1); - declare!(SPI2); - declare!(USART1); - declare!(USART2); - declare!(AES_RNG_LPUART1); -} - -#[cfg(feature = "stm32l0x2")] -mod irqs { - use super::*; - declare!(WWDG); - declare!(PVD); - declare!(RTC); - declare!(RCC); - declare!(EXTI0_1); - declare!(EXTI2_3); - declare!(EXTI4_15); - declare!(TSC); - declare!(DMA1_CHANNEL1); - declare!(DMA1_CHANNEL2_3); - declare!(DMA1_CHANNEL4_7); - declare!(ADC_COMP); - declare!(LPTIM1); - declare!(USART4_USART5); - declare!(TIM2); - declare!(TIM3); - declare!(TIM6_DAC); - declare!(TIM7); - declare!(TIM21); - declare!(I2C3); - declare!(TIM22); - declare!(I2C1); - declare!(I2C2); - declare!(SPI1); - declare!(SPI2); - declare!(USART1); - declare!(USART2); - declare!(AES_RNG_LPUART1); - declare!(USB); -} - -#[cfg(feature = "stm32l0x3")] -mod irqs { - use super::*; - declare!(WWDG); - declare!(PVD); - declare!(RTC); - declare!(RCC); - declare!(EXTI0_1); - declare!(EXTI2_3); - declare!(EXTI4_15); - declare!(TSC); - declare!(DMA1_CHANNEL1); - declare!(DMA1_CHANNEL2_3); - declare!(DMA1_CHANNEL4_7); - declare!(ADC_COMP); - declare!(LPTIM1); - declare!(USART4_USART5); - declare!(TIM2); - declare!(TIM3); - declare!(TIM6_DAC); - declare!(TIM7); - declare!(TIM21); - declare!(I2C3); - declare!(TIM22); - declare!(I2C1); - declare!(I2C2); - declare!(SPI1); - declare!(SPI2); - declare!(USART1); - declare!(USART2); - declare!(AES_RNG_LPUART1); - declare!(LCD); - declare!(USB); -} - -pub use irqs::*; diff --git a/embassy-stm32l0/src/lib.rs b/embassy-stm32l0/src/lib.rs index d030713c..e80fe6cb 100644 --- a/embassy-stm32l0/src/lib.rs +++ b/embassy-stm32l0/src/lib.rs @@ -19,11 +19,6 @@ compile_error!( "Multile chip features activated. You must activate exactly one of the following features: " ); -pub use stm32l0xx_hal as hal; -pub use stm32l0xx_hal::pac; - -// This mod MUST go first, so that the others see its macros. -pub(crate) mod fmt; +pub use embassy_stm32::{fmt, hal, interrupt, pac}; pub mod exti; -pub mod interrupt;