4.8. RTC

The Real-time Clock (RTC) provides time in human-readable format and can be used to generate interrupts at specific times.

4.8.1. Storage Format

Time is stored in binary, separated in seven fields:

Table 551. RTC storage format

Date/Time FieldSizeLegal values
Year12 bits0..4095
Month4 bits1..12
Day5 bits1..[28,29,30,31], depending on the month
Day of Week3 bits0..6. Sunday = 0
Hour5 bits0..23
Minute6 bits0..59
Seconds6 bits0..59

The RTC does not check that the programmed values are in range. Illegal values may cause unexpected behaviour.

4.8.1.1. Day of the week

Day of the week is encoded as Sun 0, Mon 1, ..., Sat 6 (i.e. ISO8601 mod 7).

There is no built-in calendar function. The RTC will not compute the correct day of the week; it will only increment the existing value.

4.8.2. Leap year

If the current value of YEAR in SETUP_0 is evenly divisible by 4, a leap year is detected, and Feb 28th is followed by Feb 29th instead of March 1st. Since this is not always true (century years for example), the leap year checking can be forced off by setting CTRL.FORCE_NOTLEAPYEAR .

NOTE

The leap year check is done only when needed (the second following Feb 28, 23:59:59). The software can set FORCE_NOTLEAPYEAR anytime after 2096 Mar 1 00:00:00 as long as it arrives before 2100 Feb 28 23:59:59 (i.e. taking into account the clock domain crossing latency)

4.8.3. Interrupts

The RTC can generate an interrupt at a configured time. There is a global bit, MATCH_ENA in IRQ_SETUP_0 to enable this feature, and individual enables for each time field (year, month, day, day-of-the-week, hour, minute, second). The individual enables can be used to implement repeating interrupts at specified times.

The alarm interrupt is sent to the processors and also to the ROSC and XOSC to wake them from dormant mode. See Section 4.8.5.5 for more information on dormant mode.

4.8.4. Reference clock

The RTC uses a reference clock clk_rtc , which should be any integer frequency in the range 1...65536Hz.

The internal 1Hz reference is created by an internal clock divider which divides clk_rtc by an integer value. The divide value minus 1 is set in CLKDIV_M1 .

⚠ WARNING

While it is possible to change CLKDIV_M1 while the RTC is enabled, it is not recommended.

clk_rtc can be driven either from an internal or external clock source. Those sources can be prescaled, using a fractional divider (see Section 2.15 ).

Examples of possible clock sources include:

i NOTE

All RTC register reads and writes are done from the processor clock domain clk_sys. All data are synchronised back and forth between the domains. Writing to the RTC will take 2 clk_rtc clock periods to arrive, additional to the clk_sys domain. This should be taken into account especially when the reference is slow (e.g. 1Hz).

4.8.5. Programmer's Model

There are three setup tasks:

4.8.5.1. Configuring the 1 second reference clock:

Select the source for clk_rtc. This is done outside the RTC registers (see Section 4.8.4 ).

SDK: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_rtc/rtc.c Lines 22 - 39

22 void rtc_init(void) {
23     // Get clk_rtc freq and make sure it is running
24     uint rtc_freq = clock_get_hz(clk_rtc);
25     assert(rtc_freq != 0);
26
27     // Take rtc out of reset now that we know clk_rtc is running
28     reset_unreset_block_num_wait_blocking(RESET_RTC);
29
30     // Set up the 1 second divider.
31     // If rtc_freq is 400 then clkdiv_m1 should be 399
32     rtc_freq -= 1;
33
34     // Check the freq is not too big to divide
35     assert(rtc_freq <= RTC_CLKDIV_M1_BITS);
36
37     // Write divide value
38     rtc_hw->clkdiv_m1 = rtc_freq;
39 }

4.8.5.2. Setting up the clock

SDK: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_rtc/rtc.c Lines 54 - 85

