Skip to content

Commit 4982a32

Browse files
committed
Try to make syst delay work as before
1 parent a413df7 commit 4982a32

File tree

9 files changed

+88
-60
lines changed

9 files changed

+88
-60
lines changed

examples/adc-continious-dma.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ use crate::hal::{
1010
config::{Continuous, Dma as AdcDma, SampleTime, Sequence},
1111
AdcClaim, ClockSource, Temperature, Vref,
1212
},
13-
delay::DelayFromCountDownTimer,
13+
delay::SYSTDelayExt,
1414
dma::{config::DmaConfig, stream::DMAExt, TransferExt},
1515
gpio::GpioExt,
1616
pwr::PwrExt,
1717
rcc::{Config, RccExt},
1818
signature::{VrefCal, VDDA_CALIB},
1919
stm32::Peripherals,
20-
time::ExtU32,
21-
timer::Timer,
2220
};
2321
use stm32g4xx_hal as hal;
2422

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

3331
let dp = Peripherals::take().unwrap();
34-
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
32+
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
3533

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

5149
info!("Setup Adc1");
52-
// let mut delay = cp.SYST.delay(&rcc.clocks);
53-
let mut delay = DelayFromCountDownTimer::new(
54-
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
55-
);
50+
let mut delay = cp.SYST.delay(&rcc.clocks);
51+
5652
let mut adc = dp
5753
.ADC1
5854
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);

examples/adc-continious.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ use crate::hal::{
66
config::{Continuous, Resolution, SampleTime, Sequence},
77
AdcClaim, ClockSource, Temperature, Vref,
88
},
9-
delay::DelayFromCountDownTimer,
109
gpio::GpioExt,
1110
pwr::PwrExt,
1211
rcc::{Config, RccExt},
1312
signature::{VrefCal, VDDA_CALIB},
1413
stm32::Peripherals,
15-
time::ExtU32,
16-
timer::Timer,
1714
};
18-
use stm32g4xx_hal as hal;
15+
use stm32g4xx_hal::{self as hal, delay::SYSTDelayExt};
1916

2017
use cortex_m_rt::entry;
2118

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

3330
let dp = Peripherals::take().unwrap();
34-
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
31+
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
3532

3633
info!("rcc");
3734

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

4643
info!("Setup Adc1");
47-
// let mut delay = cp.SYST.delay(&rcc.clocks);
48-
let mut delay = DelayFromCountDownTimer::new(
49-
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
50-
);
44+
let mut delay = cp.SYST.delay(&rcc.clocks);
5145
let mut adc = dp
5246
.ADC1
5347
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);

examples/adc-one-shot-dma.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ use crate::hal::{
88
config::{Continuous, Dma as AdcDma, Resolution, SampleTime, Sequence},
99
AdcClaim, ClockSource, Temperature,
1010
},
11-
delay::DelayFromCountDownTimer,
1211
dma::{config::DmaConfig, stream::DMAExt, TransferExt},
1312
gpio::GpioExt,
1413
pwr::PwrExt,
1514
rcc::{Config, RccExt},
1615
stm32::Peripherals,
17-
time::ExtU32,
18-
timer::Timer,
1916
};
20-
use stm32g4xx_hal as hal;
17+
use stm32g4xx_hal::{self as hal, delay::SYSTDelayExt};
2118

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

3229
let dp = Peripherals::take().unwrap();
33-
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
30+
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
3431

3532
info!("rcc");
3633

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

5148
info!("Setup Adc1");
52-
// let mut delay = cp.SYST.delay(&rcc.clocks);
53-
let mut delay = DelayFromCountDownTimer::new(
54-
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
55-
);
49+
let mut delay = cp.SYST.delay(&rcc.clocks);
50+
5651
let mut adc = dp
5752
.ADC1
5853
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);

examples/adc-one-shot.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33

