Skip to content

Commit

Permalink
HardwareTimer: call refresh() after parameter update when timer not r…
Browse files Browse the repository at this point in the history
…unning

In case timer is not running,
after an update of parameters (setOverflow(), setCaptureCompare(),
setPrescaleFactor()),
call refresh() to force register update independently of Preload setting.

Fixes https://www.stm32duino.com/viewtopic.php?f=7&t=1563

Signed-off-by: Alexandre Bourdiol <[email protected]>
  • Loading branch information
ABOSTM authored and fpistm committed May 18, 2022
1 parent 0e00a8d commit f8bf924
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cores/arduino/HardwareTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ class HardwareTimer {
static void captureCompareCallback(TIM_HandleTypeDef *htim); // Generic Capture and Compare callback which will call user callback
static void updateCallback(TIM_HandleTypeDef *htim); // Generic Update (rollover) callback which will call user callback

void updateRegistersIfNotRunning(TIM_TypeDef *TIMx); // Take into account registers update immediately if timer is not running,

bool isRunning(); // return true if HardwareTimer is running
bool isRunningChannel(uint32_t channel); // return true if channel is running

Expand Down
32 changes: 30 additions & 2 deletions libraries/SrcWrapper/src/HardwareTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ void HardwareTimer::setPrescaleFactor(uint32_t prescaler)
{
// Hardware register correspond to prescaler-1. Example PSC register value 0 means divided by 1
LL_TIM_SetPrescaler(_timerObj.handle.Instance, prescaler - 1);

updateRegistersIfNotRunning(_timerObj.handle.Instance);
}

/**
Expand Down Expand Up @@ -550,6 +552,8 @@ void HardwareTimer::setOverflow(uint32_t overflow, TimerFormat_t format)
ARR_RegisterValue = 0;
}
__HAL_TIM_SET_AUTORELOAD(&_timerObj.handle, ARR_RegisterValue);

updateRegistersIfNotRunning(_timerObj.handle.Instance);
}

/**
Expand Down Expand Up @@ -640,7 +644,7 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin)

/* Configure some default values. Maybe overwritten later */
channelOC.OCMode = TIMER_NOT_USED;
channelOC.Pulse = __HAL_TIM_GET_COMPARE(&(_timerObj.handle), timChannel); // keep same value already written in hardware <register
channelOC.Pulse = __HAL_TIM_GET_COMPARE(&(_timerObj.handle), timChannel); // keep same value already written in hardware register
channelOC.OCPolarity = TIM_OCPOLARITY_HIGH;
channelOC.OCFastMode = TIM_OCFAST_DISABLE;
#if defined(TIM_CR2_OIS1)
Expand Down Expand Up @@ -857,6 +861,8 @@ void HardwareTimer::setCaptureCompare(uint32_t channel, uint32_t compare, TimerC
}

__HAL_TIM_SET_COMPARE(&(_timerObj.handle), timChannel, CCR_RegisterValue);

updateRegistersIfNotRunning(_timerObj.handle.Instance);
}

/**
Expand Down Expand Up @@ -1082,7 +1088,8 @@ bool HardwareTimer::hasInterrupt(uint32_t channel)

/**
* @brief Generate an update event to force all registers (Autoreload, prescaler, compare) to be taken into account
* @note Refresh() can only be called after a 1st call to resume() to be sure timer is initialised.
* @note @note Refresh() can only be called after timer has been initialized,
either by calling setup() function or thanks to constructor with TIM instance parameter.
* It is useful while timer is running after some registers update
* @retval None
*/
Expand Down Expand Up @@ -1198,6 +1205,27 @@ bool HardwareTimer::isRunningChannel(uint32_t channel)
return (isRunning() && ret);
}

/**
* @brief Take into account registers update immediately if timer is not running,
* (independently from Preload setting)
* @param TIMx Timer instance
* @retval None
*/
void HardwareTimer::updateRegistersIfNotRunning(TIM_TypeDef *TIMx)
{
if (!isRunning()) {
if (LL_TIM_IsEnabledIT_UPDATE(TIMx)) {
// prevent Interrupt generation from refresh()
LL_TIM_DisableIT_UPDATE(TIMx);
refresh();
LL_TIM_ClearFlag_UPDATE(TIMx);
LL_TIM_EnableIT_UPDATE(TIMx);
} else {
refresh();
}
}
}

/**
* @brief HardwareTimer destructor
* @retval None
Expand Down

0 comments on commit f8bf924

Please sign in to comment.