Emulated RTC driver (RTC_EMUL)

Hi everybody.

I'm developing my own board based on nRF52840 SoC. I'm using nRF Connect SDK 2.6.1.
I need to log the environment events by date & time details on QSPI using LittleFS.
So, I must use Real-Time Clock. But after sometime I found the RTC unit of nRF52840 is Real-Time Counter.
I searched between available solutions and I got Zephyr RTOS has an Emulated RTC driver. But I couldn't find any example of using this ability.
I'll be very happy if you show me a complete example of using Emulated RTC driver functionality.

Thanks in advance
Mehdi Sadeghian

  • Hi the two samples that I recommend you have a look at is the following:

    Synchronized RTC sample: This sample shows how RTC clocks used for system clock on application and network core are synchronized. The result of synchronization is an offset value on network core which can be applied to the system tick for logging timestamping. The sample uses IPM driver and IPC HAL to produce events which occur at the same time on both cores. You can find more details about this sample https://docs.nordicsemi.com/bundle/ncs-latest/page/zephyr/samples/boards/nrf/nrf53_sync_rtc/README.html#overview.

    Additionally, there is a RTC device driver test suite available in Zephyr. https://docs.nordicsemi.com/bundle/ncs-latest/page/zephyr/hardware/peripherals/rtc.html#rtc_device_driver_test_suite.

    Other than this we don't have any complete example of using Emulated RTC.

    Kind regards,
    Andreas

  • Hi again.
    Thanks a lot Andreas.

    I finally could implement Zephyr Emulated RTC driver after 3 days effort. So, I decided to share it with others.
    Just consider that I'm using nRF Connect SDK 2.6.1.

    1. From "Devicetree Visual Editor", select "Add Global Node", click on "Search all devices...", select "rtc-emul", press enter and save the file:

    Now you must see the below configuration in your devicetree or overlay file:

    / {
    	rtc-emul {
    		compatible = "zephyr,rtc-emul";
    	};
    };

    2. From "nRF GUI Config", enable "RTC" and "RTC_EMUL" configurations. Click on "Apply" and "Save to File":

    The new configuration must be visible in "prj.conf" (CONFIG_RTC_EMUL is enabled but it's invisible):

    CONFIG_RTC=y

    3. Add "rtc.h" to main.c:

    #include <zephyr/drivers/rtc.h>

    4. Define an RTC device:

    static const struct device *const rtc_device = DEVICE_DT_GET_ANY(zephyr_rtc_emul);
     

    5. Create "initial" and "current" instances by date/time structure:

    struct rtc_time date_time_init = {
    	.tm_year = 2024,
    	.tm_mon = 8,
    	.tm_mday = 19,
    	.tm_hour = 15,
    	.tm_min = 39,
    	.tm_sec = 0,
    };
    
    struct rtc_time date_time_current;

    6. Set the RTC time:

    if (rtc_device == NULL)
    {
    	printk("No rtc_device found\n");
    	return;
    }
    
    rtc_set_time(rtc_device, &date_time_init);

    7. Get the current RTC time when you need:

    rtc_get_time(rtc_device, &date_time_current);
    printf("Current Date & Time: %d/%02d/%02d %02d:%02d:%02d\n",
    		date_time_current.tm_year,
    		date_time_current.tm_mon,
    		date_time_current.tm_mday,
    		date_time_current.tm_hour,
    		date_time_current.tm_min,
    		date_time_current.tm_sec);

    8. Make "Pristine Build".

    9. Debug:

    Best regards,
    Mehdi Sadeghian

  • That was really helpful thanks. I've been banging my head against how to set time in Zephyr for hours

Related