Skip to main content
Visitor II
July 3, 2020
Question

How to get peripheral clock programmatically?

  • July 3, 2020
  • 3 replies
  • 1700 views

Hello,

I have a generic function for the timer configuration. The time base address is an input parameter to this function. I'm looking for a way to get the timer clock so that I can set the timer prescaler. How can I do that with HAL?

I guess I need to get first the peripheral (APB1, APB2, ..) on which my timer is connected ? But how?

Thank you for your help,

Greg

    This topic has been closed for replies.

    3 replies

    Graduate II
    July 3, 2020

    Usually helper routines in the HAL RCC sources (see UART baud for example)

    Not sure there is a method to determine which peripheral bus is being used, but the base address/range of the APB is containable, and comparable against the specific TIM instance.

    Graduate II
    July 4, 2020

    Look at the memory map of the device. Each bus has an address range. If HAL would be high level library, it would have a function, which returns bus frequency from peripheral's address. But you can't expect that from ST's team of brainless code monkeys...

    Visitor II
    July 5, 2020

    This would be a natural need to be served in an API yielding the clock frequency in Hz (with 32 bit, shouldn't be so hard?)

    The clock frequency depends on which clock source and prescalers are selected through the clock tree.

    Probably when MX enable you to choose your PWM frequency in Hz and number of duty cycles the timer frequency will be closer to the real world more than a PDF spec.