cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H743ZI2 TIM2 which clock source?

LHans.2
Associate II

Hello

I have configured TIM2 to throw an interrupt every 10 ms. Looking at APB1 Timer clocks clocks in the clock configuration, a prescaler of 199 with AutoReload set to 9999 should call the interrupt every 10ms. In reality the interrupt is called every 30 ms, if I do the calculations backwards the timer clock runs at around 64 MHz which I don't quite get. According to the reference manual for the H743ZI2 TIM2 gets its clock from APB1 which is set to 200 MHz.

static void MX_TIM2_Init(void)
{
 
  /* USER CODE BEGIN TIM2_Init 0 */
 
  /* USER CODE END TIM2_Init 0 */
 
  LL_TIM_InitTypeDef TIM_InitStruct = {0};
 
  /* Peripheral clock enable */
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
 
  /* TIM2 interrupt Init */
  NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),3, 0));
  NVIC_EnableIRQ(TIM2_IRQn);
 
  /* USER CODE BEGIN TIM2_Init 1 */
 
  /* USER CODE END TIM2_Init 1 */
  TIM_InitStruct.Prescaler = 199;
  TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  TIM_InitStruct.Autoreload = 9999;
  TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  LL_TIM_Init(TIM2, &TIM_InitStruct);
  LL_TIM_DisableARRPreload(TIM2);
  LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
  LL_TIM_DisableMasterSlaveMode(TIM2);
  /* USER CODE BEGIN TIM2_Init 2 */
 
  LL_TIM_EnableIT_UPDATE(TIM2);
 
   LL_TIM_EnableCounter(TIM2);
  /* USER CODE END TIM2_Init 2 */
 
}

0693W00000QLMtiQAH.png

6 REPLIES 6

The default startup speed for the MCU is 64 MHz, it runs that way until you start other clock source, the PLL, etc.

The source clocks for the TIMs can also be selected on H7 parts.

Here we print out what's going on internally so it's easier to keep track of boards and settings whilst jumping between platforms.

 printf("SystemCoreClock: %d\n", SystemCoreClock);

 printf("HCLK=%d\n", HAL_RCC_GetHCLKFreq());

 printf("APB1=%d\n", HAL_RCC_GetPCLK1Freq());

 printf("APB2=%d\n", HAL_RCC_GetPCLK2Freq());

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
LHans.2
Associate II

If I print out the values right when entering MX_TIM2_Init() I get:

SystemCoreClock = 400MHz

HCLK = 200MHz

APB1 = APB2 = 100MHz

Yet still TIM2 gets a source clock of 64 MHz. I configured the clock source as internal clock , does that mean it gets it's source from the HSI?

Internal clock comes from the APBx clock x2.

External clock comes from GPIO: TIx (mode1) or ETR (mode2).

Please show us the TIM2 register content.

It is a pity that the Cube does not show the clock source for timers (and other modules).

If it does not calculate the counters and prescalers, at least it can tell the user the clock value.

I ask for this feature every so often... Silence.

Nikita91
Lead II

Is what you're asking different from that?

0693W00000QLN5UQAX.png

> Is what you're asking different from that?

Yes, a bit. How the user can know, is a specific timer on APB1 or APB4?

Not having to switch fervently between the timer tree and config views, and still having to browse the RM would be nice.

On 'H7 there's also some RCC enable that controls the global(?) prescale for timers.