Skip to content

Commit a1316e8

Browse files
committed
Update to embedded-hal v1.0.0-alpha.1
1 parent eaefada commit a1316e8

18 files changed

+329
-237
lines changed

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ default-features = false
4848
version = "1.0.2"
4949

5050
[dependencies.embedded-hal]
51-
features = ["unproven"]
52-
version = "0.2.3"
51+
version = "=1.0.0-alpha.1"
5352

5453
[dev-dependencies]
5554
panic-semihosting = "0.5.3"

examples/delay-blinky.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ fn main() -> ! {
3131

3232
loop {
3333
// On for 1s, off for 1s.
34-
led.set_high().unwrap();
35-
delay.delay_ms(1000_u32);
36-
led.set_low().unwrap();
37-
delay.delay_ms(1000_u32);
34+
led.try_set_high().unwrap();
35+
delay.try_delay_ms(1000_u32).unwrap();
36+
led.try_set_low().unwrap();
37+
delay.try_delay_ms(1000_u32).unwrap();
3838
}
3939
}
4040

examples/pwm.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ fn main() -> ! {
2424

2525
let pwm = pwm::tim1(dp.TIM1, channels, clocks, 20u32.khz());
2626
let (mut ch1, _ch2) = pwm;
27-
let max_duty = ch1.get_max_duty();
28-
ch1.set_duty(max_duty / 2);
29-
ch1.enable();
27+
let max_duty = ch1.try_get_max_duty().unwrap();
28+
ch1.try_set_duty(max_duty / 2);
29+
ch1.try_enable();
3030
}
3131

3232
loop {

examples/rng-display.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn main() -> ! {
9999
}
100100
disp.flush().unwrap();
101101
//delay a little while between refreshes so the display is readable
102-
delay_source.delay_ms(100u8);
102+
delay_source.try_delay_ms(100u8).unwrap();
103103
}
104104
}
105105

examples/timer-syst.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Start and stop a periodic system timer.
1+
//! Start and stop a periodic system timer.try_
22
//!
33
//! This example should run on all stm32f4xx boards but it was tested with
44
//! stm32f4-discovery board (model STM32F407G-DISC1).
@@ -36,29 +36,29 @@ fn main() -> ! {
3636

3737
hprintln!("hello!").unwrap();
3838
// wait until timer expires
39-
nb::block!(timer.wait()).unwrap();
39+
nb::block!(timer.try_wait()).unwrap();
4040
hprintln!("timer expired 1").unwrap();
4141

4242
// the function syst() creates a periodic timer, so it is automatically
4343
// restarted
44-
nb::block!(timer.wait()).unwrap();
44+
nb::block!(timer.try_wait()).unwrap();
4545
hprintln!("timer expired 2").unwrap();
4646

4747
// cancel current timer
48-
timer.cancel().unwrap();
48+
timer.try_cancel().unwrap();
4949

5050
// start it again
51-
timer.start(24.hz());
52-
nb::block!(timer.wait()).unwrap();
51+
timer.try_start(24.hz());
52+
nb::block!(timer.try_wait()).unwrap();
5353
hprintln!("timer expired 3").unwrap();
5454

55-
timer.cancel().unwrap();
56-
let cancel_outcome = timer.cancel();
55+
timer.try_cancel().unwrap();
56+
let cancel_outcome = timer.try_cancel();
5757
assert_eq!(cancel_outcome, Err(timer::Error::Disabled));
5858
hprintln!("ehy, you cannot cancel a timer two times!").unwrap();
5959
// this time the timer was not restarted, therefore this function should
6060
// wait forever
61-
nb::block!(timer.wait()).unwrap();
61+
nb::block!(timer.try_wait()).unwrap();
6262
// you should never see this print
6363
hprintln!("if you see this there is something wrong").unwrap();
6464
panic!();

src/adc.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ macro_rules! adc_pins {
2626
$(
2727
impl Channel<stm32::$adc> for $pin {
2828
type ID = u8;
29-
fn channel() -> u8 { $chan }
29+
const CHANNEL: Self::ID = $chan;
3030
}
3131
)+
3232
};
@@ -665,7 +665,7 @@ macro_rules! adc {
665665
}
666666

667667
let vref_cal = VrefCal::get().read();
668-
let vref_samp = self.read(&mut Vref).unwrap(); //This can't actually fail, it's just in a result to satisfy hal trait
668+
let vref_samp = self.try_read(&mut Vref).unwrap(); //This can't actually fail, it's just in a result to satisfy hal trait
669669

670670
self.calibrated_vdda = (VDDA_CALIB * u32::from(vref_cal)) / u32::from(vref_samp);
671671
if !vref_en {
@@ -873,7 +873,7 @@ macro_rules! adc {
873873
}
874874
});
875875

