Skip to content

Commit 33d175c

Browse files
burrbullTheZoq2
authored andcommitted
Update to digital v2 traits (#93)
1 parent eff7f30 commit 33d175c

File tree

10 files changed

+86
-60
lines changed

10 files changed

+86
-60
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1919

2020
### Breaking changes
2121

22+
- Replace gpio traits with digital::v2
2223
- Bump `stm32f1` dependency (`0.8.0`)
2324
- ADC now requires the clock configuration for intialisation
2425
- `disable_jtag` now transforms PA15, PB3 and PB4 to forbid their use without desactivating JTAG

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ default-features = false
3333
version = "0.2.2"
3434

3535
[dependencies.embedded-hal]
36+
version = "0.2.3"
3637
features = ["unproven"]
37-
version = "0.2.2"
3838

3939
[dev-dependencies]
4040
panic-halt = "0.2.0"

examples/blinky.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use stm32f1xx_hal::{
1919
timer::Timer,
2020
};
2121
use cortex_m_rt::entry;
22+
use embedded_hal::digital::v2::OutputPin;
2223

2324
#[entry]
2425
fn main() -> ! {
@@ -48,8 +49,8 @@ fn main() -> ! {
4849
// Wait for the timer to trigger an update and change the state of the LED
4950
loop {
5051
block!(timer.wait()).unwrap();
51-
led.set_high();
52+
led.set_high().unwrap();
5253
block!(timer.wait()).unwrap();
53-
led.set_low();
54+
led.set_low().unwrap();
5455
}
5556
}

examples/blinky_rtc.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use stm32f1xx_hal::{
2020

2121
use nb::block;
2222
use cortex_m_rt::entry;
23+
use embedded_hal::digital::v2::OutputPin;
2324

2425
#[entry]
2526
fn main() -> ! {
@@ -46,11 +47,11 @@ fn main() -> ! {
4647
rtc.set_alarm(5);
4748
block!(rtc.wait_alarm()).unwrap();
4849
if led_on {
49-
led.set_low();
50+
led.set_low().unwrap();
5051
led_on = false;
5152
}
5253
else {
53-
led.set_high();
54+
led.set_high().unwrap();
5455
led_on = true;
5556
}
5657
}

examples/delay.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use stm32f1xx_hal::{
1212
delay::Delay,
1313
};
1414
use cortex_m_rt::entry;
15+
use embedded_hal::digital::v2::OutputPin;
1516

1617
#[entry]
1718
fn main() -> ! {
@@ -37,9 +38,9 @@ fn main() -> ! {
3738
let mut delay = Delay::new(cp.SYST, clocks);
3839

3940
loop {
40-
led.set_high();
41+
led.set_high().unwrap();
4142
delay.delay_ms(1_000_u16);
42-
led.set_low();
43+
led.set_low().unwrap();
4344
delay.delay_ms(1_000_u16);
4445
}
4546
}

examples/led.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use stm32f1xx_hal::{
2020
pac,
2121
};
2222
use cortex_m_rt::entry;
23+
use embedded_hal::digital::v2::OutputPin;
2324

2425
#[entry]
2526
fn main() -> ! {
@@ -29,13 +30,13 @@ fn main() -> ! {
2930
let mut gpioc = p.GPIOC.split(&mut rcc.apb2);
3031

3132
#[cfg(feature = "stm32f100")]
32-
gpioc.pc9.into_push_pull_output(&mut gpioc.crh).set_high();
33+
gpioc.pc9.into_push_pull_output(&mut gpioc.crh).set_high().unwrap();
3334

3435
#[cfg(feature = "stm32f101")]
35-
gpioc.pc9.into_push_pull_output(&mut gpioc.crh).set_high();
36+
gpioc.pc9.into_push_pull_output(&mut gpioc.crh).set_high().unwrap();
3637

3738
#[cfg(feature = "stm32f103")]
38-
gpioc.pc13.into_push_pull_output(&mut gpioc.crh).set_low();
39+
gpioc.pc13.into_push_pull_output(&mut gpioc.crh).set_low().unwrap();
3940

4041
loop {}
4142
}

examples/mfrc522.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use stm32f1xx_hal::{
1313
};
1414
use mfrc522::Mfrc522;
1515
use cortex_m_rt::entry;
16+
use embedded_hal::digital::{v1_compat::OldOutputPin, v2::OutputPin};
1617

1718
#[entry]
1819
fn main() -> ! {
@@ -42,10 +43,10 @@ fn main() -> ! {
4243
);
4344

4445
let nss = gpioa.pa4.into_push_pull_output(&mut gpioa.crl);
45-
let mut mfrc522 = Mfrc522::new(spi, nss).unwrap();
46+
let mut mfrc522 = Mfrc522::new(spi, OldOutputPin::from(nss)).unwrap();
4647

4748
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
48-
led.set_high();
49+
led.set_high().unwrap();
4950

5051
loop {
5152
if let Ok(atqa) = mfrc522.reqa() {

examples/serial_config.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
use panic_halt as _;
1010

11-
use cortex_m::asm;
12-
1311
use nb::block;
1412

1513
use stm32f1xx_hal::{
@@ -73,7 +71,7 @@ fn main() -> ! {
7371
);
7472

7573
// Split the serial struct into a receiving and a transmitting part
76-
let (mut tx, mut rx) = serial.split();
74+
let (mut tx, _rx) = serial.split();
7775

7876
let sent = b'U';
7977
block!(tx.write(sent)).ok();

src/gpio.rs

Lines changed: 65 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
//! # General Purpose I/Os
2+
//!
3+
//! # Interfacing with v1 traits
4+
//!
5+
//! `embedded-hal` has two versions of the digital traits, `v2` which is used
6+
//! by this crate and `v1` which is deprecated but still used by a lot of drivers.
7+
//! If you want to use such a driver with this crate, you need to convert the digital pins to the `v1` type.
8+
//!
9+
//! This is done using `embedded-hal::digital::v1_compat::OldOutputPin`. For example:
10+
//!
11+
//! ```rust
12+
//! let nss = gpioa.pa4.into_push_pull_output(&mut gpioa.crl);
13+
//! let mut mfrc522 = Mfrc522::new(spi, OldOutputPin::from(nss)).unwrap();
14+
//! ```
15+
//!
216
317
use core::marker::PhantomData;
418

@@ -63,9 +77,10 @@ macro_rules! gpio {
6377
]) => {
6478
/// GPIO
6579
pub mod $gpiox {
80+
use void::Void;
6681
use core::marker::PhantomData;
6782

68-
use crate::hal::digital::{InputPin, OutputPin, StatefulOutputPin, toggleable};
83+
use crate::hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, toggleable};
6984
use crate::pac::{$gpioy, $GPIOX};
7085

7186
use crate::rcc::APB2;
@@ -143,49 +158,52 @@ macro_rules! gpio {
143158
}
144159

145160
impl<MODE> OutputPin for $PXx<Output<MODE>> {
146-
fn set_high(&mut self) {
161+
type Error = Void;
162+
fn set_high(&mut self) -> Result<(), Self::Error> {
147163
// NOTE(unsafe) atomic write to a stateless register
148-
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << self.i)) }
164+
Ok(unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << self.i)) })
149165
}
150166

151-
fn set_low(&mut self) {
167+
fn set_low(&mut self) -> Result<(), Self::Error> {
152168
// NOTE(unsafe) atomic write to a stateless register
153-
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (16 + self.i))) }
169+
Ok(unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (16 + self.i))) })
154170
}
155171
}
156172

