cancel
Showing results for 
Search instead for 
Did you mean: 

Function to return value of APB1 & APB2 timer clocks

Barry Richards
Associate II

Is there a way to retrieve the value APBx timer clocks are set to?

I have found functions to get the PCLK1 and PCLK2 frequencies but there is a multiplier that can change the final timer clock so that is not reliable.

0690X000006C6RtQAK.png

3 REPLIES 3

You'd need to look at the source for HAL_RCC_GetPCLK1() and read the RCC registers to determine if you are the DIV1 case, or not. In the other cases the TIMCLK is 2X

uint32_t PCLK1TIM(void) // F4 Example
{
  /* Get PCLK1 frequency */
  uint32 pclk1 = HAL_RCC_GetPCLK1Freq();
 
  /* Get PCLK1 prescaler */
  if((RCC->CFGR & RCC_CFGR_PPRE1) == 0)
  {
    /* PCLK1 prescaler equal to 1 => TIMCLK = PCLK1 */
    return (pclk1);
  }
  else
  {
    /* PCLK1 prescaler different from 1 => TIMCLK = 2 * PCLK1 */
    return(2 * pclk1);
  }
}

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

Very odd there is not a function for this, seems like it would be wanted since you need that value to calculate the period and prescaler for timer functions.

Thanks Clive.

People often use constants because they set up the frequency plan to being with.

Although given the proliferation of STM32 parts, and now some using other clock sources, it might be a good idea to have a function, haven't dug into the API or macros that deeply recently.

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