Skip to content

Commit 888e942

Browse files
authored
chore: Update TimerClockConfig to support period updating method (esp-rs#1898)
* chore: Update `TimerClockConfig` to support period updating method * docs: added a change log entry
1 parent 63a2d40 commit 888e942

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

esp-hal/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Added new `Io::new_no_bind_interrupt` constructor (#1861)
1313
- Added touch pad support for esp32 (#1873)
14+
- Allow configuration of period updating method for MCPWM timers (#1898)
1415

1516
### Changed
1617

esp-hal/src/mcpwm/timer.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,28 @@ impl<const TIM: u8, PWM: PwmPeripheral> Timer<TIM, PWM> {
3232
/// Apply the given timer configuration.
3333
///
3434
/// ### Note:
35-
/// The prescaler and period configuration will be applied immediately and
36-
/// before setting the [`PwmWorkingMode`].
35+
/// The prescaler and period configuration will be applied immediately by
36+
/// default and before setting the [`PwmWorkingMode`].
3737
/// If the timer is already running you might want to call [`Timer::stop`]
3838
/// and/or [`Timer::set_counter`] first
3939
/// (if the new period is larger than the current counter value this will
4040
/// cause weird behavior).
4141
///
42+
/// If configured via [`TimerClockConfig::with_period_updating_method`],
43+
/// another behavior can be applied. Currently, only
44+
/// [`PeriodUpdatingMethod::Immediately`]
45+
/// and [`PeriodUpdatingMethod::TimerEqualsZero`] are useful as the sync
46+
/// method is not yet implemented.
47+
///
4248
/// The hardware supports writing these settings in sync with certain timer
4349
/// events but this HAL does not expose these for now.
4450
pub fn start(&mut self, timer_config: TimerClockConfig<'_>) {
4551
// write prescaler and period with immediate update method
4652
self.cfg0().write(|w| unsafe {
4753
w.prescale().bits(timer_config.prescaler);
4854
w.period().bits(timer_config.period);
49-
w.period_upmethod().bits(0)
55+
w.period_upmethod()
56+
.bits(timer_config.period_updating_method as u8)
5057
});
5158

5259
// set timer to continuously run and set the timer working mode
@@ -111,6 +118,7 @@ impl<const TIM: u8, PWM: PwmPeripheral> Timer<TIM, PWM> {
111118
pub struct TimerClockConfig<'a> {
112119
frequency: HertzU32,
113120
period: u16,
121+
period_updating_method: PeriodUpdatingMethod,
114122
prescaler: u8,
115123
mode: PwmWorkingMode,
116124
phantom: PhantomData<&'a Clocks<'a>>,
@@ -134,6 +142,7 @@ impl<'a> TimerClockConfig<'a> {
134142
frequency,
135143
prescaler,
136144
period,
145+
period_updating_method: PeriodUpdatingMethod::Immediately,
137146
mode,
138147
phantom: PhantomData,
139148
}
@@ -167,11 +176,20 @@ impl<'a> TimerClockConfig<'a> {
167176
frequency,
168177
prescaler: prescaler as u8,
169178
period,
179+
period_updating_method: PeriodUpdatingMethod::Immediately,
170180
mode,
171181
phantom: PhantomData,
172182
})
173183
}
174184

185+
/// Set the method for updating the PWM period
186+
pub fn with_period_updating_method(self, method: PeriodUpdatingMethod) -> Self {
187+
Self {
188+
period_updating_method: method,
189+
..self
190+
}
191+
}
192+
175193
/// Get the timer clock frequency.
176194
///
177195
/// ### Note:
@@ -181,6 +199,21 @@ impl<'a> TimerClockConfig<'a> {
181199
}
182200
}
183201

202+
/// Method for updating the PWM period
203+
#[derive(Clone, Copy)]
204+
#[repr(u8)]
205+
pub enum PeriodUpdatingMethod {
206+
/// The period is updated immediately.
207+
Immediately = 0,
208+
/// The period is updated when the timer equals zero.
209+
TimerEqualsZero = 1,
210+
/// The period is updated on a synchronization event.
211+
Sync = 2,
212+
/// The period is updated either when the timer equals zero or on a
213+
/// synchronization event.
214+
TimerEqualsZeroOrSync = 3,
215+
}
216+
184217
/// PWM working mode
185218
#[derive(Copy, Clone)]
186219
#[repr(u8)]

0 commit comments

Comments
 (0)