157173
impl<MODE> InputPin for $PXx<Input<MODE>> {
158-
fn is_high(&self) -> bool {
159-
!self.is_low()
174+
type Error = Void;
175+
fn is_high(&self) -> Result<bool, Self::Error> {
176+
self.is_low().map(|b| !b)
160177
}
161178

162-
fn is_low(&self) -> bool {
179+
fn is_low(&self) -> Result<bool, Self::Error> {
163180
// NOTE(unsafe) atomic read with no side effects
164-
unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0 }
181+
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0 })
165182
}
166183
}
167184

168185
impl <MODE> StatefulOutputPin for $PXx<Output<MODE>> {
169-
fn is_set_high(&self) -> bool {
170-
!self.is_set_low()
186+
fn is_set_high(&self) -> Result<bool, Self::Error> {
187+
self.is_set_low().map(|b| !b)
171188
}
172189

173-
fn is_set_low(&self) -> bool {
190+
fn is_set_low(&self) -> Result<bool, Self::Error> {
174191
// NOTE(unsafe) atomic read with no side effects
175-
unsafe { (*$GPIOX::ptr()).odr.read().bits() & (1 << self.i) == 0 }
192+
Ok(unsafe { (*$GPIOX::ptr()).odr.read().bits() & (1 << self.i) == 0 })
176193
}
177194
}
178195

179196
impl <MODE> toggleable::Default for $PXx<Output<MODE>> {}
180197

181198
impl InputPin for $PXx<Output<OpenDrain>> {
182-
fn is_high(&self) -> bool {
183-
!self.is_low()
199+
type Error = Void;
200+
fn is_high(&self) -> Result<bool, Self::Error> {
201+
self.is_low().map(|b| !b)
184202
}
185203

186-
fn is_low(&self) -> bool {
204+
fn is_low(&self) -> Result<bool, Self::Error> {
187205
// NOTE(unsafe) atomic read with no side effects
188-
unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0 }
206+
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0 })
189207
}
190208
}
191209

@@ -354,7 +372,7 @@ macro_rules! gpio {
354372
match initial_state {
355373
State::High => res.set_high(),
356374
State::Low => res.set_low(),
357-
}
375+
}.unwrap();
358376

359377
cr
360378
.cr()
@@ -392,7 +410,7 @@ macro_rules! gpio {
392410
match initial_state {
393411
State::High => res.set_high(),
394412
State::Low => res.set_low(),
395-
}
413+
}.unwrap();
396414

397415
cr
398416
.cr()
@@ -437,72 +455,76 @@ macro_rules! gpio {
437455
}
438456

439457
impl<MODE> OutputPin for $PXi<Output<MODE>> {
440-
fn set_high(&mut self) {
458+
type Error = Void;
459+
fn set_high(&mut self) -> Result<(), Self::Error> {
441460
// NOTE(unsafe) atomic write to a stateless register
442-
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << $i)) }
461+
Ok(unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << $i)) })
443462
}
444463

