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