Available timers

Hi @Ivan,
about timers: which are available and not used by your routines?

Hi @Mangaman,

TIM9 and TIM10 are free to be used. Also TIM4 when the LedStrip library is not in use.

You can also use TIM1, TIM2 and TIM3 if you don’t use the analogWrite function. Or better, you can check the timers used to generate PWM on each pin: in the \variants\propboard_v1\variant.cpp file, there is a table that maps every PWM pin to a timer. For example, you can use TIM3 as long as you don’t call analogWrite on pins 4, 5, 10 and 11.

1 Like

Sorry @Ivan, I’m asking for help.
The scope is to generate an interrupt each second and execute some code.
I tried with TIM2 and TIM5 and it works, but I can’t use TIM9.
Do you have any advice?

Sorry but I’m not expert with this.

Thanks

Here is the code for TIM5:

#include <stm32f4xx_gpio.h>
#include <stm32f4xx_tim.h>
#include <stm32f4xx_rcc.h>
#include <misc.h>

void setup() 
{
  Serial.begin(9600);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);

  NVIC_InitTypeDef nvicStructure;
  nvicStructure.NVIC_IRQChannel = TIM5_IRQn;
  nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
  nvicStructure.NVIC_IRQChannelSubPriority = 1;
  nvicStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&nvicStructure);
  NVIC_EnableIRQ(TIM5_IRQn);

  TIM_TimeBaseInitTypeDef timerInitStructure; 
  timerInitStructure.TIM_Prescaler = 42000 -1;              // CLK 84MHz diviso 42000 -> 2KHz (500 usec)
  timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  timerInitStructure.TIM_Period = 2000 -1;                  // Durata 2000 ticks -> 1" interrupt
  timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  timerInitStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIM5, &timerInitStructure);
  TIM_Cmd(TIM5, ENABLE);
  TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);

}

void loop() 
{
  while(1);
  
}
extern "C" void TIM5_IRQHandler()
{
    if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM5, TIM_IT_Update);
        Serial.println(millis());
    }
}

And this is the code for TIM9:

#include <stm32f4xx_gpio.h>
#include <stm32f4xx_tim.h>
#include <stm32f4xx_rcc.h>
#include <misc.h>

void setup() 
{
  Serial.begin(9600);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);

  NVIC_InitTypeDef nvicStructure;
  nvicStructure.NVIC_IRQChannel = TIM1_BRK_TIM9_IRQn;
  nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
  nvicStructure.NVIC_IRQChannelSubPriority = 1;
  nvicStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&nvicStructure);
  NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);

  TIM_TimeBaseInitTypeDef timerInitStructure; 
  timerInitStructure.TIM_Prescaler = 42000 -1;              // CLK 84MHz diviso 42000 -> 2KHz (500 usec)
  timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  timerInitStructure.TIM_Period = 2000 -1;                  // Durata 2000 ticks -> 1" interrupt
  timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  timerInitStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIM9, &timerInitStructure);
  TIM_Cmd(TIM9, ENABLE);
  TIM_ITConfig(TIM9, TIM_IT_Update, ENABLE);

}

void loop() 
{
  while(1);
  
}
extern "C" void TIM9_IRQHandler()
{
    if (TIM_GetITStatus(TIM9, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM9, TIM_IT_Update);
        Serial.println(millis());
    }
}

Hi @Mangaman

Try changing this:

To

extern “C” void TIM1_BRK_TIM9_IRQHandler

And be careful with TIM5 that is connected to the LED drivers. :smile:

Changed, same result…

Strange. It works here. This is the code:

#include <stm32f4xx_gpio.h>
#include <stm32f4xx_tim.h>
#include <stm32f4xx_rcc.h>
#include <misc.h>

void setup() 
{
  Serial.begin(9600);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);

  NVIC_InitTypeDef nvicStructure;
  nvicStructure.NVIC_IRQChannel = TIM1_BRK_TIM9_IRQn;
  nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
  nvicStructure.NVIC_IRQChannelSubPriority = 1;
  nvicStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&nvicStructure);
  NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);

  TIM_TimeBaseInitTypeDef timerInitStructure; 
  timerInitStructure.TIM_Prescaler = 42000 -1;              // CLK 84MHz diviso 42000 -> 2KHz (500 usec)
  timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  timerInitStructure.TIM_Period = 2000 -1;                  // Durata 2000 ticks -> 1" interrupt
  timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  timerInitStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIM9, &timerInitStructure);
  TIM_Cmd(TIM9, ENABLE);
  TIM_ITConfig(TIM9, TIM_IT_Update, ENABLE);

}

void loop() 
{
  while(1);
  
}

extern "C" void TIM1_BRK_TIM9_IRQHandler()
{
    if (TIM_GetITStatus(TIM9, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM9, TIM_IT_Update);
        Serial.println(millis());
    }
}

And outputs something like this:

1100
2100
3100
4100
5100
6100
7100
8100
9100
10100

Thanks @Ivan it works.
Classical typing error…:grin: