Skip to main content
Barry Richards
Associate III
September 30, 2018
Question

Function to return value of APB1 & APB2 timer clocks

  • September 30, 2018
  • 3 replies
  • 4370 views

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

    This topic has been closed for replies.

    3 replies

    Tesla DeLorean
    Guru
    September 30, 2018

    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 VenmoUp vote any posts that you find helpful, it shows what's working..
    Barry Richards
    Associate III
    October 1, 2018

    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.

    Tesla DeLorean
    Guru
    October 1, 2018

    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 VenmoUp vote any posts that you find helpful, it shows what's working..