-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw_timer_esp32.cpp
75 lines (65 loc) · 2.79 KB
/
hw_timer_esp32.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/******************************************************************************
* This file is part of Dimmable Light for Arduino, a library to control *
* dimmers. *
* *
* Copyright (C) 2018-2023 Fabiano Riccardi *
* *
* Dimmable Light for Arduino is free software; you can redistribute *
* it and/or modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, see <http://www.gnu.org/licenses/>. *
******************************************************************************/
#ifdef ESP32
#include "hw_timer_esp32.h"
const static int TIMER_ID = 0;
static hw_timer_t* timer = nullptr;
void timerInit(void (*callback)()) {
#if ESP_IDF_VERSION_MAJOR >= 5
timer = timerBegin(1000000);
timerWrite(timer, 0);
timerAttachInterrupt(timer, callback);
#else
// Use 1st timer of 4 (counted from zero).
// Set 80 divider for prescaler (see ESP32 Technical Reference Manual for more
// info), count up. The counter starts to increase its value.
timer = timerBegin(TIMER_ID, 80, true);
timerStop(timer);
timerWrite(timer, 0);
timerAttachInterrupt(timer, callback, false);
#endif
}
void ARDUINO_ISR_ATTR startTimerAndTrigger(uint32_t delay) {
#if ESP_IDF_VERSION_MAJOR >= 5
timerRestart(timer);
timerAlarm(timer, delay, false, 0);
#else
timerWrite(timer, 0);
timerAlarmWrite(timer, delay, false);
timerAlarmEnable(timer);
timerStart(timer);
#endif
}
void ARDUINO_ISR_ATTR setAlarm(uint32_t delay) {
#if ESP_IDF_VERSION_MAJOR >= 5
timerAlarm(timer, delay, false, 0);
#else
timerAlarmWrite(timer, delay, false);
// On core v2.0.0-2.0.1, the timer alarm is automatically disabled after triggering,
// so re-enable the alarm
timerAlarmEnable(timer);
#endif
}
void ARDUINO_ISR_ATTR stopTimer() {
#if ESP_IDF_VERSION_MAJOR < 5
timerStop(timer);
#endif
}
#endif // END ESP32