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 Field | Size | Legal values |
|---|---|---|
| Year | 12 bits | 0..4095 |
| Month | 4 bits | 1..12 |
| Day | 5 bits | 1..[28,29,30,31], depending on the month |
| Day of Week | 3 bits | 0..6. Sunday = 0 |
| Hour | 5 bits | 0..23 |
| Minute | 6 bits | 0..59 |
| Seconds | 6 bits | 0..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 .
⚠ WARNINGWhile 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:
- • XOSC @ 12MHz / 256 = 46875Hz. To get a 1Hz reference
CLKDIV_M1should be set to 46874. - • An external reference from a GPS, which generates one pulse per second. Configure
clk_rtcto run from the GPIN0 clock source from GPIO pin 20. In this case, theclk_rtcdivider is 1 and the internal RTC clock divider is also 1 (i.e.CLKDIV_M1 = 0).
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:
- • Set the 1 sec reference
- • Set the clock
- • Set an alarm
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:
- • Sleep mode, where the processors are asleep and the unused clocks in the chip are stopped (see Section 2.15.3.5 )
- • Dormant mode, where all clocks in the chip are stopped
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:
- • the RTC must be configured to use an external reference clock (supplied by a GPIO pin)
- • Set up the RTC to run on an external reference
- • If the processor is running off the PLL, change it to run from XOSC/ROSC
- • Turn off the PLLs
- • Set up the RTC with the desired wake up time (one off, or recurring)
- • (optionally) power down most memories
- • Invoke DORMANT mode (see Section 2.16 , Section 2.17 , and Section 2.11.5.2 for more information)
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
| Offset | Name | Info |
|---|---|---|
| 0x00 | CLKDIV_M1 | Divider minus 1 for the 1 second counter. Safe to change the value when RTC is not enabled. |
| 0x04 | SETUP_0 | RTC setup register 0 |
| 0x08 | SETUP_1 | RTC setup register 1 |
| 0x0c | CTRL | RTC Control and status |
| 0x10 | IRQ_SETUP_0 | Interrupt setup register 0 |
| 0x14 | IRQ_SETUP_1 | Interrupt setup register 1 |
| 0x18 | RTC_1 | RTC register 1. |
| 0x1c | RTC_0 | RTC register 0 Read this before RTC !! |
| 0x20 | INTR | Raw Interrupts |
| 0x24 | INTE | Interrupt Enable |
| Offset | Name | Info |
|---|---|---|
| 0x28 | INTF | Interrupt Force |
| 0x2c | INTS | Interrupt status after masking & forcing |
RTC: CLKDIV_M1 Register
Offset: 0x00
Table 553. CLKDIV_M1 Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:16 | Reserved. | - | - |
| 15:0 | Divider minus 1 for the 1 second counter. Safe to change the value when RTC is not enabled. | RW | 0x0000 |
RTC: SETUP_0 Register
Offset: 0x04
Description
RTC setup register 0
Table 554. SETUP_0 Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:24 | Reserved. | - | - |
| 23:12 | YEAR: Year | RW | 0x000 |
| 11:8 | MONTH: Month (1..12) | RW | 0x0 |
| 7:5 | Reserved. | - | - |
| 4:0 | DAY: Day of the month (1..31) | RW | 0x00 |
RTC: SETUP_1 Register
Offset: 0x08
Description
RTC setup register 1
Table 555. SETUP_1 Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:27 | Reserved. | - | - |
| 26:24 | DOTW: Day of the week: 1-Monday...0-Sunday ISO 8601 mod 7 | RW | 0x0 |
| 23:21 | Reserved. | - | - |
| 20:16 | HOUR: Hours | RW | 0x00 |
| 15:14 | Reserved. | - | - |
| 13:8 | MIN: Minutes | RW | 0x00 |
| 7:6 | Reserved. | - | - |
| 5:0 | SEC: Seconds | RW | 0x00 |
RTC: CTRL Register
Offset: 0x0c
DescriptionRTC Control and status
Table 556. CTRL Register
| Bits 14:10 9:5 4:0 Bits | Description SHIFT Description | MASK_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 masking | Type RW RW RW Type | Reset 0x00 0x00 0x00 Reset |
|---|---|---|---|---|
| 31:9 | Reserved. | - | - | |
| 8 | FORCE_NOTLEAPYEAR | : If set, leapyear is forced off. | RW | 0x0 |
| 7:5 | Reserved. | - | - | |
| 4 | LOAD | : Load RTC | SC | 0x0 |
| 3:2 | Reserved. | - | - | |
| 1 | RTC_ACTIVE | : RTC enabled (running) | RO | - |
| 0 | RTC_ENABLE | : Enable RTC | RW | 0x0 |
| Bits | Description | Type | Reset | |
| 31:30 | Reserved. | - | - | |
| 29 | MATCH_ACTIVE | RO | - | |
| 28 | MATCH_ENA | : Global match enable. Don’t change any other value while this | RW | 0x0 |
| 27 | Reserved. | - | - | |
| 26 | YEAR_ENA | : Enable year matching | RW | 0x0 |
| 25 | MONTH_ENA | : Enable month matching | RW | 0x0 |
| 24 | DAY_ENA | : Enable day matching | RW | 0x0 |
| 23:12 | YEAR | : Year | RW | 0x000 |
| 11:8 | MONTH | : Month (1..12) | RW | 0x0 |
| 7:5 | Reserved. | - | - | |
| 4:0 | DAY : Day of the month (1..31) | RW | 0x00 | |
| Bits | Description | Type | Reset | |
| 31 | DOTW_ENA | : Enable day of the week matching | RW | 0x0 |
| 30 | HOUR_ENA | : Enable hour matching | RW | 0x0 |
| 29 | MIN_ENA | : Enable minute matching | RW | 0x0 |
| 28 | SEC_ENA | : Enable second matching | RW | 0x0 |
RTC: IRQ_SETUP_0 Register
Offset: 0x10 DescriptionInterrupt setup register 0
Table 557. IRQ_SETUP_0 Register
RTC: IRQ_SETUP_1 Register
Offset: 0x14 DescriptionInterrupt setup register 1
Table 558. IRQ_SETUP_1 Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 27 | Reserved. | - | - |
| 26:24 | DOTW : Day of the week | RW | 0x0 |
| 23:21 | Reserved. | - | - |
| 20:16 | HOUR : Hours | RW | 0x00 |
| 15:14 | Reserved. | - | - |
| 13:8 | MIN : Minutes | RW | 0x00 |
| 7:6 | Reserved. | - | - |
| 5:0 | SEC : Seconds | RW | 0x00 |
RTC: RTC_1 Register
Offset: 0x18
Description
RTC register 1.
Table 559. RTC_1 Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:24 | Reserved. | - | - |
| 23:12 | YEAR : Year | RO | - |
| 11:8 | MONTH : Month (1..12) | RO | - |
| 7:5 | Reserved. | - | - |
| 4:0 | DAY : 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
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:27 | Reserved. | - | - |
| 26:24 | DOTW : Day of the week | RF | - |
| 23:21 | Reserved. | - | - |
| 20:16 | HOUR : Hours | RF | - |
| 15:14 | Reserved. | - | - |
| 13:8 | MIN : Minutes | RF | - |
| 7:6 | Reserved. | - | - |
| 5:0 | SEC : Seconds | RF | - |
RTC: INTR Register
Offset: 0x20
Description
Raw Interrupts
Table 561. INTR Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:1 | Reserved. | - | - |
| 0 | RTC | RO | 0x0 |
RTC: INTE Register
Offset: 0x24
Description
Interrupt Enable
Table 562. INTE Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:1 | Reserved. | - | - |
| 0 | RTC | RW | 0x0 |
RTC: INTF Register
Offset: 0x28
Description
Interrupt Force
Table 563. INTF Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:1 | Reserved. | - | - |
| 0 | RTC | RW | 0x0 |
RTC: INTS Register
Offset: 0x2c
Description
Interrupt status after masking & forcing
Table 564. INTS Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:1 | Reserved. | - | - |
| 0 | RTC | RO | 0x0 |