Why STM32H7 PWM Timer does not start immediately after reconfiguration?
Hi,
I am using STM32H7's Timer 2 peripheral to generate a clock frequency ranging from 10Hz to 1MHz.
To reconfigure the PWM to attain certain frequency, the PSC, ARR and CCR1 registers are modified.
The sequence for modification is as follows: PWM_Start ==> PWM_Stop ==> Config ==> PWM_Start.
When I follow this sequence, jumping frequencies upwards doesn't work. For e.g. when I go from 10Hz to 100kHz, the PWM seems to start after 15 seconds.
But, going downwards always works.
As a workaround, I am de-initializing and initializing the Timer2 peripheral. This seems to fix the issue.
But I still wonder why going upwards by just changing PSC, ARR and CCR1(Pulse) doesn't work.
#define HANDLE_TIMER_DAC_CLOCK htim2
bool Clock_Start()
{
ret = HAL_TIM_PWM_Start(&HANDLE_TIMER_DAC_CLOCK, TIM_CHANNEL_1);
if (ret != HAL_OK)
{
LOG_ERROR("[DAC] Failed to start PWM %lu", ret);
Error_Handler();
}
return true;
}
bool DAC_Stop()
{
uint32_t ret = 0;
ret = HAL_TIM_PWM_Stop(&HANDLE_TIMER_DAC_CLOCK, TIM_CHANNEL_1);
if (ret != HAL_OK)
{
LOG_ERROR("[Clock] Failed to stop PWM : %lu", ret);
Error_Handler();
}
return true;
}
static void ConfigTimer(TIM_HandleTypeDef* htim,
dac_frequency_map_t* pMap,
uint32_t* pTimerPeriod)
{
uint32_t timerPrescaler = 0;
uint32_t timerClockFreq = HAL_RCC_GetSysClockFreq() / 2;
uint32_t timerPeriod = timerClockFreq / ( pMap->requestFreqHz);
while ((timerPeriod / (timerPrescaler + 1)) > UINT16_MAX)
{
timerPrescaler++;
}
timerPeriod = timerPeriod / (timerPrescaler + 1);
__HAL_TIM_SET_PRESCALER(htim, timerPrescaler);
__HAL_TIM_SET_AUTORELOAD(htim, timerPeriod);
TIM2->CCR1 = (timerPeriod / 2);
}
