diff --git a/src/main/drivers/pwm_output_dshot.c b/src/main/drivers/pwm_output_dshot.c index 4b53f7dd1..a5d0c5343 100644 --- a/src/main/drivers/pwm_output_dshot.c +++ b/src/main/drivers/pwm_output_dshot.c @@ -139,7 +139,7 @@ void pwmDigitalMotorHardwareConfig(const timerHardware_t *timerHardware, uint8_t RCC_ClockCmd(timerRCC(timer), ENABLE); TIM_Cmd(timer, DISABLE); - TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)((SystemCoreClock / timerClockDivisor(timer) / getDshotHz(pwmProtocolType)) - 1); + TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)((timerClock(timer) / getDshotHz(pwmProtocolType)) - 1); TIM_TimeBaseStructure.TIM_Period = MOTOR_BITLENGTH; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; diff --git a/src/main/drivers/pwm_output_dshot_hal.c b/src/main/drivers/pwm_output_dshot_hal.c index 9903b58df..6c38b57bc 100644 --- a/src/main/drivers/pwm_output_dshot_hal.c +++ b/src/main/drivers/pwm_output_dshot_hal.c @@ -123,7 +123,7 @@ void pwmDigitalMotorHardwareConfig(const timerHardware_t *timerHardware, uint8_t RCC_ClockCmd(timerRCC(timer), ENABLE); motor->TimHandle.Instance = timerHardware->tim; - motor->TimHandle.Init.Prescaler = (SystemCoreClock / timerClockDivisor(timer) / getDshotHz(pwmProtocolType)) - 1; + motor->TimHandle.Init.Prescaler = (timerClock(timer) / getDshotHz(pwmProtocolType)) - 1;; motor->TimHandle.Init.Period = MOTOR_BITLENGTH; motor->TimHandle.Init.RepetitionCounter = 0; motor->TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; diff --git a/src/main/drivers/timer.c b/src/main/drivers/timer.c index 9d03e215e..55c62fcee 100755 --- a/src/main/drivers/timer.c +++ b/src/main/drivers/timer.c @@ -850,10 +850,10 @@ uint16_t timerGetPrescalerByDesiredMhz(TIM_TypeDef *tim, uint16_t mhz) if ((uint32_t)(mhz * 1000000) > (SystemCoreClock / timerClockDivisor(tim))) { return 0; } - return (uint16_t)(round((SystemCoreClock / timerClockDivisor(tim) / (mhz * 1000000)) - 1)); + return (uint16_t)(round((timerClock(tim) / (mhz * 1000000)) - 1)); } uint16_t timerGetPeriodByPrescaler(TIM_TypeDef *tim, uint16_t prescaler, uint32_t hertz) { - return ((uint16_t)((SystemCoreClock / timerClockDivisor(tim) / (prescaler + 1)) / hertz)); + return ((uint16_t)((timerClock(tim) / (prescaler + 1)) / hertz)); } \ No newline at end of file diff --git a/src/main/drivers/timer.h b/src/main/drivers/timer.h index 2f5db50ab..2292ff6a7 100644 --- a/src/main/drivers/timer.h +++ b/src/main/drivers/timer.h @@ -181,6 +181,7 @@ void timerStart(void); void timerForceOverflow(TIM_TypeDef *tim); uint8_t timerClockDivisor(TIM_TypeDef *tim); +uint32_t timerClock(TIM_TypeDef *tim); void configTimeBase(TIM_TypeDef *tim, uint16_t period, uint8_t mhz); // TODO - just for migration diff --git a/src/main/drivers/timer_stm32f10x.c b/src/main/drivers/timer_stm32f10x.c index 65214199d..5f8fd63c3 100644 --- a/src/main/drivers/timer_stm32f10x.c +++ b/src/main/drivers/timer_stm32f10x.c @@ -49,3 +49,9 @@ uint8_t timerClockDivisor(TIM_TypeDef *tim) UNUSED(tim); return 1; } + +uint32_t timerClock(TIM_TypeDef *tim) +{ + UNUSED(tim); + return SystemCoreClock; +} diff --git a/src/main/drivers/timer_stm32f30x.c b/src/main/drivers/timer_stm32f30x.c index c168e4661..ddc140285 100644 --- a/src/main/drivers/timer_stm32f30x.c +++ b/src/main/drivers/timer_stm32f30x.c @@ -41,3 +41,9 @@ uint8_t timerClockDivisor(TIM_TypeDef *tim) UNUSED(tim); return 1; } + +uint32_t timerClock(TIM_TypeDef *tim) +{ + UNUSED(tim); + return SystemCoreClock; +} diff --git a/src/main/drivers/timer_stm32f4xx.c b/src/main/drivers/timer_stm32f4xx.c index 06c8acbdc..93786cd39 100644 --- a/src/main/drivers/timer_stm32f4xx.c +++ b/src/main/drivers/timer_stm32f4xx.c @@ -91,3 +91,15 @@ uint8_t timerClockDivisor(TIM_TypeDef *tim) return 2; } } + +uint32_t timerClock(TIM_TypeDef *tim) +{ +#if defined (STM32F40_41xxx) + if (tim == TIM8) return SystemCoreClock; +#endif + if (tim == TIM1 || tim == TIM9 || tim == TIM10 || tim == TIM11) { + return SystemCoreClock; + } else { + return SystemCoreClock / 2; + } +} diff --git a/src/main/drivers/timer_stm32f7xx.c b/src/main/drivers/timer_stm32f7xx.c index 69c864c72..5059d1bbc 100644 --- a/src/main/drivers/timer_stm32f7xx.c +++ b/src/main/drivers/timer_stm32f7xx.c @@ -80,3 +80,9 @@ uint8_t timerClockDivisor(TIM_TypeDef *tim) UNUSED(tim); return 1; } + +uint32_t timerClock(TIM_TypeDef *tim) +{ + UNUSED(tim); + return SystemCoreClock; +}