From 40ffd3f189585e9ac9555402819a47820b4f2574 Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Wed, 26 Aug 2020 02:29:00 +0200 Subject: [PATCH] Crude translation for embedded-hal 1.0.0-alpha.1 Signed-off-by: Daniel Egger --- Cargo.toml | 4 +- examples/blinky.rs | 4 +- examples/blinky_adc.rs | 6 +-- examples/blinky_delay.rs | 4 +- examples/blinky_multiple.rs | 10 ++-- examples/blinky_timer.rs | 4 +- examples/flash_systick.rs | 4 +- examples/flash_systick_fancier.rs | 4 +- examples/i2c_find_address.rs | 2 +- examples/led_hal_button_irq.rs | 8 +-- examples/serial_echo.rs | 4 +- examples/serial_spi_bridge.rs | 8 +-- examples/spi_hal_apa102c.rs | 20 ++++---- examples/watchdog.rs | 16 +++--- src/adc.rs | 17 +++---- src/delay.rs | 39 ++++++++++---- src/gpio.rs | 84 +++++++++++++++---------------- src/i2c.rs | 6 +-- src/prelude.rs | 8 +-- src/serial.rs | 19 ++++--- src/spi.rs | 8 +-- src/timers.rs | 20 +++++--- src/watchdog.rs | 14 +++++- 23 files changed, 172 insertions(+), 141 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e9151b5..f6a8620 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,10 +30,10 @@ features = ["stm32f042", "rt", "stm32-usbd"] targets = ["thumbv6m-none-eabi"] [dependencies] -bare-metal = { version = "0.2", features = ["const-fn"] } +bare-metal = { version = "0.2" } cast = { version = "0.2", default-features = false } cortex-m = "0.6" -embedded-hal = { version = "0.2", features = ["unproven"] } +embedded-hal = { version = "1.0.0-alpha.1" } stm32f0 = "0.11" nb = "0.1" void = { version = "1.0", default-features = false } diff --git a/examples/blinky.rs b/examples/blinky.rs index df1c90c..bf1c898 100644 --- a/examples/blinky.rs +++ b/examples/blinky.rs @@ -22,11 +22,11 @@ fn main() -> ! { loop { // Turn PA1 on a million times in a row for _ in 0..1_000_000 { - led.set_high().ok(); + led.try_set_high().ok(); } // Then turn PA1 off a million times in a row for _ in 0..1_000_000 { - led.set_low().ok(); + led.try_set_low().ok(); } } } diff --git a/examples/blinky_adc.rs b/examples/blinky_adc.rs index c74c2c3..03251cf 100644 --- a/examples/blinky_adc.rs +++ b/examples/blinky_adc.rs @@ -33,9 +33,9 @@ fn main() -> ! { let mut adc = Adc::new(p.ADC, &mut rcc); loop { - led.toggle().ok(); + led.try_toggle().ok(); - let time: u16 = if let Ok(val) = adc.read(&mut an_in) as Result { + let time: u16 = if let Ok(val) = adc.try_read(&mut an_in) as Result { /* shift the value right by 3, same as divide by 8, reduces the 0-4095 range into something approximating 1-512 */ (val >> 3) + 1 @@ -43,7 +43,7 @@ fn main() -> ! { 1000 }; - delay.delay_ms(time); + delay.try_delay_ms(time).ok(); } } diff --git a/examples/blinky_delay.rs b/examples/blinky_delay.rs index 22fdd52..1e5ae59 100644 --- a/examples/blinky_delay.rs +++ b/examples/blinky_delay.rs @@ -24,8 +24,8 @@ fn main() -> ! { let mut delay = Delay::new(cp.SYST, &rcc); loop { - led.toggle().ok(); - delay.delay_ms(1_000_u16); + led.try_toggle().ok(); + delay.try_delay_ms(1_000_u16).ok(); } } diff --git a/examples/blinky_multiple.rs b/examples/blinky_multiple.rs index a4939ea..c889b18 100644 --- a/examples/blinky_multiple.rs +++ b/examples/blinky_multiple.rs @@ -13,7 +13,7 @@ use cortex_m_rt::entry; #[entry] fn main() -> ! { if let (Some(mut p), Some(cp)) = (pac::Peripherals::take(), Peripherals::take()) { - let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH); + let mut rcc = p.RCC.configure().sysclk(8_u32.mhz()).freeze(&mut p.FLASH); let gpioa = p.GPIOA.split(&mut rcc); let gpiob = p.GPIOB.split(&mut rcc); @@ -34,14 +34,14 @@ fn main() -> ! { let mut leds = [led1.downgrade(), led2.downgrade()]; loop { for l in &mut leds { - l.set_high().ok(); + l.try_set_high().ok(); } - delay.delay_ms(1_000_u16); + delay.try_delay_ms(1_000_u16).ok(); for l in &mut leds { - l.set_low().ok(); + l.try_set_low().ok(); } - delay.delay_ms(1_000_u16); + delay.try_delay_ms(1_000_u16).ok(); } } diff --git a/examples/blinky_timer.rs b/examples/blinky_timer.rs index 994dcc0..27a4ece 100644 --- a/examples/blinky_timer.rs +++ b/examples/blinky_timer.rs @@ -23,10 +23,10 @@ fn main() -> ! { let mut timer = Timer::tim1(p.TIM1, Hertz(1), &mut rcc); loop { - led.toggle().ok(); + led.try_toggle().ok(); // Wait for the timer to expire - nb::block!(timer.wait()).ok(); + nb::block!(timer.try_wait()).ok(); } } diff --git a/examples/flash_systick.rs b/examples/flash_systick.rs index 05589de..3d1818b 100644 --- a/examples/flash_systick.rs +++ b/examples/flash_systick.rs @@ -68,13 +68,13 @@ fn SysTick() { // Check state variable, keep LED off most of the time and turn it on every 10th tick if *STATE < 10 { // Turn off the LED - led.set_low().ok(); + led.try_set_low().ok(); // And now increment state variable *STATE += 1; } else { // Turn on the LED - led.set_high().ok(); + led.try_set_high().ok(); // And set new state variable back to 0 *STATE = 0; diff --git a/examples/flash_systick_fancier.rs b/examples/flash_systick_fancier.rs index de4ca2e..b9ce8a3 100644 --- a/examples/flash_systick_fancier.rs +++ b/examples/flash_systick_fancier.rs @@ -73,13 +73,13 @@ fn SysTick() { // Check state variable, keep LED off most of the time and turn it on every 10th tick if *STATE < 10 { // Turn off the LED - led.set_low().ok(); + led.try_set_low().ok(); // And now increment state variable *STATE += 1; } else { // Turn on the LED - led.set_high().ok(); + led.try_set_high().ok(); // And set new state variable back to 0 *STATE = 0; diff --git a/examples/i2c_find_address.rs b/examples/i2c_find_address.rs index 4ca8454..a05aa30 100644 --- a/examples/i2c_find_address.rs +++ b/examples/i2c_find_address.rs @@ -33,7 +33,7 @@ fn main() -> ! { // The write method sends the specified address and checks for acknowledgement; // if no ack is given by the slave device the result is Err(), otherwise Ok() // Since we only care for an acknowledgement the data sent can be empty - if i2c.write(add, &[]).is_ok() { + if i2c.try_write(add, &[]).is_ok() { _devices += 1; } } diff --git a/examples/led_hal_button_irq.rs b/examples/led_hal_button_irq.rs index a635809..e5ea9c6 100644 --- a/examples/led_hal_button_irq.rs +++ b/examples/led_hal_button_irq.rs @@ -49,7 +49,7 @@ fn main() -> ! { let mut led = gpioa.pa1.into_push_pull_output(cs); // Turn off LED - led.set_low().ok(); + led.try_set_low().ok(); // Initialise delay provider let delay = Delay::new(cp.SYST, &rcc); @@ -96,13 +96,13 @@ fn EXTI0_1() { INT.borrow(cs).borrow_mut().deref_mut(), ) { // Turn on LED - led.set_high().ok(); + led.try_set_high().ok(); // Wait a second - delay.delay_ms(1_000_u16); + delay.try_delay_ms(1_000_u16).ok(); // Turn off LED - led.set_low().ok(); + led.try_set_low().ok(); // Clear event triggering the interrupt exti.pr.write(|w| w.pif1().set_bit()); diff --git a/examples/serial_echo.rs b/examples/serial_echo.rs index d0c70d3..5bb8731 100644 --- a/examples/serial_echo.rs +++ b/examples/serial_echo.rs @@ -28,10 +28,10 @@ fn main() -> ! { loop { // Wait for reception of a single byte - let received = nb::block!(serial.read()).unwrap(); + let received = nb::block!(serial.try_read()).unwrap(); // Send back previously received byte and wait for completion - nb::block!(serial.write(received)).ok(); + nb::block!(serial.try_write(received)).ok(); } } diff --git a/examples/serial_spi_bridge.rs b/examples/serial_spi_bridge.rs index b3bacd5..2515c16 100644 --- a/examples/serial_spi_bridge.rs +++ b/examples/serial_spi_bridge.rs @@ -57,10 +57,10 @@ fn main() -> ! { let mut data = [0]; loop { - let serial_received = block!(rx.read()).unwrap(); - spi.write(&[serial_received]).ok(); - let spi_received = spi.transfer(&mut data).unwrap(); - block!(tx.write(spi_received[0])).ok(); + let serial_received = block!(rx.try_read()).unwrap(); + spi.try_write(&[serial_received]).ok(); + let spi_received = spi.try_transfer(&mut data).unwrap(); + block!(tx.try_write(spi_received[0])).ok(); } } diff --git a/examples/spi_hal_apa102c.rs b/examples/spi_hal_apa102c.rs index f3c2007..7c41125 100644 --- a/examples/spi_hal_apa102c.rs +++ b/examples/spi_hal_apa102c.rs @@ -37,30 +37,30 @@ fn main() -> ! { }); // Configure SPI with 100kHz rate - let mut spi = Spi::spi1(p.SPI1, (sck, miso, mosi), MODE, 100_000.hz(), &mut rcc); + let mut spi = Spi::spi1(p.SPI1, (sck, miso, mosi), MODE, 100_000_u32.hz(), &mut rcc); // Cycle through colors on 16 chained APA102C LEDs loop { for r in 0..255 { - let _ = spi.write(&[0, 0, 0, 0]); + let _ = spi.try_write(&[0, 0, 0, 0]); for _i in 0..16 { - let _ = spi.write(&[0b1110_0001, 0, 0, r]); + let _ = spi.try_write(&[0b1110_0001, 0, 0, r]); } - let _ = spi.write(&[0xFF, 0xFF, 0xFF, 0xFF]); + let _ = spi.try_write(&[0xFF, 0xFF, 0xFF, 0xFF]); } for b in 0..255 { - let _ = spi.write(&[0, 0, 0, 0]); + let _ = spi.try_write(&[0, 0, 0, 0]); for _i in 0..16 { - let _ = spi.write(&[0b1110_0001, b, 0, 0]); + let _ = spi.try_write(&[0b1110_0001, b, 0, 0]); } - let _ = spi.write(&[0xFF, 0xFF, 0xFF, 0xFF]); + let _ = spi.try_write(&[0xFF, 0xFF, 0xFF, 0xFF]); } for g in 0..255 { - let _ = spi.write(&[0, 0, 0, 0]); + let _ = spi.try_write(&[0, 0, 0, 0]); for _i in 0..16 { - let _ = spi.write(&[0b1110_0001, 0, g, 0]); + let _ = spi.try_write(&[0b1110_0001, 0, g, 0]); } - let _ = spi.write(&[0xFF, 0xFF, 0xFF, 0xFF]); + let _ = spi.try_write(&[0xFF, 0xFF, 0xFF, 0xFF]); } } } diff --git a/examples/watchdog.rs b/examples/watchdog.rs index bcd265e..ac6d328 100644 --- a/examples/watchdog.rs +++ b/examples/watchdog.rs @@ -37,17 +37,17 @@ fn main() -> ! { serial.write_str("RESET \r\n").ok(); - watchdog.start(Hertz(1)); - delay.delay_ms(500_u16); - watchdog.feed(); - delay.delay_ms(500_u16); - watchdog.feed(); - delay.delay_ms(500_u16); + watchdog.try_start(Hertz(1)).ok(); + delay.try_delay_ms(500_u16).ok(); + watchdog.try_feed().ok(); + delay.try_delay_ms(500_u16).ok(); + watchdog.try_feed().ok(); + delay.try_delay_ms(500_u16).ok(); serial.write_str("This will get printed \r\n").ok(); - watchdog.feed(); + watchdog.try_feed().ok(); // Now a reset happens while delaying - delay.delay_ms(1500_u16); + delay.try_delay_ms(1500_u16).ok(); serial.write_str("This won't\r\n").ok(); } diff --git a/src/adc.rs b/src/adc.rs index 8b5c541..e963fd9 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -191,8 +191,7 @@ macro_rules! adc_pins { $( impl Channel for $pin { type ID = u8; - - fn channel() -> u8 { $chan } + const CHANNEL : u8 = $chan; } )+ }; @@ -295,7 +294,7 @@ impl VTemp { vtemp.enable(adc); if let Some(dref) = delay { - dref.delay_us(2_u16); + dref.try_delay_us(2_u16).ok(); } else { // Double read of vdda to allow sufficient startup time for the temp sensor VRef::read_vdda(adc); @@ -305,7 +304,7 @@ impl VTemp { let prev_cfg = adc.default_cfg(); - let vtemp_val = adc.read(&mut vtemp).unwrap(); + let vtemp_val = adc.try_read(&mut vtemp).unwrap(); if !vtemp_preenable { vtemp.disable(adc); @@ -346,11 +345,11 @@ impl VRef { let prev_cfg = adc.default_cfg(); let vref_val: u32 = if vref.is_enabled(&adc) { - adc.read(&mut vref).unwrap() + adc.try_read(&mut vref).unwrap() } else { vref.enable(adc); - let ret = adc.read(&mut vref).unwrap(); + let ret = adc.try_read(&mut vref).unwrap(); vref.disable(adc); ret @@ -535,7 +534,7 @@ impl Adc { /// Read the value of a channel and converts the result to milli-volts pub fn read_abs_mv>(&mut self, pin: &mut PIN) -> u16 { let vdda = u32::from(VRef::read_vdda(self)); - let v: u32 = self.read(pin).unwrap(); + let v: u32 = self.try_read(pin).unwrap(); let max_samp = u32::from(self.max_sample()); (v * vdda / max_samp) as u16 @@ -612,9 +611,9 @@ where { type Error = (); - fn read(&mut self, _pin: &mut PIN) -> nb::Result { + fn try_read(&mut self, _pin: &mut PIN) -> nb::Result { self.power_up(); - let res = self.convert(PIN::channel()); + let res = self.convert(PIN::CHANNEL); self.power_down(); Ok(res.into()) } diff --git a/src/delay.rs b/src/delay.rs index f7353a7..4c64ae0 100644 --- a/src/delay.rs +++ b/src/delay.rs @@ -26,6 +26,7 @@ //! ``` use cast::{u16, u32}; +use core::convert::Infallible; use cortex_m::peripheral::syst::SystClkSource; use cortex_m::peripheral::SYST; @@ -60,35 +61,45 @@ impl Delay { } impl DelayMs for Delay { + type Error = Infallible; + // At 48 MHz (the maximum frequency), calling delay_us with ms * 1_000 directly overflows at 0x15D86 (just over the max u16 value) // So we implement a separate, higher level, delay loop - fn delay_ms(&mut self, mut ms: u32) { + fn try_delay_ms(&mut self, mut ms: u32) -> Result<(), Self::Error> { const MAX_MS: u32 = 0x0000_FFFF; while ms != 0 { let current_ms = if ms <= MAX_MS { ms } else { MAX_MS }; - self.delay_us(current_ms * 1_000); + self.try_delay_us(current_ms * 1_000)?; ms -= current_ms; } + + Ok(()) } } impl DelayMs for Delay { - fn delay_ms(&mut self, ms: u16) { + type Error = Infallible; + + fn try_delay_ms(&mut self, ms: u16) -> Result<(), Self::Error> { // Call delay_us directly, so we don't have to use the additional // delay loop the u32 variant uses - self.delay_us(u32(ms) * 1_000); + self.try_delay_us(u32(ms) * 1_000) } } impl DelayMs for Delay { - fn delay_ms(&mut self, ms: u8) { - self.delay_ms(u16(ms)); + type Error = Infallible; + + fn try_delay_ms(&mut self, ms: u8) -> Result<(), Self::Error> { + self.try_delay_ms(u16(ms)) } } // At 48MHz (the maximum frequency), this overflows at approx. 2^32 / 48 = 89 seconds impl DelayUs 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. // Here less than maximum is used so we have some play if there's a long running interrupt. const MAX_TICKS: u32 = 0x007F_FFFF; @@ -109,17 +120,23 @@ impl DelayUs for Delay { // from 0 to 0xFFFF while (start_count.wrapping_sub(SYST::get_current()) % SYSTICK_RANGE) < current_ticks {} } + + Ok(()) } } impl DelayUs 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 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)) } } diff --git a/src/gpio.rs b/src/gpio.rs index 6bcd0d7..274e2ae 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -15,10 +15,10 @@ pub trait GpioExt { } trait GpioRegExt { - fn is_low(&self, pos: u8) -> bool; - fn is_set_low(&self, pos: u8) -> bool; - fn set_high(&self, pos: u8); - fn set_low(&self, pos: u8); + fn try_is_low(&self, pos: u8) -> bool; + fn try_is_set_low(&self, pos: u8) -> bool; + fn try_set_high(&self, pos: u8); + fn try_set_low(&self, pos: u8); } /// Alternate function 0 @@ -71,7 +71,7 @@ pub struct Output { /// Push pull output (type state) pub struct PushPull; -use embedded_hal::digital::v2::{toggleable, InputPin, OutputPin, StatefulOutputPin}; +use embedded_hal::digital::{toggleable, InputPin, OutputPin, StatefulOutputPin}; /// Fully erased pin pub struct Pin { @@ -88,13 +88,13 @@ unsafe impl Send for Pin {} impl StatefulOutputPin for Pin> { #[inline(always)] - fn is_set_high(&self) -> Result { - self.is_set_low().map(|v| !v) + fn try_is_set_high(&self) -> Result { + self.try_is_set_low().map(|v| !v) } #[inline(always)] - fn is_set_low(&self) -> Result { - Ok(unsafe { (*self.port).is_set_low(self.i) }) + fn try_is_set_low(&self) -> Result { + Ok(unsafe { (*self.port).try_is_set_low(self.i) }) } } @@ -102,14 +102,14 @@ impl OutputPin for Pin> { type Error = Infallible; #[inline(always)] - fn set_high(&mut self) -> Result<(), Self::Error> { - unsafe { (*self.port).set_high(self.i) }; + fn try_set_high(&mut self) -> Result<(), Self::Error> { + unsafe { (*self.port).try_set_high(self.i) }; Ok(()) } #[inline(always)] - fn set_low(&mut self) -> Result<(), Self::Error> { - unsafe { (*self.port).set_low(self.i) } + fn try_set_low(&mut self) -> Result<(), Self::Error> { + unsafe { (*self.port).try_set_low(self.i) } Ok(()) } } @@ -120,13 +120,13 @@ impl InputPin for Pin> { type Error = Infallible; #[inline(always)] - fn is_high(&self) -> Result { - self.is_low().map(|v| !v) + fn try_is_high(&self) -> Result { + self.try_is_low().map(|v| !v) } #[inline(always)] - fn is_low(&self) -> Result { - Ok(unsafe { (*self.port).is_low(self.i) }) + fn try_is_low(&self) -> Result { + Ok(unsafe { (*self.port).try_is_low(self.i) }) } } @@ -134,35 +134,35 @@ impl InputPin for Pin> { type Error = Infallible; #[inline(always)] - fn is_high(&self) -> Result { - self.is_low().map(|v| !v) + fn try_is_high(&self) -> Result { + self.try_is_low().map(|v| !v) } #[inline(always)] - fn is_low(&self) -> Result { - Ok(unsafe { (*self.port).is_low(self.i) }) + fn try_is_low(&self) -> Result { + Ok(unsafe { (*self.port).try_is_low(self.i) }) } } macro_rules! gpio_trait { ($gpiox:ident) => { impl GpioRegExt for crate::pac::$gpiox::RegisterBlock { - fn is_low(&self, pos: u8) -> bool { + fn try_is_low(&self, pos: u8) -> bool { // NOTE(unsafe) atomic read with no side effects self.idr.read().bits() & (1 << pos) == 0 } - fn is_set_low(&self, pos: u8) -> bool { + fn try_is_set_low(&self, pos: u8) -> bool { // NOTE(unsafe) atomic read with no side effects self.odr.read().bits() & (1 << pos) == 0 } - fn set_high(&self, pos: u8) { + fn try_set_high(&self, pos: u8) { // NOTE(unsafe) atomic write to a stateless register unsafe { self.bsrr.write(|w| w.bits(1 << pos)) } } - fn set_low(&self, pos: u8) { + fn try_set_low(&self, pos: u8) { // NOTE(unsafe) atomic write to a stateless register unsafe { self.bsrr.write(|w| w.bits(1 << (pos + 16))) } } @@ -184,7 +184,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::{ rcc::Rcc, pac::$GPIOX @@ -504,24 +504,24 @@ macro_rules! gpio { } impl StatefulOutputPin for $PXi> { - fn is_set_high(&self) -> Result { - self.is_set_low().map(|v| !v) + fn try_is_set_high(&self) -> Result { + self.try_is_set_low().map(|v| !v) } - fn is_set_low(&self) -> Result { - Ok(unsafe { (*$GPIOX::ptr()).is_set_low($i) }) + fn try_is_set_low(&self) -> Result { + Ok(unsafe { (*$GPIOX::ptr()).try_is_set_low($i) }) } } impl OutputPin for $PXi> { type Error = Infallible; - fn set_high(&mut self) -> Result<(), Self::Error> { - Ok(unsafe { (*$GPIOX::ptr()).set_high($i) }) + fn try_set_high(&mut self) -> Result<(), Self::Error> { + Ok(unsafe { (*$GPIOX::ptr()).try_set_high($i) }) } - fn set_low(&mut self) -> Result<(), Self::Error> { - Ok(unsafe { (*$GPIOX::ptr()).set_low($i) }) + fn try_set_low(&mut self) -> Result<(), Self::Error> { + Ok(unsafe { (*$GPIOX::ptr()).try_set_low($i) }) } } @@ -530,12 +530,12 @@ macro_rules! gpio { impl InputPin for $PXi> { type Error = Infallible; - fn is_high(&self) -> Result { - self.is_low().map(|v| !v) + fn try_is_high(&self) -> Result { + self.try_is_low().map(|v| !v) } - fn is_low(&self) -> Result { - Ok(unsafe { (*$GPIOX::ptr()).is_low($i) }) + fn try_is_low(&self) -> Result { + Ok(unsafe { (*$GPIOX::ptr()).try_is_low($i) }) } } @@ -556,12 +556,12 @@ macro_rules! gpio { impl InputPin for $PXi> { type Error = Infallible; - fn is_high(&self) -> Result { - self.is_low().map(|v| !v) + fn try_is_high(&self) -> Result { + self.try_is_low().map(|v| !v) } - fn is_low(&self) -> Result { - Ok(unsafe { (*$GPIOX::ptr()).is_low($i) }) + fn try_is_low(&self) -> Result { + Ok(unsafe { (*$GPIOX::ptr()).try_is_low($i) }) } } )+ diff --git a/src/i2c.rs b/src/i2c.rs index 55fa879..76e613c 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -321,7 +321,7 @@ where { type Error = Error; - fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> { + fn try_write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> { // Set up current slave address for writing and disable autoending self.i2c.cr2.modify(|_, w| { w.sadd() @@ -394,7 +394,7 @@ where { type Error = Error; - fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> { + fn try_read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> { // Set up current address for reading self.i2c.cr2.modify(|_, w| { w.sadd() @@ -429,7 +429,7 @@ where { type Error = Error; - fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> { + fn try_write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> { // Set up current slave address for writing and enable autoending self.i2c.cr2.modify(|_, w| { w.sadd() diff --git a/src/prelude.rs b/src/prelude.rs index 993fe41..04580ed 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -6,10 +6,10 @@ pub use embedded_hal::watchdog::WatchdogEnable as _stm32f0xx_hal_embedded_hal_wa pub use embedded_hal::adc::OneShot as _embedded_hal_adc_OneShot; -pub use embedded_hal::digital::v2::InputPin as _embedded_hal_gpio_InputPin; -pub use embedded_hal::digital::v2::OutputPin as _embedded_hal_gpio_OutputPin; -pub use embedded_hal::digital::v2::StatefulOutputPin as _embedded_hal_gpio_StatefulOutputPin; -pub use embedded_hal::digital::v2::ToggleableOutputPin as _embedded_hal_gpio_ToggleableOutputPin; +pub use embedded_hal::digital::InputPin as _embedded_hal_gpio_InputPin; +pub use embedded_hal::digital::OutputPin as _embedded_hal_gpio_OutputPin; +pub use embedded_hal::digital::StatefulOutputPin as _embedded_hal_gpio_StatefulOutputPin; +pub use embedded_hal::digital::ToggleableOutputPin as _embedded_hal_gpio_ToggleableOutputPin; pub use crate::gpio::GpioExt as _stm32f0xx_hal_gpio_GpioExt; pub use crate::rcc::RccExt as _stm32f0xx_hal_rcc_RccExt; diff --git a/src/serial.rs b/src/serial.rs index 5fe0541..1218801 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -64,8 +64,7 @@ use core::{ ops::Deref, }; -use embedded_hal::prelude::*; - +use crate::prelude::*; use crate::{gpio::*, rcc::Rcc, time::Bps}; use core::marker::PhantomData; @@ -430,7 +429,7 @@ where type Error = Error; /// Tries to read a byte from the uart - fn read(&mut self) -> nb::Result { + fn try_read(&mut self) -> nb::Result { read(self.usart) } } @@ -443,7 +442,7 @@ where type Error = Error; /// Tries to read a byte from the uart - fn read(&mut self) -> nb::Result { + fn try_read(&mut self) -> nb::Result { read(&*self.usart) } } @@ -455,13 +454,13 @@ where type Error = Infallible; /// Ensures that none of the previously written words are still buffered - fn flush(&mut self) -> nb::Result<(), Self::Error> { + fn try_flush(&mut self) -> nb::Result<(), Self::Error> { flush(self.usart) } /// Tries to write a byte to the uart /// Fails if the transmit buffer is full - fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> { + fn try_write(&mut self, byte: u8) -> nb::Result<(), Self::Error> { write(self.usart, byte) } } @@ -474,13 +473,13 @@ where type Error = Infallible; /// Ensures that none of the previously written words are still buffered - fn flush(&mut self) -> nb::Result<(), Self::Error> { + fn try_flush(&mut self) -> nb::Result<(), Self::Error> { flush(&*self.usart) } /// Tries to write a byte to the uart /// Fails if the transmit buffer is full - fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> { + fn try_write(&mut self, byte: u8) -> nb::Result<(), Self::Error> { write(&*self.usart, byte) } } @@ -520,7 +519,7 @@ where fn write_str(&mut self, s: &str) -> Result { s.as_bytes() .iter() - .try_for_each(|c| nb::block!(self.write(*c))) + .try_for_each(|c| nb::block!(self.try_write(*c))) .map_err(|_| core::fmt::Error) } } @@ -533,7 +532,7 @@ where fn write_str(&mut self, s: &str) -> Result { s.as_bytes() .iter() - .try_for_each(|c| nb::block!(self.write(*c))) + .try_for_each(|c| nb::block!(self.try_write(*c))) .map_err(|_| core::fmt::Error) } } diff --git a/src/spi.rs b/src/spi.rs index 8d31fd8..9fd84d0 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -439,7 +439,7 @@ where { type Error = Error; - fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { + fn try_transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { // We want to transfer bidirectionally, make sure we're in the correct mode self.set_bidi(); @@ -461,7 +461,7 @@ where { type Error = Error; - fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { + fn try_write(&mut self, words: &[u8]) -> Result<(), Self::Error> { let mut bufcap: u8 = 0; // We only want to send, so we don't need to worry about the receive buffer overflowing @@ -494,7 +494,7 @@ where { type Error = Error; - fn transfer<'w>(&mut self, words: &'w mut [u16]) -> Result<&'w [u16], Self::Error> { + fn try_transfer<'w>(&mut self, words: &'w mut [u16]) -> Result<&'w [u16], Self::Error> { // We want to transfer bidirectionally, make sure we're in the correct mode self.set_bidi(); @@ -516,7 +516,7 @@ where { type Error = Error; - fn write(&mut self, words: &[u16]) -> Result<(), Self::Error> { + fn try_write(&mut self, words: &[u16]) -> Result<(), Self::Error> { // We only want to send, so we don't need to worry about the receive buffer overflowing self.set_send_only(); diff --git a/src/timers.rs b/src/timers.rs index ecd243c..c5babe7 100644 --- a/src/timers.rs +++ b/src/timers.rs @@ -32,10 +32,10 @@ use cortex_m::peripheral::syst::SystClkSource; use cortex_m::peripheral::SYST; use crate::rcc::{Clocks, Rcc}; +use core::convert::Infallible; use crate::time::Hertz; use embedded_hal::timer::{CountDown, Periodic}; -use void::Void; /// Hardware timers pub struct Timer { @@ -60,7 +60,7 @@ impl Timer { tim: syst, clocks: rcc.clocks, }; - timer.start(timeout); + timer.try_start(timeout).ok(); timer } @@ -84,9 +84,10 @@ impl Timer { /// Be aware that intervals less than 4 Hertz may not function properly impl CountDown for Timer { type Time = Hertz; + type Error = Infallible; /// Start the timer with a `timeout` - fn start(&mut self, timeout: T) + fn try_start(&mut self, timeout: T) -> Result<(), Self::Error> where T: Into, { @@ -97,11 +98,13 @@ impl CountDown for Timer { self.tim.set_reload(rvr); self.tim.clear_current(); self.tim.enable_counter(); + + Ok(()) } /// Return `Ok` if the timer has wrapped /// Automatically clears the flag and restarts the time - fn wait(&mut self) -> nb::Result<(), Void> { + fn try_wait(&mut self) -> nb::Result<(), Self::Error> { if self.tim.has_wrapped() { Ok(()) } else { @@ -134,7 +137,7 @@ macro_rules! timers { clocks: rcc.clocks, tim, }; - timer.start(timeout); + timer.try_start(timeout).ok(); timer } @@ -172,9 +175,10 @@ macro_rules! timers { impl CountDown for Timer<$TIM> { type Time = Hertz; + type Error = Infallible; /// Start the timer with a `timeout` - fn start(&mut self, timeout: T) + fn try_start(&mut self, timeout: T) -> Result<(), Self::Error> where T: Into, { @@ -200,11 +204,13 @@ macro_rules! timers { // start counter self.tim.cr1.modify(|_, w| w.cen().set_bit()); + + Ok(()) } /// Return `Ok` if the timer has wrapped /// Automatically clears the flag and restarts the time - fn wait(&mut self) -> nb::Result<(), Void> { + fn try_wait(&mut self) -> nb::Result<(), Self::Error> { if self.tim.sr.read().uif().bit_is_clear() { Err(nb::Error::WouldBlock) } else { diff --git a/src/watchdog.rs b/src/watchdog.rs index a5c6241..c9c275b 100644 --- a/src/watchdog.rs +++ b/src/watchdog.rs @@ -46,16 +46,22 @@ use embedded_hal::watchdog; use crate::pac::IWDG; use crate::time::Hertz; +use core::convert::Infallible; + /// Watchdog instance pub struct Watchdog { iwdg: IWDG, } impl watchdog::Watchdog for Watchdog { + type Error = Infallible; + /// Feed the watchdog, so that at least one `period` goes by before the next /// reset - fn feed(&mut self) { + fn try_feed(&mut self) -> Result<(), Self::Error> { self.iwdg.kr.write(|w| w.key().reset()); + + Ok(()) } } @@ -97,7 +103,9 @@ impl Watchdog { impl watchdog::WatchdogEnable for Watchdog { type Time = IwdgTimeout; - fn start(&mut self, period: T) + type Error = Infallible; + + fn try_start(&mut self, period: T) -> Result<(), Self::Error> where T: Into, { @@ -117,5 +125,7 @@ impl watchdog::WatchdogEnable for Watchdog { // (potentially false) values while self.iwdg.sr.read().bits() != 0 {} self.iwdg.kr.write(|w| w.key().reset()); + + Ok(()) } }