Skip to content

Commit ef855a0

Browse files
committed
Added "device-selected" meta-feature and simplified feature gates
Also reworked some use statements to reduce amount of meta code Signed-off-by: Daniel Egger <daniel@eggers-club.de>
1 parent 476eb40 commit ef855a0

File tree

11 files changed

+85
-212
lines changed

11 files changed

+85
-212
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Added
1111

1212
- Added Sync & Send ability to Pin
13-
- Added overflow guards to delay
1413
- Added initial implementation of an ADC interface (#13) - @HarkonenBade
14+
- Added virtual-feature "device-selected" to simplify feature gating
15+
16+
### Changed
17+
18+
- Added overflow guards to delay
1519

1620
## [v0.10.0] - 2018-12-23
1721

Cargo.toml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,17 @@ version = "0.2.2"
4444
panic-halt = "0.2.0"
4545

4646
[features]
47+
device-selected = []
4748
rt = ["stm32f0/rt"]
48-
stm32f042 = ["stm32f0/stm32f0x2"]
49-
stm32f030 = ["stm32f0/stm32f0x0"]
50-
stm32f030x4 = ["stm32f030x6"]
51-
stm32f030x6 = ["stm32f030"]
52-
stm32f030x8 = ["stm32f030"]
53-
stm32f030xc = ["stm32f030"]
54-
stm32f070 = ["stm32f0/stm32f0x0"]
55-
stm32f070x6 = ["stm32f070"]
56-
stm32f070xb = ["stm32f070"]
49+
stm32f042 = ["stm32f0/stm32f0x2", "device-selected"]
50+
stm32f030 = ["stm32f0/stm32f0x0", "device-selected"]
51+
stm32f030x4 = ["stm32f030x6", "device-selected"]
52+
stm32f030x6 = ["stm32f030", "device-selected"]
53+
stm32f030x8 = ["stm32f030", "device-selected"]
54+
stm32f030xc = ["stm32f030", "device-selected"]
55+
stm32f070 = ["stm32f0/stm32f0x0", "device-selected"]
56+
stm32f070x6 = ["stm32f070", "device-selected"]
57+
stm32f070xb = ["stm32f070", "device-selected"]
5758

5859
[profile.dev]
5960
debug = true

src/adc.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@
3333
//! }
3434
//! ```
3535
36+
#[cfg(feature = "device-selected")]
3637
use embedded_hal::adc::{Channel, OneShot};
3738

38-
use crate::stm32;
39-
40-
use crate::gpio::*;
39+
#[cfg(feature = "device-selected")]
40+
use crate::{stm32, gpio::*};
4141

42+
#[cfg(feature = "device-selected")]
4243
/// Analog to Digital converter interface
4344
pub struct Adc {
4445
rb: stm32::ADC,
@@ -70,6 +71,7 @@ pub enum AdcSampleTime {
7071
T_239,
7172
}
7273

74+
#[cfg(feature = "device-selected")]
7375
impl AdcSampleTime {
7476
fn write_bits(&self, adc: &mut stm32::ADC) {
7577
unsafe {
@@ -117,6 +119,7 @@ pub enum AdcAlign {
117119
LeftAsRM,
118120
}
119121

122+
#[cfg(feature = "device-selected")]
120123
impl AdcAlign {
121124
fn write_bits(&self, adc: &mut stm32::ADC) {
122125
adc.cfgr1.write(|w| {
@@ -147,6 +150,7 @@ pub enum AdcPrecision {
147150
B_6,
148151
}
149152

153+
#[cfg(feature = "device-selected")]
150154
impl AdcPrecision {
151155
fn write_bits(&self, adc: &mut stm32::ADC) {
152156
unsafe {
@@ -167,6 +171,7 @@ impl AdcPrecision {
167171
}
168172
}
169173

174+
#[cfg(feature = "device-selected")]
170175
macro_rules! adc_pins {
171176
($($pin:ty => $chan:expr),+ $(,)*) => {
172177
$(
@@ -179,7 +184,7 @@ macro_rules! adc_pins {
179184
};
180185
}
181186

182-
#[cfg(any(feature = "stm32f042", feature = "stm32f030", feature = "stm32f070",))]
187+
#[cfg(feature = "device-selected")]
183188
adc_pins!(
184189
gpioa::PA0<Analog> => 0_u8,
185190
gpioa::PA1<Analog> => 1_u8,
@@ -211,11 +216,13 @@ pub struct VTemp;
211216
/// Internal voltage reference (ADC Channel 17)
212217
pub struct VRef;
213218

219+
#[cfg(feature = "device-selected")]
214220
adc_pins!(
215221
VTemp => 16_u8,
216222
VRef => 17_u8,
217223
);
218224

225+
#[cfg(feature = "device-selected")]
219226
impl VTemp {
220227
/// Init a new VTemp
221228
pub fn new() -> Self {
@@ -236,6 +243,7 @@ impl VTemp {
236243
}
237244
}
238245

246+
#[cfg(feature = "device-selected")]
239247
impl VRef {
240248
/// Init a new VRef
241249
pub fn new() -> Self {
@@ -253,17 +261,17 @@ impl VRef {
253261
}
254262
}
255263

256-
#[cfg(any(feature = "stm32f042",))]
264+
#[cfg(feature = "stm32f042")]
257265
#[derive(Debug)]
258266
/// Battery reference voltage (ADC Channel 18)
259267
pub struct VBat;
260268

261-
#[cfg(any(feature = "stm32f042",))]
269+
#[cfg(feature = "stm32f042")]
262270
adc_pins!(
263271
VBat => 18_u8,
264272
);
265273

266-
#[cfg(any(feature = "stm32f042",))]
274+
#[cfg(feature = "stm32f042")]
267275
impl VBat {
268276
/// Init a new VBat
269277
pub fn new() -> Self {
@@ -282,6 +290,7 @@ impl VBat {
282290
}
283291
}
284292

293+
#[cfg(feature = "device-selected")]
285294
impl Adc {
286295
/// Init a new Adc
287296
///
@@ -381,6 +390,7 @@ impl Adc {
381390
}
382391
}
383392

393+
#[cfg(feature = "device-selected")]
384394
impl<WORD, PIN> OneShot<Adc, WORD, PIN> for Adc
385395
where
386396
WORD: From<u16>,

src/gpio.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,10 @@ macro_rules! gpio_trait {
143143
};
144144
}
145145

146-
#[cfg(any(
147-
feature = "stm32f030",
148-
feature = "stm32f042",
149-
feature = "stm32f070"
150-
))]
146+
#[cfg(feature = "device-selected")]
151147
gpio_trait!(gpioa);
152148

153-
#[cfg(any(
154-
feature = "stm32f030",
155-
feature = "stm32f042",
156-
feature = "stm32f070"
157-
))]
149+
#[cfg(feature = "device-selected")]
158150
gpio_trait!(gpiof);
159151

160152
#[allow(unused)]
@@ -535,11 +527,7 @@ macro_rules! gpio {
535527
}
536528
}
537529

538-
#[cfg(any(
539-
feature = "stm32f030",
540-
feature = "stm32f042",
541-
feature = "stm32f070"
542-
))]
530+
#[cfg(feature = "device-selected")]
543531
gpio!(GPIOA, gpioa, iopaen, PA, [
544532
PA0: (pa0, 0, Input<Floating>),
545533
PA1: (pa1, 1, Input<Floating>),
@@ -559,11 +547,7 @@ gpio!(GPIOA, gpioa, iopaen, PA, [
559547
PA15: (pa15, 15, Input<Floating>),
560548
]);
561549

562-
#[cfg(any(
563-
feature = "stm32f030",
564-
feature = "stm32f042",
565-
feature = "stm32f070"
566-
))]
550+
#[cfg(feature = "device-selected")]
567551
gpio!(GPIOB, gpiob, iopben, PB, [
568552
PB0: (pb0, 0, Input<Floating>),
569553
PB1: (pb1, 1, Input<Floating>),

src/i2c.rs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use embedded_hal::blocking::i2c::{Write, WriteRead};
77
#[allow(unused)]
88
use crate::{
99
gpio::*,
10-
stm32,
1110
time::{KiloHertz, U32Ext},
1211
};
1312

@@ -29,10 +28,10 @@ macro_rules! i2c_pins {
2928
})+) => {
3029
$(
3130
$(
32-
impl SclPin<stm32::$I2C> for $scl {}
31+
impl SclPin<crate::stm32::$I2C> for $scl {}
3332
)+
3433
$(
35-
impl SdaPin<stm32::$I2C> for $sda {}
34+
impl SdaPin<crate::stm32::$I2C> for $sda {}
3635
)+
3736
)+
3837
}
@@ -121,7 +120,7 @@ macro_rules! i2c {
121120
SDAPIN: SdaPin<$I2C>,
122121
{
123122
// NOTE(unsafe) This executes only during initialisation
124-
let rcc = unsafe { &(*stm32::RCC::ptr()) };
123+
let rcc = unsafe { &(*crate::stm32::RCC::ptr()) };
125124

126125
/* Enable clock for I2C */
127126
rcc.$apbenr.modify(|_, w| w.$i2cXen().set_bit());
@@ -135,14 +134,12 @@ macro_rules! i2c {
135134
)+
136135
}
137136
}
138-
#[cfg(any(
139-
feature = "stm32f030",
140-
feature = "stm32f042",
141-
feature = "stm32f070"
142-
))]
137+
138+
#[cfg(feature = "device-selected")]
143139
i2c! {
144140
I2C1: (i2c1, i2c1en, i2c1rst, apb1enr, apb1rstr),
145141
}
142+
146143
#[cfg(any(
147144
feature = "stm32f030xc",
148145
// XXX: This can't be right
@@ -153,20 +150,12 @@ i2c! {
153150
I2C2: (i2c2, i2c2en, i2c2rst, apb1enr, apb1rstr),
154151
}
155152

153+
#[cfg(feature = "device-selected")]
156154
// It's s needed for the impls, but rustc doesn't recognize that
157-
#[cfg(any(
158-
feature = "stm32f030",
159-
feature = "stm32f042",
160-
feature = "stm32f070"
161-
))]
162155
#[allow(dead_code)]
163-
type I2cRegisterBlock = stm32::i2c1::RegisterBlock;
156+
type I2cRegisterBlock = crate::stm32::i2c1::RegisterBlock;
164157

