Skip to content

Commit

Permalink
Update to use try_ functions
Browse files Browse the repository at this point in the history
  • Loading branch information
maxekman committed Apr 30, 2020
1 parent f9fabf9 commit f94d109
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 219 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ default-features = false
version = "1.0.2"

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

[dev-dependencies]
panic-semihosting = "0.5.3"
Expand Down
8 changes: 4 additions & 4 deletions examples/delay-blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ fn main() -> ! {

loop {
// On for 1s, off for 1s.
led.set_high().unwrap();
delay.delay_ms(1000_u32);
led.set_low().unwrap();
delay.delay_ms(1000_u32);
led.try_set_high().unwrap();
delay.try_delay_ms(1000_u32).unwrap();
led.try_set_low().unwrap();
delay.try_delay_ms(1000_u32).unwrap();
}
}

Expand Down
6 changes: 3 additions & 3 deletions examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ fn main() -> ! {

let pwm = pwm::tim1(dp.TIM1, channels, clocks, 20u32.khz());
let (mut ch1, _ch2) = pwm;
let max_duty = ch1.get_max_duty();
ch1.set_duty(max_duty / 2);
ch1.enable();
let max_duty = ch1.try_get_max_duty().unwrap();
ch1.try_set_duty(max_duty / 2);
ch1.try_enable();
}

loop {
Expand Down
2 changes: 1 addition & 1 deletion examples/rng-display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn main() -> ! {
}
disp.flush().unwrap();
//delay a little while between refreshes so the display is readable
delay_source.delay_ms(100u8);
delay_source.try_delay_ms(100u8).unwrap();
}
}

Expand Down
18 changes: 9 additions & 9 deletions examples/timer-syst.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Start and stop a periodic system timer.
//! Start and stop a periodic system timer.try_
//!
//! This example should run on all stm32f4xx boards but it was tested with
//! stm32f4-discovery board (model STM32F407G-DISC1).
Expand Down Expand Up @@ -36,29 +36,29 @@ fn main() -> ! {

hprintln!("hello!").unwrap();
// wait until timer expires
nb::block!(timer.wait()).unwrap();
nb::block!(timer.try_wait()).unwrap();
hprintln!("timer expired 1").unwrap();

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

// cancel current timer
timer.cancel().unwrap();
timer.try_cancel().unwrap();

// start it again
timer.start(24.hz());
nb::block!(timer.wait()).unwrap();
timer.try_start(24.hz());
nb::block!(timer.try_wait()).unwrap();
hprintln!("timer expired 3").unwrap();

timer.cancel().unwrap();
let cancel_outcome = timer.cancel();
timer.try_cancel().unwrap();
let cancel_outcome = timer.try_cancel();
assert_eq!(cancel_outcome, Err(timer::Error::Disabled));
hprintln!("ehy, you cannot cancel a timer two times!").unwrap();
// this time the timer was not restarted, therefore this function should
// wait forever
nb::block!(timer.wait()).unwrap();
nb::block!(timer.try_wait()).unwrap();
// you should never see this print
hprintln!("if you see this there is something wrong").unwrap();
panic!();
Expand Down
8 changes: 4 additions & 4 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ macro_rules! adc_pins {
$(
impl Channel<stm32::$adc> for $pin {
type ID = u8;
fn channel() -> u8 { $chan }
const CHANNEL: Self::ID = $chan;
}
)+
};
Expand Down Expand Up @@ -668,7 +668,7 @@ macro_rules! adc {
}

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

self.calibrated_vdda = (VDDA_CALIB * u32::from(vref_cal)) / u32::from(vref_samp);
if !vref_en {
Expand Down Expand Up @@ -876,7 +876,7 @@ macro_rules! adc {
}
});

let channel = CHANNEL::channel();
let channel = CHANNEL::CHANNEL;

