-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgptimer.cpp
More file actions
50 lines (40 loc) · 1.48 KB
/
gptimer.cpp
File metadata and controls
50 lines (40 loc) · 1.48 KB
1
2
3
4
5
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Example: ESP-IDF v5 GPTimer — periodic alarm with auto-reload
#include "Arduino.h"
#include "driver/gptimer.h"
static volatile int alarm_fired = 0;
static bool IRAM_ATTR timer_alarm_cb(gptimer_handle_t /*timer*/, const gptimer_alarm_event_data_t *edata, void * /*ctx*/) {
alarm_fired++;
(void)edata;
return false; // no need to yield
}
static gptimer_handle_t gptimer = nullptr;
void setup() {
Serial.begin(115200);
Serial.println("GPTimer Example — 1 second periodic alarm");
gptimer_config_t cfg{};
cfg.clk_src = GPTIMER_CLK_SRC_DEFAULT;
cfg.direction = GPTIMER_COUNT_UP;
cfg.resolution_hz = 1000000; // 1MHz = 1 tick per microsecond
ESP_ERROR_CHECK(gptimer_new_timer(&cfg, &gptimer));
gptimer_alarm_config_t alarm_cfg{};
alarm_cfg.alarm_count = 1000000; // 1 second
alarm_cfg.reload_count = 0;
alarm_cfg.flags.auto_reload_on_alarm = 1;
ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer, &alarm_cfg));
gptimer_event_callbacks_t cbs{};
cbs.on_alarm = timer_alarm_cb;
ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer, &cbs, nullptr));
ESP_ERROR_CHECK(gptimer_enable(gptimer));
ESP_ERROR_CHECK(gptimer_start(gptimer));
Serial.println("Timer started!");
}
void loop() {
static int last_count = 0;
if (alarm_fired != last_count) {
Serial.print("⏰ Alarm #");
Serial.print(alarm_fired);
Serial.println(" fired!");
last_count = alarm_fired;
}
delay(100);
}