cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a specific register that would trigger an interrupt (timer)?

DGan.1554
Associate II

I am using STM32L476VG MCU.

I had set up TIM1 to generate an interrupt callback via callback function HAL_TIM_PeriodElapsedCallback.

And this work successfully.

After this CallBack function finished executing the codes, at the end of the code, I would like it to trigger another interrupt (immediately), for example TIM2_IRQHandler (or any other ISR).

Is there a specific register for TIM2 that I could set, for example TIM2->TF = 1, that would execute the function TIM2_IRQHandler immediately after returning from HAL_TIM_PeriodElapsedCallback?

Thank you in advance!

5 REPLIES 5

You can trigger events (and thus interrupts, if they are enabled for the given event) by writing 1into TIMx_EGR register's bit, see the description of this register in the RM, e.g.

TIM2->EGR = TIM_EGR_UG; // generate udate event

JW

S.Ma
Principal

You should not deviate from interrupt service routine purpose. The callback is already within an interrupt (check the call stack debug window under debuggin).

Interrupt are triggered by HW which require immediate attention. Interrupt disrupt the main loop or what is going on at top level.

If you want to do a timer chain reaction, prepare all your timers and just turn them on in the ISR or the previous one. Although there are timer to timer trigger HW signals (which I haven't played with yet).

DGan.1554
Associate II

Thank you JW and . (Never see your name username above ) for your reply!

Hello . ,

Based on your suggestion, can I implement it as follow? =>

(The reason why I cannot perform all task within TIM2 is because every 80ms, I will have to toggle a bit. And if I perform all tasks within TIM2, 80ms window might be miss..... therefore, I will need to perform the rest of the task within TIM3 and set TIM2 as higher priority than TIM3)

int main()

{

// Other init code

MX_TIM2_Init(); // Initialise the Prescaler, Period (80ms timer), CounterMode and then Init TIM2

MX_TIM3_Init(); // Initialise the Prescaler, Period, (1ms timer as need to trigger TIM3 immediately after TIM2 IRQ returned) CounterMode and then Init TIM3

HAL_TIM_Base_Start_IT(&htim2); //Start TIM2

while (1)

{

}

}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

if(htim->Instance == TIM2)

{

// toggle ONE bit //

// Reload TIM3 Prescaler, Period (as I never set it to reload automatically)

HAL_TIM_Base_Start_IT(&htim3);

}

if(htim->Instance == TIM3)

{

HAL_TIM_Base_Stop_IT(&htim3);

//Perform the rest of the task

}

}

DGan.1554
Associate II

Hello JW,

If I were to set the TIM3->EGR register's UG bit, which of the following functions will be call automatically? In the Reference Manual, it never states clearly.

TIM3_IRQHandler() ? HAL_TIM_PeriodElapsedCallback()?

Assuming that I had enabled TIM3 global interrupt.

Thank you!!!

> If I were to set the TIM3->EGR register's UG bit, which of the following functions will be call automatically?

TIM3_IRQHandler()

HAL_TIM_PeriodElapsedCallback() is called from Cube/HAL, assuming TIM3_IRQHandler() calls the appropriate Cube function (HAL_TIM_IRQHandler(&TimHandle)). Look at the examples in Cube.

I don't use Cube/HAL.

JW

PS. Please change your username to a normal nick.