Skip to content

Commit

Permalink
Try to make syst delay work as before
Browse files Browse the repository at this point in the history
  • Loading branch information
usbalbin committed Jan 19, 2025
1 parent a413df7 commit 4982a32
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 60 deletions.
12 changes: 4 additions & 8 deletions examples/adc-continious-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ use crate::hal::{
config::{Continuous, Dma as AdcDma, SampleTime, Sequence},
AdcClaim, ClockSource, Temperature, Vref,
},
delay::DelayFromCountDownTimer,
delay::SYSTDelayExt,
dma::{config::DmaConfig, stream::DMAExt, TransferExt},
gpio::GpioExt,
pwr::PwrExt,
rcc::{Config, RccExt},
signature::{VrefCal, VDDA_CALIB},
stm32::Peripherals,
time::ExtU32,
timer::Timer,
};
use stm32g4xx_hal as hal;

Expand All @@ -31,7 +29,7 @@ fn main() -> ! {
info!("start");

let dp = Peripherals::take().unwrap();
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

info!("rcc");
let rcc = dp.RCC.constrain();
Expand All @@ -49,10 +47,8 @@ fn main() -> ! {
let pa0 = gpioa.pa0.into_analog();

info!("Setup Adc1");
// let mut delay = cp.SYST.delay(&rcc.clocks);
let mut delay = DelayFromCountDownTimer::new(
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
);
let mut delay = cp.SYST.delay(&rcc.clocks);

let mut adc = dp
.ADC1
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);
Expand Down
12 changes: 3 additions & 9 deletions examples/adc-continious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ use crate::hal::{
config::{Continuous, Resolution, SampleTime, Sequence},
AdcClaim, ClockSource, Temperature, Vref,
},
delay::DelayFromCountDownTimer,
gpio::GpioExt,
pwr::PwrExt,
rcc::{Config, RccExt},
signature::{VrefCal, VDDA_CALIB},
stm32::Peripherals,
time::ExtU32,
timer::Timer,
};
use stm32g4xx_hal as hal;
use stm32g4xx_hal::{self as hal, delay::SYSTDelayExt};

use cortex_m_rt::entry;

Expand All @@ -31,7 +28,7 @@ fn main() -> ! {
info!("start");

let dp = Peripherals::take().unwrap();
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

info!("rcc");

Expand All @@ -44,10 +41,7 @@ fn main() -> ! {
let pa0 = gpioa.pa0.into_analog();

info!("Setup Adc1");
// let mut delay = cp.SYST.delay(&rcc.clocks);
let mut delay = DelayFromCountDownTimer::new(
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
);
let mut delay = cp.SYST.delay(&rcc.clocks);
let mut adc = dp
.ADC1
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);
Expand Down
13 changes: 4 additions & 9 deletions examples/adc-one-shot-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ use crate::hal::{
config::{Continuous, Dma as AdcDma, Resolution, SampleTime, Sequence},
AdcClaim, ClockSource, Temperature,
},
delay::DelayFromCountDownTimer,
dma::{config::DmaConfig, stream::DMAExt, TransferExt},
gpio::GpioExt,
pwr::PwrExt,
rcc::{Config, RccExt},
stm32::Peripherals,
time::ExtU32,
timer::Timer,
};
use stm32g4xx_hal as hal;
use stm32g4xx_hal::{self as hal, delay::SYSTDelayExt};