165-
#[cfg(any(
166-
feature = "stm32f030",
167-
feature = "stm32f042",
168-
feature = "stm32f070"
169-
))]
158+
#[cfg(feature = "device-selected")]
170159
impl<I2C, SCLPIN, SDAPIN> I2c<I2C, SCLPIN, SDAPIN>
171160
where
172161
I2C: Deref<Target = I2cRegisterBlock>,
@@ -251,11 +240,7 @@ where
251240
}
252241
}
253242

254-
#[cfg(any(
255-
feature = "stm32f042",
256-
feature = "stm32f030",
257-
feature = "stm32f070"
258-
))]
243+
#[cfg(feature = "device-selected")]
259244
impl<I2C, SCLPIN, SDAPIN> WriteRead for I2c<I2C, SCLPIN, SDAPIN>
260245
where
261246
I2C: Deref<Target = I2cRegisterBlock>,
@@ -336,11 +321,7 @@ where
336321
}
337322
}
338323

339-
#[cfg(any(
340-
feature = "stm32f030",
341-
feature = "stm32f042",
342-
feature = "stm32f070"
343-
))]
324+
#[cfg(feature = "device-selected")]
344325
impl<I2C, SCLPIN, SDAPIN> Write for I2c<I2C, SCLPIN, SDAPIN>
345326
where
346327
I2C: Deref<Target = I2cRegisterBlock>,