44
use crate::hal::{
55
adc::{config::SampleTime, AdcClaim},
6-
delay::DelayFromCountDownTimer,
76
pwr::PwrExt,
87
rcc::Config,
98
stm32::Peripherals,
10-
time::ExtU32,
11-
timer::Timer,
129
};
1310
use hal::prelude::*;
1411
use stm32g4xx_hal as hal;
@@ -26,7 +23,7 @@ fn main() -> ! {
2623
info!("start");
2724

2825
let dp = Peripherals::take().unwrap();
29-
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
26+
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
3027

3128
info!("rcc");
3229

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

3734
info!("Setup Adc1");
38-
// let mut delay = cp.SYST.delay(&rcc.clocks);
39-
let mut delay = DelayFromCountDownTimer::new(
40-
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
41-
);
35+
let mut delay = cp.SYST.delay(&rcc.clocks);
4236
let mut adc = dp.ADC2.claim_and_configure(
4337
stm32g4xx_hal::adc::ClockSource::SystemClock,
4438
&rcc,

examples/blinky_delay.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#![no_main]
44
#![no_std]
55

6-
use embedded_hal::delay::DelayNs;
7-
use hal::delay::DelayFromCountDownTimer;
86
use hal::prelude::*;
97
use hal::pwr::PwrExt;
108
use hal::rcc::Config;
@@ -38,7 +36,7 @@ fn main() -> ! {
3836

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

4341
loop {
4442
info!("Toggle");

examples/comp_w_dac.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,17 @@ fn main() -> ! {
1313
use embedded_hal_old::Direction;
1414
use hal::comparator::{self, ComparatorExt, ComparatorSplit};
1515
use hal::dac::{Dac1IntSig1, DacExt, DacOut};
16-
use hal::delay::DelayFromCountDownTimer;
1716
use hal::gpio::GpioExt;
1817
use hal::rcc::RccExt;
1918
use hal::stm32;
20-
use hal::time::ExtU32;
21-
use hal::timer::Timer;
2219
use stm32g4xx_hal as hal;
20+
use stm32g4xx_hal::delay::SYSTDelayExt;
2321

2422
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
25-
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
23+
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
2624

2725
let mut rcc = dp.RCC.constrain();
28-
// let mut delay = cp.SYST.delay(&rcc.clocks);
29-
let mut delay = DelayFromCountDownTimer::new(
30-
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
31-
);
26+
let mut delay = cp.SYST.delay(&rcc.clocks);
3227

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

examples/dac.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010

1111
use embedded_hal_old::Direction;
1212
use hal::dac::{DacExt, DacOut, GeneratorConfig};
13-
use hal::delay::DelayFromCountDownTimer;
1413
use hal::gpio::GpioExt;
1514
use hal::rcc::RccExt;
16-
use hal::time::ExtU32;
17-
use hal::timer::Timer;
1815
use stm32g4xx_hal as hal;
16+
use stm32g4xx_hal::delay::SYSTDelayExt;
1917
mod utils;
2018
extern crate cortex_m_rt as rt;
2119

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

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

3732
let gpioa = dp.GPIOA.split(&mut rcc);
3833
let (dac1ch1, dac1ch2) = dp.DAC1.constrain((gpioa.pa4, gpioa.pa5), &mut rcc);

src/delay.rs

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
3939
use crate::rcc::Clocks;
4040
use crate::time::MicroSecond;
41-
pub use cortex_m::delay::*;
41+
pub use cortex_m::delay::Delay;
4242
use cortex_m::peripheral::SYST;
4343
use fugit::ExtU32Ceil;
4444

@@ -52,12 +52,69 @@ pub trait CountDown: embedded_hal_old::timer::CountDown {
5252
}
5353

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

5858
impl SYSTDelayExt for SYST {
59-
fn delay(self, clocks: &Clocks) -> Delay {
60-
Delay::new(self, clocks.ahb_clk.raw())
59+
fn delay(self, clocks: &Clocks) -> SystDelay {
60+
SystDelay(Delay::new(self, clocks.ahb_clk.raw()))
61+
}
62+
}
63+
64+
/// Delay provider
65+
pub struct SystDelay(Delay);
66+
67+
impl DelayNs for SystDelay {
68+
// TODO: the current fallback to 1us resolution is a stopgap until the module is reworked
69+
fn delay_ns(&mut self, ns: u32) {
70+
self.0.delay_us(ns.div_ceil(1000))
71+
}
72+
fn delay_us(&mut self, us: u32) {
73+
self.0.delay_us(us);
74+
}
75+
fn delay_ms(&mut self, mut ms: u32) {
76+
const MAX_MILLIS: u32 = u32::MAX / 1000;
77+
while ms > MAX_MILLIS {
78+
ms -= MAX_MILLIS;
79+
DelayNs::delay_us(self, MAX_MILLIS * 1000);
80+
}
81+
DelayNs::delay_us(self, ms * 1000);
82+
}
83+
}
84+
85+
impl DelayUs<u32> for SystDelay {
86+
fn delay_us(&mut self, us: u32) {
87+
DelayNs::delay_us(self, us);
88+
}
89+
}
90+
91+
impl DelayUs<u16> for SystDelay {
92+
fn delay_us(&mut self, us: u16) {
93+
DelayNs::delay_us(self, us as u32);
94+
}
95+
}
96+
97+
impl DelayUs<u8> for SystDelay {
98+
fn delay_us(&mut self, us: u8) {
99+
DelayNs::delay_us(self, us as u32);
100+
}
101+
}
102+
103+
impl DelayMs<u32> for SystDelay {
104+
fn delay_ms(&mut self, ms: u32) {
105+
DelayNs::delay_ms(self, ms);
106+
}
107+
}
108+
109+
impl DelayMs<u16> for SystDelay {
110+
fn delay_ms(&mut self, ms: u16) {
111+
DelayNs::delay_ms(self, ms as u32);
112+
}
113+
}
114+
115+
impl DelayMs<u8> for SystDelay {
116+
fn delay_ms(&mut self, ms: u8) {
117+
DelayNs::delay_ms(self, ms as u32);
61118
}
62119
}
63120

@@ -67,12 +124,12 @@ pub trait DelayExt {
67124
T: Into<MicroSecond>;
68125
}
69126

70-
impl DelayExt for Delay {
127+
impl DelayExt for SystDelay {
71128
fn delay<T>(&mut self, delay: T)
72129
where
73130
T: Into<MicroSecond>,
74131
{
75-
self.delay_us(delay.into().ticks())
132+
self.0.delay_us(delay.into().ticks())
76133
}
77134
}
78135

src/timer.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Pins can be used for PWM output in both push-pull mode (`Alternate`) and open-drain mode
44
//! (`AlternateOD`).
55
6-
use crate::delay::CountDown;
6+
use crate::delay::{CountDown, DelayFromCountDownTimer};
77
use cast::{u16, u32};
88
use cortex_m::peripheral::syst::SystClkSource;
99
use cortex_m::peripheral::{DCB, DWT, SYST};
@@ -287,6 +287,10 @@ macro_rules! hal {
287287
self.tim.cr1.modify(|_, w| w.cen().clear_bit());
288288
self.tim
289289
}
290+
291+
pub fn delay(self) -> DelayFromCountDownTimer<Self> {
292+
DelayFromCountDownTimer::new(self)
293+
}
290294
}
291295

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

0 commit comments

Comments
 (0)