Datasheet

Table Of Contents
73 while (hi < hi_target) {
74 hi = timer_hw->timerawh;
75 tight_loop_contents();
76 }
77 while (hi == hi_target && timer_hw->timerawl < (uint32_t) target) {
78 hi = timer_hw->timerawh;
79 tight_loop_contents();
80 }
81 }
4.7.4.4. Complete example using Pico SDK
Pico Examples: https://github.com/raspberrypi/pico-examples/tree/pre_release/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, 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(struct repeating_timer *t) {
21 printf("Repeat at %lld\n", time_us_64());
22 return true;
23 }
24
25 int main() {
26 setup_default_uart();
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);
RP2040 Datasheet
4.7. Timer 562