As a testBefore HAL_TIM_Base_Start_ITReset the UIF flag by TIM17->SR &= ~TIM_SR_UIF Answer from dspusr on community.st.com
🌐
Reddit
reddit.com › r/stm32f4 › timer interrupts and hal_tim_periodelapsedcallback
r/stm32f4 on Reddit: Timer interrupts and HAL_TIM_PeriodElapsedCallback
March 30, 2022 -

Greetings all.

I am having an issue with getting the timer interrupts to trigger, as in, they do not enter the if-statements from user code 4. I have debugged it and cycled through several times with breakpoints at the if-statements but they will not enter. The file in question is the main.c file. I have copied in what I think is relevant, but I might be wrong about that. The setup for the timers are set in Cube IDE Version 1.9.0. I am using a STM32F407VGTxLQFP100.

TLDR: The if-statements arent being entered

I hope this wasn't too confusing.

From int main(void)

HAL_TIM_Base_Start_IT(&htim6);
  HAL_TIM_Base_Start_IT(&htim13);
  TIM3->CCR1 = 950;
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);


static void MX_TIM6_Init(void)
{

  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM6_Init 1 */

  /* USER CODE END TIM6_Init 1 */
  htim6.Instance = TIM6;
  htim6.Init.Prescaler = 16-1;
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim6.Init.Period = 50000-1;
  htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};

  /* USER CODE BEGIN TIM3_Init 1 */

  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 16-1;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 1000-1;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

static void MX_TIM13_Init(void)
{

  /* USER CODE BEGIN TIM13_Init 0 */

  /* USER CODE END TIM13_Init 0 */

  /* USER CODE BEGIN TIM13_Init 1 */

  /* USER CODE END TIM13_Init 1 */
  htim13.Instance = TIM13;
  htim13.Init.Prescaler = 16000-1;
  htim13.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim13.Init.Period = 100-1;
  htim13.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim13.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

From user code 4

/* USER CODE BEGIN 4 */


void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	// Timer for å ta inn ADC målinger og sende dem på CAN
	if(htim->Instance==&htim6)
	{
		 HAL_GPIO_TogglePin(GPIOE, EN_sikring_1300W_Pin);


	}
	// Timer for så resette sikringen, KANSKJE man skal vurdere å bruke to timere, en for 1300W sikring reset, og en for 240W sikring reset?
	if(htim->Instance==&htim13)
	{
		if (counter > 100)
		{
			HAL_TIM_Base_Stop_IT(&htim13);
		}
		else
		{
			HAL_GPIO_TogglePin(GPIOE,EN_sikring_240W_Pin);
		}
		counter++;
	}
	if(htim->Instance==&htim3)
		{

		counter++;
			TIM3->CCR1 = counter;
		}
}

/* USER CODE END 4 */
Discussions

Timer isn't accurate
What is the clock source for the timer? most internal RC clocks are ~2-3% accurate. so you may see a little slop from that, but wouldn't account for a 10% difference. How are you measuring the ~17ms interval? From the start of the UART transmit to the start of the next one? Or are you measuring from the end of the transmission to the start of the next transmission? If the latter you need to keep in mind that once your timer triggers the timer restarts. The timer is running while you are sitting there and doing the UART transmit. If your baud is set to 115.2k and you are transmitting 30 bytes of data that would account for 3 ms of transmission time. So you would only see a null time on the uart line for ~17ms. More on reddit.com
🌐 r/embedded
11
11
September 29, 2024
Timer interrupts and HAL_TIM_PeriodElapsedCallback
What if statement specifically? I mean, obviously if it doesn’t enter the outer one, it won’t enter inner ones, so show what specifically outer if it doesn’t enter? And are you sure it enters the callback function at all? More on reddit.com
🌐 r/stm32f4
6
1
March 30, 2022
Top answer
1 of 2
3

