diff --git a/Cargo.toml b/Cargo.toml index 722f28e8..78a0a798 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,23 +17,18 @@ default-target = "x86_64-unknown-linux-gnu" [dependencies] cortex-m = "0.6.0" -nb = "0.1.2" +nb = "1" cortex-m-rt = "0.6.8" stm32f1 = "0.11.0" embedded-dma = "0.1.2" bxcan = "0.4.0" -[dependencies.void] -default-features = false -version = "1.0.2" - [dependencies.cast] default-features = false version = "0.2.2" [dependencies.embedded-hal] -version = "0.2.3" -features = ["unproven"] +git = "https://github.com/rust-embedded/embedded-hal" [dependencies.stm32-usbd] version = "0.5.0" diff --git a/examples/blinky.rs b/examples/blinky.rs index 9fae270c..fe792e6f 100644 --- a/examples/blinky.rs +++ b/examples/blinky.rs @@ -14,7 +14,7 @@ use panic_halt as _; use nb::block; use cortex_m_rt::entry; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; use stm32f1xx_hal::{pac, prelude::*, timer::Timer}; #[entry] diff --git a/examples/blinky_generic.rs b/examples/blinky_generic.rs index 5809d4be..f7706501 100644 --- a/examples/blinky_generic.rs +++ b/examples/blinky_generic.rs @@ -9,7 +9,7 @@ use panic_halt as _; use nb::block; use cortex_m_rt::entry; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; use stm32f1xx_hal::{pac, prelude::*, timer::Timer}; #[entry] diff --git a/examples/blinky_rtc.rs b/examples/blinky_rtc.rs index 0cfd52d3..5bda34d8 100644 --- a/examples/blinky_rtc.rs +++ b/examples/blinky_rtc.rs @@ -15,7 +15,7 @@ use panic_halt as _; use stm32f1xx_hal::{pac, prelude::*, rtc::Rtc}; use cortex_m_rt::entry; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; use nb::block; #[entry] diff --git a/examples/blinky_rtcalarm_irq.rs b/examples/blinky_rtcalarm_irq.rs index 8a684459..c6f40f53 100644 --- a/examples/blinky_rtcalarm_irq.rs +++ b/examples/blinky_rtcalarm_irq.rs @@ -24,7 +24,7 @@ use crate::hal::{ use core::cell::RefCell; use cortex_m::{asm::wfi, interrupt::Mutex}; use cortex_m_rt::entry; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; // A type definition for the GPIO pin to be used for our LED type LEDPIN = gpioc::PC13>; diff --git a/examples/blinky_timer_irq.rs b/examples/blinky_timer_irq.rs index cc9bf57c..ee94d510 100644 --- a/examples/blinky_timer_irq.rs +++ b/examples/blinky_timer_irq.rs @@ -24,7 +24,7 @@ use crate::hal::{ use core::cell::RefCell; use cortex_m::{asm::wfi, interrupt::Mutex}; use cortex_m_rt::entry; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; // NOTE You can uncomment 'hprintln' here and in the code below for a bit more // verbosity at runtime, at the cost of throwing off the timing of the blink diff --git a/examples/can-loopback.rs b/examples/can-loopback.rs index fa4277d6..cda18ec8 100644 --- a/examples/can-loopback.rs +++ b/examples/can-loopback.rs @@ -11,7 +11,7 @@ use bxcan::{ use panic_halt as _; use cortex_m_rt::entry; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; use nb::block; use stm32f1xx_hal::{can::Can, pac, prelude::*}; diff --git a/examples/delay.rs b/examples/delay.rs index e95a50b8..2461fb70 100644 --- a/examples/delay.rs +++ b/examples/delay.rs @@ -7,7 +7,7 @@ use panic_halt as _; use cortex_m_rt::entry; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; use stm32f1xx_hal::{delay::Delay, pac, prelude::*}; #[entry] @@ -35,8 +35,8 @@ fn main() -> ! { loop { led.set_high().unwrap(); - delay.delay_ms(1_000_u16); + delay.delay_ms(1_000_u16).unwrap(); led.set_low().unwrap(); - delay.delay_ms(1_000_u16); + delay.delay_ms(1_000_u16).unwrap(); } } diff --git a/examples/dynamic_gpio.rs b/examples/dynamic_gpio.rs index 10e76fdf..3379632b 100644 --- a/examples/dynamic_gpio.rs +++ b/examples/dynamic_gpio.rs @@ -8,7 +8,7 @@ use nb::block; use cortex_m_rt::entry; use cortex_m_semihosting::hprintln; -use embedded_hal::digital::v2::{InputPin, OutputPin}; +use embedded_hal::digital::{InputPin, OutputPin}; use stm32f1xx_hal::{pac, prelude::*, timer::Timer}; #[entry] diff --git a/examples/led.rs b/examples/led.rs index 2b11679e..42fffeb0 100644 --- a/examples/led.rs +++ b/examples/led.rs @@ -16,7 +16,7 @@ use panic_halt as _; use cortex_m_rt::entry; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; use stm32f1xx_hal::{pac, prelude::*}; #[entry] diff --git a/examples/mfrc522.rs b/examples/mfrc522.rs.disabled similarity index 100% rename from examples/mfrc522.rs rename to examples/mfrc522.rs.disabled diff --git a/examples/multi_mode_gpio.rs b/examples/multi_mode_gpio.rs index 58bcd145..05ca38a2 100644 --- a/examples/multi_mode_gpio.rs +++ b/examples/multi_mode_gpio.rs @@ -8,7 +8,7 @@ use nb::block; use cortex_m_rt::entry; use cortex_m_semihosting::hprintln; -use embedded_hal::digital::v2::{InputPin, OutputPin}; +use embedded_hal::digital::{InputPin, OutputPin}; use stm32f1xx_hal::{gpio::State, pac, prelude::*, timer::Timer}; #[entry] diff --git a/examples/pwm.rs b/examples/pwm.rs index 16090885..6f046644 100644 --- a/examples/pwm.rs +++ b/examples/pwm.rs @@ -57,9 +57,9 @@ fn main() -> ! { ); // Enable clock on each of the channels - pwm.enable(Channel::C1); - pwm.enable(Channel::C2); - pwm.enable(Channel::C3); + pwm.enable(&Channel::C1); + pwm.enable(&Channel::C2); + pwm.enable(&Channel::C3); //// Operations affecting all defined channels on the Timer @@ -79,17 +79,17 @@ fn main() -> ! { //// the Pwm object or via dereferencing to the pin. // Use the Pwm object to set C3 to full strength - pwm.set_duty(Channel::C3, max); + pwm.set_duty(&Channel::C3, max); asm::bkpt(); // Use the Pwm object to set C3 to be dim - pwm.set_duty(Channel::C3, max / 4); + pwm.set_duty(&Channel::C3, max / 4); asm::bkpt(); // Use the Pwm object to set C3 to be zero - pwm.set_duty(Channel::C3, 0); + pwm.set_duty(&Channel::C3, 0); asm::bkpt(); diff --git a/examples/qei.rs b/examples/qei.rs index 8a4fce8a..b88ae654 100644 --- a/examples/qei.rs +++ b/examples/qei.rs @@ -47,7 +47,7 @@ fn main() -> ! { loop { let before = qei.count(); - delay.delay_ms(1_000_u16); + delay.delay_ms(1_000_u16).unwrap(); let after = qei.count(); let elapsed = after.wrapping_sub(before) as i16; diff --git a/examples/timer-interrupt-rtic.rs b/examples/timer-interrupt-rtic.rs index acabfcf8..6eb99836 100644 --- a/examples/timer-interrupt-rtic.rs +++ b/examples/timer-interrupt-rtic.rs @@ -13,7 +13,7 @@ use panic_halt as _; use rtic::app; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; use stm32f1xx_hal::{ gpio::{gpioc::PC13, Output, PushPull, State}, pac, diff --git a/examples/usb_serial.rs b/examples/usb_serial.rs index d4c53221..3e8461d2 100644 --- a/examples/usb_serial.rs +++ b/examples/usb_serial.rs @@ -13,9 +13,9 @@ extern crate panic_semihosting; use cortex_m::asm::delay; use cortex_m_rt::entry; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; use stm32f1xx_hal::usb::{Peripheral, UsbBus}; -use stm32f1xx_hal::{prelude::*, stm32}; +use stm32f1xx_hal::{prelude::*, pac}; use usb_device::prelude::*; use usbd_serial::{SerialPort, USB_CLASS_CDC}; diff --git a/examples/usb_serial_interrupt.rs b/examples/usb_serial_interrupt.rs index 54e61f0b..43322e77 100644 --- a/examples/usb_serial_interrupt.rs +++ b/examples/usb_serial_interrupt.rs @@ -7,10 +7,9 @@ extern crate panic_semihosting; use cortex_m::asm::{delay, wfi}; use cortex_m_rt::entry; -use embedded_hal::digital::v2::OutputPin; use stm32f1xx_hal::pac::{interrupt, Interrupt}; use stm32f1xx_hal::usb::{Peripheral, UsbBus, UsbBusType}; -use stm32f1xx_hal::{prelude::*, stm32}; +use stm32f1xx_hal::{prelude::*, pac}; use usb_device::{bus::UsbBusAllocator, prelude::*}; use usbd_serial::{SerialPort, USB_CLASS_CDC}; @@ -74,8 +73,8 @@ fn main() -> ! { let mut nvic = p.NVIC; - nvic.enable(Interrupt::USB_HP_CAN_TX); - nvic.enable(Interrupt::USB_LP_CAN_RX0); + nvic.request(Interrupt::USB_HP_CAN_TX); + nvic.request(Interrupt::USB_LP_CAN_RX0); loop { wfi(); diff --git a/examples/usb_serial_rtic.rs b/examples/usb_serial_rtic.rs index 5735b815..b2d9c97e 100644 --- a/examples/usb_serial_rtic.rs +++ b/examples/usb_serial_rtic.rs @@ -7,7 +7,7 @@ extern crate panic_semihosting; use cortex_m::asm::delay; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; use rtic::app; use stm32f1xx_hal::prelude::*; use stm32f1xx_hal::usb::{Peripheral, UsbBus, UsbBusType}; diff --git a/src/adc.rs b/src/adc.rs index a0f3384a..d171f9fa 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -108,7 +108,7 @@ macro_rules! adc_pins { impl Channel<$ADC> for $pin { type ID = u8; - fn channel() -> u8 { $chan } + fn channel(&self) -> u8 { $chan } } )+ }; @@ -426,8 +426,8 @@ macro_rules! adc_hal { { type Error = (); - fn read(&mut self, _pin: &mut PIN) -> nb::Result { - let res = self.convert(PIN::channel()); + fn read(&mut self, pin: &mut PIN) -> nb::Result { + let res = self.convert(pin.channel()); Ok(res.into()) } } @@ -616,10 +616,10 @@ impl Adc { { self.rb.cr1.modify(|_, w| w.discen().clear_bit()); self.rb.cr2.modify(|_, w| w.align().bit(self.align.into())); - self.set_channel_sample_time(PIN::channel(), self.sample_time); + self.set_channel_sample_time(pins.channel(), self.sample_time); self.rb .sqr3 - .modify(|_, w| unsafe { w.sq1().bits(PIN::channel()) }); + .modify(|_, w| unsafe { w.sq1().bits(pins.channel()) }); self.rb.cr2.modify(|_, w| w.dma().set_bit()); let payload = AdcPayload { diff --git a/src/delay.rs b/src/delay.rs index b01163de..d43e8138 100644 --- a/src/delay.rs +++ b/src/delay.rs @@ -26,27 +26,31 @@ impl Delay { self.syst } } - +use core::convert::Infallible; impl DelayMs for Delay { - fn delay_ms(&mut self, ms: u32) { - self.delay_us(ms * 1_000); + type Error = Infallible; + fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> { + self.delay_us(ms * 1_000) } } impl DelayMs for Delay { - fn delay_ms(&mut self, ms: u16) { - self.delay_ms(u32(ms)); + type Error = Infallible; + fn delay_ms(&mut self, ms: u16) -> Result<(), Self::Error> { + self.delay_ms(u32(ms)) } } impl DelayMs for Delay { - fn delay_ms(&mut self, ms: u8) { - self.delay_ms(u32(ms)); + type Error = Infallible; + fn delay_ms(&mut self, ms: u8) -> Result<(), Self::Error> { + self.delay_ms(u32(ms)) } } impl DelayUs for Delay { - fn delay_us(&mut self, us: u32) { + type Error = Infallible; + fn 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; @@ -70,17 +74,20 @@ impl DelayUs for Delay { self.syst.disable_counter(); } + Ok(()) } } impl DelayUs for Delay { - fn delay_us(&mut self, us: u16) { + type Error = Infallible; + fn delay_us(&mut self, us: u16) -> Result<(), Self::Error> { self.delay_us(u32(us)) } } impl DelayUs for Delay { - fn delay_us(&mut self, us: u8) { + type Error = Infallible; + fn delay_us(&mut self, us: u8) -> Result<(), Self::Error> { self.delay_us(u32(us)) } } diff --git a/src/gpio.rs b/src/gpio.rs index 918330eb..bf4f45c2 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -245,7 +245,7 @@ macro_rules! gpio { use core::convert::Infallible; use core::marker::PhantomData; - use crate::hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, toggleable}; + use crate::hal::digital::{InputPin, OutputPin, StatefulOutputPin, toggleable}; use crate::pac::{$gpioy, $GPIOX}; use crate::pac::EXTI; use crate::afio; @@ -1120,7 +1120,7 @@ macro_rules! gpio { macro_rules! impl_pxx { ($(($port:ident :: $pin:ident)),*) => { - use embedded_hal::digital::v2::{InputPin, StatefulOutputPin, OutputPin}; + use embedded_hal::digital::{InputPin, StatefulOutputPin, OutputPin}; use core::convert::Infallible; pub enum Pxx { diff --git a/src/prelude.rs b/src/prelude.rs index ff349473..607843bc 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -8,8 +8,8 @@ pub use crate::dma::WriteDma as _stm32_hal_dma_WriteDma; pub use crate::flash::FlashExt as _stm32_hal_flash_FlashExt; pub use crate::gpio::GpioExt as _stm32_hal_gpio_GpioExt; pub use crate::hal::adc::OneShot as _embedded_hal_adc_OneShot; -pub use crate::hal::digital::v2::StatefulOutputPin as _embedded_hal_digital_StatefulOutputPin; -pub use crate::hal::digital::v2::ToggleableOutputPin as _embedded_hal_digital_ToggleableOutputPin; +pub use crate::hal::digital::StatefulOutputPin as _embedded_hal_digital_StatefulOutputPin; +pub use crate::hal::digital::ToggleableOutputPin as _embedded_hal_digital_ToggleableOutputPin; pub use crate::hal::prelude::*; pub use crate::rcc::RccExt as _stm32_hal_rcc_RccExt; pub use crate::time::U32Ext as _stm32_hal_time_U32Ext; diff --git a/src/pwm.rs b/src/pwm.rs index 53529242..44c009f7 100644 --- a/src/pwm.rs +++ b/src/pwm.rs @@ -64,6 +64,7 @@ use crate::pac::TIM1; use crate::pac::TIM4; use crate::pac::{TIM2, TIM3}; use cast::{u16, u32}; +use core::convert::Infallible; use crate::afio::MAPR; use crate::bb; @@ -307,29 +308,25 @@ macro_rules! hal { } } - /* - The following implemention of the embedded_hal::Pwm uses Hertz as a time type. This was choosen - because of the timescales of operations being on the order of nanoseconds and not being able to - efficently represent a float on the hardware. It might be possible to change the time type to - a different time based using such as the nanosecond. The issue with doing so is that the max - delay would then be at just a little over 2 seconds because of the 32 bit depth of the number. - Using milliseconds is also an option, however, using this as a base unit means that only there - could be resolution issues when trying to get a specific value, because of the integer nature. - - To find a middle ground, the Hertz type is used as a base here and the Into trait has been - defined for several base time units. This will allow for calling the set_period method with - something that is natural to both the MCU and the end user. - */ - impl hal::Pwm for Pwm<$TIMX, REMAP, P, PINS> where - REMAP: Remap, - PINS: Pins, + /* + The following implemention of the embedded_hal::Pwm uses Hertz as a time type. This was choosen + because of the timescales of operations being on the order of nanoseconds and not being able to + efficently represent a float on the hardware. It might be possible to change the time type to + a different time based using such as the nanosecond. The issue with doing so is that the max + delay would then be at just a little over 2 seconds because of the 32 bit depth of the number. + Using milliseconds is also an option, however, using this as a base unit means that only there + could be resolution issues when trying to get a specific value, because of the integer nature. + + To find a middle ground, the Hertz type is used as a base here and the Into trait has been + defined for several base time units. This will allow for calling the set_period method with + something that is natural to both the MCU and the end user. + */ + impl Pwm<$TIMX, REMAP, P, PINS> where + REMAP: Remap, + PINS: Pins, { - type Channel = Channel; - type Duty = u16; - type Time = Hertz; - - fn enable(&mut self, channel: Self::Channel) { - match PINS::check_used(channel) { + pub fn enable(&mut self, channel: &Channel) { + match PINS::check_used(*channel) { Channel::C1 => unsafe { bb::set(&(*$TIMX::ptr()).ccer, 0) }, Channel::C2 => unsafe { bb::set(&(*$TIMX::ptr()).ccer, 4) }, Channel::C3 => unsafe { bb::set(&(*$TIMX::ptr()).ccer, 8) }, @@ -337,8 +334,8 @@ macro_rules! hal { } } - fn disable(&mut self, channel: Self::Channel) { - match PINS::check_used(channel) { + pub fn disable(&mut self, channel: &Channel) { + match PINS::check_used(*channel) { Channel::C1 => unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 0) }, Channel::C2 => unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 4) }, Channel::C3 => unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 8) }, @@ -346,8 +343,8 @@ macro_rules! hal { } } - fn get_duty(&self, channel: Self::Channel) -> Self::Duty { - match PINS::check_used(channel) { + pub fn get_duty(&self, channel: &Channel) -> u16 { + match PINS::check_used(*channel) { Channel::C1 => unsafe { (*$TIMX::ptr()).ccr1.read().ccr().bits() }, Channel::C2 => unsafe { (*$TIMX::ptr()).ccr2.read().ccr().bits() }, Channel::C3 => unsafe { (*$TIMX::ptr()).ccr3.read().ccr().bits() }, @@ -355,8 +352,8 @@ macro_rules! hal { } } - fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty) { - match PINS::check_used(channel) { + pub fn set_duty(&mut self, channel: &Channel, duty: u16) { + match PINS::check_used(*channel) { Channel::C1 => unsafe { (*$TIMX::ptr()).ccr1.write(|w| w.ccr().bits(duty)) }, Channel::C2 => unsafe { (*$TIMX::ptr()).ccr2.write(|w| w.ccr().bits(duty)) }, Channel::C3 => unsafe { (*$TIMX::ptr()).ccr3.write(|w| w.ccr().bits(duty)) }, @@ -364,11 +361,11 @@ macro_rules! hal { } } - fn get_max_duty(&self) -> Self::Duty { + pub fn get_max_duty(&self) -> u16 { unsafe { (*$TIMX::ptr()).arr.read().arr().bits() } } - fn get_period(&self) -> Self::Time { + pub fn get_period(&self) -> Hertz { let clk = self.clk; let psc: u16 = unsafe{(*$TIMX::ptr()).psc.read().psc().bits()}; let arr: u16 = unsafe{(*$TIMX::ptr()).arr.read().arr().bits()}; @@ -377,115 +374,192 @@ macro_rules! hal { (clk.0 / u32(psc * arr)).hz() } - fn set_period(&mut self, period: T) where - T: Into { - let clk = self.clk; + pub fn set_period(&mut self, period: T) + where + T: Into + { + let clk = self.clk; - let ticks = clk.0 / period.into().0; - let psc = u16(ticks / (1 << 16)).unwrap(); - let arr = u16(ticks / u32(psc + 1)).unwrap(); - unsafe { - (*$TIMX::ptr()).psc.write(|w| w.psc().bits(psc)); - (*$TIMX::ptr()).arr.write(|w| w.arr().bits(arr)); - } + let ticks = clk.0 / period.into().0; + let psc = u16(ticks / (1 << 16)).unwrap(); + let arr = u16(ticks / u32(psc + 1)).unwrap(); + unsafe { + (*$TIMX::ptr()).psc.write(|w| w.psc().bits(psc)); + (*$TIMX::ptr()).arr.write(|w| w.arr().bits(arr)); + } } } - - impl hal::PwmPin for PwmChannel<$TIMX, C1> { + impl hal::pwm::Pwm for Pwm<$TIMX, REMAP, P, PINS> where + REMAP: Remap, + PINS: Pins, + { + type Channel = Channel; type Duty = u16; + type Time = Hertz; + type Error = Infallible; + + fn enable(&mut self, channel: &Self::Channel) -> Result<(), Self::Error> { + self.enable(channel); + Ok(()) + } + + fn disable(&mut self, channel: &Self::Channel) -> Result<(), Self::Error> { + self.disable(channel); + Ok(()) + } + + fn get_duty(&self, channel: &Self::Channel) -> Result { + Ok(self.get_duty(channel)) + } + + fn set_duty(&mut self, channel: &Self::Channel, duty: Self::Duty) -> Result<(), Self::Error> { + self.set_duty(channel, duty); + Ok(()) + } + + fn get_max_duty(&self) -> Result { + Ok(self.get_max_duty()) + } - fn disable(&mut self) { + fn get_period(&self) -> Result { + Ok(self.get_period()) + } + + fn set_period(&mut self, period: T) -> Result<(), Self::Error> + where + T: Into + { + self.set_period(period); + Ok(()) + } + } + + impl PwmChannel<$TIMX, C1> { + pub fn disable(&mut self) { unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 0) } } - fn enable(&mut self) { + pub fn enable(&mut self) { unsafe { bb::set(&(*$TIMX::ptr()).ccer, 0) } } - fn get_duty(&self) -> u16 { + pub fn get_duty(&self) -> u16 { unsafe { (*$TIMX::ptr()).ccr1.read().ccr().bits() } } - fn get_max_duty(&self) -> u16 { + pub fn get_max_duty(&self) -> u16 { unsafe { (*$TIMX::ptr()).arr.read().arr().bits() } } - fn set_duty(&mut self, duty: u16) { + pub fn set_duty(&mut self, duty: u16) { unsafe { (*$TIMX::ptr()).ccr1.write(|w| w.ccr().bits(duty)) } } } - impl hal::PwmPin for PwmChannel<$TIMX, C2> { - type Duty = u16; - - fn disable(&mut self) { + impl PwmChannel<$TIMX, C2> { + pub fn disable(&mut self) { unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 4) } } - fn enable(&mut self) { + pub fn enable(&mut self) { unsafe { bb::set(&(*$TIMX::ptr()).ccer, 4) } } - fn get_duty(&self) -> u16 { + pub fn get_duty(&self) -> u16 { unsafe { (*$TIMX::ptr()).ccr2.read().ccr().bits() } } - fn get_max_duty(&self) -> u16 { + pub fn get_max_duty(&self) -> u16 { unsafe { (*$TIMX::ptr()).arr.read().arr().bits() } } - fn set_duty(&mut self, duty: u16) { + pub fn set_duty(&mut self, duty: u16) { unsafe { (*$TIMX::ptr()).ccr2.write(|w| w.ccr().bits(duty)) } } } - impl hal::PwmPin for PwmChannel<$TIMX, C3> { - type Duty = u16; - - fn disable(&mut self) { + impl PwmChannel<$TIMX, C3> { + pub fn disable(&mut self) { unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 8) } } - fn enable(&mut self) { + pub fn enable(&mut self) { unsafe { bb::set(&(*$TIMX::ptr()).ccer, 8) } } - fn get_duty(&self) -> u16 { + pub fn get_duty(&self) -> u16 { unsafe { (*$TIMX::ptr()).ccr3.read().ccr().bits() } } - fn get_max_duty(&self) -> u16 { + pub fn get_max_duty(&self) -> u16 { unsafe { (*$TIMX::ptr()).arr.read().arr().bits() } } - fn set_duty(&mut self, duty: u16) { + pub fn set_duty(&mut self, duty: u16) { unsafe { (*$TIMX::ptr()).ccr3.write(|w| w.ccr().bits(duty)) } + } } - impl hal::PwmPin for PwmChannel<$TIMX, C4> { - type Duty = u16; - - fn disable(&mut self) { + impl PwmChannel<$TIMX, C4> { + pub fn disable(&mut self) { unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 12) } } - fn enable(&mut self) { + pub fn enable(&mut self) { unsafe { bb::set(&(*$TIMX::ptr()).ccer, 12) } } - fn get_duty(&self) -> u16 { + pub fn get_duty(&self) -> u16 { unsafe { (*$TIMX::ptr()).ccr4.read().ccr().bits() } } - fn get_max_duty(&self) -> u16 { + pub fn get_max_duty(&self) -> u16 { unsafe { (*$TIMX::ptr()).arr.read().arr().bits() } } - fn set_duty(&mut self, duty: u16) { + pub fn set_duty(&mut self, duty: u16) { unsafe { (*$TIMX::ptr()).ccr4.write(|w| w.ccr().bits(duty)) } } } + + macro_rules! impl_pwmpin { + ( + $C:ident + ) => { + impl hal::pwm::PwmPin for PwmChannel<$TIMX, $C> { + type Duty = u16; + type Error = Infallible; + + fn disable(&mut self) -> Result<(), Self::Error> { + self.disable(); + Ok(()) + } + + fn enable(&mut self) -> Result<(), Self::Error> { + self.enable(); + Ok(()) + } + + fn get_duty(&self) -> Result { + Ok(self.get_duty()) + } + + fn get_max_duty(&self) -> Result { + Ok(self.get_max_duty()) + } + + fn set_duty(&mut self, duty: Self::Duty) -> Result<(), Self::Error> { + self.set_duty(duty); + Ok(()) + } + } + } + } + impl_pwmpin!(C1); + impl_pwmpin!(C2); + impl_pwmpin!(C3); + impl_pwmpin!(C4); )+ } } diff --git a/src/qei.rs b/src/qei.rs index 86ba49f3..edbee120 100644 --- a/src/qei.rs +++ b/src/qei.rs @@ -8,12 +8,13 @@ use core::u16; use core::marker::PhantomData; -use crate::hal::{self, Direction}; +use crate::hal::{self, qei::Direction}; #[cfg(any(feature = "stm32f100", feature = "stm32f103", feature = "connectivity",))] use crate::pac::TIM1; #[cfg(feature = "medium")] use crate::pac::TIM4; use crate::pac::{TIM2, TIM3}; +use core::convert::Infallible; use crate::afio::MAPR; @@ -179,22 +180,33 @@ macro_rules! hal { } } - impl hal::Qei for Qei<$TIMX, REMAP, PINS> { - type Count = u16; - - fn count(&self) -> u16 { + impl Qei<$TIMX, REMAP, PINS> { + pub fn count(&self) -> u16 { self.tim.cnt.read().cnt().bits() } - fn direction(&self) -> Direction { + pub fn direction(&self) -> Direction { if self.tim.cr1.read().dir().bit_is_clear() { - hal::Direction::Upcounting + Direction::Upcounting } else { - hal::Direction::Downcounting + Direction::Downcounting } } } + impl hal::qei::Qei for Qei<$TIMX, REMAP, PINS> { + type Count = u16; + type Error = Infallible; + + fn count(&self) -> Result { + Ok(self.count()) + } + + fn direction(&self) -> Result { + Ok(self.direction()) + } + } + )+ } } diff --git a/src/timer.rs b/src/timer.rs index f93b3dd2..d2f5bd27 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -73,7 +73,6 @@ use crate::rcc::{sealed::RccBus, Clocks, Enable, GetBusFreq, Reset, APB1}; use cast::{u16, u32, u64}; use cortex_m::peripheral::syst::SystClkSource; use cortex_m::peripheral::SYST; -use void::Void; use crate::time::Hertz; @@ -234,12 +233,8 @@ impl CountDownTimer { pub fn release(self) -> SYST { self.stop().release() } -} -impl CountDown for CountDownTimer { - type Time = Hertz; - - fn start(&mut self, timeout: T) + pub fn start(&mut self, timeout: T) where T: Into, { @@ -251,8 +246,21 @@ impl CountDown for CountDownTimer { self.tim.clear_current(); self.tim.enable_counter(); } +} + +impl CountDown for CountDownTimer { + type Time = Hertz; + type Error = Error; - fn wait(&mut self) -> nb::Result<(), Void> { + fn start(&mut self, timeout: T) -> Result<(), Self::Error> + where + T: Into, + { + self.start(timeout); + Ok(()) + } + + fn wait(&mut self) -> Result<(), nb::Error> { if self.tim.has_wrapped() { Ok(()) } else { @@ -262,8 +270,6 @@ impl CountDown for CountDownTimer { } impl Cancel for CountDownTimer { - type Error = Error; - fn cancel(&mut self) -> Result<(), Self::Error> { if !self.tim.is_counter_enabled() { return Err(Self::Error::Canceled); @@ -390,12 +396,8 @@ macro_rules! hal { self.tim.egr.write(|w| w.ug().set_bit()); self.tim.cr1.modify(|_, w| w.urs().clear_bit()); } - } - impl CountDown for CountDownTimer<$TIMX> { - type Time = Hertz; - - fn start(&mut self, timeout: T) + pub fn start(&mut self, timeout: T) where T: Into, { @@ -415,8 +417,21 @@ macro_rules! hal { // start counter self.tim.cr1.modify(|_, w| w.cen().set_bit()); } + } + + impl CountDown for CountDownTimer<$TIMX> { + type Time = Hertz; + type Error = Error; - fn wait(&mut self) -> nb::Result<(), Void> { + fn start(&mut self, timeout: T) -> Result<(), Self::Error> + where + T: Into, + { + self.start(timeout); + Ok(()) + } + + fn wait(&mut self) -> Result<(), nb::Error> { if self.tim.sr.read().uif().bit_is_clear() { Err(nb::Error::WouldBlock) } else { @@ -428,8 +443,6 @@ macro_rules! hal { impl Cancel for CountDownTimer<$TIMX> { - type Error = Error; - fn cancel(&mut self) -> Result<(), Self::Error> { let is_counter_enabled = self.tim.cr1.read().cen().is_enabled(); if !is_counter_enabled { diff --git a/src/usb.rs b/src/usb.rs index c5a784ee..90ebd5f6 100644 --- a/src/usb.rs +++ b/src/usb.rs @@ -26,7 +26,7 @@ unsafe impl UsbPeripheral for Peripheral { const EP_MEMORY_SIZE: usize = 512; fn enable() { - let rcc = unsafe { (&*RCC::ptr()) }; + let rcc = unsafe { &*RCC::ptr() }; cortex_m::interrupt::free(|_| { // Enable USB peripheral diff --git a/src/watchdog.rs b/src/watchdog.rs index f1bf3888..5e759fb1 100644 --- a/src/watchdog.rs +++ b/src/watchdog.rs @@ -1,7 +1,9 @@ //! Watchdog peripherals +use core::convert::Infallible; + use crate::{ - hal::watchdog::{Watchdog, WatchdogEnable}, + hal::watchdog::{Enable, Watchdog}, pac::{DBGMCU as DBG, IWDG}, time::MilliSeconds, }; @@ -88,18 +90,24 @@ impl IndependentWatchdog { } } -impl WatchdogEnable for IndependentWatchdog { +impl Enable for IndependentWatchdog { type Time = MilliSeconds; + type Error = Infallible; + type Target = Self; - fn start>(&mut self, period: T) { + fn start>(self, period: T) -> Result { self.setup(period.into().0); self.iwdg.kr.write(|w| unsafe { w.key().bits(KR_START) }); + Ok(self) } } impl Watchdog for IndependentWatchdog { - fn feed(&mut self) { + type Error = Infallible; + + fn feed(&mut self) -> Result<(), Self::Error> { self.iwdg.kr.write(|w| unsafe { w.key().bits(KR_RELOAD) }); + Ok(()) } }