#[macro_use]
mod utils;
Expand All @@ -30,7 +27,7 @@ fn main() -> ! {
info!("start");

let dp = Peripherals::take().unwrap();
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

info!("rcc");

Expand All @@ -49,10 +46,8 @@ fn main() -> ! {
let pa0 = gpioa.pa0.into_analog();

info!("Setup Adc1");
// let mut delay = cp.SYST.delay(&rcc.clocks);
let mut delay = DelayFromCountDownTimer::new(
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
);
let mut delay = cp.SYST.delay(&rcc.clocks);

let mut adc = dp
.ADC1
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);
Expand Down
10 changes: 2 additions & 8 deletions examples/adc-one-shot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@

use crate::hal::{
adc::{config::SampleTime, AdcClaim},
delay::DelayFromCountDownTimer,
pwr::PwrExt,
rcc::Config,
stm32::Peripherals,
time::ExtU32,
timer::Timer,
};
use hal::prelude::*;
use stm32g4xx_hal as hal;
Expand All @@ -26,7 +23,7 @@ fn main() -> ! {
info!("start");

let dp = Peripherals::take().unwrap();
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

info!("rcc");

Expand All @@ -35,10 +32,7 @@ fn main() -> ! {
let mut rcc = rcc.freeze(Config::hsi(), pwr);

info!("Setup Adc1");
// let mut delay = cp.SYST.delay(&rcc.clocks);
let mut delay = DelayFromCountDownTimer::new(
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
);
let mut delay = cp.SYST.delay(&rcc.clocks);
let mut adc = dp.ADC2.claim_and_configure(
stm32g4xx_hal::adc::ClockSource::SystemClock,
&rcc,
Expand Down
4 changes: 1 addition & 3 deletions examples/blinky_delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#![no_main]
#![no_std]

use embedded_hal::delay::DelayNs;
use hal::delay::DelayFromCountDownTimer;
use hal::prelude::*;
use hal::pwr::PwrExt;
use hal::rcc::Config;
Expand Down Expand Up @@ -38,7 +36,7 @@ fn main() -> ! {

info!("Init Timer2 delay");
let timer2 = Timer::new(dp.TIM2, &rcc.clocks);
let mut delay_tim2 = DelayFromCountDownTimer::new(timer2.start_count_down(100.millis()));
let mut delay_tim2 = timer2.start_count_down(100.millis()).delay();

loop {
info!("Toggle");
Expand Down
11 changes: 3 additions & 8 deletions examples/comp_w_dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,17 @@ fn main() -> ! {
use embedded_hal_old::Direction;
use hal::comparator::{self, ComparatorExt, ComparatorSplit};
use hal::dac::{Dac1IntSig1, DacExt, DacOut};
use hal::delay::DelayFromCountDownTimer;
use hal::gpio::GpioExt;
use hal::rcc::RccExt;
use hal::stm32;
use hal::time::ExtU32;
use hal::timer::Timer;
use stm32g4xx_hal as hal;
use stm32g4xx_hal::delay::SYSTDelayExt;

let dp = stm32::Peripherals::take().expect("cannot take peripherals");
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

let mut rcc = dp.RCC.constrain();
// let mut delay = cp.SYST.delay(&rcc.clocks);
let mut delay = DelayFromCountDownTimer::new(
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
);
let mut delay = cp.SYST.delay(&rcc.clocks);

let gpioa = dp.GPIOA.split(&mut rcc);

Expand Down
11 changes: 3 additions & 8 deletions examples/dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@

use embedded_hal_old::Direction;
use hal::dac::{DacExt, DacOut, GeneratorConfig};
use hal::delay::DelayFromCountDownTimer;
use hal::gpio::GpioExt;
use hal::rcc::RccExt;
use hal::time::ExtU32;
use hal::timer::Timer;
use stm32g4xx_hal as hal;
use stm32g4xx_hal::delay::SYSTDelayExt;
mod utils;
extern crate cortex_m_rt as rt;

Expand All @@ -25,14 +23,11 @@ use rt::entry;
#[entry]
fn main() -> ! {
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

let mut rcc = dp.RCC.constrain();
// cortex-m doesn't yet support hal-1 DelayNs on systick (PR #504)
// let mut delay = cp.SYST.delay(&rcc.clocks);
let mut delay = DelayFromCountDownTimer::new(
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
);
let mut delay = cp.SYST.delay(&rcc.clocks);

let gpioa = dp.GPIOA.split(&mut rcc);
let (dac1ch1, dac1ch2) = dp.DAC1.constrain((gpioa.pa4, gpioa.pa5), &mut rcc);
Expand Down
69 changes: 63 additions & 6 deletions src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
use crate::rcc::Clocks;
use crate::time::MicroSecond;
pub use cortex_m::delay::*;
pub use cortex_m::delay::Delay;
use cortex_m::peripheral::SYST;
use fugit::ExtU32Ceil;

Expand All @@ -52,12 +52,69 @@ pub trait CountDown: embedded_hal_old::timer::CountDown {
}

pub trait SYSTDelayExt {
fn delay(self, clocks: &Clocks) -> Delay;
fn delay(self, clocks: &Clocks) -> SystDelay;
}

impl SYSTDelayExt for SYST {
fn delay(self, clocks: &Clocks) -> Delay {
Delay::new(self, clocks.ahb_clk.raw())
fn delay(self, clocks: &Clocks) -> SystDelay {
SystDelay(Delay::new(self, clocks.ahb_clk.raw()))
}
}

/// Delay provider
pub struct SystDelay(Delay);

impl DelayNs for SystDelay {
// TODO: the current fallback to 1us resolution is a stopgap until the module is reworked
fn delay_ns(&mut self, ns: u32) {
self.0.delay_us(ns.div_ceil(1000))
}
fn delay_us(&mut self, us: u32) {
self.0.delay_us(us);
}
fn delay_ms(&mut self, mut ms: u32) {
const MAX_MILLIS: u32 = u32::MAX / 1000;
while ms > MAX_MILLIS {
ms -= MAX_MILLIS;
DelayNs::delay_us(self, MAX_MILLIS * 1000);
}
DelayNs::delay_us(self, ms * 1000);
}
}

impl DelayUs<u32> for SystDelay {
fn delay_us(&mut self, us: u32) {
DelayNs::delay_us(self, us);
}
}

impl DelayUs<u16> for SystDelay {
fn delay_us(&mut self, us: u16) {
DelayNs::delay_us(self, us as u32);
}
}

impl DelayUs<u8> for SystDelay {
fn delay_us(&mut self, us: u8) {
DelayNs::delay_us(self, us as u32);
}
}

impl DelayMs<u32> for SystDelay {
fn delay_ms(&mut self, ms: u32) {
DelayNs::delay_ms(self, ms);
}
}

impl DelayMs<u16> for SystDelay {
fn delay_ms(&mut self, ms: u16) {
DelayNs::delay_ms(self, ms as u32);
}
}

impl DelayMs<u8> for SystDelay {
fn delay_ms(&mut self, ms: u8) {
DelayNs::delay_ms(self, ms as u32);
}
}

Expand All @@ -67,12 +124,12 @@ pub trait DelayExt {
T: Into<MicroSecond>;
}

impl DelayExt for Delay {
impl DelayExt for SystDelay {
fn delay<T>(&mut self, delay: T)
where
T: Into<MicroSecond>,
{
self.delay_us(delay.into().ticks())
self.0.delay_us(delay.into().ticks())
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Pins can be used for PWM output in both push-pull mode (`Alternate`) and open-drain mode
//! (`AlternateOD`).
use crate::delay::CountDown;
use crate::delay::{CountDown, DelayFromCountDownTimer};
use cast::{u16, u32};
use cortex_m::peripheral::syst::SystClkSource;
use cortex_m::peripheral::{DCB, DWT, SYST};
Expand Down Expand Up @@ -287,6 +287,10 @@ macro_rules! hal {
self.tim.cr1.modify(|_, w| w.cen().clear_bit());
self.tim
}

pub fn delay(self) -> DelayFromCountDownTimer<Self> {
DelayFromCountDownTimer::new(self)
}
}

impl embedded_hal_old::timer::CountDown for CountDownTimer<$TIM> {
Expand Down

0 comments on commit 4982a32

Please sign in to comment.