RTC prescaler has no effect in Zephyr

I'm trying to use the RTC0 instance of an nrf52840 controller with a prescaler of 3277 so that its counter increments at a rate of approx. 10ms. Since I use Zephyr (SDK2.5.2) I defined the rtc0 in the device tree with the following lines:

rtc0: &rtc0 {
    compatible = "nordic,nrf-rtc";
    status = "okay";
    // With prescaler of 3277 a time precision of 2^15Hz/3277 = 9.999Hz can be achieved
    prescaler = < 3277 >;
    wakeup-source;
};

In the c-file I get the rtc0 instance and simply start the counter. Using ksleep I implemented a loop that prints the rtc0 counter value every second:

    rslt = counter_start(g_rtc_device);

    while(1) {
        uint32_t rtc_value;

        k_msleep(1000);

        rslt = counter_get_value(g_rtc_device, &rtc_value);

        LOG_INF("Current ticks: %d", rtc_value);
    }

The output on UART is as follows:

16:07:59:615 -> [00:00:01.119,354] <inf> resettable_rtc: Current ticks: 36679
16:08:00:622 -> [00:00:02.125,518] <inf> resettable_rtc: Current ticks: 69649
16:08:01:629 -> [00:00:03.131,683] <inf> resettable_rtc: Current ticks: 102619
16:08:02:636 -> [00:00:04.137,908] <inf> resettable_rtc: Current ticks: 135591

The rtc_value inceases by 36679 within 1.119354 seconds. I.e., the counter increases at a frequency of 32768Hz. This is actually the RTC clock without a prescaler. When having a look at the Peripherial View in VS Code's nRF Connect extension the RTC0 prescaler is also 0.

So my quesion is: Where is my fault? Where to set the prescaler of RTC0 accordingly when the device-tree setting has no effect?

Many thanks for any hints given!

Parents
  • Hello,

    I believe you are almost there. Your configurations seems right, but I assume you are using some sort of network protocol (probably Bluetooth Low Energy), which will take control of your RTC0. In addition, Zephyr itself will use RTC1, so that is off limits as well. This leaves you with RTC2 for these kind of applications.

    So either, you need to switch to using RTC2, or you need to disable the bluetooth. (CONFIG_BT=n in your prj.conf). 

    Please see the attached application (written in NCS v2.5.2). It works as you expected it to, but if you enable CONFIG_BT=y in your prj. conf, you can see that the ticks per second goes from 10 to 32k, but changing the my_rtc to use rtc2, will take it back down to 10 ticks per second.

    rtc_prescaler.zip

    Best regards,

    Edvin

  • Hey Edvin, 

    Sorry to piggyback on JoDev's question

    Is there an sample that you can point to or share where RTC wakeup is used to wake the device up form deep sleep state ?

    thank you 

Reply Children
No Data
Related