@@ -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> {
111118pub 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