Timer 1 is configured to trigger timer 2 after the the first timeout, then TIM2 interrupt will be triggered on each timeout. TIM1 only triggers TIM2 the first time, If for some reason you stop TIM2 you can again trigger its count with timer TIM1.
This is very useful when you want to delay the execution of any timer or to synchronize the beginning of several channels or timers like PWM, IC or OC, or just a delay.
#include "bsp.h"
int main( void )
{
TIM_HandleTypeDef TIM1_Handler = {0}; /*TIM 1 initial structure*/
TIM_MasterConfigTypeDef TIM1_Master = {0};/*TIM 1 Master configuration structure*/
TIM_HandleTypeDef TIM2_Handler = {0}; /*TIM 2 initial structure*/
TIM_SlaveConfigTypeDef TIM2_Slave = {0}; /*TIM 2 Slave configuration structure*/
__TIM1_CLK_ENABLE(); /*enable timer 1 clock*/
__TIM2_CLK_ENABLE(); /*enable timer 2 clock*/
HAL_Init(); /*Init HAL library*/
/*configure timer 1 with a preescaler of 1000 and to count up to 20000
by default the timer get a Tfrec of 16MHz from the APB bus*/
TIM1_Handler.Instance = TIM1; /*Timer TIM to configure*/
TIM1_Handler.Init.Prescaler = 1000; /*preescaler Tfrec / Prescaler*/
TIM1_Handler.Init.CounterMode = TIM_COUNTERMODE_UP; /*count from Period value to zero*/
TIM1_Handler.Init.Period = 20000; /*down count from 20000*/
/*use the previous parameters to set configuration on TIM1*/
HAL_TIM_Base_Init( &TIM1_Handler );
/*Configure TIM1 as Master to trigger a timer after the first timeout*/
TIM1_Master.MasterOutputTrigger = TIM_TRGO_UPDATE; /*trigger on each update event (timeout)*/
TIM1_Master.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; /*Timer TIM 1 as a master*/
/*set TIM1 as naster with this configuration*/
HAL_TIMEx_MasterConfigSynchronization( &TIM1_Handler, &TIM1_Master );
/*configure timer 2 with a preescaler of 1000 and to count up to 20000
by default the timer get a Tfrec of 16MHz from the APB bus*/
TIM2_Handler.Instance = TIM2; /*Timer TIM to configure*/
TIM2_Handler.Init.Prescaler = 1000; /*preescaler Tfrec / Prescaler*/
TIM2_Handler.Init.CounterMode = TIM_COUNTERMODE_UP; /*count from Period value to zero*/
TIM2_Handler.Init.Period = 20000; /*down count from 20000*/
/*use the previous parameters to set configuration on TIM2*/
HAL_TIM_Base_Init( &TIM2_Handler );
/*Configure TIM2 as slave to be triggered*/
TIM2_Slave.SlaveMode = TIM_SLAVEMODE_TRIGGER; /*start the timer on trigger*/
TIM2_Slave.InputTrigger = TIM_TS_ITR0; /*triggered by internal trigger 0*/
HAL_TIM_SlaveConfigSynchro( &TIM2_Handler, &TIM2_Slave );
/*Set the Master Slave configuration*/
HAL_TIMEx_MasterConfigSynchronization( &TIM2_Handler, &TIM1_Master );
/*IF YOU WNAT TO TRIGGER MORE TIMERS ONLY REPEAT THE PREVIOUS CONFIGURATION
just change TIM2_Slave.InputTrigger = TIM_TS_ITR0; with a differnet value for each trigger*/
/*Enable interrupt vector TIM2_IRQnwhere the CPU will jump in any of
the following events: TIM2 */
HAL_NVIC_SetPriority( TIM2_IRQn, 2, 0 );
HAL_NVIC_EnableIRQ( TIM2_IRQn );
/*start timer 2, but is not going to start counting until TIM1
reach its timeout*/
HAL_TIM_Base_Start_IT( &TIM2_Handler );
/*start timer 1*/
HAL_TIM_Base_Start( &TIM1_Handler );
while (1)
{
}
}
/* for this example only timer 2 has enable its interrupts, the code will
reach here after TIM1 + TIM2 timeout ONLY for the first time, after that
it will only after TIM2 timeout*/
void HAL_TIM_PeriodElapsedCallback( TIM_HandleTypeDef *htim )
{
/*TIM2 timeout, do something*/
}
inst.c
/*it is necesary to make a reference to TIMs control structures*/
extern TIM_HandleTypeDef TIM2_Handler;
/*Declare interrupt service rutine as it is declare in startup_stm32g0b1xx.s file*/
void TIM2_IRQHandler( void )
{
/*HAL library function that attend interrupt on TIM timers,
in this particular case we want the Timer2 and we pass the control structure
for timer 2*/
HAL_TIM_IRQHandler( &TIM2_Handler );
}