4.6. Timer

4.6.1. Overview

The system timer peripheral on RP2040 provides a global microsecond timebase for the system, and generates interrupts based on this timebase. It supports the following features:

The timer uses a one microsecond reference that is generated in the Watchdog (see Section 4.7.2 ), and derived from

the reference clock (Figure 28), which itself is usually connected directly to the crystal oscillator (Section 2.16).

The 64-bit counter effectively can not overflow (thousands of years at 1MHz), so the system timer is completely monotonic in practice.

4.6.1.1. Other Timer Resources on RP2040

The system timer is intended to provide a global timebase for software. RP2040 has a number of other programmable counter resources which can provide regular interrupts, or trigger DMA transfers.

4.6.2. Counter

The timer has a 64-bit counter, but RP2040 only has a 32-bit data bus. This means that the TIME value is accessed through a pair of registers. These are:

These pairs are used by accessing the lower register, L , followed by the higher register, H . In the read case, reading the L register latches the value in the H register so that an accurate time can be read. Alternatively, TIMERAWH and TIMERAWL can be used to read the raw time without any latching.

⚠ CAUTION

While it is technically possible to force a new time value by writing to the TIMEHW and TIMELW registers, programmers are discouraged from doing this. This is because the timer value is expected to be monotonically increasing by the SDK which uses it for timeouts, elapsed time etc.

4.6.3. Alarms

The timer has 4 alarms, and outputs a separate interrupt for each alarm. The alarms match on the lower 32 bits of the 64-bit counter which means they can be fired at a maximum of \( 2^{32} \) microseconds into the future. This is equivalent to:

i NOTE

This timer is expected to be used for short sleeps. If you want a longer alarm see Section 4.8.

To enable an alarm:

Once the alarm has fired, the ARMED bit will be set to 0 . To clear the latched interrupt, write a 1 to the appropriate bit in INTR .

4.6.4. Programmer's Model

i NOTE

The Watchdog tick (see Section 4.7.2 ) must be running for the timer to start counting. The SDK starts this tick as part of the platform initialisation code.

4.6.4.1. Reading the time

i NOTE

Time here refers to the number of microseconds since the timer was started, it is not a clock. For that - see Section 4.8 .

The simplest form of reading the 64-bit time is to read TIMELR followed by TIMEHR . However, because RP2040 has 2 cores, it is unsafe to do this if the second core is executing code that can also access the timer, or if the timer is read concurrently in an IRQ handler and in thread mode. This is because reading TIMELR latches the value in TIMEHR (i.e. stops it updating) until TIMEHR is read. If one core reads TIMELR followed by another core reading TIMELR , the value in TIMEHR isn't necessarily accurate. The example below shows the simplest form of getting the 64-bit time.

Pico Examples: https://github.com/raspberrypi/pico-examples/blob/master/timer/timer_lowlevel/timer_lowlevel.c Lines 15 - 23

15 // Simplest form of getting 64 bit time from the timer.
16 // It isn't safe when called from 2 cores because of the latching
17 // so isn't implemented this way in the sdk
18 static uint64_t get_time(void) {
19     // Reading low latches the high value
20     uint32_t lo = timer_hw->timelr;
21     uint32_t hi = timer_hw->timehr;
22     return ((uint64_t) hi << 32u) | lo;
23 }

The SDK provides a time_us_64 function that uses a more thorough method to get the 64-bit time, which makes use of the TIMERAWH and TIMERAWL registers. The RAW registers don't latch, and therefore make time_us_64 safe to call from multiple cores at once.

SDK: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_timer/timer.c Lines 57 - 73

57 uint64_t timer_time_us_64(timer_hw_t *timer) {
58     // Need to make sure that the upper 32 bits of the timer
59     // don't change, so read that first
60     uint32_t hi = timer->timerawh;
61     uint32_t lo;
62     do {
63         // Read the lower 32 bits
64         lo = timer->timerawl;
65         // Now read the upper 32 bits again and
66         // check that it hasn't incremented. If it has loop around
67         // and read the lower 32 bits again to get an accurate value
68         uint32_t next_hi = timer->timerawh;
69         if (hi == next_hi) break;
70         hi = next_hi;
71     } while (true);
72     return ((uint64_t) hi << 32u) | lo;
73 }

4.6.4.2. Set an alarm

The standalone timer example, timer_lowlevel, demonstrates how to set an alarm at a hardware level, without the additional abstraction over the timer that the SDK provides. To use these abstractions see Section 4.6.4.4 .

Pico Examples: https://github.com/raspberrypi/pico-examples/blob/master/timer/timer_lowlevel/timer_lowlevel.c Lines 27 - 74

