Skip to content

Commit

Permalink
Fix T1 examples (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeaurivage committed Apr 29, 2024
1 parent 4b5889a commit 375c640
Show file tree
Hide file tree
Showing 18 changed files with 106 additions and 54 deletions.
13 changes: 8 additions & 5 deletions boards/feather_m0/examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
}
}
4 changes: 3 additions & 1 deletion boards/feather_m0/examples/timers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 3 additions & 2 deletions boards/feather_m4/examples/neopixel_rainbow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -62,7 +63,7 @@ fn main() -> ! {
val: 2,
})];
neopixel.write(colors.iter().cloned()).unwrap();
delay.delay_ms(5u8);
delay.delay_ms(5);
}
}
}
7 changes: 5 additions & 2 deletions boards/feather_m4/examples/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() -> ! {
Expand Down Expand Up @@ -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);
}
}
4 changes: 3 additions & 1 deletion boards/feather_m4/examples/timers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion boards/feather_m4/examples/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 7 additions & 3 deletions boards/feather_m4/examples/uart_poll_echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() -> ! {
Expand Down Expand Up @@ -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),
};
}
}
2 changes: 2 additions & 0 deletions boards/metro_m4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 4 additions & 3 deletions boards/metro_m4/examples/neopixel_blink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
5 changes: 3 additions & 2 deletions boards/metro_m4/examples/neopixel_rainbow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -54,7 +55,7 @@ fn main() -> ! {
val: 32,
})];
neopixel.write(colors.iter().cloned()).unwrap();
delay.delay_ms(5u8);
delay.delay_ms(5);
}
}
}
7 changes: 5 additions & 2 deletions boards/metro_m4/examples/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() -> ! {
Expand Down Expand Up @@ -49,6 +52,6 @@ fn main() -> ! {
// `Result<(), Error>`
nb::block!(uart.write(*byte)).unwrap();
}
delay.delay_ms(1000u16);
delay.delay_ms(1000);
}
}
9 changes: 6 additions & 3 deletions boards/metro_m4/examples/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() -> ! {
Expand All @@ -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);
}
}
4 changes: 3 additions & 1 deletion boards/metro_m4/examples/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 5 additions & 2 deletions boards/samd11_bare/examples/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _;
Expand Down Expand Up @@ -61,6 +64,6 @@ fn main() -> ! {
// `Result<(), Error>`
nb::block!(uart.write(*byte)).unwrap();
}
delay.delay_ms(1000u16);
delay.delay_ms(1000);
}
}
4 changes: 3 additions & 1 deletion boards/samd11_bare/examples/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
62 changes: 40 additions & 22 deletions hal/src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,25 +31,14 @@ impl Delay {
}
}

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

impl DelayMs<u16> 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<u8> for Delay {
fn delay_ms(&mut self, ms: u8) {
self.delay_ms(ms as u32);
}
}

impl DelayUs<u32> 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;
Expand All @@ -74,16 +64,44 @@ impl DelayUs<u32> for Delay {
self.syst.disable_counter();
}
}

fn delay_ms(&mut self, ms: u32) {
self.delay_us(ms * 1000);
}
}

impl ehal_02::blocking::delay::DelayMs<u32> for Delay {
fn delay_ms(&mut self, ms: u32) {
<Self as DelayNs>::delay_us(self, ms * 1_000);
}
}

impl ehal_02::blocking::delay::DelayMs<u16> for Delay {
fn delay_ms(&mut self, ms: u16) {
<Self as ehal_02::blocking::delay::DelayMs<u32>>::delay_ms(self, ms as u32);
}
}

impl ehal_02::blocking::delay::DelayMs<u8> for Delay {
fn delay_ms(&mut self, ms: u8) {
<Self as ehal_02::blocking::delay::DelayMs<u32>>::delay_ms(self, ms as u32);
}
}

impl ehal_02::blocking::delay::DelayUs<u32> for Delay {
fn delay_us(&mut self, us: u32) {
<Self as DelayNs>::delay_us(self, us);
}
}

impl DelayUs<u16> for Delay {
impl ehal_02::blocking::delay::DelayUs<u16> for Delay {
fn delay_us(&mut self, us: u16) {
self.delay_us(us as u32)
<Self as ehal_02::blocking::delay::DelayUs<u32>>::delay_us(self, us as u32);
}
}

impl DelayUs<u8> for Delay {
impl ehal_02::blocking::delay::DelayUs<u8> for Delay {
fn delay_us(&mut self, us: u8) {
self.delay_us(us as u32)
<Self as ehal_02::blocking::delay::DelayUs<u32>>::delay_us(self, us as u32);
}
}
2 changes: 2 additions & 0 deletions hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading

0 comments on commit 375c640

Please sign in to comment.