54 bool rtc_set_datetime(const datetime_t *t) {
55     if (!valid_datetime(t)) {
56         return false;
57     }
58
59     // Disable RTC
60     rtc_hw->ctrl = 0;
61     // Wait while it is still active
62     while (rtc_running()) {
63         tight_loop_contents();
64     }
65
66     // Write to setup registers
67     rtc_hw->setup_0 = (((uint32_t)t->year) << RTC_SETUP_0_YEAR_LSB) |
68                     (((uint32_t)t->month) << RTC_SETUP_0_MONTH_LSB) |
69                     (((uint32_t)t->day) << RTC_SETUP_0_DAY_LSB);
70     rtc_hw->setup_1 = (((uint32_t)t->dotw) << RTC_SETUP_1_DOTW_LSB) |
71                     (((uint32_t)t->hour) << RTC_SETUP_1_HOUR_LSB) |
72                     (((uint32_t)t->min) << RTC_SETUP_1_MIN_LSB) |
73                     (((uint32_t)t->sec) << RTC_SETUP_1_SEC_LSB);
74
75     // Load setup values into rtc clock domain
76     rtc_hw->ctrl = RTC_CTRL_LOAD_BITS;
77
78     // Enable RTC and wait for it to be running
79     rtc_hw->ctrl = RTC_CTRL_RTC_ENABLE_BITS;
80     while (!rtc_running()) {
81         tight_loop_contents();
82     }
83
84     return true;
85 }

i NOTE

It is possible to change the current time while the RTC is running. Write the desired values, then set the LOAD bit in the CTRL register.

4.8.5.3. Reading the current time

The RTC time is stored across two 32-bit registers. To ensure a consistent value, RTC_0 should be read before RTC_1 . Reading RTC_0 latches the value of RTC_1 .

SDK: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_rtc/rtc.c Lines 87 - 106

87 bool rtc_get_datetime(datetime_t *t) {
88     // Make sure RTC is running
89     if (!rtc_running()) {
90         return false;
91     }
92
93     // Note: RTC_0 should be read before RTC_1
94     uint32_t rtc_0 = rtc_hw->rtc_0;
95     uint32_t rtc_1 = rtc_hw->rtc_1;
96
97     t->dotw = (int8_t) ((rtc_0 & RTC_RTC_0_DOTW_BITS) >> RTC_RTC_0_DOTW_LSB);
98     t->hour = (int8_t) ((rtc_0 & RTC_RTC_0_HOUR_BITS) >> RTC_RTC_0_HOUR_LSB);
99     t->min = (int8_t) ((rtc_0 & RTC_RTC_0_MIN_BITS) >> RTC_RTC_0_MIN_LSB);
100    t->sec = (int8_t) ((rtc_0 & RTC_RTC_0_SEC_BITS) >> RTC_RTC_0_SEC_LSB);
101    t->year = (int16_t) ((rtc_1 & RTC_RTC_1_YEAR_BITS) >> RTC_RTC_1_YEAR_LSB);
102    t->month = (int8_t) ((rtc_1 & RTC_RTC_1_MONTH_BITS) >> RTC_RTC_1_MONTH_LSB);
103    t->day = (int8_t) ((rtc_1 & RTC_RTC_1_DAY_BITS) >> RTC_RTC_1_DAY_LSB);
104
105    return true;
106 }

4.8.5.4. Configuring an Alarm

SDK: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_rtc/rtc.c Lines 146 - 182

