Skip to content

Commit 68ebb59

Browse files
authored
Merge pull request #900 from 3devo/hardwaretimer-preload
HardwareTimer: Allow setting preload enable bits
2 parents 0878b66 + e761507 commit 68ebb59

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

cores/arduino/HardwareTimer.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ HardwareTimer::HardwareTimer(TIM_TypeDef *instance)
9393
#if defined(TIM_RCR_REP)
9494
_timerObj.handle.Init.RepetitionCounter = 0;
9595
#endif
96-
_timerObj.handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
96+
_timerObj.handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
9797
HAL_TIM_Base_Init(&(_timerObj.handle));
9898
}
9999

@@ -442,6 +442,11 @@ uint32_t HardwareTimer::getOverflow(TimerFormat_t format)
442442

443443
/**
444444
* @brief Set overflow (rollover)
445+
*
446+
* Note that by default, the new value will not be applied
447+
* immediately, but become effective at the next update event
448+
* (usually the next timer overflow). See setPreloadEnable()
449+
* for controlling this behaviour.
445450
* @param overflow: depend on format parameter
446451
* @param format of overflow parameter. If ommited default format is Tick
447452
* TICK_FORMAT: overflow is the number of tick for overflow
@@ -685,6 +690,29 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin)
685690
}
686691
}
687692

693+
/**
694+
* @brief Enable or disable preloading for overflow value
695+
* When disabled, changes to the overflow value take effect
696+
* immediately. When enabled (the default), the value takes
697+
* effect only at the next update event (typically the next
698+
* overflow).
699+
*
700+
* Note that the capture/compare register has its own preload
701+
* enable bit, which is independent and enabled in PWM modes
702+
* and disabled otherwise. If you need more control of that
703+
* bit, you can use the HAL functions directly.
704+
* @param value: true to enable preloading, false to disable
705+
* @retval None
706+
*/
707+
void HardwareTimer::setPreloadEnable(bool value)
708+
{
709+
if (value) {
710+
LL_TIM_EnableARRPreload(_timerObj.handle.Instance);
711+
} else {
712+
LL_TIM_DisableARRPreload(_timerObj.handle.Instance);
713+
}
714+
}
715+
688716
/**
689717
* @brief Set channel Capture/Compare register
690718
* @param channel: Arduino channel [1..4]

cores/arduino/HardwareTimer.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ class HardwareTimer {
112112
void setPWM(uint32_t channel, PinName pin, uint32_t frequency, uint32_t dutycycle, void (*PeriodCallback)(HardwareTimer *) = NULL, void (*CompareCallback)(HardwareTimer *) = NULL); // Set all in one command freq in HZ, Duty in percentage. Including both interrup.
113113
void setPWM(uint32_t channel, uint32_t pin, uint32_t frequency, uint32_t dutycycle, void (*PeriodCallback)(HardwareTimer *) = NULL, void (*CompareCallback)(HardwareTimer *) = NULL);
114114

115-
116115
void setCount(uint32_t val, TimerFormat_t format = TICK_FORMAT); // set timer counter to value 'val' depending on format provided
117116
uint32_t getCount(TimerFormat_t format = TICK_FORMAT); // return current counter value of timer depending on format provided
118117

119118
void setMode(uint32_t channel, TimerModes_t mode, PinName pin = NC); // Configure timer channel with specified mode on specified pin if available
120119
void setMode(uint32_t channel, TimerModes_t mode, uint32_t pin);
121120

122-
uint32_t getCaptureCompare(uint32_t channel, TimerCompareFormat_t format = TICK_COMPARE_FORMAT); // return Capture/Compare register value of specified channel depending on format provided
121+
void setPreloadEnable(bool value); // Configure overflow preload enable setting
123122

123+
uint32_t getCaptureCompare(uint32_t channel, TimerCompareFormat_t format = TICK_COMPARE_FORMAT); // return Capture/Compare register value of specified channel depending on format provided
124124
void setCaptureCompare(uint32_t channel, uint32_t compare, TimerCompareFormat_t format = TICK_COMPARE_FORMAT); // set Compare register value of specified channel depending on format provided
125125

126126
void setInterruptPriority(uint32_t preemptPriority, uint32_t subPriority); // set interrupt priority
@@ -139,26 +139,24 @@ class HardwareTimer {
139139
// Refresh() is usefull while timer is running after some registers update
140140
void refresh(void); // Generate update event to force all registers (Autoreload, prescaler, compare) to be taken into account
141141

142-
143142
uint32_t getTimerClkFreq(); // return timer clock frequency in Hz.
144143

145144
static void captureCompareCallback(TIM_HandleTypeDef *htim); // Generic Caputre and Compare callback which will call user callback
146145
static void updateCallback(TIM_HandleTypeDef *htim); // Generic Update (rollover) callback which will call user callback
147146

148147
// The following function(s) are available for more advanced timer options
149148
TIM_HandleTypeDef *getHandle(); // return the handle address for HAL related configuration
150-
151-
private:
152-
TimerModes_t _ChannelMode[TIMER_CHANNELS];
153-
timerObj_t _timerObj;
154-
void (*callbacks[1 + TIMER_CHANNELS])(HardwareTimer *); //Callbacks: 0 for update, 1-4 for channels. (channel5/channel6, if any, doesn't have interrupt)
155149
int getChannel(uint32_t channel);
156150
int getLLChannel(uint32_t channel);
157151
int getIT(uint32_t channel);
158152
int getAssociatedChannel(uint32_t channel);
159153
#if defined(TIM_CCER_CC1NE)
160154
bool isComplementaryChannel[TIMER_CHANNELS];
161155
#endif
156+
private:
157+
TimerModes_t _ChannelMode[TIMER_CHANNELS];
158+
timerObj_t _timerObj;
159+
void (*callbacks[1 + TIMER_CHANNELS])(HardwareTimer *); //Callbacks: 0 for update, 1-4 for channels. (channel5/channel6, if any, doesn't have interrupt)
162160
};
163161

164162
extern timerObj_t *HardwareTimer_Handle[TIMER_NUM];
@@ -168,4 +166,4 @@ extern timer_index_t get_timer_index(TIM_TypeDef *htim);
168166
#endif /* __cplusplus */
169167

170168
#endif // HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY
171-
#endif // HARDWARETIMER_H_
169+
#endif // HARDWARETIMER_H_

0 commit comments

Comments
 (0)