Skip to content

Commit d2be502

Browse files
bors[bot]burrbull
andauthored
Merge #452
452: extend 16-bit Delay's to 32 r=burrbull a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents ef6bdc2 + dbdc636 commit d2be502

File tree

9 files changed

+45
-30
lines changed

9 files changed

+45
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12+
- Extend timers to 32bit on `Delay`
1213
- Move `MonoTimer` from `timer` to dwt mode [#448]
1314
- Unify serial trait impls for embedded-hal 0.2 & 1.0 [#447]
1415
- Add possibility to select Timer master mode

examples/delay-timer-blinky.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn main() -> ! {
3737
delay.delay_ms(1000_u32);
3838
led.set_low();
3939
// or use `fugit::ExtU32` trait
40-
delay.delay(3.secs()).unwrap();
40+
delay.delay(3.secs());
4141
}
4242
}
4343

examples/rtc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ fn main() -> ! {
4343
// rtc.set_seconds(50).unwrap();
4444
loop {
4545
rprintln!("{}", rtc.get_datetime());
46-
delay.delay(500.millis()).unwrap();
46+
delay.delay(500.millis());
4747
}
4848
}

examples/serial.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ fn main() -> ! {
3636
// print some value every 500 ms, value will overflow after 255
3737
writeln!(tx, "value: {:02}\r", value).unwrap();
3838
value = value.wrapping_add(1);
39-
delay.delay(2.secs()).unwrap();
39+
delay.delay(2.secs());
4040
}
4141
}

examples/ws2812_spi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ fn main() -> ! {
3434
let mut data = [RGB8::default(); NUM_LEDS];
3535

3636
// Wait before start write for syncronization
37-
delay.delay(200.micros()).unwrap();
37+
delay.delay(200.micros());
3838

3939
loop {
4040
for j in 0..(256 * 5) {
4141
for (i, b) in data.iter_mut().enumerate() {
4242
*b = wheel((((i * 256) as u16 / NUM_LEDS as u16 + j as u16) & 255) as u8);
4343
}
4444
ws.write(brightness(data.iter().cloned(), 32)).unwrap();
45-
delay.delay(10.millis()).unwrap();
45+
delay.delay(10.millis());
4646
}
4747
}
4848
}

