Interrupt behavior and its priority
Hello,
I have a basic question regarding timer behavior in STM32.
I have a common callback function HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) that handles timer interrupts at defined periods. I have set different priorities for timer global interrupts in my CubeMX NVIC configuration.
My questions are:
Does setting the TIMx global interrupt preemption priority configure the priority for all interrupts generated by the timer (e.g., HAL_TIM_PeriodElapsedCallback, HAL_TIM_TriggerCallback, TIMx_IRQHandler)?
What is the difference between TIMx_IRQHandler and HAL_TIM_PeriodElapsedCallback?
If I use a common callback function HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) and check the timer instance, will it respect the interrupt priorities and return to the currently running task? For example, if a higher priority timer interrupt occurs while a lower priority timer callback is being executed, will the CPU handle the higher priority interrupt first, then resume and complete the lower priority ISR from where it was interrupted?
Thank you!
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM1) {
// Handle Timer 1 interrupt
// Code specific to Timer 1
} else if (htim->Instance == TIM2) {
// Handle Timer 2 interrupt
// Code specific to Timer 2
}
}
void TIM1_IRQHandler(void) {
HAL_TIM_IRQHandler(&htim1); // Calls the HAL_TIM_PeriodElapsedCallback with htim1
}
void TIM2_IRQHandler(void) {
HAL_TIM_IRQHandler(&htim2); // Calls the HAL_TIM_PeriodElapsedCallback with htim2
}
int main(void) {
HAL_Init();
SystemClock_Config();
// Timer configurations
MX_TIM1_Init();
MX_TIM2_Init();
// Set priorities
HAL_NVIC_SetPriority(TIM1_UP_IRQn, 3, 0); // Higher priority
HAL_NVIC_SetPriority(TIM2_IRQn, 5, 0); // Lower priority
// Enable IRQs
HAL_NVIC_EnableIRQ(TIM1_UP_IRQn);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
while (1) {
// Main loop
}
}
