Datasheet

Table Of Contents
Pico Examples: https://github.com/raspberrypi/pico-examples/tree/pre_release/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 Pico 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.
Pico SDK: https://github.com/raspberrypi/pico-sdk/tree/pre_release/src/rp2_common/hardware_timer/timer.c Lines 33 - 49
33 uint64_t time_us_64() {
34 // Need to make sure that the upper 32 bits of the timer
35 // don't change, so read that first
36 uint32_t hi = timer_hw->timerawh;
37 uint32_t lo;
38 do {
39 // Read the lower 32 bits
40 lo = timer_hw->timerawl;
41 // Now read the upper 32 bits again and
42 // check that it hasn't incremented. If it has loop around
43 // and read the lower 32 bits again to get an accurate value
44 uint32_t next_hi = timer_hw->timerawh;
45 if (hi == next_hi) break;
46 hi = next_hi;
47 } while (true);
48 return ((uint64_t) hi << 32u) | lo;
49 }
4.7.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 Pico SDK provides. To use these abstractions see Section 4.7.4.4.
Pico Examples: https://github.com/raspberrypi/pico-examples/tree/pre_release/timer/timer_lowlevel/timer_lowlevel.c Lines 25 - 72
25 // Use alarm 0
26 #define ALARM_NUM 0
27 #define ALARM_IRQ TIMER_IRQ_0
28
29 // Alarm interrupt handler
30 static volatile bool alarm_fired;
31
32 static void alarm_irq(void) {
33 // Clear the alarm irq
34 hw_clear_bits(&timer_hw->intr, 1u << ALARM_NUM);
35
36 // Assume alarm 0 has fired
37 printf("Alarm IRQ fired\n");
38 alarm_fired = true;
39 }
RP2040 Datasheet
4.7. Timer 560