876-
let channel = CHANNEL::channel();
876+
let channel = CHANNEL::CHANNEL;
877877

878878
//Set the channel in the right sequence field
879879
match sequence {
@@ -976,7 +976,7 @@ macro_rules! adc {
976976
{
977977
type Error = ();
978978

979-
fn read(&mut self, pin: &mut PIN) -> nb::Result<u16, Self::Error> {
979+
fn try_read(&mut self, pin: &mut PIN) -> nb::Result<u16, Self::Error> {
980980
let enabled = self.is_enabled();
981981
if !enabled {
982982
self.enable();

src/delay.rs

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Delays
22
33
use cast::u32;
4+
use core::convert::Infallible;
5+
46
use cortex_m::peripheral::syst::SystClkSource;
57
use cortex_m::peripheral::SYST;
68

@@ -28,25 +30,33 @@ impl Delay {
2830
}
2931

3032
impl DelayMs<u32> for Delay {
31-
fn delay_ms(&mut self, ms: u32) {
32-
self.delay_us(ms * 1_000);
33+
type Error = Infallible;
34+
35+
fn try_delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
36+
self.try_delay_us(ms * 1_000)
3337
}
3438
}
3539

3640
impl DelayMs<u16> for Delay {
37-
fn delay_ms(&mut self, ms: u16) {
38-
self.delay_ms(u32(ms));
41+
type Error = Infallible;
42+
43+
fn try_delay_ms(&mut self, ms: u16) -> Result<(), Self::Error> {
44+
self.try_delay_ms(u32(ms))
3945
}
4046
}
4147

4248
impl DelayMs<u8> for Delay {
43-
fn delay_ms(&mut self, ms: u8) {
44-
self.delay_ms(u32(ms));
49+
type Error = Infallible;
50+
51+
fn try_delay_ms(&mut self, ms: u8) -> Result<(), Self::Error> {
52+
self.try_delay_ms(u32(ms))
4553
}
4654
}
4755

4856
impl DelayUs<u32> for Delay {
49-
fn delay_us(&mut self, us: u32) {
57+
type Error = Infallible;
58+
59+
fn try_delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
5060
// The SysTick Reload Value register supports values between 1 and 0x00FFFFFF.
5161
const MAX_RVR: u32 = 0x00FF_FFFF;
5262

@@ -70,17 +80,23 @@ impl DelayUs<u32> for Delay {
7080

7181
self.syst.disable_counter();
7282
}
83+
84+
Ok(())
7385
}
7486
}
7587

7688
impl DelayUs<u16> for Delay {
77-
fn delay_us(&mut self, us: u16) {
78-
self.delay_us(u32(us))
89+
type Error = Infallible;
90+
91+
fn try_delay_us(&mut self, us: u16) -> Result<(), Self::Error> {
92+
self.try_delay_us(u32(us))
7993
}
8094
}
8195

8296
impl DelayUs<u8> for Delay {
83-
fn delay_us(&mut self, us: u8) {
84-
self.delay_us(u32(us))
97+
type Error = Infallible;
98+
99+
fn try_delay_us(&mut self, us: u8) -> Result<(), Self::Error> {
100+
self.try_delay_us(u32(us))
85101
}
86102
}

src/dwt.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
//! Debug and trace and stuff
22
3+
use core::convert::Infallible;
4+
35
use crate::rcc::Clocks;
46
use crate::time::Hertz;
57
use cortex_m::peripheral::{DCB, DWT};
8+
69
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
710

811
pub trait DwtExt {
@@ -65,13 +68,13 @@ pub struct Delay {
6568
}
6669
impl Delay {
6770
/// Delay for `ClockDuration::ticks`
68-
pub fn delay(duration: ClockDuration) {
71+
pub fn try_delay(duration: ClockDuration) -> Result<(), Infallible> {
6972
let ticks = duration.ticks as u64;
70-
Delay::delay_ticks(DWT::get_cycle_count(), ticks);
73+
Delay::try_delay_ticks(DWT::get_cycle_count(), ticks)
7174
}
7275
/// Delay ticks
7376
/// NOTE DCB and DWT need to be set up for this to work, so it is private
74-
fn delay_ticks(mut start: u32, ticks: u64) {
77+
fn try_delay_ticks(mut start: u32, ticks: u64) -> Result<(), Infallible> {
7578
if ticks < (core::u32::MAX / 2) as u64 {
7679
// Simple delay
7780
let ticks = ticks as u32;
@@ -96,24 +99,30 @@ impl Delay {
9699
while (DWT::get_cycle_count().wrapping_sub(start)) > ticks {}
97100
}
98101
}
102+
103+
Ok(())
99104
}
100105
}
101106

102107
// Implement DelayUs/DelayMs for various integer types
103108
impl<T: Into<u64>> DelayUs<T> for Delay {
104-
fn delay_us(&mut self, us: T) {
109+
type Error = Infallible;
110+
111+
fn try_delay_us(&mut self, us: T) -> Result<(), Self::Error> {
105112
// Convert us to ticks
106113
let start = DWT::get_cycle_count();
107114
let ticks = (us.into() * self.clock.0 as u64) / 1_000_000;
108-
Delay::delay_ticks(start, ticks);
115+
Delay::try_delay_ticks(start, ticks)
109116
}
110117
}
111118
impl<T: Into<u64>> DelayMs<T> for Delay {
112-
fn delay_ms(&mut self, ms: T) {
119+
type Error = Infallible;
120+
121+
fn try_delay_ms(&mut self, ms: T) -> Result<(), Self::Error> {
113122
// Convert ms to ticks
114123
let start = DWT::get_cycle_count();
115124
let ticks = (ms.into() * self.clock.0 as u64) / 1_000;
116-
Delay::delay_ticks(start, ticks);
125+
Delay::try_delay_ticks(start, ticks)
117126
}
118127
}
119128

src/gpio.rs

+23-23
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ macro_rules! gpio {
244244
use core::marker::PhantomData;
245245
use core::convert::Infallible;
246246

247-
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, toggleable};
247+
use embedded_hal::digital::{InputPin, OutputPin, StatefulOutputPin, toggleable};
248248
use crate::stm32::$GPIOX;
249249

250250
use crate::stm32::{RCC, EXTI, SYSCFG};
@@ -293,25 +293,25 @@ macro_rules! gpio {
293293
impl<MODE> OutputPin for $PXx<Output<MODE>> {
294294
type Error = Infallible;
295295

296-
fn set_high(&mut self) -> Result<(), Self::Error> {
296+
fn try_set_high(&mut self) -> Result<(), Self::Error> {
297297
// NOTE(unsafe) atomic write to a stateless register
298298
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << self.i)) };
299299
Ok(())
300300
}
301301

302-
fn set_low(&mut self) -> Result<(), Self::Error> {
302+
fn try_set_low(&mut self) -> Result<(), Self::Error> {
303303
// NOTE(unsafe) atomic write to a stateless register
304304
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (self.i + 16))) };
305305
Ok(())
306306
}
307307
}
308308

309309
impl<MODE> StatefulOutputPin for $PXx<Output<MODE>> {
310-
fn is_set_high(&self) -> Result<bool, Self::Error> {
311-
self.is_set_low().map(|v| !v)
310+
fn try_is_set_high(&self) -> Result<bool, Self::Error> {
311+
self.try_is_set_low().map(|v| !v)
312312
}
313313

314-
fn is_set_low(&self) -> Result<bool, Self::Error> {
314+
fn try_is_set_low(&self) -> Result<bool, Self::Error> {
315315
// NOTE(unsafe) atomic read with no side effects
316316
Ok(unsafe { (*$GPIOX::ptr()).odr.read().bits() & (1 << self.i) == 0 })
317317
}
@@ -322,11 +322,11 @@ macro_rules! gpio {
322322
impl<MODE> InputPin for $PXx<Output<MODE>> {
323323
type Error = Infallible;
324324

325-
fn is_high(&self) -> Result<bool, Self::Error> {
326-
self.is_low().map(|v| !v)
325+
fn try_is_high(&self) -> Result<bool, Self::Error> {
326+
self.try_is_low().map(|v| !v)
327327
}
328328

329-
fn is_low(&self) -> Result<bool, Self::Error> {
329+
fn try_is_low(&self) -> Result<bool, Self::Error> {
330330
// NOTE(unsafe) atomic read with no side effects
331331
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0 })
332332
}
@@ -335,11 +335,11 @@ macro_rules! gpio {
335335
impl<MODE> InputPin for $PXx<Input<MODE>> {
336336
type Error = Infallible;
337337

338-
fn is_high(&self) -> Result<bool, Self::Error> {
339-
self.is_low().map(|v| !v)
338+
fn try_is_high(&self) -> Result<bool, Self::Error> {
339+
self.try_is_low().map(|v| !v)
340340
}
341341

342-
fn is_low(&self) -> Result<bool, Self::Error> {
342+
fn try_is_low(&self) -> Result<bool, Self::Error> {
343343
// NOTE(unsafe) atomic read with no side effects
344344
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0 })
345345
}
@@ -754,25 +754,25 @@ macro_rules! gpio {
754754
impl<MODE> OutputPin for $PXi<Output<MODE>> {
755755
type Error = Infallible;
756756

757-
fn set_high(&mut self) -> Result<(), Self::Error> {
757+
fn try_set_high(&mut self) -> Result<(), Self::Error> {
758758
// NOTE(unsafe) atomic write to a stateless register
759759
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << $i)) };
760760
Ok(())
761761
}
762762

763-
fn set_low(&mut self) -> Result<(), Self::Error> {
763+
fn try_set_low(&mut self) -> Result<(), Self::Error> {
764764
// NOTE(unsafe) atomic write to a stateless register
765765
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << ($i + 16))) };
766766
Ok(())
767767
}
768768
}
769769

770770
impl<MODE> StatefulOutputPin for $PXi<Output<MODE>> {
771-
fn is_set_high(&self) -> Result<bool, Self::Error> {
772-
self.is_set_low().map(|v| !v)
771+
fn try_is_set_high(&self) -> Result<bool, Self::Error> {
772+
self.try_is_set_low().map(|v| !v)
773773
}
774774

775-
fn is_set_low(&self) -> Result<bool, Self::Error> {
775+
fn try_is_set_low(&self) -> Result<bool, Self::Error> {
776776
// NOTE(unsafe) atomic read with no side effects
777777
Ok(unsafe { (*$GPIOX::ptr()).odr.read().bits() & (1 << $i) == 0 })
778778
}
@@ -783,11 +783,11 @@ macro_rules! gpio {
783783
impl<MODE> InputPin for $PXi<Output<MODE>> {
784784
type Error = Infallible;
785785

786-
fn is_high(&self) -> Result<bool, Self::Error> {
787-
self.is_low().map(|v| !v)
786+
fn try_is_high(&self) -> Result<bool, Self::Error> {
787+
self.try_is_low().map(|v| !v)
788788
}
789789

790-
fn is_low(&self) -> Result<bool, Self::Error> {
790+
fn try_is_low(&self) -> Result<bool, Self::Error> {
791791
// NOTE(unsafe) atomic read with no side effects
792792
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 })
793793
}
@@ -796,11 +796,11 @@ macro_rules! gpio {
796796
impl<MODE> InputPin for $PXi<Input<MODE>> {
797797
type Error = Infallible;
798798

799-
fn is_high(&self) -> Result<bool, Self::Error> {
800-
self.is_low().map(|v| !v)
799+
fn try_is_high(&self) -> Result<bool, Self::Error> {
800+
self.try_is_low().map(|v| !v)
801801
}
802802

803-
fn is_low(&self) -> Result<bool, Self::Error> {
803+
fn try_is_low(&self) -> Result<bool, Self::Error> {
804804
// NOTE(unsafe) atomic read with no side effects
805805
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 })
806806
}

0 commit comments

Comments
 (0)