146 void rtc_set_alarm(const datetime_t *t, rtc_callback_t user_callback) {
147     rtc_disable_alarm();
148
149     // Only add to setup if it isn't -1
150     rtc_hw->irq_setup_0 = ((t->year < 0) ? 0 : (((uint32_t)t->year) <<
        RTC_IRQ_SETUP_0_YEAR_LSB)) |
151         ((t->month < 0) ? 0 : (((uint32_t)t->month) <<
        RTC_IRQ_SETUP_0_MONTH_LSB)) |
152         ((t->day < 0) ? 0 : (((uint32_t)t->day) <<
        RTC_IRQ_SETUP_0_DAY_LSB));
153     rtc_hw->irq_setup_1 = ((t->dotw < 0) ? 0 : (((uint32_t)t->dotw) <<
        RTC_IRQ_SETUP_1_DOTW_LSB)) |
154         ((t->hour < 0) ? 0 : (((uint32_t)t->hour) <<
        RTC_IRQ_SETUP_1_HOUR_LSB)) |
155         ((t->min < 0) ? 0 : (((uint32_t)t->min) <<
        RTC_IRQ_SETUP_1_MIN_LSB)) |
156         ((t->sec < 0) ? 0 : (((uint32_t)t->sec) <<
        RTC_IRQ_SETUP_1_SEC_LSB));
157
158     // Set the match enable bits for things we care about
159     if (t->year >= 0) hw_set_bits(&rtc_hw->irq_setup_0, RTC_IRQ_SETUP_0_YEAR_ENA_BITS);
160     if (t->month >= 0) hw_set_bits(&rtc_hw->irq_setup_0, RTC_IRQ_SETUP_0_MONTH_ENA_BITS);
161     if (t->day >= 0) hw_set_bits(&rtc_hw->irq_setup_0, RTC_IRQ_SETUP_0_DAY_ENA_BITS);
162     if (t->dotw >= 0) hw_set_bits(&rtc_hw->irq_setup_1, RTC_IRQ_SETUP_1_DOTW_ENA_BITS);
163     if (t->hour >= 0) hw_set_bits(&rtc_hw->irq_setup_1, RTC_IRQ_SETUP_1_HOUR_ENA_BITS);
164     if (t->min >= 0) hw_set_bits(&rtc_hw->irq_setup_1, RTC_IRQ_SETUP_1_MIN_ENA_BITS);
165     if (t->sec >= 0) hw_set_bits(&rtc_hw->irq_setup_1, RTC_IRQ_SETUP_1_SEC_ENA_BITS);
166
167     // Does it repeat? I.e. do we not match on any of the bits
168     _alarm_repeats = rtc_alarm_repeats(t);
169
170     // Store function pointer we can call later
171     _callback = user_callback;
172
173     irq_set_exclusive_handler(RTC_IRQ, rtc_irq_handler);
174
175     // Enable the IRQ at the peri
176     rtc_hw->inte = RTC_INTE_RTC_BITS;
177
178     // Enable the IRQ at the proc
179     irq_set_enabled(RTC_IRQ, true);
180
181     rtc_enable_alarm();

182 }

NOTE

Recurring alarms can be created by using fewer enable bits when setting up the alarm interrupt. For example, if you only matched on seconds and the second was configured as 54 then the alarm interrupt would fire once a minute when the second was 54.

4.8.5.5. Interaction with Dormant / Sleep mode

RP2040 supports two power saving levels:

The RTC can wake the chip up from both of these modes. In sleep mode, RP2040 can be configured such that only clk_rtc(a slow RTC reference clock) is running, as well as a small amount of logic that allows the processor to wake back up. The processor is woken from sleep mode when the RTC alarm interrupt fires. See Section 2.11.5.1 for more information.

To wake the chip from dormant mode:

4.8.6. List of Registers

The RTC registers start at a base address of 0x4005c000(defined as RTC_BASE in SDK).

Table 552. List of RTC registers

OffsetNameInfo
0x00CLKDIV_M1Divider minus 1 for the 1 second counter. Safe to change the value when RTC is not enabled.
0x04SETUP_0RTC setup register 0
0x08SETUP_1RTC setup register 1
0x0cCTRLRTC Control and status
0x10IRQ_SETUP_0Interrupt setup register 0
0x14IRQ_SETUP_1Interrupt setup register 1
0x18RTC_1RTC register 1.
0x1cRTC_0RTC register 0
Read this before RTC !!
0x20INTRRaw Interrupts
0x24INTEInterrupt Enable
OffsetNameInfo
0x28INTFInterrupt Force
0x2cINTSInterrupt status after masking & forcing

RTC: CLKDIV_M1 Register

Offset: 0x00

Table 553. CLKDIV_M1 Register

BitsDescriptionTypeReset
31:16Reserved.--
15:0Divider minus 1 for the 1 second counter. Safe to change the value when RTC is not enabled.RW0x0000

RTC: SETUP_0 Register

Offset: 0x04

Description

RTC setup register 0

Table 554. SETUP_0 Register

BitsDescriptionTypeReset
31:24Reserved.--
23:12YEAR: YearRW0x000
11:8MONTH: Month (1..12)RW0x0
7:5Reserved.--
4:0DAY: Day of the month (1..31)RW0x00

RTC: SETUP_1 Register

Offset: 0x08

Description

RTC setup register 1

