STM32G474 TIM1 Update Event Triggered Twice In a Row and Channels Enable Was Delayed
void TIM1_UP_TIM16_IRQHandler (void) {
volatile uint8_t t;
uint32_t sr;
sr = TIM1->SR;
sr &= ~TIM_SR_UIF;
gpio_set(TIM1_PORT, TIM1_PIN);
gpio_clr(TIM1_PORT, TIM1_PIN);
TIM1->SR = sr;
while (TIM1->SR & TIM_SR_UIF);
}Above is the code for TIM1 Update Event Interrupt Handler. My repetition counter register TIM1_RCR value is set to 1, and I configured this to be center-aligned PWM mode.
I am triggering TIM1_PORT/TIM1_PIN GPIO just to see the interrupt happening on the logic analyzer, and I expected to see this happens every twice when overflow/underflow happens (RCR is 1)
However, as shown in the picture attached, this interrupt happens "twice in a row" IF above line 10 while loop is commented out? And when it is not commented out, it only appears once.
So the question is why did this delay happen when trying to clear the status register TIM1_SR?
Also, from this picture below, the red arrow shows that in the very beginning, when the first interrupt of Update Event happens, no Channel PWM was shown from the output pins. These channels were enabled by writing to the CCER register and BDTR register MOE bit before NVIC of this update event interrupt was even enabled.
Does this channel enable also require delay and why?
Thanks!