src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ pub use stm32f0::stm32f0x2 as stm32;
99
#[cfg(any(feature = "stm32f030", feature = "stm32f070"))]
1010
pub use stm32f0::stm32f0x0 as stm32;
1111

12-
13-
#[cfg(not(any(
14-
feature = "stm32f030",
15-
feature = "stm32f042",
16-
feature = "stm32f070"
17-
)))]
18-
pub mod stm32 {}
19-
2012
pub mod adc;
2113
pub mod delay;
2214
pub mod gpio;

src/rcc.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
#[cfg(any(
2-
feature = "stm32f030",
3-
feature = "stm32f042",
4-
feature = "stm32f070"
5-
))]
6-
use crate::stm32::{FLASH, RCC};
7-
81
use crate::time::Hertz;
92

103
/// Extension trait that constrains the `RCC` peripheral
@@ -13,12 +6,8 @@ pub trait RccExt {
136
fn constrain(self) -> Rcc;
147
}
158

16-
#[cfg(any(
17-
feature = "stm32f030",
18-
feature = "stm32f042",
19-
feature = "stm32f070"
20-
))]
21-
impl RccExt for RCC {
9+
#[cfg(feature = "device-selected")]
10+
impl RccExt for crate::stm32::RCC {
2211
fn constrain(self) -> Rcc {
2312
Rcc {
2413
cfgr: CFGR {
@@ -45,11 +34,7 @@ pub struct CFGR {
4534
sysclk: Option<u32>,
4635
}
4736

48-
#[cfg(any(
49-
feature = "stm32f030",
50-
feature = "stm32f042",
51-
feature = "stm32f070"
52-
))]
37+
#[cfg(feature = "device-selected")]
5338
impl CFGR {
5439
pub fn hclk<F>(mut self, freq: F) -> Self
5540
where
@@ -121,7 +106,7 @@ impl CFGR {
121106

122107
// adjust flash wait states
123108
unsafe {
124-
let flash = &*FLASH::ptr();
109+
let flash = &*crate::stm32::FLASH::ptr();
125110
flash.acr.write(|w| {
126111
w.latency().bits(if sysclk <= 24_000_000 {
127112
0b000
@@ -133,7 +118,7 @@ impl CFGR {
133118
})
134119
}
135120

136-
let rcc = unsafe { &*RCC::ptr() };
121+
let rcc = unsafe { &*crate::stm32::RCC::ptr() };
137122
if let Some(pllmul_bits) = pllmul_bits {
138123
// use PLL as source
139124

0 commit comments

Comments
 (0)