1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
39
40
41
42
43
44
45
46
54
55
56
57
58
59
60
61
62
63
64
65
66
71
72
79
80
87
91
92
99
100
101
102
103
107
108
109
110
111
118
119
120
121
122
126
127
128
129
130
136
139
140
146
149
150
151
152
153
154
155
156
157
158
159
160
161
164
165
172
175
176
181
182
183
189
192
193
196
197
198
204
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
449
450
456
457
458
461
467
468
469
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
674
681
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
728
729
730
735
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
827
828
829
836
837
838
839
840
841
842
/* ... */
#ifndef _PICO_TIME_H
#define _PICO_TIME_H
#include "pico.h"
#include "hardware/timer.h"
#ifdef __cplusplus
extern "C" {
#endif
/* ... */
#ifndef PARAM_ASSERTIONS_ENABLED_PICO_TIME
#ifdef PARAM_ASSERTIONS_ENABLED_PICO_TIME
#define PARAM_ASSERTIONS_ENABLED_PICO_TIME PARAM_ASSERTIONS_ENABLED_TIME
#else
#define PARAM_ASSERTIONS_ENABLED_PICO_TIME 0
#endif/* ... */
#endif
#ifndef PICO_TIME_SLEEP_OVERHEAD_ADJUST_US
#define PICO_TIME_SLEEP_OVERHEAD_ADJUST_US 6
#endif
/* ... */
/* ... */
static inline absolute_time_t get_absolute_time(void) {
absolute_time_t t;
update_us_since_boot(&t, time_us_64());
return t;
}{ ... }
static inline uint32_t us_to_ms(uint64_t us) {
if (us >> 32u) {
return (uint32_t)(us / 1000u);
}if (us >> 32u) { ... } else {
return ((uint32_t)us) / 1000u;
}else { ... }
}{ ... }
/* ... */
static inline uint32_t to_ms_since_boot(absolute_time_t t) {
uint64_t us = to_us_since_boot(t);
return us_to_ms(us);
}{ ... }
/* ... */
static inline absolute_time_t delayed_by_us(const absolute_time_t t, uint64_t us) {
absolute_time_t t2;
uint64_t base = to_us_since_boot(t);
uint64_t delayed = base + us;
if ((int64_t)delayed < 0) {
delayed = INT64_MAX;
}if ((int64_t)delayed < 0) { ... }
update_us_since_boot(&t2, delayed);
return t2;
}{ ... }
/* ... */
static inline absolute_time_t delayed_by_ms(const absolute_time_t t, uint32_t ms) {
absolute_time_t t2;
uint64_t base = to_us_since_boot(t);
uint64_t delayed = base + ms * 1000ull;
if ((int64_t)delayed < 0) {
delayed = INT64_MAX;
}if ((int64_t)delayed < 0) { ... }
update_us_since_boot(&t2, delayed);
return t2;
}{ ... }
/* ... */
static inline absolute_time_t make_timeout_time_us(uint64_t us) {
return delayed_by_us(get_absolute_time(), us);
}{ ... }
/* ... */
static inline absolute_time_t make_timeout_time_ms(uint32_t ms) {
return delayed_by_ms(get_absolute_time(), ms);
}{ ... }
/* ... */
static inline int64_t absolute_time_diff_us(absolute_time_t from, absolute_time_t to) {
return (int64_t)(to_us_since_boot(to) - to_us_since_boot(from));
}{ ... }
/* ... */
static inline absolute_time_t absolute_time_min(absolute_time_t a, absolute_time_t b) {
return to_us_since_boot(a) < to_us_since_boot(b) ? a : b;
}{ ... }
/* ... */
extern const absolute_time_t at_the_end_of_time;
/* ... */
static inline bool is_at_the_end_of_time(absolute_time_t t) {
return to_us_since_boot(t) == to_us_since_boot(at_the_end_of_time);
}{ ... }
/* ... */
extern const absolute_time_t nil_time;
/* ... */
static inline bool is_nil_time(absolute_time_t t) {
return !to_us_since_boot(t);
}{ ... }
/* ... */
/* ... */
void sleep_until(absolute_time_t target);
/* ... */
void sleep_us(uint64_t us);
/* ... */
void sleep_ms(uint32_t ms);
/* ... */
bool best_effort_wfe_or_timeout(absolute_time_t timeout_timestamp);
/* ... */
#ifndef PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
/* ... */
#define PICO_TIME_DEFAULT_ALARM_POOL_DISABLED 0
/* ... */#endif
#ifndef PICO_TIME_DEFAULT_ALARM_POOL_HARDWARE_ALARM_NUM
/* ... */
#define PICO_TIME_DEFAULT_ALARM_POOL_HARDWARE_ALARM_NUM 3
/* ... */#endif
#ifndef PICO_TIME_DEFAULT_ALARM_POOL_MAX_TIMERS
/* ... */
#define PICO_TIME_DEFAULT_ALARM_POOL_MAX_TIMERS 16
/* ... */#endif
/* ... */
typedef int32_t alarm_id_t;
/* ... */
typedef int64_t (*alarm_callback_t)(alarm_id_t id, void *user_data);
typedef struct alarm_pool alarm_pool_t;
typedef void alarm_pool_timer_t;
/* ... */
void alarm_pool_init_default(void);
void runtime_init_default_alarm_pool(void);
#if !PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
/* ... */
alarm_pool_t *alarm_pool_get_default(void);/* ... */
#endif
alarm_pool_t *alarm_pool_create_on_timer(alarm_pool_timer_t *timer, uint timer_alarm_num, uint max_timers);
alarm_pool_timer_t *alarm_pool_timer_for_timer_num(uint timer_num);
alarm_pool_timer_t *alarm_pool_get_default_timer(void);
/* ... */
static inline alarm_pool_t *alarm_pool_create(uint timer_alarm_num, uint max_timers) {
return alarm_pool_create_on_timer(alarm_pool_get_default_timer(), timer_alarm_num, max_timers);
}{ ... }
alarm_pool_t *alarm_pool_create_on_timer_with_unused_hardware_alarm(alarm_pool_timer_t *timer, uint max_timers);
/* ... */
static inline alarm_pool_t *alarm_pool_create_with_unused_hardware_alarm(uint max_timers) {
return alarm_pool_create_on_timer_with_unused_hardware_alarm(alarm_pool_get_default_timer(), max_timers);
}{ ... }
/* ... */
uint alarm_pool_timer_alarm_num(alarm_pool_t *pool);
static inline uint alarm_pool_hardware_alarm_num(alarm_pool_t *pool) {
return alarm_pool_timer_alarm_num(pool);
}{ ... }
/* ... */
uint alarm_pool_core_num(alarm_pool_t *pool);
/* ... */
void alarm_pool_destroy(alarm_pool_t *pool);
/* ... */
alarm_id_t alarm_pool_add_alarm_at(alarm_pool_t *pool, absolute_time_t time, alarm_callback_t callback, void *user_data, bool fire_if_past);
/* ... */
alarm_id_t alarm_pool_add_alarm_at_force_in_context(alarm_pool_t *pool, absolute_time_t time, alarm_callback_t callback,
void *user_data);
/* ... */
static inline alarm_id_t alarm_pool_add_alarm_in_us(alarm_pool_t *pool, uint64_t us, alarm_callback_t callback, void *user_data, bool fire_if_past) {
return alarm_pool_add_alarm_at(pool, delayed_by_us(get_absolute_time(), us), callback, user_data, fire_if_past);
}{ ... }
/* ... */
static inline alarm_id_t alarm_pool_add_alarm_in_ms(alarm_pool_t *pool, uint32_t ms, alarm_callback_t callback, void *user_data, bool fire_if_past) {
return alarm_pool_add_alarm_at(pool, delayed_by_ms(get_absolute_time(), ms), callback, user_data, fire_if_past);
}{ ... }
/* ... */
int64_t alarm_pool_remaining_alarm_time_us(alarm_pool_t *pool, alarm_id_t alarm_id);
/* ... */
int32_t alarm_pool_remaining_alarm_time_ms(alarm_pool_t *pool, alarm_id_t alarm_id);
/* ... */
bool alarm_pool_cancel_alarm(alarm_pool_t *pool, alarm_id_t alarm_id);
#if !PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
/* ... */
static inline alarm_id_t add_alarm_at(absolute_time_t time, alarm_callback_t callback, void *user_data, bool fire_if_past) {
return alarm_pool_add_alarm_at(alarm_pool_get_default(), time, callback, user_data, fire_if_past);
}{ ... }
/* ... */
static inline alarm_id_t add_alarm_in_us(uint64_t us, alarm_callback_t callback, void *user_data, bool fire_if_past) {
return alarm_pool_add_alarm_in_us(alarm_pool_get_default(), us, callback, user_data, fire_if_past);
}{ ... }
/* ... */
static inline alarm_id_t add_alarm_in_ms(uint32_t ms, alarm_callback_t callback, void *user_data, bool fire_if_past) {
return alarm_pool_add_alarm_in_ms(alarm_pool_get_default(), ms, callback, user_data, fire_if_past);
}{ ... }
/* ... */
static inline bool cancel_alarm(alarm_id_t alarm_id) {
return alarm_pool_cancel_alarm(alarm_pool_get_default(), alarm_id);
}{ ... }
/* ... */
int64_t remaining_alarm_time_us(alarm_id_t alarm_id);
/* ... */
int32_t remaining_alarm_time_ms(alarm_id_t alarm_id);
/* ... */
#endif
/* ... */
typedef struct repeating_timer repeating_timer_t;
/* ... */
typedef bool (*repeating_timer_callback_t)(repeating_timer_t *rt);
/* ... */
struct repeating_timer {
int64_t delay_us;
alarm_pool_t *pool;
alarm_id_t alarm_id;
repeating_timer_callback_t callback;
void *user_data;
...};
/* ... */
bool alarm_pool_add_repeating_timer_us(alarm_pool_t *pool, int64_t delay_us, repeating_timer_callback_t callback, void *user_data, repeating_timer_t *out);
/* ... */
static inline bool alarm_pool_add_repeating_timer_ms(alarm_pool_t *pool, int32_t delay_ms, repeating_timer_callback_t callback, void *user_data, repeating_timer_t *out) {
return alarm_pool_add_repeating_timer_us(pool, delay_ms * (int64_t)1000, callback, user_data, out);
}{ ... }
#if !PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
/* ... */
static inline bool add_repeating_timer_us(int64_t delay_us, repeating_timer_callback_t callback, void *user_data, repeating_timer_t *out) {
return alarm_pool_add_repeating_timer_us(alarm_pool_get_default(), delay_us, callback, user_data, out);
}{ ... }
/* ... */
static inline bool add_repeating_timer_ms(int32_t delay_ms, repeating_timer_callback_t callback, void *user_data, repeating_timer_t *out) {
return alarm_pool_add_repeating_timer_us(alarm_pool_get_default(), delay_ms * (int64_t)1000, callback, user_data, out);
}{ ... }
#endif/* ... */
/* ... */
bool cancel_repeating_timer(repeating_timer_t *timer);
#ifdef __cplusplus
}extern "C" { ... }
#endif
/* ... */
#endif