This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How can I use Zephyr's API to set clock source to external 32MHz crystal for NRF52840-DK

Hi there, 

I have a question about using zephyr on my nrf52840-DK board: I want to set the system to run at 64MHz for Zephyr. However, I can't find an option to do that. And if I run the following code block to measure the cycles of a second: 

```

u32_t then = k_cycle_get_32();

k_sleep(1000);

u32_t now = k_cycle_get_32();

printk("then: %d, now: %d, diff = %d\n", then, now, now - then);

```

It always return a diff number around 32768. Does the MCU run at only 32KHz by default?  How can I force it to run at 64MHz given that the board has the external 32MHz crystal? 

Thank you! 

Jun 

  • Thanks for the reply! I read the PS. So my understanding is 

    1. The MCU always works at 64MHz once waking up no matter if the external crystal is used or not. 

    2. The reason to use the external 32MHz crystal is just to provide higher accuracy  HCLK.

    3. Likewise, the reason to enable the external 32KHz is to give more accuracy clock to RTC when the system goes to sleep. 

    right? 

    So, my last question, why is the default setting for CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC set to 32768 in Zephyr given that the hw clock is actually 64MHz? Any special considerations behind that? 

  • Hi Jun,

    jli157@intel said:
    1. The default clock source of a Zephyr application is LFCLK, i.e., 32K xtal, right? 

    No. The CPU always runs of the 64 MHz clock, either RC or xtal (using 32 MHz xtal as reference). The 32.768 kHz clock is very low power and typically always running, and used for keeping track of time (for instance to wake up the device for BLE connection events etc.)

    jli157@intel said:
    3. In what way can I verify the MCU runs at 64MHz after calling the `clock control API`? 

    There is no point in verifying this, since 64 MHz is the only possibility.

    Einar

  • Hi,

    jli157@intel said:
    1. The MCU always works at 64MHz once waking up no matter if the external crystal is used or not. 

    Yes.

    jli157@intel said:
    2. The reason to use the external 32MHz crystal is just to provide higher accuracy  HCLK.

    Yes.

    jli157@intel said:
    3. Likewise, the reason to enable the external 32KHz is to give more accuracy clock to RTC when the system goes to sleep. 

    Yes, though not only when the system is going to sleep. The 32 kHz clock is typically always running.

    jli157@intel said:
    So, my last question, why is the default setting for CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC set to 32768 in Zephyr given that the hw clock is actually 64MHz? Any special considerations behind that? 

    That is because this is the clock that is used to keep track of time, and is always running. Most nRF applications are low power, and it makes no sense to use the high-frequency clock always in a low power application (it would make the average current consumption very high).

  • Thanks very much for the comprehensive answers! 

    My project actually doesn't use the radio. It is using the CryptoCell as a security application, and the MCU most time keeps running without the need to go to sleep. In that case, does the setting CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=32768 affect the accuracy of the system clock functions, like k_sleep()? 

  • Hi,

    Not the accuracy, given that the 32.768 kHz LFCLK is used. That is the resolution of that clock, and the accuracy is given by the LF crystal (if used), or the internal LFRC (+-500 ppm in that case).

    As mentioned, if you need a higher resolution for some things you could use a timer instead.

Related