Tutorials

Schedule an RP2040 Timer Alarm Across the 32-Bit Microsecond Wrap

Published · Updated

The RP2040 exposes a 64-bit microsecond timebase, but its hardware alarms match the low 32 bits. A timestamp that looks smaller than the current low word can still be the next valid deadline.

Keep the full deadline and the hardware match value separately

Component side of the original Raspberry Pi Pico. U1 contains the RP2040 peripherals discussed in this example.
Component side of the original Raspberry Pi Pico. U1 contains the RP2040 peripherals discussed in this example. Phiarc, Raspberry Pi Pico top (Wikimedia Commons), CC BY-SA 4.0. WebP adaptation.

At one tick per microsecond, the low word rolls over every 4,294,967,296 µs, about 71 minutes and 35 seconds. Compare long deadlines in a 64-bit software time domain. Before programming a low-word alarm, verify the intended occurrence lies in the hardware scheduling horizon and allow time to arm it.

If setup can overrun the deadline, implement an explicit late path. Waiting for another equality could delay the event for a complete low-word cycle.

Test modular timestamp differences · Test a counter at its boundaries

Add one millisecond near the rollover point

Initial low word / 0xFFFFFF00 / 256 µs to rollover; Requested interval / 1,000 µs / Crosses zero; Hardware match / 0x000002E8 / 744 µs after zero
Checkpoint / Value / Interpretation.

Start from low word 0xFFFFFF00. Adding 1,000 µs advances 256 ticks to zero and then 744 ticks farther, so the new low word is 0x000002E8. That target is in the future in this example.

A plain unsigned comparison of target against current low word says otherwise. Test three cases in firmware: a normal future deadline, a future deadline across rollover and a target already missed while arming. Record the full timestamp and whether the late branch fired.

Can a single low-word match schedule an event two hours away unambiguously?

The low word repeats sooner than two hours. Retain the long deadline in software and arm a nearer rendezvous or use the SDK scheduling facility after checking its documented horizon.

Read the example one step at a timeInitial low word / 0xFFFFFF00 / 256 µs to rollover; Requested interval / 1,000 µs / Crosses zero; Hardware match / 0x000002E8 / 744 µs after zero
  1. Initial low word · 0xFFFFFF00 · 256 µs to rollover
  2. Requested interval · 1,000 µs · Crosses zero
  3. Hardware match · 0x000002E8 · 744 µs after zero

Look for missed trigger edges · Check elapsed time and throughput separately

Use the board bring-up checklist.

Raspberry Pi RP2040 Datasheet, 4.6 Timer.

Photograph credit and licence

Phiarc, Raspberry Pi Pico top (Wikimedia Commons), CC BY-SA 4.0. WebP adaptation.

Original photograph https://commons.wikimedia.org/wiki/File:Raspberry_Pi_Pico_top.jpg

Licence https://creativecommons.org/licenses/by-sa/4.0/

Back to Tutorials

Related evidence and next tasks

Continue the task with the relevant source scope. These reading paths do not establish interchangeability or current supply.

Source addresses and citation trail

Full source addresses are retained below. Read each citation with its document revision, page, table and applicability conditions above.

  1. https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf#page=511