Skip to content

Commit f94d109

Browse files
committed
Update to use try_ functions
1 parent f9fabf9 commit f94d109

17 files changed

+308
-219
lines changed

Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ default-features = false
4747
version = "1.0.2"
4848

4949
[dependencies.embedded-hal]
50-
features = ["unproven"]
51-
version = "0.2.3"
50+
# TODO: Update version on next release.
51+
# version = "0.2.3"
52+
git = "https://github.com/rust-embedded/embedded-hal/"
53+
branch = "master"
5254

5355
[dev-dependencies]
5456
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
@@ -92,7 +92,7 @@ fn main() -> ! {
9292
}
9393
disp.flush().unwrap();
9494
//delay a little while between refreshes so the display is readable
95-
delay_source.delay_ms(100u8);
95+
delay_source.try_delay_ms(100u8).unwrap();
9696
}
9797
}
9898

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
};
@@ -668,7 +668,7 @@ macro_rules! adc {
668668
}
669669

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

673673
self.calibrated_vdda = (VDDA_CALIB * u32::from(vref_cal)) / u32::from(vref_samp);
674674
if !vref_en {
@@ -876,7 +876,7 @@ macro_rules! adc {
876876
}
877877
});
878878

879-
let channel = CHANNEL::channel();
879+
let channel = CHANNEL::CHANNEL;
880880

