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
  • Hi,

    Your device creation and rtc0 from devicetree seem to be without obvious errors. However, you might be missing some properties. Nordic nrf RTC properties can be found in the documentation.  As a reference, here is a relevant config from zephyr.dts file after building the Zephyr alarm sample:

    rtc0: rtc@4000b000 {
    			compatible = "nordic,nrf-rtc";
    			reg = < 0x4000b000 0x1000 >;
    			cc-num = < 0x3 >;
    			interrupts = < 0xb 0x1 >;
    			status = "okay";
    			clock-frequency = < 0x8000 >;
    			prescaler = < 0x1 >;
    		};


    You can find information about struct tm in Time Utilities and about rtc_time in Real Time Clock Zephyr documentation.  There is also API Reference for RTC. In addition, structure rtc_time can be found in rtc.h.

    Best regards,
    Dejan

Reply
  • Hi,

    Your device creation and rtc0 from devicetree seem to be without obvious errors. However, you might be missing some properties. Nordic nrf RTC properties can be found in the documentation.  As a reference, here is a relevant config from zephyr.dts file after building the Zephyr alarm sample:

    rtc0: rtc@4000b000 {
    			compatible = "nordic,nrf-rtc";
    			reg = < 0x4000b000 0x1000 >;
    			cc-num = < 0x3 >;
    			interrupts = < 0xb 0x1 >;
    			status = "okay";
    			clock-frequency = < 0x8000 >;
    			prescaler = < 0x1 >;
    		};


    You can find information about struct tm in Time Utilities and about rtc_time in Real Time Clock Zephyr documentation.  There is also API Reference for RTC. In addition, structure rtc_time can be found in rtc.h.

    Best regards,
    Dejan

Children
Related