nRF52840 with Zephyr RTOS: How to use RTC with struct rtc_time?

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. I am not sure if in Zephyr it even is intended to use struct rtc_time.

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

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 description 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

Parents
  • Dominic,

    I also have a custom board with nRF52840 and a 32.768KHz clock source. I think what you (and some others here) want to do is the following:

    In the context of nRF with Zephyr RTC is your Real Time Counter. If you have an actual external crystal: it does not necessarily mean that it will be accurate out of the box. Have a look at setting up the crystal in your prj.conf (CONFIG_CLOCK_CONTROL_NRF_K32SRC_30PPM and others), and have a look at how to calibrate the RTC.

    For keeping time in terms of hours and calendar and what not, you need a Real Time Clock. You either have the hardware on your board (I don't) or you can use the emulator:

    rtc-emul {
        compatible = "zephyr,rtc-emul";
        alarms-count = < 1 >;
    };

    You also need to enable the whole RTC story in the prj.conf, and the alarm if you wish to use it:

    CONFIG_RTC=y
    CONFIG_RTC_EMUL=y
    CONFIG_RTC_ALARM=y
    

    So basically you can add a real time clock emulator device and use it as a hardware RTC. You can set alarms and the whole lot. I think it will use your real time counter and your crystal as backend.

    Have fun

    Jozsef

Reply
  • Dominic,

    I also have a custom board with nRF52840 and a 32.768KHz clock source. I think what you (and some others here) want to do is the following:

    In the context of nRF with Zephyr RTC is your Real Time Counter. If you have an actual external crystal: it does not necessarily mean that it will be accurate out of the box. Have a look at setting up the crystal in your prj.conf (CONFIG_CLOCK_CONTROL_NRF_K32SRC_30PPM and others), and have a look at how to calibrate the RTC.

    For keeping time in terms of hours and calendar and what not, you need a Real Time Clock. You either have the hardware on your board (I don't) or you can use the emulator:

    rtc-emul {
        compatible = "zephyr,rtc-emul";
        alarms-count = < 1 >;
    };

    You also need to enable the whole RTC story in the prj.conf, and the alarm if you wish to use it:

    CONFIG_RTC=y
    CONFIG_RTC_EMUL=y
    CONFIG_RTC_ALARM=y
    

    So basically you can add a real time clock emulator device and use it as a hardware RTC. You can set alarms and the whole lot. I think it will use your real time counter and your crystal as backend.

    Have fun

    Jozsef

Children
No Data
Related