//Set the channel in the right sequence field
match sequence {
Expand Down Expand Up @@ -979,7 +979,7 @@ macro_rules! adc {
{
type Error = ();

fn read(&mut self, pin: &mut PIN) -> nb::Result<u16, Self::Error> {
fn try_read(&mut self, pin: &mut PIN) -> nb::Result<u16, Self::Error> {
let enabled = self.is_enabled();
if !enabled {
self.enable();
Expand Down
38 changes: 27 additions & 11 deletions src/delay.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Delays

use cast::u32;
use core::convert::Infallible;

use cortex_m::peripheral::syst::SystClkSource;
use cortex_m::peripheral::SYST;

Expand Down Expand Up @@ -28,25 +30,33 @@ impl Delay {
}

impl DelayMs<u32> for Delay {
fn delay_ms(&mut self, ms: u32) {
self.delay_us(ms * 1_000);
type Error = Infallible;

fn try_delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
self.try_delay_us(ms * 1_000)
}
}

impl DelayMs<u16> for Delay {
fn delay_ms(&mut self, ms: u16) {
self.delay_ms(u32(ms));
type Error = Infallible;

fn try_delay_ms(&mut self, ms: u16) -> Result<(), Self::Error> {
self.try_delay_ms(u32(ms))
}
}

impl DelayMs<u8> for Delay {
fn delay_ms(&mut self, ms: u8) {
self.delay_ms(u32(ms));
type Error = Infallible;

fn try_delay_ms(&mut self, ms: u8) -> Result<(), Self::Error> {
self.try_delay_ms(u32(ms))
}
}

impl DelayUs<u32> for Delay {
fn delay_us(&mut self, us: u32) {
type Error = Infallible;

fn try_delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
// The SysTick Reload Value register supports values between 1 and 0x00FFFFFF.
const MAX_RVR: u32 = 0x00FF_FFFF;

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

self.syst.disable_counter();
}

Ok(())
}
}

impl DelayUs<u16> for Delay {
fn delay_us(&mut self, us: u16) {
self.delay_us(u32(us))
type Error = Infallible;

fn try_delay_us(&mut self, us: u16) -> Result<(), Self::Error> {
self.try_delay_us(u32(us))
}
}

impl DelayUs<u8> for Delay {
fn delay_us(&mut self, us: u8) {
self.delay_us(u32(us))
type Error = Infallible;

fn try_delay_us(&mut self, us: u8) -> Result<(), Self::Error> {
self.try_delay_us(u32(us))
}
}
46 changes: 23 additions & 23 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ macro_rules! gpio {
use core::marker::PhantomData;
use core::convert::Infallible;

use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, toggleable};
use embedded_hal::digital::{InputPin, OutputPin, StatefulOutputPin, toggleable};
use crate::stm32::$GPIOX;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

fn is_low(&self) -> Result<bool, Self::Error> {
fn try_is_low(&self) -> Result<bool, Self::Error> {
// NOTE(unsafe) atomic read with no side effects
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 })
}
Expand Down
15 changes: 10 additions & 5 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,14 @@ where
{
type Error = Error;

fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> {
self.write(addr, bytes)?;
self.read(addr, buffer)?;
fn try_write_read(
&mut self,
addr: u8,
bytes: &[u8],
buffer: &mut [u8],
) -> Result<(), Self::Error> {
self.try_write(addr, bytes)?;
self.try_read(addr, buffer)?;

Ok(())
}
Expand All @@ -753,7 +758,7 @@ where
{
type Error = Error;

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

Expand Down Expand Up @@ -793,7 +798,7 @@ where
{
type Error = Error;

fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
fn try_read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
if let Some((last, buffer)) = buffer.split_last_mut() {
// Send a START condition and set ACK bit
self.i2c
Expand Down
8 changes: 4 additions & 4 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub use embedded_hal::digital::v2::InputPin as _embedded_hal_digital_v2_InputPin;
pub use embedded_hal::digital::v2::OutputPin as _embedded_hal_digital_v2_OutputPin;
pub use embedded_hal::digital::v2::StatefulOutputPin as _embedded_hal_digital_v2_StatefulOutputPin;
pub use embedded_hal::digital::v2::ToggleableOutputPin as _embedded_hal_digital_v2_ToggleableOutputPin;
pub use embedded_hal::digital::InputPin as _embedded_hal_digital_v2_InputPin;
pub use embedded_hal::digital::OutputPin as _embedded_hal_digital_v2_OutputPin;
pub use embedded_hal::digital::StatefulOutputPin as _embedded_hal_digital_v2_StatefulOutputPin;
pub use embedded_hal::digital::ToggleableOutputPin as _embedded_hal_digital_v2_ToggleableOutputPin;
pub use embedded_hal::prelude::*;

pub use crate::gpio::GpioExt as _stm32f4xx_hal_gpio_GpioExt;
Expand Down
Loading

0 comments on commit f94d109

Please sign in to comment.