cancel
Showing results for 
Search instead for 
Did you mean: 

HAL Timer Interrupt Counter Reset

Konami
Senior II
Posted on February 02, 2018 at 20:43

I'm trying to configure timer 7 in one-shot mode on the STM32L431 and am having an issue with my timer IRQ triggering as soon as I enable the interrupt. It seems like its preloaded with the period value, however I have the Auto Preload Reload disabled. This only happens the first time I enable the function. After that the code works as expected with my below functions. I have two questions:

  1. If I want to reset the counter mid-count is this the proper way to do it: __HAL_TIM_SET_COUNTER(&hTimAsk, 0);
  2. Am I missing something with my initialization that's causing the IRQ to trigger as soon as the interrupt is enabled?

// Restart timer function

void timerStartAskTimeout(void)

{

  HAL_TIM_Base_Stop_IT(&hTimAsk);    

  __HAL_TIM_SET_COUNTER(&hTimAsk, 0);

  HAL_TIM_Base_Start_IT(&hTimAsk);

}   

// Timer Initialization Function

void timerInitializeAsk(void)

{

  TIM_MasterConfigTypeDef sMasterConfig;

  hTimAsk.Instance = TIM7;

  hTimAsk.Init.Prescaler = (SYS_CLOCK_HZ / 1000) - 1;     /* 1000Hz (1ms) */

  hTimAsk.Init.CounterMode = TIM_COUNTERMODE_UP;

  hTimAsk.Init.Period = 1000;                              /* 250ms */

  hTimAsk.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

  if (HAL_TIM_Base_Init(&hTimAsk) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

  if (HAL_TIM_OnePulse_Init(&hTimAsk, TIM_OPMODE_SINGLE) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

 

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  if (HAL_TIMEx_MasterConfigSynchronization(&hTimAsk, &sMasterConfig) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

}

#stm32l4-hal-timer
1 ACCEPTED SOLUTION

Accepted Solutions
Jan Waclawek
Senior II
Posted on February 02, 2018 at 21:25

I guess for configuration of timer you use a Cube function which deliberately sets the Update event through TIMx_EGR (in order to make effective any prescaler change).

In other words, clear the update flag in TIMx_SR before you enable the timer (I don't Cube so don't know the incantation for that, sorry).

JW

View solution in original post

3 REPLIES 3
Jan Waclawek
Senior II
Posted on February 02, 2018 at 21:25

I guess for configuration of timer you use a Cube function which deliberately sets the Update event through TIMx_EGR (in order to make effective any prescaler change).

In other words, clear the update flag in TIMx_SR before you enable the timer (I don't Cube so don't know the incantation for that, sorry).

JW

henry.dick
Senior II
Posted on February 02, 2018 at 21:49

I would 2nd JW's suggestion. I think forcing an update during initialization is a safer way of doing things but needs to be better documented and understood by the programmer. It is typical to reset the flags during initialization to avoid jumping to the ISR right away.

BTW, with all this trouble, it may be worth it to just poke at the registers directly.

Posted on February 07, 2018 at 19:25

Thanks, that seems to have done the trick. For reference the below line will clear the update flag:

__HAL_TIM_CLEAR_IT(&hTimAsk ,TIM_IT_UPDATE);