cancel
Showing results for 
Search instead for 
Did you mean: 

Confusion with interrupts and handlers in HAL

MPera
Associate

I'm using stm32f103c6, i'm confused as to how should i handle the interrups, what i need is to count pulses from an external source, it has to be accurate so the best way is to do it with timers right? So i've configured the cnt_en from timer2 to act as trigger output for timer1 (gated mode), it will start counting until cnt_en goes down when timer2 reaches 1second (internal clock presscaller set to 36000-1 with the bus clock set to 36Mhz).

In the nvic tab i enabled:

-timer1: trigger and conmutation interrups and the update interrupt

-timer2: global interrupt

All this was done with cubeMx V4.27

My question is: what should i use to handle those interrupts? If i hover the mouse over the interrupt names in cubemx it shows the name if the interrupt handler, or what is it? For example i see for tim1:

HAL_TIM_IRQHandler

TIM1_TRG_COM_IRQn

TIM1_TRG_COM_IRQHandler

TIM1_TRG_COM_TIM11_IRQHandler

and for tim2: TIM2_IRQn

what i want to know is when tim2 ends so that i can stop it and get the count from timer1 and if timer1 overflows before the windows ends, with this i can reduce the time window or set the input prescaller for timer1 and try again.

Furthermore, i saw in stm32f1xx_hal_msp.c the function HAL_TIM_Base_MspInit

by it's content i assume that it sets the nvic and gpios etc, should it be called only once or every time i want to start the timer?

Sorry if this are basic questions, i searched a lot and asked my teachers (electronic engeneers, but they are useless).

Hope i explained myself correctly, as you may have already guessed, english is not my first language.

Attached is the timer init code generated by cubemx.

static void MX_TIM1_Init(void)
{
 
  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_SlaveConfigTypeDef sSlaveConfig;
  TIM_MasterConfigTypeDef sMasterConfig;
 
  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 0xffff;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_ETRMODE2;
  sClockSourceConfig.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED;
  sClockSourceConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
  sClockSourceConfig.ClockFilter = 0;
  if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
  sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED;
  sSlaveConfig.InputTrigger = TIM_TS_ITR1;
  if (HAL_TIM_SlaveConfigSynchronization(&htim1, &sSlaveConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
}
 
/* TIM2 init function */
static void MX_TIM2_Init(void)
{
 
  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_MasterConfigTypeDef sMasterConfig;
  TIM_OC_InitTypeDef sConfigOC;
 
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 36000-1;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 0xffff;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
  if (HAL_TIM_OC_Init(&htim2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
  sConfigOC.OCMode = TIM_OCMODE_TIMING;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
}

1 REPLY 1
S.Ma
Principal

Use the timer channel 1 or 2 to put your signal as the timer clock.

Start the counting from zero, wait 1 second, read the result.

If you don't need accuracy, you can probably run this by polling and no interrupt to get started.

Then just get the 1 second overflow to read the first edge counter timer and make a relative difference.