Skip to content

HardwareTimer: call refresh() after parameter update when timer not running #1721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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