Datasheet

Table Of Contents
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
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
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/tree/master/timer/timer_lowlevel/timer_lowlevel.c Lines 13 - 21
13 // Simplest form of getting 64 bit time from the timer.
14 // It isn't safe when called from 2 cores because of the latching
15 // so isn't implemented this way in the sdk
16 static uint64_t get_time(void) {
17 // Reading low latches the high value
18 uint32_t lo = timer_hw->timelr;
19 uint32_t hi = timer_hw->timehr;
20 return ((uint64_t) hi << 32u) | lo;
21 }
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/tree/master/src/rp2_common/hardware_timer/timer.c Lines 37 - 53
37 uint64_t time_us_64() {
38 // Need to make sure that the upper 32 bits of the timer
39 // don't change, so read that first
40 uint32_t hi = timer_hw->timerawh;
41 uint32_t lo;
42 do {
43 // Read the lower 32 bits
44 lo = timer_hw->timerawl;
45 // Now read the upper 32 bits again and
46 // check that it hasn't incremented. If it has loop around
47 // and read the lower 32 bits again to get an accurate value
48 uint32_t next_hi = timer_hw->timerawh;
49 if (hi == next_hi) break;
50 hi = next_hi;
RP2040 Datasheet
4.6. Timer 557