You don't show the full code, but my guess is, you set and enable the callback after the timer has already elapsed once or many times, so there is a pending elapsed event which triggers immediately when you try to enable the functionality. It may be due to you using debugger and the timer runs even if code is stopped. It may be that you enable the callback after timer has alredy been running due to spending time running any other code before enabling the callback.

And why the HAL seems to wait one extra millisecod; it can be simply explained by looking at the source code of HAL_Delay - surprised that your source of information did not bother to check it and left it as mystery.

First of all, the HAL code adds 1 timer tick, and as they happen at 1ms in your system, asking for a 50ms delay will guarantee you get a delay of 51 ticks of the timer that increases the time variable.

The comment also explains why it adds the 1 timer tick. As when you call the HAL_Delay with some parameter like 50, you likely call it at a random time compared to the system timer ticks. So there can be a time range between 0 and 1 milliseconds before the next tick happens. If you want a 50ms delay, the HAL is guaranteed to give you a 50ms delay, in the range of 50 to 51 milliseconds, by adding one more tick to wait. If it did not do this, then you would be asking why you get time delays between 49 and 50 milliseconds even if you asked for 50 because you thought it will be enough and you get exactly 50ms but no less. It is hard to please everyone and with 1ms timer ticks, you only get 1ms precision or granularity in the times you need to delay.

2 of 2
3

A couple of gurus at community.st.com answered this for me. "Cube/HAL functions generate an Update during setup, so the UIF flag is already set."

I had to reset the UIF flag before calling HAL_TIM_Base_Start().

    TIM17->SR &= ~TIM_SR_UIF;
    HAL_TIM_Base_Start_IT(&htim17); //start 30 msec timeout timer 

while waiting for MM to assert our NSS pin

I don't know why STM didn't reset the flag as the first instruction in HAL_TIM_Base_Start(), but there it is.