27 // Use alarm 0
28 #define ALARM_NUM 0
29 #define ALARM_IRQ timer_hardware_alarm_get_irq_num(timer_hw, ALARM_NUM)
30
31 // Alarm interrupt handler
32 static volatile bool alarm_fired;
33
34 static void alarm_irq(void) {
35     // Clear the alarm irq
36     hw_clear_bits(&timer_hw->intr, 1u << ALARM_NUM);
37
38     // Assume alarm 0 has fired
39     printf("Alarm IRQ fired\n");
40     alarm_fired = true;
41 }
42
43 static void alarm_in_us(uint32_t delay_us) {
44     // Enable the interrupt for our alarm (the timer outputs 4 alarm irqs)
45     hw_set_bits(&timer_hw->inte, 1u << ALARM_NUM);
46     // Set irq handler for alarm irq
47     irq_set_exclusive_handler(ALARM_IRQ, alarm_irq);
48     // Enable the alarm irq
49     irq_set_enabled(ALARM_IRQ, true);
50     // Enable interrupt in block and at processor
51
52     // Alarm is only 32 bits so if trying to delay more
53     // than that need to be careful and keep track of the upper
54     // bits
55     uint64_t target = timer_hw->timerawl + delay_us;
56
57     // Write the lower 32 bits of the target time to the alarm which
58     // will arm it
59     timer_hw->alarm[ALARM_NUM] = (uint32_t) target;
60 }
61
62 int main() {
63     stdio_init_all();
64     printf("Timer lowlevel!\n");
65
66     // Set alarm every 2 seconds
67     while (1) {
68         alarm_fired = false;
69         alarm_in_us(1000000 * 2);
70         // Wait for alarm to fire
71         while (!alarm_fired);
72     }
73 }

4.6.4.3. Busy wait

If you don't want to use an alarm to wait for a period of time, instead use a while loop. The SDK provides various busy_wait_ functions to do this:

SDK: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_timer/timer.c Lines 77 - 122

77 void timer_busy_wait_us_32(timer_hw_t *timer, uint32_t delay_us) {
78     if (0 <= (int32_t)delay_us) {
79         // we only allow 31 bits, otherwise we could have a race in the loop below with
80         // values very close to 2^32
81         uint32_t start = timer->timerawl;
82         while (timer->timerawl - start < delay_us) {
83             tight_loop_contents();
84         }
85     } else {
86         busy_wait_us(delay_us);
87     }
88 }
89
90 void timer_busy_wait_us(timer_hw_t *timer, uint64_t delay_us) {
91     uint64_t base = timer_time_us_64(timer);
92     uint64_t target = base + delay_us;
93     if (target < base) {
94         target = (uint64_t)-1;
95     }
96     absolute_time_t t;
97     update_us_since_boot(&t, target);
98     timer_busy_wait_until(timer, t);
99 }
100
101 void timer_busy_wait_ms(timer_hw_t *timer, uint32_t delay_ms)
102 {
103     if (delay_ms <= 0x7fffffffu / 1000) {
104         timer_busy_wait_us_32(timer, delay_ms * 1000);
105     } else {
106         timer_busy_wait_us(timer, delay_ms * 1000ull);
107     }
108 }
109
110 void timer_busy_wait_until(timer_hw_t *timer, absolute_time_t t) {
111     uint64_t target = to_us_since_boot(t);
112     uint32_t hi_target = (uint32_t)(target >> 32u);
113     uint32_t hi = timer->timerawh;
114     while (hi < hi_target) {
115         hi = timer->timerawh;
116         tight_loop_contents();
117     }
118     while (hi == hi_target && timer->timerawl < (uint32_t) target) {
119         hi = timer->timerawh;
120         tight_loop_contents();
121     }
122 }

4.6.4.4. Complete example using SDK

Pico Examples: https://github.com/raspberrypi/pico-examples/blob/master/timer/hello_timer/hello_timer.c Lines 11 - 57

