Why is there so much latency in my interrupt?
I am working with the STM32 Nucleo-64 development board, which has an STM32L476R microcontroller. My SystemCoreClock is 16 MHz and TIM17 is clocked at 4 MHz. To my surprise, the code below only works well (the timer doesn't miss the next interrupt and wraps around) if I increment with at least 23:
#pragma GCC push_options
#pragma GCC optimize ("O3")
void TIM1_TRG_COM_TIM17_IRQHandler(void) {
GPIOC->BSRR = 0x00000400; // Set test pin PC10 high
GPIOC->BSRR = 0x04000000; // Set test pin PC10 low
TIM17->SR = 0; // Clear interrupt flags
TIM17->CCR1 += 23; // OK
// TIM17->CCR1 += 22; // Not ok
}
#pragma GCC pop_optionsNow, 23 timer ticks corresponds to 23 x 4 = 92 CPU clock cycles and it seems unlikely that the 4 lines of code would occupy 92 instructions. When I store the TIM17->CNT value in a global variable first thing in the interrupt routine above I can see that TIM17->CNT is 8 (!) more than TIM17->CCR1 meaning it took roughly 8 x 4 = 32 CPU clock instructions just to enter the interrupt routine! I tried to put the interrupt vector in RAM, but that made it worse! What am I doing wrong, why is there so much latency in my interrupt?
