12.8. System timers

12.8.1. Overview

The system timer peripheral on RP2350 provides a microsecond timebase for the system, and generates interrupts based on this timebase. RP2350 has two instances of the system timer: TIMER0 and TIMER1 . This allows for two separately controlled timers, each in a different security domain. It supports the following features:

The timer uses a one microsecond reference generated by the tick generators (see Section 8.5 ), and derived from the reference clock ( Figure 33 ), which itself is usually connected directly to the crystal oscillator ( Section 8.2 ).

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

12.8.1.1. Changes from RP2040

count system clock cycles rather than a 1 \( \mu \) s tick

12.8.1.2. Other timer resources on RP2350

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

12.8.2. Counter

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

To use these pairs, access 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 to provide an accurate time. To read the raw time without any latching, use TIMERAWH and TIMERAWL.

Caution icon CAUTION

Don't write to the TIMEHW and TIMELW registers to force a new time value if other software may be using the timer. The SDK uses the time value for timeouts, elapsed time, and more, and expects the value to increase monotonically.

12.8.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:

NOTE

This timer supports alarm intervals on the order of one microsecond to one hour. For a longer alarm, see Section 12.10 .

To enable an alarm:

  1. 1. Enable the interrupt at the timer with a write to the appropriate alarm bit in INTE (e.g. (1 << 0) for ALARM0 ).
  2. 2. Enable the appropriate timer interrupt at the processor (see Section 3.2 ).
  3. 3. Write the time you would like the interrupt to fire to ALARM0 (i.e. the current value in TIMERAWL plus your desired alarm time in microseconds). Writing the time to the ALARM register sets the ARMED bit as a side effect.

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

12.8.4. Programmer's model

NOTE

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

12.8.4.1. Reading the time

NOTE

Time here refers to the number of microseconds since the timer was started, not a clock. For a clock, see Section 12.10 .

To read the 64-bit time, read TIMELR followed by TIMEHR . Reading TIMELR latches (stops) the value in TIMEHR until TIMEHR is read. Because RP2350 has 2 cores, it is unsafe to do this if the second core executes code that can also access the timer, or if the timer is read concurrently in an IRQ handler and in thread mode. 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, making 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 }

12.8.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 provided by SDK. To use these abstractions, see Section 12.8.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;
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 }

12.8.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
    next starting.
39     // If the delay is negative (see below) then the next call to the callback will be exactly
    500ms after the
40     // start of the call to the last callback
41     struct repeating_timer timer;
42     add_repeating_timer_ms(500, repeating_timer_callback, NULL, &timer);
43     sleep_ms(3000);
44     bool cancelled = cancel_repeating_timer(&timer);
45     printf("cancelled... %d\n", cancelled);
46     sleep_ms(2000);
47
48     // Negative delay so means we will call repeating_timer_callback, and call it again
49     // 500ms later regardless of how long the callback took to execute
50     add_repeating_timer_ms(-500, repeating_timer_callback, NULL, &timer);
51     sleep_ms(3000);
52     cancelled = cancel_repeating_timer(&timer);
53     printf("cancelled... %d\n", cancelled);
54     sleep_ms(2000);
55     printf("Done\n");
56     return 0;
57 }

12.8.5. List of registers

The TIMER0 and TIMER1 registers start at base addresses of 0x400b0000 and 0x400b8000 respectively (defined as TIMER0_BASE and TIMER1_BASE in SDK).

Table 1227. List of TIMER registers

OffsetNameInfo
0x00TIMEHWWrite to bits 63:32 of time
always write timelw before timehw
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
0x34LOCKEDSet locked bit to disable write access to timer
Once set, cannot be cleared (without a reset)
OffsetNameInfo
0x38SOURCESelects the source for the timer. Defaults to the normal tick configured in the ticks block (typically configured to 1 microsecond). Writing to 1 will ignore the tick and count clk_sys cycles instead.
0x3cINTRRaw Interrupts
0x40INTEInterrupt Enable
0x44INTFInterrupt Force
0x48INTSInterrupt status after masking & forcing

TIMER: TIMEHW Register

Offset: 0x00

Table 1228. TIMEHW Register

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

TIMER: TIMELW Register

Offset: 0x04

Table 1229. 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 1230. TIMEHR Register

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

TIMER: TIMELR Register

Offset: 0x0c

Table 1231. TIMELR Register

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

TIMER: ALARM0 Register

Offset: 0x10

Table 1232. 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 1233. 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 1234. 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 1235. 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 1236. 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 1237.
TIMERAWH Register

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

TIMER: TIMERAWL Register

Offset: 0x28

Table 1238.
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 1239.
DBGPAUSE Register

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

TIMER: PAUSE Register

Offset: 0x30

Table 1240. PAUSE Register

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

TIMER: LOCKED Register

Offset: 0x34

Table 1241. LOCKED Register

BitsDescriptionTypeReset
31:1Reserved.--
0Set locked bit to disable write access to timer
Once set, cannot be cleared (without a reset)
RW0x0

TIMER: SOURCE Register

Offset: 0x38

Description

Selects the source for the timer. Defaults to the normal tick configured in the ticks block (typically configured to 1 microsecond). Writing to 1 will ignore the tick and count clk_sys cycles instead.

Table 1242. SOURCE Register

BitsDescriptionTypeReset
31:1Reserved.--
0CLK_SYSRW0x0
BitsDescriptionTypeReset
Enumerated values:
0x0 → TICK
0x1 → CLK_SYS

TIMER: INTR Register

Offset: 0x3c

Description

Raw Interrupts

Table 1243. INTR Register

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

TIMER: INTE Register

Offset: 0x40

Description

Interrupt Enable

Table 1244. INTE Register

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

TIMER: INTF Register

Offset: 0x44

Description

Interrupt Force

Table 1245. INTF Register

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

TIMER: INTS Register

Offset: 0x48

Description

Interrupt status after masking & forcing

Table 1246. INTS Register

BitsDescriptionTypeReset
31:4Reserved.--
3ALARM_3RO0x0
2ALARM_2RO0x0
1ALARM_1RO0x0
0ALARM_0RO0x0