Skip to content

Commit 207d67f

Browse files
committed
Update to use try_ functions
1 parent de1b149 commit 207d67f

File tree

12 files changed

+124
-78
lines changed

12 files changed

+124
-78
lines changed

Cargo.toml

Lines changed: 4 additions & 2 deletions
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"

src/adc.rs

Lines changed: 4 additions & 4 deletions
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

Lines changed: 27 additions & 11 deletions
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

Lines changed: 23 additions & 23 deletions
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

Lines changed: 10 additions & 5 deletions
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

Lines changed: 4 additions & 4 deletions
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;

src/qei.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! # Quadrature Encoder Interface
2-
use crate::hal::{self, Direction};
2+
use crate::hal::{self, qei::Direction};
33
use crate::stm32::RCC;
4+
use core::convert::Infallible;
45

56
use crate::gpio::gpioa::*;
67
#[cfg(any(
@@ -825,18 +826,19 @@ macro_rules! hal {
825826
}
826827
}
827828

828-
impl<PINS> hal::Qei for Qei<$TIM, PINS> {
829+
impl<PINS> hal::qei::Qei for Qei<$TIM, PINS> {
830+
type Error = Infallible;
829831
type Count = $bits;
830832

831-
fn count(&self) -> $bits {
832-
self.tim.cnt.read().bits() as $bits
833+
fn try_count(&self) -> Result<Self::Count, Self::Error> {
834+
Ok(self.tim.cnt.read().bits() as $bits)
833835
}
834836

835-
fn direction(&self) -> Direction {
837+
fn try_direction(&self) -> Result<Direction, Self::Error> {
836838
if self.tim.cr1.read().dir().bit_is_clear() {
837-
hal::Direction::Upcounting
839+
Ok(Direction::Upcounting)
838840
} else {
839-
hal::Direction::Downcounting
841+
Ok(Direction::Downcounting)
840842
}
841843
}
842844
}

src/rng.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl Rng {
9393
impl rng::Read for Rng {
9494
type Error = rand_core::Error;
9595

96-
fn read(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error> {
96+
fn try_read(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error> {
9797
self.try_fill_bytes(buffer)
9898
}
9999
}

0 commit comments

Comments
 (0)