445-
fn set_low(&mut self) {
464+
fn set_low(&mut self) -> Result<(), Self::Error> {
446465
// NOTE(unsafe) atomic write to a stateless register
447-
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (16 + $i))) }
466+
Ok(unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (16 + $i))) })
448467
}
449468
}
450469

451470
impl<MODE> StatefulOutputPin for $PXi<Output<MODE>> {
452-
fn is_set_high(&self) -> bool {
453-
!self.is_set_low()
471+
fn is_set_high(&self) -> Result<bool, Self::Error> {
472+
self.is_set_low().map(|b| !b)
454473
}
455474

456-
fn is_set_low(&self) -> bool {
475+
fn is_set_low(&self) -> Result<bool, Self::Error> {
457476
// NOTE(unsafe) atomic read with no side effects
458-
unsafe { (*$GPIOX::ptr()).odr.read().bits() & (1 << $i) == 0 }
477+
Ok(unsafe { (*$GPIOX::ptr()).odr.read().bits() & (1 << $i) == 0 })
459478
}
460479
}
461480

462481
impl<MODE> toggleable::Default for $PXi<Output<MODE>> {}
463482

464483
impl<MODE> OutputPin for $PXi<Alternate<MODE>> {
465-
fn set_high(&mut self) {
484+
type Error = Void;
485+
fn set_high(&mut self) -> Result<(), Self::Error> {
466486
// NOTE(unsafe) atomic write to a stateless register
467-
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << $i)) }
487+
Ok(unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << $i)) })
468488
}
469489

470-
fn set_low(&mut self) {
490+
fn set_low(&mut self) -> Result<(), Self::Error> {
471491
// NOTE(unsafe) atomic write to a stateless register
472-
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (16 + $i))) }
492+
Ok(unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (16 + $i))) })
473493
}
474494
}
475495

476496
impl<MODE> StatefulOutputPin for $PXi<Alternate<MODE>> {
477-
fn is_set_high(&self) -> bool {
478-
!self.is_set_low()
497+
fn is_set_high(&self) -> Result<bool, Self::Error> {
498+
self.is_set_low().map(|b| !b)
479499
}
480500

481-
fn is_set_low(&self) -> bool {
501+
fn is_set_low(&self) -> Result<bool, Self::Error> {
482502
// NOTE(unsafe) atomic read with no side effects
483-
unsafe { (*$GPIOX::ptr()).odr.read().bits() & (1 << $i) == 0 }
503+
Ok(unsafe { (*$GPIOX::ptr()).odr.read().bits() & (1 << $i) == 0 })
484504
}
485505
}
486506

487507
impl<MODE> InputPin for $PXi<Input<MODE>> {
488-
fn is_high(&self) -> bool {
489-
!self.is_low()
508+
type Error = Void;
509+
fn is_high(&self) -> Result<bool, Self::Error> {
510+
self.is_low().map(|b| !b)
490511
}
491512

492-
fn is_low(&self) -> bool {
513+
fn is_low(&self) -> Result<bool, Self::Error> {
493514
// NOTE(unsafe) atomic read with no side effects
494-
unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 }
515+
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 })
495516
}
496517
}
497518

498519
impl InputPin for $PXi<Output<OpenDrain>> {
499-
fn is_high(&self) -> bool {
500-
!self.is_low()
520+
type Error = Void;
521+
fn is_high(&self) -> Result<bool, Self::Error> {
522+
self.is_low().map(|b| !b)
501523
}
502524

503-
fn is_low(&self) -> bool {
525+
fn is_low(&self) -> Result<bool, Self::Error> {
504526
// NOTE(unsafe) atomic read with no side effects
505-
unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 }
527+
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 })
506528
}
507529
}
508530
)+

src/prelude.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ pub use crate::dma::DmaExt as _stm32_hal_dma_DmaExt;
33
pub use crate::flash::FlashExt as _stm32_hal_flash_FlashExt;
44
pub use crate::gpio::GpioExt as _stm32_hal_gpio_GpioExt;
55
pub use crate::hal::adc::OneShot as _embedded_hal_adc_OneShot;
6-
pub use crate::hal::digital::StatefulOutputPin as _embedded_hal_digital_StatefulOutputPin;
7-
pub use crate::hal::digital::ToggleableOutputPin as _embedded_hal_digital_ToggleableOutputPin;
6+
pub use crate::hal::digital::v2::StatefulOutputPin as _embedded_hal_digital_StatefulOutputPin;
7+
pub use crate::hal::digital::v2::ToggleableOutputPin as _embedded_hal_digital_ToggleableOutputPin;
88
pub use crate::hal::prelude::*;
99
pub use crate::pwm::PwmExt as _stm32_hal_pwm_PwmExt;
1010
pub use crate::rcc::RccExt as _stm32_hal_rcc_RccExt;

0 commit comments

Comments
 (0)