11 volatile bool timer_fired = false;
12
13 int64_t alarm_callback(alarm_id_t id, __unused void *user_data) {
14     printf("Timer %d fired!\n", (int) id);
15     timer_fired = true;
16     // Can return a value here in us to fire in the future
17     return 0;
18 }
19
20 bool repeating_timer_callback(__unused struct repeating_timer *t) {
21     printf("Repeat at %lld\n", time_us_64());
22     return true;
23 }
24
25 int main() {
26     stdio_init_all();
27     printf("Hello Timer!\n");
28
29     // Call alarm_callback in 2 seconds
30     add_alarm_in_ms(2000, alarm_callback, NULL, false);
31
32     // Wait for alarm callback to set timer_fired
33     while (!timer_fired) {
34         tight_loop_contents();
35     }
36
37     // Create a repeating timer that calls repeating_timer_callback.
38     // If the delay is > 0 then this is the delay between the previous callback ending and the
39     // next starting.
40     // If the delay is negative (see below) then the next call to the callback will be exactly
41     // 500ms after the
42     // start of the call to the last callback
43     struct repeating_timer timer;
44     add_repeating_timer_ms(500, repeating_timer_callback, NULL, &timer);
45     sleep_ms(3000);
46     bool cancelled = cancel_repeating_timer(&timer);
47     printf("cancelled... %d\n", cancelled);
48     sleep_ms(2000);
49
50     // Negative delay so means we will call repeating_timer_callback, and call it again
51     // 500ms later regardless of how long the callback took to execute
52     add_repeating_timer_ms(-500, repeating_timer_callback, NULL, &timer);
53     sleep_ms(3000);
54     cancelled = cancel_repeating_timer(&timer);
55     printf("cancelled... %d\n", cancelled);
56     sleep_ms(2000);
57     printf("Done\n");
58     return 0;
59 }

4.6.5. List of Registers

The Timer registers start at a base address of 0x40054000 (defined as TIMER_BASE in SDK).

Table 527. List of
TIMER registers

OffsetNameInfo
0x00TIMEHWWrite to bits 63:32 of time
always write timelw before timehw
OffsetNameInfo
0x04TIMELWWrite to bits 31:0 of time
writes do not get copied to time until timehw is written
0x08TIMEHRRead from bits 63:32 of time
always read timelr before timehr
0x0cTIMELRRead from bits 31:0 of time
0x10ALARM0Arm alarm 0, and configure the time it will fire.
Once armed, the alarm fires when TIMER_ALARM0 == TIMELR.
The alarm will disarm itself once it fires, and can be disarmed early using the ARMED status register.
0x14ALARM1Arm alarm 1, and configure the time it will fire.
Once armed, the alarm fires when TIMER_ALARM1 == TIMELR.
The alarm will disarm itself once it fires, and can be disarmed early using the ARMED status register.
0x18ALARM2Arm alarm 2, and configure the time it will fire.
Once armed, the alarm fires when TIMER_ALARM2 == TIMELR.
The alarm will disarm itself once it fires, and can be disarmed early using the ARMED status register.
0x1cALARM3Arm alarm 3, and configure the time it will fire.
Once armed, the alarm fires when TIMER_ALARM3 == TIMELR.
The alarm will disarm itself once it fires, and can be disarmed early using the ARMED status register.
0x20ARMEDIndicates the armed/disarmed status of each alarm.
A write to the corresponding ALARMx register arms the alarm.
Alarms automatically disarm upon firing, but writing ones here will disarm immediately without waiting to fire.
0x24TIMERAWHRaw read from bits 63:32 of time (no side effects)
0x28TIMERAWLRaw read from bits 31:0 of time (no side effects)
0x2cDBGPAUSESet bits high to enable pause when the corresponding debug ports are active
0x30PAUSESet high to pause the timer
0x34INTRRaw Interrupts
0x38INTEInterrupt Enable
0x3cINTFInterrupt Force
0x40INTSInterrupt status after masking & forcing

TIMER: TIMEHW Register

Offset: 0x00

Table 528. TIMEHW Register

BitsDescriptionTypeReset
31:0Write to bits 63:32 of time
always write timelw before timehw
WF0x00000000

TIMER: TIMELW Register

Offset: 0x04

Table 529. TIMELW Register

BitsDescriptionTypeReset
31:0Write to bits 31:0 of time
writes do not get copied to time until timehw is written
WF0x00000000

TIMER: TIMEHR Register

Offset: 0x08

Table 530. TIMEHR Register

BitsDescriptionTypeReset
31:0Read from bits 63:32 of time
always read timelr before timehr
RO0x00000000

TIMER: TIMELR Register

Offset: 0x0c

Table 531. TIMELR Register

BitsDescriptionTypeReset
31:0Read from bits 31:0 of timeRO0x00000000

TIMER: ALARM0 Register

Offset: 0x10

Table 532. ALARM0 Register

BitsDescriptionTypeReset
31:0Arm alarm 0, and configure the time it will fire.
Once armed, the alarm fires when TIMER_ALARM0 == TIMELR.
The alarm will disarm itself once it fires, and can
be disarmed early using the ARMED status register.
RW0x00000000

TIMER: ALARM1 Register

Offset: 0x14

Table 533. ALARM1 Register

