From 375c640b93d07a315425a8b40348fd898fe869ea Mon Sep 17 00:00:00 2001 From: Justin Beaurivage Date: Mon, 29 Apr 2024 16:14:10 -0400 Subject: [PATCH] Fix T1 examples (again) --- boards/feather_m0/examples/pwm.rs | 13 ++-- boards/feather_m0/examples/timers.rs | 4 +- .../feather_m4/examples/neopixel_rainbow.rs | 5 +- boards/feather_m4/examples/serial.rs | 7 ++- boards/feather_m4/examples/timers.rs | 4 +- boards/feather_m4/examples/uart.rs | 4 +- boards/feather_m4/examples/uart_poll_echo.rs | 10 ++- boards/metro_m4/Cargo.toml | 2 + boards/metro_m4/examples/neopixel_blink.rs | 7 ++- boards/metro_m4/examples/neopixel_rainbow.rs | 5 +- boards/metro_m4/examples/serial.rs | 7 ++- boards/metro_m4/examples/spi.rs | 9 ++- boards/metro_m4/examples/timer.rs | 4 +- boards/samd11_bare/examples/serial.rs | 7 ++- boards/samd11_bare/examples/timer.rs | 4 +- hal/src/delay.rs | 62 ++++++++++++------- hal/src/lib.rs | 2 + hal/src/prelude.rs | 4 +- 18 files changed, 106 insertions(+), 54 deletions(-) diff --git a/boards/feather_m0/examples/pwm.rs b/boards/feather_m0/examples/pwm.rs index 8deb883a76bd..3dc284a79f9f 100644 --- a/boards/feather_m0/examples/pwm.rs +++ b/boards/feather_m0/examples/pwm.rs @@ -15,6 +15,7 @@ use feather_m0 as bsp; use bsp::pin_alias; use hal::clock::GenericClockController; use hal::delay::Delay; +use hal::ehal::{delay::DelayNs, pwm::SetDutyCycle}; use hal::fugit::RateExtU32; use hal::pwm::Pwm3; use pac::{CorePeripherals, Peripherals}; @@ -42,12 +43,14 @@ fn main() -> ! { peripherals.TC3, &mut peripherals.PM, ); - let max_duty = pwm3.get_max_duty(); + let max_duty = pwm3.max_duty_cycle(); loop { - pwm3.set_duty(max_duty / 2); - delay.delay_ms(1000u16); - pwm3.set_duty(max_duty / 8); - delay.delay_ms(1000u16); + // The embedded-hal spec requires that set_duty_cycle returns a Result. + // In our case, the function is infaillible so we can safely ignore the result. + let _ = pwm3.set_duty_cycle(max_duty / 2); + delay.delay_ms(1000); + let _ = pwm3.set_duty_cycle(max_duty / 8); + delay.delay_ms(1000); } } diff --git a/boards/feather_m0/examples/timers.rs b/boards/feather_m0/examples/timers.rs index 9e7907e6aa5e..5618649e3fbf 100644 --- a/boards/feather_m0/examples/timers.rs +++ b/boards/feather_m0/examples/timers.rs @@ -12,9 +12,11 @@ use feather_m0 as bsp; use bsp::{entry, pin_alias}; use hal::clock::GenericClockController; -use hal::prelude::*; +use hal::ehal::digital::OutputPin; +use hal::nb; use hal::time::Hertz; use hal::timer::TimerCounter; +use hal::timer_traits::InterruptDrivenTimer; use pac::Peripherals; #[entry] diff --git a/boards/feather_m4/examples/neopixel_rainbow.rs b/boards/feather_m4/examples/neopixel_rainbow.rs index 5bd27f58d621..2dbfe83187ac 100644 --- a/boards/feather_m4/examples/neopixel_rainbow.rs +++ b/boards/feather_m4/examples/neopixel_rainbow.rs @@ -19,10 +19,11 @@ use panic_semihosting as _; use bsp::entry; use hal::clock::GenericClockController; use hal::delay::Delay; +use hal::ehal::delay::DelayNs; use hal::pac::{CorePeripherals, Peripherals}; -use hal::prelude::*; use hal::time::Hertz; use hal::timer::*; +use hal::timer_traits::InterruptDrivenTimer; use smart_leds::{ hsv::{hsv2rgb, Hsv}, @@ -62,7 +63,7 @@ fn main() -> ! { val: 2, })]; neopixel.write(colors.iter().cloned()).unwrap(); - delay.delay_ms(5u8); + delay.delay_ms(5); } } } diff --git a/boards/feather_m4/examples/serial.rs b/boards/feather_m4/examples/serial.rs index 6f84c465c8e5..5d54cbdfeebe 100644 --- a/boards/feather_m4/examples/serial.rs +++ b/boards/feather_m4/examples/serial.rs @@ -12,10 +12,13 @@ use panic_semihosting as _; use bsp::{entry, periph_alias, pin_alias}; use hal::clock::GenericClockController; use hal::delay::Delay; +use hal::ehal::delay::DelayNs; +use hal::embedded_hal_nb::serial::Write; +use hal::fugit::RateExtU32; +use hal::nb; use hal::pac::gclk::genctrl::SRCSELECT_A; use hal::pac::gclk::pchctrl::GENSELECT_A; use hal::pac::{CorePeripherals, Peripherals}; -use hal::prelude::*; #[entry] fn main() -> ! { @@ -49,6 +52,6 @@ fn main() -> ! { for byte in b"Hello, world!" { nb::block!(uart.write(*byte)).unwrap(); } - delay.delay_ms(1000u16); + delay.delay_ms(1000); } } diff --git a/boards/feather_m4/examples/timers.rs b/boards/feather_m4/examples/timers.rs index 84e0851af524..abdceed60dbf 100644 --- a/boards/feather_m4/examples/timers.rs +++ b/boards/feather_m4/examples/timers.rs @@ -13,9 +13,11 @@ use panic_semihosting as _; use bsp::entry; use hal::clock::GenericClockController; +use hal::ehal::digital::OutputPin; +use hal::nb; use hal::pac::Peripherals; -use hal::prelude::*; use hal::time::Hertz; +use hal::timer_traits::InterruptDrivenTimer; use hal::timer::TimerCounter; diff --git a/boards/feather_m4/examples/uart.rs b/boards/feather_m4/examples/uart.rs index eed5be270ea8..a2fe007febe2 100644 --- a/boards/feather_m4/examples/uart.rs +++ b/boards/feather_m4/examples/uart.rs @@ -13,7 +13,9 @@ use feather_m4 as bsp; use bsp::{entry, periph_alias, pin_alias}; use hal::clock::GenericClockController; use hal::dmac::{DmaController, PriorityLevel}; -use hal::prelude::*; +use hal::embedded_hal_nb::serial::{Read, Write}; +use hal::fugit::RateExtU32; +use hal::nb; use pac::Peripherals; diff --git a/boards/feather_m4/examples/uart_poll_echo.rs b/boards/feather_m4/examples/uart_poll_echo.rs index 5d923087573c..bb467e1ee365 100644 --- a/boards/feather_m4/examples/uart_poll_echo.rs +++ b/boards/feather_m4/examples/uart_poll_echo.rs @@ -19,10 +19,14 @@ use panic_semihosting as _; use bsp::{entry, periph_alias, pin_alias}; use hal::clock::GenericClockController; use hal::delay::Delay; +use hal::ehal::delay::DelayNs; +use hal::ehal::digital::OutputPin; +use hal::embedded_hal_nb::serial::{Read, Write}; +use hal::fugit::RateExtU32; +use hal::nb; use hal::pac::gclk::genctrl::SRCSELECT_A; use hal::pac::gclk::pchctrl::GENSELECT_A; use hal::pac::{CorePeripherals, Peripherals}; -use hal::prelude::*; #[entry] fn main() -> ! { @@ -67,10 +71,10 @@ fn main() -> ! { // Blink the red led to show that a character has arrived red_led.set_high().unwrap(); - delay.delay_ms(2u16); + delay.delay_ms(2); red_led.set_low().unwrap(); } - Err(_) => delay.delay_ms(5u16), + Err(_) => delay.delay_ms(5), }; } } diff --git a/boards/metro_m4/Cargo.toml b/boards/metro_m4/Cargo.toml index 1ca44e6090c2..483a061541fa 100644 --- a/boards/metro_m4/Cargo.toml +++ b/boards/metro_m4/Cargo.toml @@ -33,6 +33,8 @@ features = ["critical-section-single-core"] [dev-dependencies] cortex-m = "0.7" +embedded-hal = "1.0" +embedded-hal-nb = "1.0" usbd-serial = "0.2" panic-probe = "0.3" panic-halt = "0.2" diff --git a/boards/metro_m4/examples/neopixel_blink.rs b/boards/metro_m4/examples/neopixel_blink.rs index 1196499853b2..ed52cf8d2c67 100644 --- a/boards/metro_m4/examples/neopixel_blink.rs +++ b/boards/metro_m4/examples/neopixel_blink.rs @@ -14,9 +14,10 @@ use panic_semihosting as _; use bsp::entry; use hal::clock::GenericClockController; use hal::delay::Delay; -use hal::prelude::*; +use hal::ehal::delay::DelayNs; use hal::time::Hertz; use hal::timer::TimerCounter; +use hal::timer_traits::InterruptDrivenTimer; use pac::{CorePeripherals, Peripherals}; use ws2812_timer_delay as ws2812; @@ -49,11 +50,11 @@ fn main() -> ! { neopixel .write(brightness(data.iter().cloned(), 32)) .unwrap(); - delay.delay_ms(250u8); + delay.delay_ms(250); let data2 = [RGB8::default(); 1]; neopixel .write(brightness(data2.iter().cloned(), 32)) .unwrap(); - delay.delay_ms(250u8); + delay.delay_ms(250); } } diff --git a/boards/metro_m4/examples/neopixel_rainbow.rs b/boards/metro_m4/examples/neopixel_rainbow.rs index 29a0ed91fe32..761bdd4a08b7 100644 --- a/boards/metro_m4/examples/neopixel_rainbow.rs +++ b/boards/metro_m4/examples/neopixel_rainbow.rs @@ -14,9 +14,10 @@ use panic_semihosting as _; use bsp::entry; use hal::clock::GenericClockController; use hal::delay::Delay; -use hal::prelude::*; +use hal::ehal::delay::DelayNs; use hal::time::Hertz; use hal::timer::TimerCounter; +use hal::timer_traits::InterruptDrivenTimer; use pac::{CorePeripherals, Peripherals}; use smart_leds::{ @@ -54,7 +55,7 @@ fn main() -> ! { val: 32, })]; neopixel.write(colors.iter().cloned()).unwrap(); - delay.delay_ms(5u8); + delay.delay_ms(5); } } } diff --git a/boards/metro_m4/examples/serial.rs b/boards/metro_m4/examples/serial.rs index b671ba90f1ec..50ce36e66848 100644 --- a/boards/metro_m4/examples/serial.rs +++ b/boards/metro_m4/examples/serial.rs @@ -13,8 +13,11 @@ use panic_semihosting as _; use bsp::{entry, periph_alias, pin_alias}; use hal::clock::GenericClockController; use hal::delay::Delay; +use hal::ehal::delay::DelayNs; +use hal::ehal_nb::serial::Write; +use hal::fugit::RateExtU32; +use hal::nb; use hal::pac::{CorePeripherals, Peripherals}; -use hal::prelude::*; #[entry] fn main() -> ! { @@ -49,6 +52,6 @@ fn main() -> ! { // `Result<(), Error>` nb::block!(uart.write(*byte)).unwrap(); } - delay.delay_ms(1000u16); + delay.delay_ms(1000); } } diff --git a/boards/metro_m4/examples/spi.rs b/boards/metro_m4/examples/spi.rs index 56266536d16f..09980afe4a36 100644 --- a/boards/metro_m4/examples/spi.rs +++ b/boards/metro_m4/examples/spi.rs @@ -13,8 +13,11 @@ use panic_semihosting as _; use bsp::{entry, periph_alias}; use hal::clock::GenericClockController; use hal::delay::Delay; +use hal::ehal::delay::DelayNs; +use hal::ehal_nb::serial::Write; +use hal::fugit::RateExtU32; +use hal::nb; use hal::pac::{CorePeripherals, Peripherals}; -use hal::prelude::*; #[entry] fn main() -> ! { @@ -41,8 +44,8 @@ fn main() -> ! { loop { for byte in b"Hello, world!" { - nb::block!(spi.send(*byte)).unwrap(); + nb::block!(spi.write(*byte)).unwrap(); } - delay.delay_ms(1000u16); + delay.delay_ms(1000); } } diff --git a/boards/metro_m4/examples/timer.rs b/boards/metro_m4/examples/timer.rs index c2a90b71ecc4..3ddff29493b6 100644 --- a/boards/metro_m4/examples/timer.rs +++ b/boards/metro_m4/examples/timer.rs @@ -12,10 +12,12 @@ use panic_semihosting as _; use bsp::entry; use hal::clock::GenericClockController; +use hal::ehal::digital::OutputPin; +use hal::nb; use hal::pac::Peripherals; -use hal::prelude::*; use hal::time::Hertz; use hal::timer::TimerCounter; +use hal::timer_traits::InterruptDrivenTimer; use nb::block; diff --git a/boards/samd11_bare/examples/serial.rs b/boards/samd11_bare/examples/serial.rs index 375e76414d23..a47350fb3b2f 100644 --- a/boards/samd11_bare/examples/serial.rs +++ b/boards/samd11_bare/examples/serial.rs @@ -6,7 +6,10 @@ use samd11_bare as bsp; use bsp::entry; use hal::clock::GenericClockController; -use hal::prelude::*; +use hal::ehal::delay::DelayNs; +use hal::ehal_nb::serial::Write; +use hal::fugit::RateExtU32; +use hal::nb; #[cfg(not(feature = "use_semihosting"))] use panic_halt as _; @@ -61,6 +64,6 @@ fn main() -> ! { // `Result<(), Error>` nb::block!(uart.write(*byte)).unwrap(); } - delay.delay_ms(1000u16); + delay.delay_ms(1000); } } diff --git a/boards/samd11_bare/examples/timer.rs b/boards/samd11_bare/examples/timer.rs index 15be06be8233..6026dcad8a4e 100644 --- a/boards/samd11_bare/examples/timer.rs +++ b/boards/samd11_bare/examples/timer.rs @@ -13,9 +13,11 @@ use bsp::pac; use bsp::entry; use hal::clock::GenericClockController; -use hal::prelude::*; +use hal::ehal::digital::OutputPin; +use hal::nb; use hal::time::Hertz; use hal::timer::TimerCounter; +use hal::timer_traits::InterruptDrivenTimer; use pac::Peripherals; #[entry] diff --git a/hal/src/delay.rs b/hal/src/delay.rs index af74664f9262..f11c2857c306 100644 --- a/hal/src/delay.rs +++ b/hal/src/delay.rs @@ -4,7 +4,8 @@ use cortex_m::peripheral::syst::SystClkSource; use cortex_m::peripheral::SYST; use crate::clock::GenericClockController; -use crate::ehal_02::blocking::delay::{DelayMs, DelayUs}; +use crate::ehal::delay::DelayNs; +use crate::ehal_02; use crate::time::Hertz; /// System timer (SysTick) as a delay provider @@ -30,25 +31,14 @@ impl Delay { } } -impl DelayMs for Delay { - fn delay_ms(&mut self, ms: u32) { - self.delay_us(ms * 1_000); - } -} - -impl DelayMs for Delay { - fn delay_ms(&mut self, ms: u16) { - self.delay_ms(ms as u32); +impl DelayNs for Delay { + // The default method is delay_ns. If we don't provide implementations for delay_us and delay_ms, the trait impl + // will use delay_ns to implement the other two methods. As the delay implementation is actually defined in terms + // of microseconds, we need to provide implementations for all three methods. + fn delay_ns(&mut self, ns: u32) { + self.delay_us(ns / 1000); } -} -impl DelayMs for Delay { - fn delay_ms(&mut self, ms: u8) { - self.delay_ms(ms as u32); - } -} - -impl DelayUs for Delay { fn delay_us(&mut self, us: u32) { // The SysTick Reload Value register supports values between 1 and 0x00FFFFFF. const MAX_RVR: u32 = 0x00FF_FFFF; @@ -74,16 +64,44 @@ impl DelayUs for Delay { self.syst.disable_counter(); } } + + fn delay_ms(&mut self, ms: u32) { + self.delay_us(ms * 1000); + } +} + +impl ehal_02::blocking::delay::DelayMs for Delay { + fn delay_ms(&mut self, ms: u32) { + ::delay_us(self, ms * 1_000); + } +} + +impl ehal_02::blocking::delay::DelayMs for Delay { + fn delay_ms(&mut self, ms: u16) { + >::delay_ms(self, ms as u32); + } +} + +impl ehal_02::blocking::delay::DelayMs for Delay { + fn delay_ms(&mut self, ms: u8) { + >::delay_ms(self, ms as u32); + } +} + +impl ehal_02::blocking::delay::DelayUs for Delay { + fn delay_us(&mut self, us: u32) { + ::delay_us(self, us); + } } -impl DelayUs for Delay { +impl ehal_02::blocking::delay::DelayUs for Delay { fn delay_us(&mut self, us: u16) { - self.delay_us(us as u32) + >::delay_us(self, us as u32); } } -impl DelayUs for Delay { +impl ehal_02::blocking::delay::DelayUs for Delay { fn delay_us(&mut self, us: u8) { - self.delay_us(us as u32) + >::delay_us(self, us as u32); } } diff --git a/hal/src/lib.rs b/hal/src/lib.rs index 3c8b3ac3ca9f..b96934e528a5 100644 --- a/hal/src/lib.rs +++ b/hal/src/lib.rs @@ -2,8 +2,10 @@ use embedded_hal_02 as ehal_02; pub use embedded_hal_1 as ehal; +pub use embedded_hal_nb as ehal_nb; pub use embedded_io; pub use fugit; +pub use nb; pub use paste; pub mod typelevel; diff --git a/hal/src/prelude.rs b/hal/src/prelude.rs index 10271f207feb..e68cca365ac9 100644 --- a/hal/src/prelude.rs +++ b/hal/src/prelude.rs @@ -1,6 +1,6 @@ //! Import the prelude to gain convenient access to helper traits pub use crate::eic::pin::EicPin; -pub use crate::timer_traits::InterruptDrivenTimer as _atsamd_hal_timer_traits_InterruptDrivenTimer; +pub use crate::timer_traits::InterruptDrivenTimer; pub use fugit::ExtU32 as _; pub use fugit::RateExtU32 as _; @@ -11,5 +11,3 @@ pub use crate::ehal_02::digital::v2::OutputPin as _atsamd_hal_embedded_hal_digit pub use crate::ehal_02::digital::v2::ToggleableOutputPin as _atsamd_hal_embedded_hal_digital_v2_ToggleableOutputPin; pub use crate::ehal_02::prelude::*; - -pub use nb;