Question to Hardware / RTC: datetime #2540

Closed
opened 2025-06-24 03:06:58 -05:00 by frankhaefele · 14 comments
frankhaefele commented 2025-06-24 03:06:58 -05:00 (Migrated from github.com)

Dear Pico and Raspberry Pi developers,

regarding to hardawre/rtc it came up a simple question to me.

The stuct for datetime looks like this:
pico hardware/rtc

    // Start on Friday 5th of June 2020 15:45:00
    datetime_t t = {
            .year  = 2020,
            .month = 06,
            .day   = 05,
            .dotw  = 5, // 0 is Sunday, so 5 is Friday
            .hour  = 15,
            .min   = 45,
            .sec   = 00
    };

Would it be not great do it like that:

#include "hardware/rtc.h"
#include "time.h"


void set_time(struct *tm) {
    rtc_init();
    rtc_set_datetime(tm);
}

instead of do it like that?

#include "hardware/rtc.h"

void set_time() {
    datetime_t t = {
        .year  = 2025,
        .month = 6,
        .day   = 24,
        .dotw  = 2, // 0 = Sonntag, 1 = Montag, ..., 6 = Samstag
        .hour  = 14,
        .min   = 30,
        .sec   = 0
    };
    rtc_init();
    rtc_set_datetime(&t);
}

you can reuse struct tm and feed directly into rtc?

Would it not be a great thing to align it to the official struct tm
cpp struct tm
To be able to use directly the struct tm to feed the rtc and also be able to get a struct tm back from rtc?

Then you easily ca request a timestamp from ntp create a tm and set rtc with the tm.

Dear Pico and Raspberry Pi developers, regarding to `hardawre/rtc` it came up a simple question to me. The stuct for datetime looks like this: [pico hardware/rtc](https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#group_hardware_rtc) ``` // Start on Friday 5th of June 2020 15:45:00 datetime_t t = { .year = 2020, .month = 06, .day = 05, .dotw = 5, // 0 is Sunday, so 5 is Friday .hour = 15, .min = 45, .sec = 00 }; ``` Would it be not great do it like that: ``` #include "hardware/rtc.h" #include "time.h" void set_time(struct *tm) { rtc_init(); rtc_set_datetime(tm); } ``` instead of do it like that? ``` #include "hardware/rtc.h" void set_time() { datetime_t t = { .year = 2025, .month = 6, .day = 24, .dotw = 2, // 0 = Sonntag, 1 = Montag, ..., 6 = Samstag .hour = 14, .min = 30, .sec = 0 }; rtc_init(); rtc_set_datetime(&t); } ``` you can reuse `struct tm` and feed directly into `rtc`? Would it not be a great thing to align it to the official `struct tm` [cpp struct tm](https://cplusplus.com/reference/ctime/tm/) To be able to use directly the struct tm to feed the rtc and also be able to get a struct tm back from rtc? Then you easily ca request a `timestamp` from ntp create a `tm` and set rtc with the `tm`.
lurch commented 2025-06-24 03:20:20 -05:00 (Migrated from github.com)

hardware_rtc is a low-level wrapper around the hardware itself (and is also specific to RP2040 - see section 12.10.2 of https://datasheets.raspberrypi.com/rp2350/rp2350-datasheet.pdf for RP2350 details).
For a higher-level API, see pico_time and datetime (the latter includes conversion to and from sruct tm).

[`hardware_rtc`](https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#group_hardware_rtc) is a low-level wrapper around the hardware itself (and is also specific to RP2040 - see section 12.10.2 of https://datasheets.raspberrypi.com/rp2350/rp2350-datasheet.pdf for RP2350 details). For a higher-level API, see [`pico_time`](https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#group_pico_time) and [`datetime`](https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#group_util_datetime) (the latter includes conversion to and from `sruct tm`).
frankhaefele commented 2025-06-24 09:11:42 -05:00 (Migrated from github.com)

@lurch
this results in a further question.

How can I then set the rtc from High-level API functions?
And how is the procedure when getting the time from rtc and to use in pico_time?

PS: the datetime_t has the same member information than struct tm why don't use existing stuff?

@lurch this results in a further question. How can I then set the rtc from High-level API functions? And how is the procedure when getting the time from rtc and to use in pico_time? PS: the `datetime_t` has the same member information than `struct tm` why don't use existing stuff?
lurch commented 2025-06-24 09:19:18 -05:00 (Migrated from github.com)

Perhaps you also want to look at https://github.com/raspberrypi/pico-examples/?tab=readme-ov-file#rtc and https://github.com/raspberrypi/pico-examples/?tab=readme-ov-file#timer ?

And in terms of "Why does the API work the way it does", those are questions that only @kilograham can answer.
But if you have any suggestions for how the existing APIs can be improved, we happily take Pull Requests 🙂

Perhaps you also want to look at https://github.com/raspberrypi/pico-examples/?tab=readme-ov-file#rtc and https://github.com/raspberrypi/pico-examples/?tab=readme-ov-file#timer ? And in terms of "Why does the API work the way it does", those are questions that only @kilograham can answer. But if you have any suggestions for how the existing APIs can be improved, we happily take Pull Requests 🙂
frankhaefele commented 2025-06-24 09:21:24 -05:00 (Migrated from github.com)

The point is what I want to do with the pico W is;

  1. query the actual time from ntp
  2. feed the rtc to set it to actual time
  3. do some stuff by including pico time

But if I understand the things right I need to convert the struct tm => datetime_t to feed the rtc, isn't it right?

The point is what I want to do with the pico W is; 1) query the actual time from ntp 2) feed the rtc to set it to actual time 3) do some stuff by including pico time But if I understand the things right I need to convert the `struct tm => datetime_t` to feed the rtc, isn't it right?
lurch commented 2025-06-24 09:26:22 -05:00 (Migrated from github.com)