src/timer.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ pub trait TimerExt: Sized {
101101
fn delay<const FREQ: u32>(self, clocks: &Clocks) -> FDelay<Self, FREQ>;
102102
/// Blocking [Delay] with fixed precision of 1 ms (1 kHz sampling)
103103
///
104-
/// Can wait from 2 ms to 65 sec for 16-bit timer and from 2 ms to 49 days for 32-bit timer.
104+
/// Can wait from 2 ms to 49 days.
105105
///
106106
/// NOTE: don't use this if your system frequency more than 65 MHz
107107
fn delay_ms(self, clocks: &Clocks) -> DelayMs<Self> {
108108
self.delay::<1_000>(clocks)
109109
}
110110
/// Blocking [Delay] with fixed precision of 1 μs (1 MHz sampling)
111111
///
112-
/// Can wait from 2 μs to 65 ms for 16-bit timer and from 2 μs to 71 min for 32-bit timer.
112+
/// Can wait from 2 μs to 71 min.
113113
fn delay_us(self, clocks: &Clocks) -> DelayUs<Self> {
114114
self.delay::<1_000_000>(clocks)
115115
}
@@ -224,6 +224,7 @@ mod sealed {
224224
pub trait General {
225225
type Width: Into<u32> + From<u16>;
226226
fn max_auto_reload() -> u32;
227+
unsafe fn set_auto_reload_unchecked(&mut self, arr: u32);
227228
fn set_auto_reload(&mut self, arr: u32) -> Result<(), super::Error>;
228229
fn read_auto_reload() -> u32;
229230
fn enable_preload(&mut self, b: bool);
@@ -282,11 +283,15 @@ macro_rules! hal {
282283
<$bits>::MAX as u32
283284
}
284285
#[inline(always)]
286+
unsafe fn set_auto_reload_unchecked(&mut self, arr: u32) {
287+
self.arr.write(|w| w.bits(arr))
288+
}
289+
#[inline(always)]
285290
fn set_auto_reload(&mut self, arr: u32) -> Result<(), Error> {
286291
// Note: Make it impossible to set the ARR value to 0, since this
287292
// would cause an infinite loop.
288293
if arr > 0 && arr <= Self::max_auto_reload() {
289-
Ok(self.arr.write(|w| unsafe { w.bits(arr) }))
294+
Ok(unsafe { self.set_auto_reload_unchecked(arr) })
290295
} else {
291296
Err(Error::WrongAutoReload)
292297
}

src/timer/delay.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,28 @@ pub type DelayMs<TIM> = FDelay<TIM, 1_000>;
114114

115115
impl<TIM: Instance, const FREQ: u32> FDelay<TIM, FREQ> {
116116
/// Sleep for given time
117-
pub fn delay(&mut self, time: TimerDurationU32<FREQ>) -> Result<(), Error> {
118-
// Write Auto-Reload Register (ARR)
119-
self.tim.set_auto_reload(time.ticks() - 1)?;
120-
121-
// Trigger update event (UEV) in the event generation register (EGR)
122-
// in order to immediately apply the config
123-
self.tim.trigger_update();
117+
pub fn delay(&mut self, time: TimerDurationU32<FREQ>) {
118+
let mut ticks = time.ticks() - 1;
119+
while ticks > 0 {
120+
let reload = ticks.min(TIM::max_auto_reload());
121+
ticks -= reload;
122+
123+
// Write Auto-Reload Register (ARR)
124+
unsafe {
125+
self.tim.set_auto_reload_unchecked(reload);
126+
}
124127

125-
// Configure the counter in one-pulse mode (counter stops counting at
126-
// the next updateevent, clearing the CEN bit) and enable the counter.
127-
self.tim.start_one_pulse();
128+
// Trigger update event (UEV) in the event generation register (EGR)
129+
// in order to immediately apply the config
130+
self.tim.trigger_update();
128131

129-
// Wait for CEN bit to clear
130-
while self.tim.is_counter_enabled() { /* wait */ }
132+
// Configure the counter in one-pulse mode (counter stops counting at
133+
// the next updateevent, clearing the CEN bit) and enable the counter.
134+
self.tim.start_one_pulse();
131135

132-
Ok(())
136+
// Wait for CEN bit to clear
137+
while self.tim.is_counter_enabled() { /* wait */ }
138+
}
133139
}
134140

135141
pub fn max_delay(&self) -> TimerDurationU32<FREQ> {
@@ -148,6 +154,7 @@ impl<TIM: Instance, const FREQ: u32> fugit_timer::Delay<FREQ> for FDelay<TIM, FR
148154
type Error = Error;
149155

150156
fn delay(&mut self, duration: TimerDurationU32<FREQ>) -> Result<(), Self::Error> {
151-
self.delay(duration)
157+
self.delay(duration);
158+
Ok(())
152159
}
153160
}

src/timer/hal_02.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,40 +297,40 @@ where
297297
impl<TIM: Instance, const FREQ: u32> DelayUs<u32> for FDelay<TIM, FREQ> {
298298
/// Sleep for `us` microseconds
299299
fn delay_us(&mut self, us: u32) {
300-
self.delay(us.micros()).unwrap()
300+
self.delay(us.micros())
301301
}
302302
}
303303

304304
impl<TIM: Instance, const FREQ: u32> DelayMs<u32> for FDelay<TIM, FREQ> {
305305
/// Sleep for `ms` milliseconds
306306
fn delay_ms(&mut self, ms: u32) {
307-
self.delay(ms.millis()).unwrap()
307+
self.delay(ms.millis())
308308
}
309309
}
310310

311311
impl<TIM: Instance, const FREQ: u32> DelayUs<u16> for FDelay<TIM, FREQ> {
312312
/// Sleep for `us` microseconds
313313
fn delay_us(&mut self, us: u16) {
314-
self.delay((us as u32).micros()).unwrap()
314+
self.delay((us as u32).micros())
315315
}
316316
}
317317
impl<TIM: Instance, const FREQ: u32> DelayMs<u16> for FDelay<TIM, FREQ> {
318318
/// Sleep for `ms` milliseconds
319319
fn delay_ms(&mut self, ms: u16) {
320-
self.delay((ms as u32).millis()).unwrap()
320+
self.delay((ms as u32).millis())
321321
}
322322
}
323323

324324
impl<TIM: Instance, const FREQ: u32> DelayUs<u8> for FDelay<TIM, FREQ> {
325325
/// Sleep for `us` microseconds
326326
fn delay_us(&mut self, us: u8) {
327-
self.delay((us as u32).micros()).unwrap()
327+
self.delay((us as u32).micros())
328328
}
329329
}
330330
impl<TIM: Instance, const FREQ: u32> DelayMs<u8> for FDelay<TIM, FREQ> {
331331
/// Sleep for `ms` milliseconds
332332
fn delay_ms(&mut self, ms: u8) {
333-
self.delay((ms as u32).millis()).unwrap()
333+
self.delay((ms as u32).millis())
334334
}
335335
}
336336

src/timer/hal_1.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ impl<TIM: Instance, const FREQ: u32> DelayUs for FDelay<TIM, FREQ> {
102102
type Error = Error;
103103

104104
fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
105-
self.delay(us.micros())
105+
self.delay(us.micros());
106+
Ok(())
106107
}
107108

108109
fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
109-
self.delay(ms.millis())
110+
self.delay(ms.millis());
111+
Ok(())
110112
}
111113
}

0 commit comments

Comments
 (0)