diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ae8de9..6ca3931 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,8 +10,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - Added Sync & Send ability to Pin -- Added overflow guards to delay - Added initial implementation of an ADC interface (#13) - @HarkonenBade +- Added virtual-feature "device-selected" to simplify feature gating + +### Changed + +- Added overflow guards to delay ## [v0.10.0] - 2018-12-23 diff --git a/Cargo.toml b/Cargo.toml index f21ed48..9a9048d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,16 +44,17 @@ version = "0.2.2" panic-halt = "0.2.0" [features] +device-selected = [] rt = ["stm32f0/rt"] -stm32f042 = ["stm32f0/stm32f0x2"] -stm32f030 = ["stm32f0/stm32f0x0"] -stm32f030x4 = ["stm32f030x6"] -stm32f030x6 = ["stm32f030"] -stm32f030x8 = ["stm32f030"] -stm32f030xc = ["stm32f030"] -stm32f070 = ["stm32f0/stm32f0x0"] -stm32f070x6 = ["stm32f070"] -stm32f070xb = ["stm32f070"] +stm32f042 = ["stm32f0/stm32f0x2", "device-selected"] +stm32f030 = ["stm32f0/stm32f0x0", "device-selected"] +stm32f030x4 = ["stm32f030x6", "device-selected"] +stm32f030x6 = ["stm32f030", "device-selected"] +stm32f030x8 = ["stm32f030", "device-selected"] +stm32f030xc = ["stm32f030", "device-selected"] +stm32f070 = ["stm32f0/stm32f0x0", "device-selected"] +stm32f070x6 = ["stm32f070", "device-selected"] +stm32f070xb = ["stm32f070", "device-selected"] [profile.dev] debug = true diff --git a/src/adc.rs b/src/adc.rs index 2d70ae0..ea8dac9 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -33,12 +33,13 @@ //! } //! ``` +#[cfg(feature = "device-selected")] use embedded_hal::adc::{Channel, OneShot}; -use crate::stm32; - -use crate::gpio::*; +#[cfg(feature = "device-selected")] +use crate::{stm32, gpio::*}; +#[cfg(feature = "device-selected")] /// Analog to Digital converter interface pub struct Adc { rb: stm32::ADC, @@ -70,6 +71,7 @@ pub enum AdcSampleTime { T_239, } +#[cfg(feature = "device-selected")] impl AdcSampleTime { fn write_bits(&self, adc: &mut stm32::ADC) { unsafe { @@ -117,6 +119,7 @@ pub enum AdcAlign { LeftAsRM, } +#[cfg(feature = "device-selected")] impl AdcAlign { fn write_bits(&self, adc: &mut stm32::ADC) { adc.cfgr1.write(|w| { @@ -147,6 +150,7 @@ pub enum AdcPrecision { B_6, } +#[cfg(feature = "device-selected")] impl AdcPrecision { fn write_bits(&self, adc: &mut stm32::ADC) { unsafe { @@ -167,6 +171,7 @@ impl AdcPrecision { } } +#[cfg(feature = "device-selected")] macro_rules! adc_pins { ($($pin:ty => $chan:expr),+ $(,)*) => { $( @@ -179,7 +184,7 @@ macro_rules! adc_pins { }; } -#[cfg(any(feature = "stm32f042", feature = "stm32f030", feature = "stm32f070",))] +#[cfg(feature = "device-selected")] adc_pins!( gpioa::PA0 => 0_u8, gpioa::PA1 => 1_u8, @@ -211,11 +216,13 @@ pub struct VTemp; /// Internal voltage reference (ADC Channel 17) pub struct VRef; +#[cfg(feature = "device-selected")] adc_pins!( VTemp => 16_u8, VRef => 17_u8, ); +#[cfg(feature = "device-selected")] impl VTemp { /// Init a new VTemp pub fn new() -> Self { @@ -236,6 +243,7 @@ impl VTemp { } } +#[cfg(feature = "device-selected")] impl VRef { /// Init a new VRef pub fn new() -> Self { @@ -253,17 +261,17 @@ impl VRef { } } -#[cfg(any(feature = "stm32f042",))] +#[cfg(feature = "stm32f042")] #[derive(Debug)] /// Battery reference voltage (ADC Channel 18) pub struct VBat; -#[cfg(any(feature = "stm32f042",))] +#[cfg(feature = "stm32f042")] adc_pins!( VBat => 18_u8, ); -#[cfg(any(feature = "stm32f042",))] +#[cfg(feature = "stm32f042")] impl VBat { /// Init a new VBat pub fn new() -> Self { @@ -282,6 +290,7 @@ impl VBat { } } +#[cfg(feature = "device-selected")] impl Adc { /// Init a new Adc /// @@ -381,6 +390,7 @@ impl Adc { } } +#[cfg(feature = "device-selected")] impl OneShot for Adc where WORD: From, diff --git a/src/gpio.rs b/src/gpio.rs index 0a500d5..c9246a0 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -143,18 +143,10 @@ macro_rules! gpio_trait { }; } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] gpio_trait!(gpioa); -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] gpio_trait!(gpiof); #[allow(unused)] @@ -535,11 +527,7 @@ macro_rules! gpio { } } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] gpio!(GPIOA, gpioa, iopaen, PA, [ PA0: (pa0, 0, Input), PA1: (pa1, 1, Input), @@ -559,11 +547,7 @@ gpio!(GPIOA, gpioa, iopaen, PA, [ PA15: (pa15, 15, Input), ]); -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] gpio!(GPIOB, gpiob, iopben, PB, [ PB0: (pb0, 0, Input), PB1: (pb1, 1, Input), diff --git a/src/i2c.rs b/src/i2c.rs index 9942abd..e6f2729 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -7,7 +7,6 @@ use embedded_hal::blocking::i2c::{Write, WriteRead}; #[allow(unused)] use crate::{ gpio::*, - stm32, time::{KiloHertz, U32Ext}, }; @@ -29,10 +28,10 @@ macro_rules! i2c_pins { })+) => { $( $( - impl SclPin for $scl {} + impl SclPin for $scl {} )+ $( - impl SdaPin for $sda {} + impl SdaPin for $sda {} )+ )+ } @@ -121,7 +120,7 @@ macro_rules! i2c { SDAPIN: SdaPin<$I2C>, { // NOTE(unsafe) This executes only during initialisation - let rcc = unsafe { &(*stm32::RCC::ptr()) }; + let rcc = unsafe { &(*crate::stm32::RCC::ptr()) }; /* Enable clock for I2C */ rcc.$apbenr.modify(|_, w| w.$i2cXen().set_bit()); @@ -135,14 +134,12 @@ macro_rules! i2c { )+ } } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] + +#[cfg(feature = "device-selected")] i2c! { I2C1: (i2c1, i2c1en, i2c1rst, apb1enr, apb1rstr), } + #[cfg(any( feature = "stm32f030xc", // XXX: This can't be right @@ -153,20 +150,12 @@ i2c! { I2C2: (i2c2, i2c2en, i2c2rst, apb1enr, apb1rstr), } +#[cfg(feature = "device-selected")] // It's s needed for the impls, but rustc doesn't recognize that -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] #[allow(dead_code)] -type I2cRegisterBlock = stm32::i2c1::RegisterBlock; +type I2cRegisterBlock = crate::stm32::i2c1::RegisterBlock; -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl I2c where I2C: Deref, @@ -251,11 +240,7 @@ where } } -#[cfg(any( - feature = "stm32f042", - feature = "stm32f030", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl WriteRead for I2c where I2C: Deref, @@ -336,11 +321,7 @@ where } } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl Write for I2c where I2C: Deref, diff --git a/src/lib.rs b/src/lib.rs index 9a18905..8b5c921 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,14 +9,6 @@ pub use stm32f0::stm32f0x2 as stm32; #[cfg(any(feature = "stm32f030", feature = "stm32f070"))] pub use stm32f0::stm32f0x0 as stm32; - -#[cfg(not(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -)))] -pub mod stm32 {} - pub mod adc; pub mod delay; pub mod gpio; diff --git a/src/rcc.rs b/src/rcc.rs index 2de718d..6d08ae4 100644 --- a/src/rcc.rs +++ b/src/rcc.rs @@ -1,10 +1,3 @@ -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] -use crate::stm32::{FLASH, RCC}; - use crate::time::Hertz; /// Extension trait that constrains the `RCC` peripheral @@ -13,12 +6,8 @@ pub trait RccExt { fn constrain(self) -> Rcc; } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] -impl RccExt for RCC { +#[cfg(feature = "device-selected")] +impl RccExt for crate::stm32::RCC { fn constrain(self) -> Rcc { Rcc { cfgr: CFGR { @@ -45,11 +34,7 @@ pub struct CFGR { sysclk: Option, } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl CFGR { pub fn hclk(mut self, freq: F) -> Self where @@ -121,7 +106,7 @@ impl CFGR { // adjust flash wait states unsafe { - let flash = &*FLASH::ptr(); + let flash = &*crate::stm32::FLASH::ptr(); flash.acr.write(|w| { w.latency().bits(if sysclk <= 24_000_000 { 0b000 @@ -133,7 +118,7 @@ impl CFGR { }) } - let rcc = unsafe { &*RCC::ptr() }; + let rcc = unsafe { &*crate::stm32::RCC::ptr() }; if let Some(pllmul_bits) = pllmul_bits { // use PLL as source diff --git a/src/serial.rs b/src/serial.rs index 0e1d045..7e9d57e 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -34,7 +34,7 @@ use core::{ use embedded_hal::prelude::*; #[allow(unused)] -use crate::{gpio::*, rcc::Clocks, stm32, time::Bps}; +use crate::{gpio::*, rcc::Clocks, time::Bps}; /// Interrupt event pub enum Event { @@ -62,7 +62,7 @@ pub enum Error { pub trait TxPin {} pub trait RxPin {} -#[allow(unused)] +#[cfg(feature = "device-selected")] macro_rules! usart_pins { ($($USART:ident => { tx => [$($tx:ty),+ $(,)*], @@ -70,10 +70,10 @@ macro_rules! usart_pins { })+) => { $( $( - impl TxPin for $tx {} + impl TxPin for $tx {} )+ $( - impl RxPin for $rx {} + impl RxPin for $rx {} )+ )+ } @@ -163,7 +163,7 @@ pub struct Tx { // NOTE(unsafe) Required to allow protected shared access in handlers unsafe impl Send for Tx {} -#[allow(unused)] +#[cfg(feature = "device-selected")] macro_rules! usart { ($($USART:ident: ($usart:ident, $usartXen:ident, $apbenr:ident),)+) => { $( @@ -176,7 +176,7 @@ macro_rules! usart { RXPIN: RxPin<$USART>, { // NOTE(unsafe) This executes only during initialisation - let rcc = unsafe { &(*stm32::RCC::ptr()) }; + let rcc = unsafe { &(*crate::stm32::RCC::ptr()) }; /* Enable clock for USART */ rcc.$apbenr.modify(|_, w| w.$usartXen().set_bit()); @@ -199,11 +199,7 @@ macro_rules! usart { } } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] usart! { USART1: (usart1, usart1en, apb2enr), } @@ -229,18 +225,10 @@ usart! { // It's s needed for the impls, but rustc doesn't recognize that #[allow(dead_code)] -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] -type SerialRegisterBlock = stm32::usart1::RegisterBlock; +#[cfg(feature = "device-selected")] +type SerialRegisterBlock = crate::stm32::usart1::RegisterBlock; -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl embedded_hal::serial::Read for Rx where USART: Deref, @@ -269,11 +257,7 @@ where } } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl embedded_hal::serial::Write for Tx where USART: Deref, @@ -309,11 +293,7 @@ where } } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl Serial where USART: Deref, @@ -335,11 +315,7 @@ where } } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl Write for Tx where Tx: embedded_hal::serial::Write, diff --git a/src/spi.rs b/src/spi.rs index 9ac2c5d..f781c77 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -6,23 +6,15 @@ use nb; pub use embedded_hal::spi::{Mode, Phase, Polarity}; -#[allow(unused)] -use crate::stm32; - // TODO Put this inside the macro // Currently that causes a compiler panic -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] use crate::stm32::SPI1; #[cfg(any( feature = "stm32f030x8", feature = "stm32f030xc", feature = "stm32f070xb" ))] -#[allow(unused)] use crate::stm32::SPI2; #[allow(unused)] @@ -58,7 +50,7 @@ pub trait SckPin {} pub trait MisoPin {} pub trait MosiPin {} -#[allow(unused)] +#[cfg(feature = "device-selected")] macro_rules! spi_pins { ($($SPI:ident => { sck => [$($sck:ty),+ $(,)*], @@ -67,23 +59,19 @@ macro_rules! spi_pins { })+) => { $( $( - impl SckPin for $sck {} + impl SckPin for $sck {} )+ $( - impl MisoPin for $miso {} + impl MisoPin for $miso {} )+ $( - impl MosiPin for $mosi {} + impl MosiPin for $mosi {} )+ )+ } } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] spi_pins! { SPI1 => { sck => [gpioa::PA5>, gpiob::PB3>], @@ -139,7 +127,7 @@ macro_rules! spi { F: Into, { // NOTE(unsafe) This executes only during initialisation - let rcc = unsafe { &(*stm32::RCC::ptr()) }; + let rcc = unsafe { &(*crate::stm32::RCC::ptr()) }; /* Enable clock for SPI */ rcc.$apbenr.modify(|_, w| w.$spiXen().set_bit()); @@ -154,11 +142,7 @@ macro_rules! spi { } } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] spi! { SPI1: (spi1, spi1en, spi1rst, apb2enr, apb2rstr), } @@ -173,18 +157,10 @@ spi! { // It's s needed for the impls, but rustc doesn't recognize that #[allow(dead_code)] -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] -type SpiRegisterBlock = stm32::spi1::RegisterBlock; +#[cfg(feature = "device-selected")] +type SpiRegisterBlock = crate::stm32::spi1::RegisterBlock; -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl Spi where SPI: Deref, @@ -255,11 +231,7 @@ where } } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl ::embedded_hal::spi::FullDuplex for Spi where @@ -304,22 +276,14 @@ where } } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl ::embedded_hal::blocking::spi::transfer::Default for Spi where SPI: Deref, {} -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl ::embedded_hal::blocking::spi::write::Default for Spi where diff --git a/src/timers.rs b/src/timers.rs index 1ec8173..4c3fe4b 100644 --- a/src/timers.rs +++ b/src/timers.rs @@ -211,11 +211,7 @@ macro_rules! timers { } } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] timers! { TIM1: (tim1, tim1en, tim1rst, apb2enr, apb2rstr), TIM3: (tim3, tim3en, tim3rst, apb1enr, apb1rstr), diff --git a/src/watchdog.rs b/src/watchdog.rs index fa5aa73..a3fb569 100644 --- a/src/watchdog.rs +++ b/src/watchdog.rs @@ -44,29 +44,17 @@ #[allow(unused)] use embedded_hal::watchdog; -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] use crate::stm32::IWDG; use crate::time::Hertz; -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] /// Watchdog instance pub struct Watchdog { iwdg: IWDG, } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl watchdog::Watchdog for Watchdog { /// Feed the watchdog, so that at least one `period` goes by before the next /// reset @@ -105,22 +93,14 @@ impl Into for Hertz { } } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl Watchdog { pub fn new(iwdg: IWDG) -> Self { Self { iwdg } } } -#[cfg(any( - feature = "stm32f030", - feature = "stm32f042", - feature = "stm32f070" -))] +#[cfg(feature = "device-selected")] impl watchdog::WatchdogEnable for Watchdog { type Time = IwdgTimeout; fn start(&mut self, period: T)