From 5d698588f244f442b55921ef04d2cd6e8993046a Mon Sep 17 00:00:00 2001 From: Alexandre Bourdiol Date: Wed, 18 May 2022 11:46:20 +0200 Subject: [PATCH] HardwareTimer: call refresh() after parameter update when timer not running 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 --- cores/arduino/HardwareTimer.h | 2 ++ libraries/SrcWrapper/src/HardwareTimer.cpp | 32 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/cores/arduino/HardwareTimer.h b/cores/arduino/HardwareTimer.h index 2b6027a941..d975ffb1b2 100644 --- a/cores/arduino/HardwareTimer.h +++ b/cores/arduino/HardwareTimer.h @@ -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 diff --git a/libraries/SrcWrapper/src/HardwareTimer.cpp b/libraries/SrcWrapper/src/HardwareTimer.cpp index 2f6bcf1608..9e948bc53a 100644 --- a/libraries/SrcWrapper/src/HardwareTimer.cpp +++ b/libraries/SrcWrapper/src/HardwareTimer.cpp @@ -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); } /** @@ -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); } /** @@ -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