Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why HAL_TIM_PeriodElapsedCallback gets called immediately after HAL_TIM_Base_Start_IT called?

Tags:

timer

hal

stm32

I have this timer setup

TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM2_Init 1 */

  /* USER CODE END TIM2_Init 1 */
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 80-1;
  htim2.Init.CounterMode = TIM_COUNTERMODE_DOWN;
  htim2.Init.Period = 4294967295;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }

Then I set counter value and start timer like so

__HAL_TIM_SET_COUNTER(&htim2, 10000000);
  HAL_TIM_Base_Start_IT(&htim2);

and HAL_TIM_PeriodElapsedCallback gets called immediately. Why so and how to avoid that?

like image 742
featherbits Avatar asked Sep 15 '25 06:09

featherbits


1 Answers

Had to clear TIM_IT_UPDATE bit from SR register before running HAL_TIM_Base_Start_IT. Using HAL: __HAL_TIM_CLEAR_IT(&htim2 ,TIM_IT_UPDATE)

like image 194
featherbits Avatar answered Sep 17 '25 20:09

featherbits