nrf54l15-dk rtc

Hi,Team,

I'm currently using the nrf54l15-dk. I want to set the rtc of the nrf54l15-dk to obtain the timestamp. How should I configure the rtc? Which examples should I refer to?
My ncs version is v2.8.0.

Thanks.

  • To set the time, you can save the difference between GRTC and the timestamp (epoch microseconds) in memory. To get the time, you add the same difference to the GRTC. If you save the difference in a retained memory region, it will survive soft reboots.

  • Hi

    I can understand what you mean. But I don't know how to do it. Could you please provide an example of a code engineering project?

    Thanks.

  • This is how to read the GRTC:

    In prj.conf:

    CONFIG_NRFX_GRTC=y

    In your code:

    #include <nrfx_grtc.h>
    ...
    uint64_t syscounter;
    int ret = nrfx_grtc_syscounter_get(&syscounter);
    if (ret != NRFX_SUCCESS) {
       ...
    }

    As for retained memory, see zephyr/samples/boards/nordic/system_off for an example.

  • Hi,

    Thank you very much for your reply.

    Thank you very much for your reply.
    Now I can use the nrfx_grtc_syscounter_get() function to obtain the value. The app will send 8 bytes to my device: 0x10, 0xDB, 0x4D, 0xEE, 0x98, 0x01, 0x00, 0x00. Converting these to 13-digit decimal gives 1756344736528. This represents a Unix millisecond-level timestamp format. Converting it again to UTC format results in 20-09-27 21:32:16.528.
    The first time, nrfx_grtc_syscounter_get(&syscounter) was called, and syscounter = . A delay of 1 second was then applied. Subsequently, nrfx_grtc_syscounter_get(&syscounter) was called again, and syscounter = 1127894. Based on the difference you described, I should do this: 1756344736528 + 1127894 - 126117 = 1756335738305. Right? If I'm wrong, please let me know.
    This is my simple test code:

        unsigned char bytes[] = {0x10, 0xDB, 0x4D, 0xEE, 0x98, 0x01, 0x00, 0x00};
    	uint64_t timestamp = 0;
    
    	// 从小端序字节构建64位整数
    	for (int i = 0; i < 8; i++) {
    		timestamp |= (uint64_t)bytes[i] << (i * 8);
    	}
    	// 输出时间戳(13位十进制数字)
    	printf("\n");
    	printf("Timestamp1: %" PRIu64 "\n", timestamp);
    	uint64_t syscounter;
    
    	ret = nrfx_grtc_syscounter_get(&syscounter);
    	if (ret != NRFX_SUCCESS) {
     		 LOG_ERR("get grtc err");
    	}
    	printf("Timestamp2: %" PRIu64 "\n", syscounter);
    	k_msleep(1000);
    	ret = nrfx_grtc_syscounter_get(&syscounter);
    	if (ret != NRFX_SUCCESS) {
     		 LOG_ERR("get grtc err");
    	}
    	printf("Timestamp3: %" PRIu64 "\n", syscounter);

    The log printed by the device is as follows:

    Timestamp1: 1756344736528
    Timestamp2: 126117
    Timestamp3: 1127894

    Thanks.

  • I suggest you do like this:

    uint64_t offset = 0;
    
    int set_time(uint64_t timestamp)
    {
        uint64_t syscounter;
        int ret = nrfx_grtc_syscounter_get(&syscounter);
        if (ret != NRFX_SUCCESS) {
            return ret;
        }
        offset = timestamp - syscounter;
        return 0;
    }
    
    int get_time(uint64_t *timestamp)
    {
        uint64_t syscounter;
        int ret = nrfx_grtc_syscounter_get(&syscounter);
        if (ret != NRFX_SUCCESS) {
            return ret;
        }
        *timestamp = syscounter + offset;
        return 0;
    }
    

    If you call set_time() with your initial timestamp, then k_msleep(1000), then get_time() should give the initial timestamp plus one second.

Related