Datasheet

Table Of Contents
40
41 static void alarm_in_us(uint32_t delay_us) {
42 // Enable the interrupt for our alarm (the timer outputs 4 alarm irqs)
43 hw_set_bits(&timer_hw->inte, 1u << ALARM_NUM);
44 // Set irq handler for alarm irq
45 irq_set_exclusive_handler(ALARM_IRQ, alarm_irq);
46 // Enable the alarm irq
47 irq_enable(ALARM_IRQ, true);
48 // Enable interrupt in block and at processor
49
50 // Alarm is only 32 bits so if trying to delay more
51 // than that need to be careful and keep track of the upper
52 // bits
53 uint64_t target = timer_hw->timerawl + delay_us;
54
55 // Write the lower 32 bits of the target time to the alarm which
56 // will arm it
57 timer_hw->alarm[ALARM_NUM] = (uint32_t) target;
58 }
59
60 int main() {
61 setup_default_uart();
62 printf("Timer lowlevel!\n");
63
64 // Set alarm every 2 seconds
65 while (1) {
66 alarm_fired = false;
67 alarm_in_us(1000000 * 2);
68 // Wait for alarm to fire
69 while (!alarm_fired);
70 }
71 }
4.7.4.3. Busy wait
If you don’t want to use an alarm to wait for a period of time, instead use a while loop. The Pico SDK provides various
busy_wait_ functions to do this:
Pico SDK: https://github.com/raspberrypi/pico-sdk/tree/pre_release/src/rp2_common/hardware_timer/timer.c Lines 53 - 81
53 void busy_wait_us_31(uint32_t delay_us) {
54 // we only allow 31 bits, otherwise we could have a race in the loop below with
55 // values very close to 2^32
56 assert(0 <= (int32_t)delay_us);
57 uint32_t start = timer_hw->timerawl;
58 while (timer_hw->timerawl - start < delay_us) {
59 tight_loop_contents();
60 }
61 }
62
63 void busy_wait_us(uint64_t delay_us) {
64 absolute_time_t t;
65 update_us_since_boot(&t, time_us_64() + delay_us);
66 busy_wait_until(t);
67 }
68
69 void busy_wait_until(absolute_time_t t) {
70 uint64_t target = to_us_since_boot(t);
71 uint32_t hi_target = target >> 32u;
72 uint32_t hi = timer_hw->timerawh;
RP2040 Datasheet
4.7. Timer 561