One timer ISR overflow will use to trigger another timer count with its own ISR active on overflow
#include "bsp.h"
int main( void )
{
TIM_HandleTypeDef TIM6_Handler = {0}; /*TIM 6 initial structure*/
TIM_HandleTypeDef TIM7_Handler = {0}; /*TIM 7 initial structure*/
HAL_Init(); /*Init HAL library*/
/*configure timer 6 with a preescaler of 1000 and to count up to 20000
by default the timer get a Tfrec of 16MHz from the APB bus*/
TIM6_Handler.Instance = TIM6; /*Timer TIM to configure*/
TIM6_Handler.Init.Prescaler = 1000; /*preescaler Tfrec / Prescaler*/
TIM6_Handler.Init.CounterMode = TIM_COUNTERMODE_UP; /*count from Period value to zero*/
TIM6_Handler.Init.Period = 20000; /*down count from 20000*/
/*use the previous parameters to set configuration on TIM6*/
HAL_TIM_Base_Init( &TIM6_Handler );
/*configure timer 7 with a preescaler of 1000 and to count up to 4000
by default the timer get a Tfrec of 16MHz from the APB bus*/
TIM6_Handler.Instance = TIM7; /*Timer TIM to configure*/
TIM6_Handler.Init.Prescaler = 1000; /*preescaler Tfrec / Prescaler*/
TIM6_Handler.Init.CounterMode = TIM_COUNTERMODE_UP; /*count from Period value to zero*/
TIM6_Handler.Init.Period = 4000; /*down count from 4000*/
/*use the previous parameters to set configuration on TIM7*/
HAL_TIM_Base_Init( &TIM7_Handler );
/*init the timer 6 count from zero to overflow value (20000 in this case) and trigger an interrupts*/
HAL_TIM_Base_Start_IT( &TIM6_Handler );
while (1)
{
}
}
/* when the timer TIM6 ot 7 reaches its count overflow value this function will be called by
HAL_TIM_IRQHandler which is in turn called from the TIM6_DAC_LPTIM1_IRQHandler or
TIM7_LPTIM2_IRQHandler interrupt vectors*/
void HAL_TIM_PeriodElapsedCallback( TIM_HandleTypeDef *htim )
{
/*since we are using the same callback for both vector interrupts we need to inquire
which timer triggered the interrupt*/
if( htim->Instance == TIM6 )
{
/*if TIM6, start timer 7*/
HAL_TIM_Base_Start_IT( htim );
}
if( htim->Instance == TIM7 )
{
/*if TIM7, stop timer 7*/
HAL_TIM_Base_Stop_IT( htim );
}
}
msps.c
#include "bsp.h"
/*This fucntion is called from HAL_TIM_Init at the very begining and it is use in our case
to enable both TIM clocks and its corresponding ISR vectors*/
void HAL_TIM_Base_MspInit( TIM_HandleTypeDef *htim )
{
/*we need to figure it out wich call to HAL_TIM_Init function was*/
if( htim->Instance == TIM6 )
{
__TIM6_CLK_ENABLE(); /*enable timer 6 clock*/
/*Enable interrupt vector TIM6_DAC_LPTIM1_IRQ where the CPU will jump in any of
the following events: TIM6, DAC and LPTIM1 */
HAL_NVIC_SetPriority( TIM6_DAC_LPTIM1_IRQn, 2, 0 );/*se fija la prioridad a nivel 2*/
HAL_NVIC_EnableIRQ( TIM6_DAC_LPTIM1_IRQn );
}
if( htim->Instance == TIM7 )
{
__TIM7_CLK_ENABLE(); /*enable timer 7 clock*/
/*Enable interrupt vector TIM7_LPTIM2_IRQ where the CPU will jump in any of
the following events: TIM7 and LPTIM2 */
HAL_NVIC_SetPriority( TIM7_LPTIM2_IRQn, 2, 0 );/*se fija la prioridad a nivel 2*/
HAL_NVIC_EnableIRQ( TIM7_LPTIM2_IRQn );
}
}
ints.c
/*it is necesary to make a reference to TIMs control structures*/
extern TIM_HandleTypeDef TIM6_Handler;
extern TIM_HandleTypeDef TIM7_Handler;
/*Declare interrupt service rutine as it is declare in startup_stm32g0b1xx.s file*/
void TIM6_DAC_LPTIM1_IRQHandler( void )
{
/*HAL library function that attend interrupt on TIM timers,
in this particular case we want the Timer6 and we pass the control structure
for timer 6*/
HAL_TIM_IRQHandler( &TIM6_Handler );
}
/*Declare interrupt service rutine as it is declare in startup_stm32g0b1xx.s file*/
void TIM7_LPTIM2_IRQHandler( void )
{
/*HAL library function that attend interrupt on TIM timers,
in this particular case we want the Timer6 and we pass the control structure
for timer 7*/
HAL_TIM_IRQHandler( &TIM7_Handler );
}