Datasheet

Table Of Contents
4.9.5.1. Initialise the RTC
Pico SDK: https://github.com/raspberrypi/pico-sdk/tree/pre_release/src/rp2_common/hardware_rtc/rtc.c Lines 21 - 43
21 int rtc_init(void) {
22 // Get clk_rtc freq and make sure it is running
23 uint rtc_freq = clock_get_hz(clk_rtc);
24 if (rtc_freq == 0) {
25 return -1;
26 }
27
28 // Take rtc out of reset now that we know clk_rtc is running
29 reset_block(RESETS_RESET_RTC_BITS);
30 unreset_block_wait(RESETS_RESET_RTC_BITS);
31
32 // Set up the 1 second divider.
33 // If rtc_freq is 400 then clkdiv_m1 should be 399
34 rtc_freq -= 1;
35
36 // Check the freq is not too big to divide
37 assert(rtc_freq <= RTC_CLKDIV_M1_BITS);
38
39 // Write divide value
40 rtc_hw->clkdiv_m1 = rtc_freq;
41
42 return 0;
43 }
4.9.5.2. Set the time
Pico SDK: https://github.com/raspberrypi/pico-sdk/tree/pre_release/src/rp2_common/hardware_rtc/rtc.c Lines 58 - 89
58 int rtc_set_datetime(datetime_t *t) {
59 if (!valid_datetime(t)) {
60 return -1;
61 }
62
63 // Disable RTC
64 rtc_hw->ctrl = 0;
65 // Wait while it is still active
66 while (rtc_running()) {
67 tight_loop_contents();
68 }
69
70 // Write to setup registers
71 rtc_hw->setup_0 = (t->year << RTC_SETUP_0_YEAR_LSB ) |
72 (t->month << RTC_SETUP_0_MONTH_LSB) |
73 (t->day << RTC_SETUP_0_DAY_LSB);
74 rtc_hw->setup_1 = (t->dotw << RTC_SETUP_1_DOTW_LSB) |
75 (t->hour << RTC_SETUP_1_HOUR_LSB) |
76 (t->min << RTC_SETUP_1_MIN_LSB) |
77 (t->sec << RTC_SETUP_1_SEC_LSB);
78
79 // Load setup values into rtc clock domain
80 rtc_hw->ctrl = RTC_CTRL_LOAD_BITS;
81
82 // Enable RTC and wait for it to be running
83 rtc_hw->ctrl = RTC_CTRL_RTC_ENABLE_BITS;
84 while (!rtc_running()) {
RP2040 Datasheet
4.9. RTC 576