Presumably you've seen the picow_ntp_client example then? (although it looks like that just retrieves the time, and doesn't update the RTC)

Presumably you've seen the [picow_ntp_client example](https://github.com/raspberrypi/pico-examples/?tab=readme-ov-file#pico-networking) then? (although it looks like that just retrieves the time, and doesn't update the RTC)
frankhaefele commented 2025-06-24 09:30:29 -05:00 (Migrated from github.com)

Thanks for answering so fast yes I have seen the ntp client example.
Thats right it is not updating the rtc, but what is then the benefit of rtc?
I think it is much mor accurate and keeps also track of time during sleep, right?

Thanks for answering so fast yes I have seen the ntp client example. Thats right it is not updating the rtc, but what is then the benefit of rtc? I think it is much mor accurate and keeps also track of time during sleep, right?
frankhaefele commented 2025-06-24 09:39:23 -05:00 (Migrated from github.com)

Presumably you've seen the picow_ntp_client example then? (although it looks like that just retrieves the time, and doesn't update the RTC)

The ntp client example does not set the time on cpu/pico, it keeps the time only in variables!
The ntp client example query the ntp time and show it, but I dont want to query the ntp all the time.

I want to set the time on the pico and use it and only query the ntp very less amounts.

Please correct when I am wrong!

See picture:

Image

