Skip to content

Commit ce3436b

Browse files
committed
extend Delay
1 parent ef6bdc2 commit ce3436b

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
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

src/timer.rs

Lines changed: 2 additions & 2 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
}

src/timer/delay.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,25 @@ pub type DelayMs<TIM> = FDelay<TIM, 1_000>;
115115
impl<TIM: Instance, const FREQ: u32> FDelay<TIM, FREQ> {
116116
/// Sleep for given time
117117
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)?;
118+
let mut ticks = time.ticks() - 1;
119+
while ticks > 0 {
120+
let reload = ticks.min(TIM::max_auto_reload());
121+
ticks -= reload;
120122

121-
// Trigger update event (UEV) in the event generation register (EGR)
122-
// in order to immediately apply the config
123-
self.tim.trigger_update();
123+
// Write Auto-Reload Register (ARR)
124+
self.tim.set_auto_reload(reload)?;
124125

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();
126+
// Trigger update event (UEV) in the event generation register (EGR)
127+
// in order to immediately apply the config
128+
self.tim.trigger_update();
128129

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

132138
Ok(())
133139
}

0 commit comments

Comments
 (0)