881881
//Set the channel in the right sequence field
882882
match sequence {
@@ -979,7 +979,7 @@ macro_rules! adc {
979979
{
980980
type Error = ();
981981

982-
fn read(&mut self, pin: &mut PIN) -> nb::Result<u16, Self::Error> {
982+
fn try_read(&mut self, pin: &mut PIN) -> nb::Result<u16, Self::Error> {
983983
let enabled = self.is_enabled();
984984
if !enabled {
985985
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/gpio.rs

+23-23
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ macro_rules! gpio {
101101
use core::marker::PhantomData;
102102
use core::convert::Infallible;
103103

104-
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, toggleable};
104+
use embedded_hal::digital::{InputPin, OutputPin, StatefulOutputPin, toggleable};
105105
use crate::stm32::$GPIOX;
106106

107107
use crate::stm32::{RCC, EXTI, SYSCFG};
@@ -150,25 +150,25 @@ macro_rules! gpio {
150150
impl<MODE> OutputPin for $PXx<Output<MODE>> {
151151
type Error = Infallible;
152152

153-
fn set_high(&mut self) -> Result<(), Self::Error> {
153+
fn try_set_high(&mut self) -> Result<(), Self::Error> {
154154
// NOTE(unsafe) atomic write to a stateless register
155155
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << self.i)) };
156156
Ok(())
157157
}
158158

159-
fn set_low(&mut self) -> Result<(), Self::Error> {
159+
fn try_set_low(&mut self) -> Result<(), Self::Error> {
160160
// NOTE(unsafe) atomic write to a stateless register
161161
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (self.i + 16))) };
162162
Ok(())
163163
}
164164
}
165165

166166
impl<MODE> StatefulOutputPin for $PXx<Output<MODE>> {
167-
fn is_set_high(&self) -> Result<bool, Self::Error> {
168-
self.is_set_low().map(|v| !v)
167+
fn try_is_set_high(&self) -> Result<bool, Self::Error> {
168+
self.try_is_set_low().map(|v| !v)
169169
}
170170

171-
fn is_set_low(&self) -> Result<bool, Self::Error> {
171+
fn try_is_set_low(&self) -> Result<bool, Self::Error> {
172172
// NOTE(unsafe) atomic read with no side effects
173173
Ok(unsafe { (*$GPIOX::ptr()).odr.read().bits() & (1 << self.i) == 0 })
174174
}
@@ -179,11 +179,11 @@ macro_rules! gpio {
179179
impl<MODE> InputPin for $PXx<Output<MODE>> {
180180
type Error = Infallible;
181181

182-
fn is_high(&self) -> Result<bool, Self::Error> {
183-
self.is_low().map(|v| !v)
182+
fn try_is_high(&self) -> Result<bool, Self::Error> {
183+
self.try_is_low().map(|v| !v)
184184
}
185185

186-
fn is_low(&self) -> Result<bool, Self::Error> {
186+
fn try_is_low(&self) -> Result<bool, Self::Error> {
187187
// NOTE(unsafe) atomic read with no side effects
188188
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0 })
189189
}
@@ -192,11 +192,11 @@ macro_rules! gpio {
192192
impl<MODE> InputPin for $PXx<Input<MODE>> {
193193
type Error = Infallible;
194194

195-
fn is_high(&self) -> Result<bool, Self::Error> {
196-
self.is_low().map(|v| !v)
195+
fn try_is_high(&self) -> Result<bool, Self::Error> {
196+
self.try_is_low().map(|v| !v)
197197
}
198198

199-
fn is_low(&self) -> Result<bool, Self::Error> {
199+
fn try_is_low(&self) -> Result<bool, Self::Error> {
200200
// NOTE(unsafe) atomic read with no side effects
201201
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0 })
202202
}
@@ -670,25 +670,25 @@ macro_rules! gpio {
670670
impl<MODE> OutputPin for $PXi<Output<MODE>> {
671671
type Error = Infallible;
672672

673-
fn set_high(&mut self) -> Result<(), Self::Error> {
673+
fn try_set_high(&mut self) -> Result<(), Self::Error> {
674674
// NOTE(unsafe) atomic write to a stateless register
675675
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << $i)) };
676676
Ok(())
677677
}
678678

679-
fn set_low(&mut self) -> Result<(), Self::Error> {
679+
fn try_set_low(&mut self) -> Result<(), Self::Error> {
680680
// NOTE(unsafe) atomic write to a stateless register
681681
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << ($i + 16))) };
682682
Ok(())
683683
}
684684
}
685685

686686
impl<MODE> StatefulOutputPin for $PXi<Output<MODE>> {
687-
fn is_set_high(&self) -> Result<bool, Self::Error> {
688-
self.is_set_low().map(|v| !v)
687+
fn try_is_set_high(&self) -> Result<bool, Self::Error> {
688+
self.try_is_set_low().map(|v| !v)
689689
}
690690

691-
fn is_set_low(&self) -> Result<bool, Self::Error> {
691+
fn try_is_set_low(&self) -> Result<bool, Self::Error> {
692692
// NOTE(unsafe) atomic read with no side effects
693693
Ok(unsafe { (*$GPIOX::ptr()).odr.read().bits() & (1 << $i) == 0 })
694694
}
@@ -699,11 +699,11 @@ macro_rules! gpio {
699699
impl<MODE> InputPin for $PXi<Output<MODE>> {
700700
type Error = Infallible;
701701

702-
fn is_high(&self) -> Result<bool, Self::Error> {
703-
self.is_low().map(|v| !v)
702+
fn try_is_high(&self) -> Result<bool, Self::Error> {
703+
self.try_is_low().map(|v| !v)
704704
}
705705

706-
fn is_low(&self) -> Result<bool, Self::Error> {
706+
fn try_is_low(&self) -> Result<bool, Self::Error> {
707707
// NOTE(unsafe) atomic read with no side effects
708708
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 })
709709
}
@@ -712,11 +712,11 @@ macro_rules! gpio {
712712
impl<MODE> InputPin for $PXi<Input<MODE>> {
713713
type Error = Infallible;
714714

715-
fn is_high(&self) -> Result<bool, Self::Error> {
716-
self.is_low().map(|v| !v)
715+
fn try_is_high(&self) -> Result<bool, Self::Error> {
716+
self.try_is_low().map(|v| !v)
717717
}
718718

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

src/i2c.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,14 @@ where
739739
{
740740
type Error = Error;
741741

742-
fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> {
743-
self.write(addr, bytes)?;
744-
self.read(addr, buffer)?;
742+
fn try_write_read(
743+
&mut self,
744+
addr: u8,
745+
bytes: &[u8],
746+
buffer: &mut [u8],
747+
) -> Result<(), Self::Error> {
748+
self.try_write(addr, bytes)?;
749+
self.try_read(addr, buffer)?;
745750

746751
Ok(())
747752
}
@@ -753,7 +758,7 @@ where
753758
{
754759
type Error = Error;
755760

756-
fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> {
761+
fn try_write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> {
757762
// Send a START condition
758763
self.i2c.cr1.modify(|_, w| w.start().set_bit());
759764

@@ -793,7 +798,7 @@ where
793798
{
794799
type Error = Error;
795800

796-
fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
801+
fn try_read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
797802
if let Some((last, buffer)) = buffer.split_last_mut() {
798803
// Send a START condition and set ACK bit
799804
self.i2c

src/prelude.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
pub use embedded_hal::digital::v2::InputPin as _embedded_hal_digital_v2_InputPin;
2-
pub use embedded_hal::digital::v2::OutputPin as _embedded_hal_digital_v2_OutputPin;
3-
pub use embedded_hal::digital::v2::StatefulOutputPin as _embedded_hal_digital_v2_StatefulOutputPin;
4-
pub use embedded_hal::digital::v2::ToggleableOutputPin as _embedded_hal_digital_v2_ToggleableOutputPin;
1+
pub use embedded_hal::digital::InputPin as _embedded_hal_digital_v2_InputPin;
2+
pub use embedded_hal::digital::OutputPin as _embedded_hal_digital_v2_OutputPin;
3+
pub use embedded_hal::digital::StatefulOutputPin as _embedded_hal_digital_v2_StatefulOutputPin;
4+
pub use embedded_hal::digital::ToggleableOutputPin as _embedded_hal_digital_v2_ToggleableOutputPin;
55
pub use embedded_hal::prelude::*;
66

77
pub use crate::gpio::GpioExt as _stm32f4xx_hal_gpio_GpioExt;

0 commit comments

Comments
 (0)