🌐
DeepBlue
deepbluembedded.com › home › blog › stm32 timer interrupt hal example – timer mode lab
STM32 Timer Interrupt HAL Example - Timer Mode LAB – DeepBlueMbedded
February 17, 2025 - Now, we’ve got the callback function’s name that gets called whenever timer overflow occurs. It’s HAL_TIM_PeriodElapsedCallback(). So, we’ll write our own implementation for it in the application file ( main.c).
🌐
GitHub
github.com › eleciawhite › STM32Cube › blob › master › STM32Cube_FW_F3_V1.3.0 › Projects › STM32303C_EVAL › Examples › HAL › HAL_TimeBase › Src › main.c
STM32Cube/STM32Cube_FW_F3_V1.3.0/Projects/STM32303C_EVAL/Examples/HAL/HAL_TimeBase/Src/main.c at master · eleciawhite/STM32Cube
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) · { · HAL_IncTick(); · } · · /** · * @brief EXTI line detection callback. · * @param GPIO_Pin: Specifies the pins connected EXTI line · * @retval None · */ · void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) ·
Author   eleciawhite
Find elsewhere
🌐
Bfh
mikrocontroller.ti.bfh.ch › halDoc › group__TIM__Exported__Functions__Group9.html
STM32F4xx HAL Documentation: TIM Callbacks functions
Referenced by TIM_DMACaptureHalfCplt(). ... PWM Pulse finished callback in non-blocking mode. ... Definition at line 5732 of file stm32f4xx_hal_tim.c.
🌐
Steppeschool
steppeschool.com › blog › stm32-timer-encoder-mode
STM32 Timer Encoder: Rotary Position & Velocity Estimation
October 13, 2025 - void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { timer_counter = __HAL_TIM_GET_COUNTER(&htim3); // measure velocity, position update_encoder(&enc_instance_mot, &htim3); encoder_position = enc_instance_mot.position encoder_velocity = enc_instance_mot.velocity } This function is presented below.
🌐
HackMD
hackmd.io › @ampheo › how-do-i-use-stm32-timers-for-delay-and-periodic-tasks
How do I use STM32 timers for delay and periodic tasks? - HackMD
August 14, 2025 - **Code (HAL):** ``` TIM_HandleTypeDef htim3; static void TIM3_Init_1kHz(void){ __HAL_RCC_TIM3_CLK_ENABLE(); // Compute prescaler & ARR for 1 kHz uint32_t pclk = HAL_RCC_GetPCLK1Freq(); uint32_t timclk = pclk; if ((RCC->CFGR & RCC_CFGR_PPRE1) != RCC_CFGR_PPRE1_DIV1) timclk *= 2; // Aim for tick = 1 MHz (1 µs), then ARR = 1000-1 -> 1 kHz uint32_t target_tick = 1000000; // 1 MHz uint32_t psc = (timclk / target_tick) - 1; uint32_t arr = (target_tick / 1000) - 1; // 1 kHz period htim3.Instance = TIM3; htim3.Init.Prescaler = psc; htim3.Init.CounterMode = TIM_COUNTERMODE_UP; htim3.Init.Period = arr;
🌐
TechQA
techqa.club › v › q › why-am-i-getting-make-makefile-181-build-serialusbtest-elf-error-1-77364702
Why Am I Getting "make: *** [Makefile:181: build/SerialUSBtest.elf] Error 1" on c, makefile, stm32, hal | TechQA
December 16, 2025 - */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.
🌐
Arm Developer
developer.arm.com › documentation › ka002799 › 1-0
ARMLINK: Error: L6200E: Symbol SysTick_Handler multiply defined
March 2, 2021 - .\Flash\Blinky.axf: Error: L6200E: ...X_CM3.lib(hal_cm3.o) and .\flash\blinky.o). The library file RTX_CM3.lib relates to a Software Component that can be identified in the Project Window. Once this Software Component is removed from the project, the second definition of the SysTick_Handler function disappears, and the error does no longer occur. Refer to Linker Command-line Options in the Linker User Guide. Refer to ARM: SysTick system timer does not generate ...
🌐
Rtems
docs.rtems.org › doxygen › 6.1 › group__TIM__Exported__Functions__Group9.html
RTEMS: TIM Callbacks functions
It makes a direct call to HAL_IncTick() to increment a global variable "uwTick" used as application time base.
🌐
GitLab
code.umd.edu › robotics at maryland › qubo software › qubo_embedded › repository
drivers/Src/stm32g0xx_hal_timebase_tim_template.c · main · Robotics at Maryland / Qubo Software / qubo_embedded · GitLab
* * This file overrides the native HAL time base functions (defined as weak) * the TIM time base: * + Initializes the TIM peripheral to generate a Period elapsed Event each 1ms * + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback ie each 1ms * ************************************...
🌐
Arm Community
community.arm.com › support-forums › f › keil-forum › 44383 › stm32f746-wave-share-board
STM32F746 Wave Share Board - Keil forum - Support forums - Arm Community
Dear All I am New to arm so i need u help I am trying to configure GPTM14 and do the pin toggle at PF9 , I am not getting any errors in the code but the led
🌐
Rtems
docs.rtems.org › doxygen › 6.1 › stm32h7xx__hal__timebase__tim__template_8c.html
RTEMS: bsps/arm/stm32h7/hal/stm32h7xx_hal_timebase_tim_template.c File Reference
This file overrides the native HAL time base functions (defined as weak) the TIM time base: + Initializes the TIM peripheral generate a Period elapsed Event each 1ms + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback ie each 1ms
🌐
Upv
disca.upv.es › aperles › arm_cortex_m3 › llibre › st › STM32F439xx_User_Manual › group__tim__exported__functions__group9.html
STM32F439xx HAL User Manual: TIM Callbacks functions
Referenced by TIM_DMAError(). Input Capture callback in non blocking mode. ... Definition at line 4334 of file stm32f4xx_hal_tim.c.