AnsweredAssumed Answered

Why is my timer 4 interrupt issued as soon as I enable the interrupt?

Question asked by arnold_w on Feb 1, 2016
Latest reply on Feb 1, 2016 by arnold_w
I want to generate an interrupt after 2 seconds and I am running at 32 MHz. However, my interrupt is issued as soon as I enable the interrupt. What am I doing wrong?

void testTimer4(EventCallback_t timerInterruptCallback)
{
    timerInterruptCallback_ = timerInterruptCallback;
    RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;
    TIM4->PSC = 976;
    TIM4->ARR = 65505;
    TIM4->CNT = 0;
    TIM4->SR &= (~TIM_SR_UIF);
    TIM4->DIER |= TIM_DIER_UIE;
    HAL_NVIC_EnableIRQ(TIM4_IRQn);
    TIM4->CR1 |= TIM_CR1_CEN;
}
 
void TIM4_IRQHandler(void)
{
    TIM4->SR &= (~TIM_SR_UIF);
    HAL_NVIC_DisableIRQ(TIM4_IRQn);
    TIM4->DIER &= (~TIM_DIER_UIE);
    TIM4->CR1  &= (~TIM_CR1_CEN);
    RCC->APB1ENR &= (~RCC_APB1ENR_TIM4EN);
    (void)timerInterruptCallback_((uint32_t)TIMER_4_EXPIRED, 0);
}

Outcomes