BitsDescriptionTypeReset
31:0Arm alarm 1, and configure the time it will fire.
Once armed, the alarm fires when TIMER_ALARM1 == TIMELR.
The alarm will disarm itself once it fires, and can
be disarmed early using the ARMED status register.
RW0x00000000

TIMER: ALARM2 Register

Offset: 0x18

Table 534. ALARM2 Register

BitsDescriptionTypeReset
31:0Arm alarm 2, and configure the time it will fire.
Once armed, the alarm fires when TIMER_ALARM2 == TIMELR.
The alarm will disarm itself once it fires, and can be disarmed early using the ARMED status register.
RW0x00000000

TIMER: ALARM3 Register

Offset: 0x1c

Table 535. ALARM3 Register

BitsDescriptionTypeReset
31:0Arm alarm 3, and configure the time it will fire.
Once armed, the alarm fires when TIMER_ALARM3 == TIMELR.
The alarm will disarm itself once it fires, and can be disarmed early using the ARMED status register.
RW0x00000000

TIMER: ARMED Register

Offset: 0x20

Table 536. ARMED Register

BitsDescriptionTypeReset
31:4Reserved.--
3:0Indicates the armed/disarmed status of each alarm.
A write to the corresponding ALARMx register arms the alarm.
Alarms automatically disarm upon firing, but writing ones here will disarm immediately without waiting to fire.
WC0x0

TIMER: TIMERAWH Register

Offset: 0x24

Table 537. TIMERAWH Register

BitsDescriptionTypeReset
31:0Raw read from bits 63:32 of time (no side effects)RO0x00000000

TIMER: TIMERAWL Register

Offset: 0x28

Table 538. TIMERAWL Register

BitsDescriptionTypeReset
31:0Raw read from bits 31:0 of time (no side effects)RO0x00000000

TIMER: DBGPAUSE Register

Offset: 0x2c

Description

Set bits high to enable pause when the corresponding debug ports are active

Table 539. DBGPAUSE Register

BitsDescriptionTypeReset
31:3Reserved.--
2DBG1 : Pause when processor 1 is in debug modeRW0x1
1DBG0 : Pause when processor 0 is in debug modeRW0x1
BitsDescriptionTypeReset
0Reserved.--

TIMER: PAUSE Register

Offset: 0x30

Table 540. PAUSE Register

BitsDescriptionTypeReset
31:1Reserved.--
0Set high to pause the timerRW0x0

TIMER: INTR Register

Offset: 0x34

Description

Raw Interrupts

Table 541. INTR Register

BitsDescriptionTypeReset
31:4Reserved.--
3ALARM_3WC0x0
2ALARM_2WC0x0
1ALARM_1WC0x0
0ALARM_0WC0x0

TIMER: INTE Register

Offset: 0x38

Description

Interrupt Enable

Table 542. INTE Register

BitsDescriptionTypeReset
31:4Reserved.--
3ALARM_3RW0x0
2ALARM_2RW0x0
1ALARM_1RW0x0
0ALARM_0RW0x0

TIMER: INTF Register

Offset: 0x3c

Description

Interrupt Force

Table 543. INTF Register

BitsDescriptionTypeReset
31:4Reserved.--
3ALARM_3RW0x0
Offset 0x0 0x4 0x8 0xc BitsName FRCE_ON FRCE_OFF WDSEL DONE DescriptionInfo Force block out of reset (i.e. power it on) Force into reset (i.e. power it off) Set to 1 if this peripheral should be reset when the watchdog Indicates the peripheral’s registers are ready to access. TypeReset
31:4Reserved.--
3ALARM_3RO0x0
2ALARM_2RO0x0
1ALARM_1RO0x0
0ALARM_0RO0x0
stop it from reaching zero. The watchdog is reset by rst_n_run, which is deasserted as soon as the digital core supply (DVDD) is powered and
2.13)and reset controller (see Section 2.14 register exists in both the power-on state machine and reset controller.), resetting their dependants if they are selected in theSection WDSEL register. The
4.7.2. Tick generation The watchdog reference clock, clk_tick, is driven from clk_ref . Ideally clk_refwill be configured to use the Crystal
Oscillator (Section 2.16) so that it provides an accurate reference clock. The reference clock is divided internally to
generate a tick (nominally 1 μs) to use as the watchdog tick. The tick is configured using theTICK register.
To avoid duplicating logic, this tick is also distributed to the timer (seeSection 4.6)and used as the timer reference.
17watchdog_start_tick(uint cycles) { tick_start(TICK_WATCHDOG, cycles);

TIMER: INTS Register

Offset: 0x40

Description

Interrupt status after masking & forcing

Table 544. INTS Register