HAL Timer Interrupt Counter Reset
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:
- If I want to reset the counter mid-count is this the proper way to do it: __HAL_TIM_SET_COUNTER(&hTimAsk, 0);
- 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