How to RTC with Nordic and Zephyr

I am using an NRF52840 custom board with a high quality crystal on the low frequency clock. 

I see that to configure this I should specify the following in the .conf file:

CONFIG_CLOCK_CONTROL_NRF_K32SRC_XTAL=y

My question is simply, "What is the best way to query the value of this clock". Seems a simple question, but looking at the
documentation and this forum, there is just a mind-boggling number of possible answers given.

Here are the possibilites I have come up with so far:

1)
k_uptime_get(); Does this actually query this clock, or is it using the program counter's clock?

2)
k_uptime_ticks();


3)
CONFIG_COUNTER
=y in .conf, and
counter_start(); followed by
counter_get_value();

4)
From the samples/subsys/logging/logger, though its really unclear where NRF_RTC1 comes from.
NRF_RTC1->COUNTER;

5)
nrfx_rtc_counter_get(); // from nrfx_rtc.h

6)
z_nrf_rtc_timer_read(); // from the timer tests


Questions:

Which of these actually are based on the LFCLK?
Which are recommended?
Which of these are best for low power consumption?
Which of these will continue to count if the SOC is put in a low-power mode?
What if the main thread is put to sleep waiting on an interrupt, will it keep counting?
How does the following effect the recommended call, if at all?
- Using an app timer
- Using a watchdog timer
- Using BLE
- Using MPSL





Parents
  • Hi

    1. All of the above (kernel functions, RTC, and counter) use the LF clock as the source here I believe, and can be used for different things depending on what you want to do. 

    2. If your goal is to just see the value of the clock, I would think the k_uptime_get(); function is best suited which just lets you get the time of how long the LF clock has been up and running.

    3. The power consumption between these should be neglectable.

    4. As long as the device is put in a system ON mode, you should be able to let the counter/timer run even in low power mode, both with the RTC and the k_... functions.

    5. The kernel will keep counting as long as you don't put the device to system OFF or specifically disable the LF clock.

    6. I think only the watchdog timer will be able to effect the timer if it is triggered and restarts the device. The BLE stack can interrupt the functions or take priority over them I guess, but won't restart it as far as I know.

    Best regards,

    Simon

  • Hello,
    I have a custom board containing a module from Raytac with a Nordic nRF52840 microcontroller on it. The board also has a 32768kHz oscillator connected to the module to have a more accurate LFCLK. I am using VSCode with the nRF Connect SDK and the Zephyr RTOS and want to make use of Zephyrs RTC (https://docs.zephyrproject.org/latest/hardware/peripherals/rtc.html#overview) using the external oscillator to keep track of time, up to the level of months and years.

    Here (How to enable NRF52840 RTC using Zephyr? ) you recommend to use the kernel timer, but this is not suitable for the handling of months and years.

    I noticed that RTC0 still should be able to use by the user as its stated here:  Zephyr & RTC.  So I also want to use the RTC0.

    I also came across https://devzone.nordicsemi.com/f/nordic-q-a/90810/how-to-rtc-with-nordic-and-zephyrbut it does not tell how to use it.

    Also: I do not know if the describtion of the rtc in the device tree is correct. I simply enabled rtc0 in the device tree , resulting in the device tree node:

    &rtc0 {
        status = "okay";
        prescaler = <4096>;
        wakeup-source;
    };

    Is the following define and code line the correct way to access the devicetree node and create a device?

    #define RTC_TIMER DT_NODELABEL(rtc0)

    const struct device *timer_dev = DEVICE_DT_GET(RTC_TIMER);

    Kind regards,

    Dominik

  • Hi Dominik

    This case is close to one year old now. Please create a new ticket describing your issue in detail and linking to this one if you find it relevant.

    Best regards,

    Simon

Reply Children
No Data
Related