1
6
7
8
9
10
11
12
13
14
15
16
17
18
24
25
26
27
28
29
30
31
32
33
34
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
74
75
/* ... */
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/timer.h"
#include "hardware/irq.h"
static uint64_t get_time(void) {
uint32_t lo = timer_hw->timelr;
uint32_t hi = timer_hw->timehr;
return ((uint64_t) hi << 32u) | lo;
}{ ... }
#define ALARM_NUM 0
#define ALARM_IRQ hardware_alarm_get_irq_num(timer_hw, ALARM_NUM)
static volatile bool alarm_fired;
static void alarm_irq(void) {
hw_clear_bits(&timer_hw->intr, 1u << ALARM_NUM);
printf("Alarm IRQ fired\n");
alarm_fired = true;
}{ ... }
static void alarm_in_us(uint32_t delay_us) {
hw_set_bits(&timer_hw->inte, 1u << ALARM_NUM);
irq_set_exclusive_handler(ALARM_IRQ, alarm_irq);
irq_set_enabled(ALARM_IRQ, true);
uint64_t target = timer_hw->timerawl + delay_us;
timer_hw->alarm[ALARM_NUM] = (uint32_t) target;
}{ ... }
int main() {
stdio_init_all();
printf("Timer lowlevel!\n");
while (1) {
alarm_fired = false;
alarm_in_us(1000000 * 2);
while (!alarm_fired);
}while (1) { ... }
}{ ... }