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:
- • A single 64-bit counter, incrementing once per microsecond
- ◦ Read from a pair of latching registers for race-free reads over a 32-bit bus
- • Four alarms that match on the lower 32 bits of the counter and generate IRQ on match
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
- • RP2350 now has two timer instances: TIMER0 and TIMER1
- • On RP2350, the tick source for each timer comes from the system-level tick generators (see Section 8.5 )
- • RP2350 added two new registers: LOCKED is used to disable write access to the timer, and SOURCE allows the timer to
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.
- • The PWM (
Section 12.5
) contains 12× 16-bit programmable counters. These counters:
- ◦ run at up to system speed
- ◦ can generate interrupts to either of two system IRQ lines
- ◦ can be continuously reprogrammed via the DMA
- ◦ can trigger DMA transfers to other peripherals
- • 12× PIO state machines ( Chapter 11 ) can count 32-bit values at system speed, and generate interrupts.
- • The DMA ( Section 12.6 ) has four internal pacing timers which trigger transfers at regular intervals.
- • Each Cortex-M33 core ( Section 3.7 ) has a standard 24-bit SysTick timer, counting either the microsecond tick ( Section 8.5 ) or the system clock.
- • SIO has a standard 64-bit RISC-V platform timer ( Section 3.1.8 ). Arm and RISC-V software can use this timer.
- • The Power Manager ( Chapter 6 ) incorporates a 64-bit timer (AON Timer) which nominally counts milliseconds (see Section 12.10 ). This is the only timer that runs when the chip is in its lowest power state, with all switchable power domains powered down. It is used to schedule power-ups.
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:
- •
TIMEHWandTIMELWto write the time - •
TIMEHRandTIMELRto read the time
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
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:
- • \( 2^{32} \div 10^6 \) : ~4295 seconds
- • \( 4295 \div 60 \) : ~72 minutes
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. Enable the interrupt at the timer with a write to the appropriate alarm bit in
INTE
(e.g.
(1 << 0)for ALARM0 ). - 2. Enable the appropriate timer interrupt at the processor (see Section 3.2 ).
- 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
NOTEThe 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
NOTETime 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
| Offset | Name | Info |
|---|---|---|
| 0x00 | TIMEHW | Write to bits 63:32 of time always write timelw before
timehw |
| 0x04 | TIMELW | Write to bits 31:0 of time writes do not get copied to time until timehw is written |
| 0x08 | TIMEHR | Read from bits 63:32 of time always read timelr before
timehr |
| 0x0c | TIMELR | Read from bits 31:0 of time |
| 0x10 | ALARM0 | Arm 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. |
| 0x14 | ALARM1 | Arm 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. |
| 0x18 | ALARM2 | Arm 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. |
| 0x1c | ALARM3 | Arm 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. |
| 0x20 | ARMED | Indicates 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. |
| 0x24 | TIMERAWH | Raw read from bits 63:32 of time (no side effects) |
| 0x28 | TIMERAWL | Raw read from bits 31:0 of time (no side effects) |
| 0x2c | DBGPAUSE | Set bits high to enable pause when the corresponding debug ports are active |
| 0x30 | PAUSE | Set high to pause the timer |
| 0x34 | LOCKED | Set locked bit to disable write access to timer Once set, cannot be cleared (without a reset) |
| Offset | Name | Info |
|---|---|---|
| 0x38 | SOURCE | 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. |
| 0x3c | INTR | Raw Interrupts |
| 0x40 | INTE | Interrupt Enable |
| 0x44 | INTF | Interrupt Force |
| 0x48 | INTS | Interrupt status after masking & forcing |
TIMER: TIMEHW Register
Offset: 0x00
Table 1228. TIMEHW Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | Write to bits 63:32 of time always write timelw before timehw | WF | 0x00000000 |
TIMER: TIMELW Register
Offset: 0x04
Table 1229. TIMELW Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | Write to bits 31:0 of time writes do not get copied to time until timehw is written | WF | 0x00000000 |
TIMER: TIMEHR Register
Offset: 0x08
Table 1230. TIMEHR Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | Read from bits 63:32 of time always read timelr before timehr | RO | 0x00000000 |
TIMER: TIMELR Register
Offset: 0x0c
Table 1231. TIMELR Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | Read from bits 31:0 of time | RO | 0x00000000 |
TIMER: ALARM0 Register
Offset: 0x10
Table 1232. ALARM0 Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | Arm 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. | RW | 0x00000000 |
Offset: 0x14
Table 1233. ALARM1 Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | Arm 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. | RW | 0x00000000 |
Offset: 0x18
Table 1234. ALARM2 Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | Arm 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. | RW | 0x00000000 |
Offset: 0x1c
Table 1235. ALARM3 Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | Arm 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. | RW | 0x00000000 |
Offset: 0x20
Table 1236. ARMED Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:4 | Reserved. | - | - |
| 3:0 | Indicates 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. | WC | 0x0 |
Offset: 0x24
Table 1237.
TIMERAWH Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | Raw read from bits 63:32 of time (no side effects) | RO | 0x00000000 |
TIMER: TIMERAWL Register
Offset: 0x28
Table 1238.
TIMERAWL Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | Raw read from bits 31:0 of time (no side effects) | RO | 0x00000000 |
TIMER: DBGPAUSE Register
Offset: 0x2c
Description
Set bits high to enable pause when the corresponding debug ports are active
Table 1239.
DBGPAUSE Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:3 | Reserved. | - | - |
| 2 | DBG1 : Pause when processor 1 is in debug mode | RW | 0x1 |
| 1 | DBG0 : Pause when processor 0 is in debug mode | RW | 0x1 |
| 0 | Reserved. | - | - |
TIMER: PAUSE Register
Offset: 0x30
Table 1240. PAUSE Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:1 | Reserved. | - | - |
| 0 | Set high to pause the timer | RW | 0x0 |
TIMER: LOCKED Register
Offset: 0x34
Table 1241. LOCKED Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:1 | Reserved. | - | - |
| 0 | Set locked bit to disable write access to timer Once set, cannot be cleared (without a reset) | RW | 0x0 |
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
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:1 | Reserved. | - | - |
| 0 | CLK_SYS | RW | 0x0 |
| Bits | Description | Type | Reset |
|---|---|---|---|
| Enumerated values: | |||
| 0x0 → TICK | |||
| 0x1 → CLK_SYS |
TIMER: INTR Register
Offset: 0x3c
Description
Raw Interrupts
Table 1243. INTR Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:4 | Reserved. | - | - |
| 3 | ALARM_3 | WC | 0x0 |
| 2 | ALARM_2 | WC | 0x0 |
| 1 | ALARM_1 | WC | 0x0 |
| 0 | ALARM_0 | WC | 0x0 |
TIMER: INTE Register
Offset: 0x40
Description
Interrupt Enable
Table 1244. INTE Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:4 | Reserved. | - | - |
| 3 | ALARM_3 | RW | 0x0 |
| 2 | ALARM_2 | RW | 0x0 |
| 1 | ALARM_1 | RW | 0x0 |
| 0 | ALARM_0 | RW | 0x0 |
TIMER: INTF Register
Offset: 0x44
Description
Interrupt Force
Table 1245. INTF Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:4 | Reserved. | - | - |
| 3 | ALARM_3 | RW | 0x0 |
| 2 | ALARM_2 | RW | 0x0 |
| 1 | ALARM_1 | RW | 0x0 |
| 0 | ALARM_0 | RW | 0x0 |
TIMER: INTS Register
Offset: 0x48
Description
Interrupt status after masking & forcing
Table 1246. INTS Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:4 | Reserved. | - | - |
| 3 | ALARM_3 | RO | 0x0 |
| 2 | ALARM_2 | RO | 0x0 |
| 1 | ALARM_1 | RO | 0x0 |
| 0 | ALARM_0 | RO | 0x0 |