How do I get system clock frequency?

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
dhruvin91
Posts: 12
Joined: Tue Oct 01, 2013 8:50 am

How do I get system clock frequency?

Post by dhruvin91 »

Hi,
I am looking to add 10 ms delay in the code. I am not sure if there is any delay function in chibios. But I can add a delay function if I knew the clock frequency. I tried to find out what clock frequency my code has but didn't get anywhere.

Does anyone know how to find clock frequency in mycode or add delay?
Thank you.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: How do I get system clock frequency?

Post by Giovanni »

It is in any demo...

chThdSleepMilliseconds()

...or in the documentation...

http://chibios.sourceforge.net/html/gro ... e4cad9f56f

...or you may also look at this article...

http://www.chibios.org/dokuwiki/doku.ph ... :kb:timing

Giovanni
dhruvin91
Posts: 12
Joined: Tue Oct 01, 2013 8:50 am

Re: How do I get system clock frequency?

Post by dhruvin91 »

does this work when I am not using threads?
Thank you.
Faisal Mehmood
Posts: 25
Joined: Sat Jan 26, 2013 10:18 pm

Re: How do I get system clock frequency?

Post by Faisal Mehmood »

I guess you would have figured it out already. In case you didn't:
When the kernel has been initialized, the main() function becomes a thread as well. It is documented in the demo:

Code: Select all

/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();
  (remaining demo follows...)
 


ChThdSleepMilliseconds() will 'sleep' the main 'thread' for the specified milliseconds.
Guillaume227
Posts: 15
Joined: Tue Apr 18, 2017 6:52 pm
Has thanked: 10 times

Re: How do I get system clock frequency?

Post by Guillaume227 »

Going back to the title question (the op was actually interested in adding delay to his code so the discussion stopped there) :
I do not see anywhere in the API a method that returns the clock frequency which the MCU is set to.
What's the recommended way to access that value?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: How do I get system clock frequency?

Post by Giovanni »

Hi,

For STM32 it is STM32_HCLK, it is HAL exporting it.

Giovanni
electronic_eel
Posts: 95
Joined: Sat Mar 19, 2016 8:07 pm
Has thanked: 8 times
Been thanked: 23 times

Re: How do I get system clock frequency?

Post by electronic_eel »

Wouldn't it make sense to provide the frequency in a hardware-independent way?

Or is there some limitation that makes it hardware dependant?
Post Reply