AnsweredAssumed Answered

Is safe update CNT, ARR and CCR with the timer stopped?

Question asked by Laureg on May 29, 2017
Latest reply on Aug 28, 2017 by Laureg



I am using PWM 2 mode to generate step pulses for a step/direction interface (and counting pulses thru the timer update interrupt), 


But sometimes I am loosing the first step because of the preload of the CCR1 (update happens but the step was not given)


After reading the RM, I found that I can update the CCR value directly when I disable the preload (OC1PE), but the RM also warns about undefined behavior in PWM mode:


So, for testing I used the following code for start the pulsing:


     /* RESET the Preload enable bit for channel1 */
     (&XTIMPERIOD)->Instance->CCMR1 &= ~TIM_CCMR1_OC1PE;
     __HAL_TIM_SET_COUNTER(&XTIMPERIOD, 1); /* Reset period counter value */
     __HAL_TIM_SET_AUTORELOAD(&XTIMPERIOD, (1000000/(uint32_t)actual_step_freq)-1); /* Adjust pulse period to step period */
     __HAL_TIM_SET_COMPARE(&XTIMPERIOD, TIM_CHANNEL_1, (500000/(uint32_t)actual_step_freq) ); /* Adjust toggle period to step period/2 */

     /* Set the Preload enable bit for channel1 */
     (&XTIMPERIOD)->Instance->CCMR1 |= TIM_CCMR1_OC1PE;

     HAL_TIM_PWM_Start(&XTIMPERIOD, TIM_CHANNEL_1); /* start toggling */


And is working flawlessly, but I want to confirm that this is safe to be used in this way (remembering that everytime I execute this code the timer & the PWM output will be stopped)


(for doing the acceleration and decceleration I am also changing the ARR and CCR on the fly inside the interrupt, but in this case with the preload enabled and knowing that the changes will be applied only after the next update event/interrupt)


Thank you.