Simplified getting timerClock

This commit is contained in:
blckmn 2017-05-11 13:16:58 +10:00
parent 681b5e79b6
commit 0d13736e2f
8 changed files with 35 additions and 4 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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));
}

View File

@ -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

View File

@ -49,3 +49,9 @@ uint8_t timerClockDivisor(TIM_TypeDef *tim)
UNUSED(tim);
return 1;
}
uint32_t timerClock(TIM_TypeDef *tim)
{
UNUSED(tim);
return SystemCoreClock;
}

View File

@ -41,3 +41,9 @@ uint8_t timerClockDivisor(TIM_TypeDef *tim)
UNUSED(tim);
return 1;
}
uint32_t timerClock(TIM_TypeDef *tim)
{
UNUSED(tim);
return SystemCoreClock;
}

View File

@ -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;
}
}

View File

@ -80,3 +80,9 @@ uint8_t timerClockDivisor(TIM_TypeDef *tim)
UNUSED(tim);
return 1;
}
uint32_t timerClock(TIM_TypeDef *tim)
{
UNUSED(tim);
return SystemCoreClock;
}