I setup TIM3
on my STM32F411 using the following code:
void Enable_Timer_3()
{
__TIM3_CLK_ENABLE();
TIM3->PSC = 8399; // Prescaler value, 8400 - 1, 84 MhZ / 8400 = 10000 Hz
TIM3->EGR = TIM_EGR_UG; // Update prescaler
TIM3->ARR = 9999; // Period, 10000-1, 1 second period
TIM3->DIER = TIM_DIER_UIE; // Enable interrupts for this timer
TIM3->CR1 = TIM_CR1_CEN; // Enable this timer
}
The timer counts just fine - inspecting the TIM3->CNT
register shows it correctly counting from 0 to 9999 in the span of a second. But my interrupt handler is not called.
My handler:
void TIM3_IRQHandler(void)
{
TIM3->SR = 0;
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
}
It is set as the TIM3 interrupt in the interrupt vector:
.word TIM3_IRQHandler /* TIM3 */
Did I do something wrong?
2 Answers 2
I do not know much about coding.. but I cannot see the place where you are enabling the interrupts..
The interrupt for timer TIM3
can be enabled by adding the following line to the timer setup function:
NVIC_EnableIRQ(TIM3_IRQn);
-
1\$\begingroup\$ @Katharina Ah yes, the ARM core specific stuff that is never in the ST's manual. Interrupt priority is set similarly and won't be in ST's manual. \$\endgroup\$DKNguyen– DKNguyen2020年01月29日 14:26:59 +00:00Commented Jan 29, 2020 at 14:26
-
\$\begingroup\$ @DKNguyen Yea, it's quite annoying. I worked through the I2C section of the ST manual prior to this and it didn't mention this at all. \$\endgroup\$Katharina– Katharina2020年01月30日 23:31:00 +00:00Commented Jan 30, 2020 at 23:31
-
\$\begingroup\$ @Katharina If you think it has to do with the core and not the peripherals, don't expect to find it in the ST manual. Look at the materials from ARM. For example, the SysTick timer. \$\endgroup\$DKNguyen– DKNguyen2020年01月30日 23:32:37 +00:00Commented Jan 30, 2020 at 23:32
-
\$\begingroup\$ @DKNguyen Yea, this is all new to me still. I come from an AVR background. \$\endgroup\$Katharina– Katharina2020年01月30日 23:35:44 +00:00Commented Jan 30, 2020 at 23:35
-
\$\begingroup\$ @Katharina Not sure if AVR has this issue, but in ARM, if you are in C++ and the libraries in your compiler are in C, the interrupt handler in your code names will not be associated with the pre-designated interrupt handler names unless you use "extern C" to preserve the function name, due to C++ name function name mangling. It will just appear as if none of your interrupts are running with no errors (because they aren't, because they can't be found so the dummy interrupt handler runs while your interrupt handlers just sit there as functions that aren't called). \$\endgroup\$DKNguyen– DKNguyen2020年01月30日 23:37:50 +00:00Commented Jan 30, 2020 at 23:37
I was using CubeMX to generate the code, For some reason the line NVIC_EnableIRQ(TIM3_IRQn);
was missing in the timers setup function, adding this solved my issue.
Explore related questions
See similar questions with these tags.
TIM3
-Interrupt. If you want, you can post your comment as an answer and I'll accept it. \$\endgroup\$