> Presumably you've seen the [picow_ntp_client example](https://github.com/raspberrypi/pico-examples/?tab=readme-ov-file#pico-networking) then? (although it looks like that just retrieves the time, and doesn't update the RTC) The ntp client example does not set the time on cpu/pico, it keeps the time only in variables! The ntp client example query the ntp time and show it, but I dont want to query the ntp all the time. I want to set the time on the pico and use it and only query the ntp very less amounts. Please correct when I am wrong! See picture: ![Image](https://github.com/user-attachments/assets/920e1a15-7158-43a9-ac2a-75aae506ed46)
lurch commented 2025-06-24 10:51:23 -05:00 (Migrated from github.com)

@frankhaefele Unless you have concrete suggestions, it's sounding more and more like you should be asking for help at https://forums.raspberrypi.com/viewforum.php?f=143 rather than on GitHub.

@frankhaefele Unless you have concrete suggestions, it's sounding more and more like you should be asking for help at https://forums.raspberrypi.com/viewforum.php?f=143 rather than on GitHub.
frankhaefele commented 2025-06-24 12:08:28 -05:00 (Migrated from github.com)

No I am not asking for help, it came up as you mentioned the example ntp_client, because it does not save the time on the pico.

No I am not asking for help, it came up as you mentioned the example `ntp_client`, because it does not save the time on the pico.
frankhaefele commented 2025-06-24 12:11:52 -05:00 (Migrated from github.com)

@kilograham as I mentioned above #1, datetime_t has the same information than struct tm.

Why there are two structs like that?

@kilograham as I mentioned above [#1](https://github.com/raspberrypi/pico-sdk/issues/2540#issue-3170868716), `datetime_t ` has the same information than `struct tm`. Why there are two structs like that?
lurch commented 2025-06-24 16:33:50 -05:00 (Migrated from github.com)

you mentioned the example ntp_client, because it does not save the time on the pico.

Nor would I expect it to. All of the examples in https://github.com/raspberrypi/pico-examples/ are deliberately small, simple and focus on doing just one thing; in order make the code easier to read and understand, without multiple things going on at once.

So if you want to know how to control the RTC, look at the hello_rtc example. If you want to know how to read the time from an NTP server, look at the picow_ntp_client example.

If you want to "set the time on the pico and use it and only query the ntp very less amounts" then you'll need to understand how the examples are working, and write that code yourself. (Or ask for help on the forums)

> you mentioned the example `ntp_client`, because it does not save the time on the pico. Nor would I expect it to. All of the examples in https://github.com/raspberrypi/pico-examples/ are deliberately small, simple and focus on doing just one thing; in order make the code easier to read and understand, without multiple things going on at once. So if you want to know how to control the RTC, look at the `hello_rtc` example. If you want to know how to read the time from an NTP server, look at the `picow_ntp_client example`. If you want to "set the time on the pico and use it and only query the ntp very less amounts" then you'll need to understand how the examples are working, and write that code yourself. (Or ask for help on the forums)
hasenradball commented 2025-07-01 04:30:33 -05:00 (Migrated from github.com)

@lurch
I checked now the pico_ntp_clientexample.

But a small feedback.
The documentation is on very poor level for this example.

Just take an example to mention only on item:

#define NTP_RESEND_TIME (10 * 1000)

Resend time has nothing to do with resend!
It is the time when the failed handler is executed.

If the user should easily understand the code, the coders should be also precise in coding.

@lurch I checked now the `pico_ntp_client`example. But a small feedback. The documentation is on very poor level for this example. Just take an example to mention only on item: `#define NTP_RESEND_TIME (10 * 1000)` Resend time has nothing to do with resend! It is the time when the failed handler is executed. If the user should easily understand the code, the coders should be also precise in coding.
lurch commented 2025-07-01 06:04:15 -05:00 (Migrated from github.com)

@hasenradball Please feel free to create an issue (or PR) on the pico-examples repo 🙂

@hasenradball Please feel free to create an issue (or PR) on the `pico-examples` repo 🙂
hasenradball commented 2025-07-01 06:53:01 -05:00 (Migrated from github.com)

@lurch
would do hat but my other PR not reviewed since years, I have not so much free space to prepare an PR which would noch reviewed for months.

By the way I restructured a litte the example where I now set the system time and RTC by the NTP.
So I am used a example look like.

Version: 7.95.49 (2271bb6 CY) CRC: b7a28ef3 Date: Mon 2021-11-29 22:50:27 PST Ucode Ver: 1043.2162 FWID 01-c51d9400
cyw43 loaded ok, mac 28:cd:c1:0c:3b:2d
API: 12.2
Data: RaspberryPi.PicoW
Compiler: 1.29.4
ClmImport: 1.47.1
Customization: v5 22/06/24
Creation: 2022-06-24 06:55:08

connect status: joining
connect status: no ip
connect status: link up

==> Call system time before NTP call!
Thu Jan  1 00:00:05 1970

>>> call of: ntp_init
hostByNameError: 0

>>> call of: ntp_request
<run_ntp_test> NTP Request State: 0

system-Time: 01/01/1970 00:00:05
RTC-Time:    00/00/0005 00:00:00

>>> call of: ntp_receive

>>> call of: ntpResult

Got NTP response!

>>> call of: setSystemTime
    Epoch (NTP): 1751367002

>>> call of: setRtc
    Struct tm (NTP): 01/07/2025 10:50:02
    Tue Jul  1 10:50:02 2025

>>> call of: clearFailedHandlerAlarm

>>> call of: setRequestTime

system-Time: 01/07/2025 10:50:11
RTC-Time:    01/07/2025 10:50:12

system-Time: 01/07/2025 10:50:21
RTC-Time:    01/07/2025 10:50:22

system-Time: 01/07/2025 10:50:31
RTC-Time:    01/07/2025 10:50:32

system-Time: 01/07/2025 10:50:41
RTC-Time:    01/07/2025 10:50:42

system-Time: 01/07/2025 10:50:51
RTC-Time:    01/07/2025 10:50:52

cheers frank

@lurch would do hat but my other PR not reviewed since years, I have not so much free space to prepare an PR which would noch reviewed for months. By the way I restructured a litte the example where I now set the `system time` and `RTC` by the NTP. So I am used a example look like. ``` Version: 7.95.49 (2271bb6 CY) CRC: b7a28ef3 Date: Mon 2021-11-29 22:50:27 PST Ucode Ver: 1043.2162 FWID 01-c51d9400 cyw43 loaded ok, mac 28:cd:c1:0c:3b:2d API: 12.2 Data: RaspberryPi.PicoW Compiler: 1.29.4 ClmImport: 1.47.1 Customization: v5 22/06/24 Creation: 2022-06-24 06:55:08 connect status: joining connect status: no ip connect status: link up ==> Call system time before NTP call! Thu Jan 1 00:00:05 1970 >>> call of: ntp_init hostByNameError: 0 >>> call of: ntp_request <run_ntp_test> NTP Request State: 0 system-Time: 01/01/1970 00:00:05 RTC-Time: 00/00/0005 00:00:00 >>> call of: ntp_receive >>> call of: ntpResult Got NTP response! >>> call of: setSystemTime Epoch (NTP): 1751367002 >>> call of: setRtc Struct tm (NTP): 01/07/2025 10:50:02 Tue Jul 1 10:50:02 2025 >>> call of: clearFailedHandlerAlarm >>> call of: setRequestTime system-Time: 01/07/2025 10:50:11 RTC-Time: 01/07/2025 10:50:12 system-Time: 01/07/2025 10:50:21 RTC-Time: 01/07/2025 10:50:22 system-Time: 01/07/2025 10:50:31 RTC-Time: 01/07/2025 10:50:32 system-Time: 01/07/2025 10:50:41 RTC-Time: 01/07/2025 10:50:42 system-Time: 01/07/2025 10:50:51 RTC-Time: 01/07/2025 10:50:52 ``` cheers frank
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-raspberrypi/pico-sdk#2540
No description provided.