Table 555. SETUP_1 Register

BitsDescriptionTypeReset
31:27Reserved.--
26:24DOTW: Day of the week: 1-Monday...0-Sunday ISO 8601 mod 7RW0x0
23:21Reserved.--
20:16HOUR: HoursRW0x00
15:14Reserved.--
13:8MIN: MinutesRW0x00
7:6Reserved.--
5:0SEC: SecondsRW0x00

RTC: CTRL Register

Offset: 0x0c

Description

RTC Control and status

Table 556. CTRL Register

Bits 14:10 9:5 4:0 BitsDescription SHIFT DescriptionMASK_MSB : The most-significant bit allowed to pass by the mask (inclusive) MASK_LSB : The least-significant bit allowed to pass by the mask (inclusive) : Logical right-shift applied to accumulator before maskingType RW RW RW TypeReset 0x00 0x00 0x00 Reset
31:9Reserved.--
8FORCE_NOTLEAPYEAR: If set, leapyear is forced off.RW0x0
7:5Reserved.--
4LOAD: Load RTCSC0x0
3:2Reserved.--
1RTC_ACTIVE: RTC enabled (running)RO-
0RTC_ENABLE: Enable RTCRW0x0
BitsDescriptionTypeReset
31:30Reserved.--
29MATCH_ACTIVERO-
28MATCH_ENA: Global match enable. Don’t change any other value while thisRW0x0
27Reserved.--
26YEAR_ENA: Enable year matchingRW0x0
25MONTH_ENA: Enable month matchingRW0x0
24DAY_ENA: Enable day matchingRW0x0
23:12YEAR: YearRW0x000
11:8MONTH: Month (1..12)RW0x0
7:5Reserved.--
4:0DAY : Day of the month (1..31)RW0x00
BitsDescriptionTypeReset
31DOTW_ENA: Enable day of the week matchingRW0x0
30HOUR_ENA: Enable hour matchingRW0x0
29MIN_ENA: Enable minute matchingRW0x0
28SEC_ENA: Enable second matchingRW0x0

RTC: IRQ_SETUP_0 Register

Offset: 0x10 Description

Interrupt setup register 0

Table 557. IRQ_SETUP_0 Register

RTC: IRQ_SETUP_1 Register

Offset: 0x14 Description

Interrupt setup register 1

Table 558. IRQ_SETUP_1 Register

BitsDescriptionTypeReset
27Reserved.--
26:24DOTW : Day of the weekRW0x0
23:21Reserved.--
20:16HOUR : HoursRW0x00
15:14Reserved.--
13:8MIN : MinutesRW0x00
7:6Reserved.--
5:0SEC : SecondsRW0x00

RTC: RTC_1 Register

Offset: 0x18

Description

RTC register 1.

Table 559. RTC_1 Register

BitsDescriptionTypeReset
31:24Reserved.--
23:12YEAR : YearRO-
11:8MONTH : Month (1..12)RO-
7:5Reserved.--
4:0DAY : Day of the month (1..31)RO-

RTC: RTC_0 Register

Offset: 0x1c

Description

RTC register 0

Read this before RTC 1!

Table 560. RTC_0 Register

BitsDescriptionTypeReset
31:27Reserved.--
26:24DOTW : Day of the weekRF-
23:21Reserved.--
20:16HOUR : HoursRF-
15:14Reserved.--
13:8MIN : MinutesRF-
7:6Reserved.--
5:0SEC : SecondsRF-

RTC: INTR Register

Offset: 0x20

Description

Raw Interrupts

Table 561. INTR Register

BitsDescriptionTypeReset
31:1Reserved.--
0RTCRO0x0

RTC: INTE Register

Offset: 0x24

Description

Interrupt Enable

Table 562. INTE Register

BitsDescriptionTypeReset
31:1Reserved.--
0RTCRW0x0

RTC: INTF Register

Offset: 0x28

Description

Interrupt Force

Table 563. INTF Register

BitsDescriptionTypeReset
31:1Reserved.--
0RTCRW0x0

RTC: INTS Register

Offset: 0x2c

Description

Interrupt status after masking & forcing

Table 564. INTS Register

BitsDescriptionTypeReset
31:1Reserved.--
0RTCRO0x0