HAL Timer Interrupt Counter Reset
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2018-02-02 11:43 AM
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-timerSolved! Go to Solution.
- Labels:
-
STM32L4 series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2018-02-02 12:25 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2018-02-02 12:25 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2018-02-02 12:49 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2018-02-07 11:25 AM
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);
