-
Notifications
You must be signed in to change notification settings - Fork 6
/
DeepSleepScheduler_esp_implementation.h
281 lines (252 loc) · 7.16 KB
/
DeepSleepScheduler_esp_implementation.h
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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
250
251
252
253
254
255
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
#ifdef ESP32
#include <esp_sleep.h>
#include <esp32-hal-timer.h>
#include <soc/rtc.h>
#elif ESP8266
#include <limits.h>
#endif
#ifndef LIBCALL_DEEP_SLEEP_SCHEDULER
// -------------------------------------------------------------------------------------------------
// Implementation (usuallly in CPP file)
// -------------------------------------------------------------------------------------------------
#define ESP8266_MAX_DELAY_TIME_WDT_MS 7500
void Scheduler::init() {}
#ifdef ESP32
// -------------------------------------------------------------------------------------------------
unsigned long Scheduler::getMillis() const {
// read RTC clock which runs from initial boot/reset (also during sleep)
// https://forum.makehackvoid.com/t/playing-with-the-esp-32/1144/11
uint64_t rtcTime = rtc_time_get();
uint64_t rtcTimeUs = rtcTime * 20 / 3; // ticks -> us 1,000,000/150,000
return rtcTimeUs / 1000;
}
void IRAM_ATTR Scheduler::isrWatchdogExpiredStatic() {
#ifdef SUPERVISION_CALLBACK
if (supervisionCallbackRunnable != NULL) {
// No need to supervise this call as this interrupt has a time limit.
// When it expires, the system is restarted.
supervisionCallbackRunnable->run();
}
#endif
ets_printf("Watchdog abort by DeepSleepScheduler\n");
ESP_ERROR_CHECK(ESP_ERR_TIMEOUT);
}
/**
Interrupt service routine called when the timer expires.
*/
void IRAM_ATTR isrWatchdogExpired() {
Scheduler::isrWatchdogExpiredStatic();
}
void Scheduler::taskWdtEnable(const uint8_t value) {
if (value != NO_SUPERVISION) {
const unsigned long durationMs = wdtTimeoutToDurationMs(value);
if (timer == NULL) {
// div 80
timer = timerBegin(ESP32_TASK_WDT_TIMER_NUMBER, 80, true);
timerAttachInterrupt(timer, &isrWatchdogExpired, true);
}
//set time in us
timerAlarmWrite(timer, durationMs * 1000, false);
//enable interrupt
//only works after taskWdtDisable() if yield() is done before
yield();
timerAlarmEnable(timer);
} else {
taskWdtDisable();
}
}
void Scheduler::taskWdtDisable() {
if (timer != NULL) {
//disable interrupt
timerAlarmDisable(timer);
timerDetachInterrupt(timer);
timerEnd(timer);
timer = NULL;
}
}
void Scheduler::taskWdtReset() {
//reset timer (feed watchdog)
if (timer != NULL) {
timerWrite(timer, 0);
}
}
#elif ESP8266
// -------------------------------------------------------------------------------------------------
unsigned long Scheduler::getMillis() const {
// on ESP8266 we do not support sleep, so millis() stays correct.
return millis();
}
void Scheduler::taskWdtEnable(const uint8_t value) {
const unsigned long durationMs = wdtTimeoutToDurationMs(value);
ESP.wdtEnable(durationMs);
}
void Scheduler::taskWdtDisable() {
ESP.wdtDisable();
}
void Scheduler::taskWdtReset() {
ESP.wdtFeed();
}
#endif
// -------------------------------------------------------------------------------------------------
inline unsigned long Scheduler::wdtTimeoutToDurationMs(const uint8_t value) {
unsigned long durationMs;
switch (value) {
case TIMEOUT_15Ms: {
durationMs = 15;
break;
}
case TIMEOUT_30MS: {
durationMs = 30;
break;
}
case TIMEOUT_60MS: {
durationMs = 60;
break;
}
case TIMEOUT_120MS: {
durationMs = 120;
break;
}
case TIMEOUT_250MS: {
durationMs = 250;
break;
}
case TIMEOUT_500MS: {
durationMs = 500;
break;
}
case TIMEOUT_1S: {
durationMs = 1000;
break;
}
case TIMEOUT_2S: {
durationMs = 2000;
break;
}
case TIMEOUT_4S: {
durationMs = 4000;
break;
}
case TIMEOUT_8S: {
durationMs = 8000;
break;
}
default: {
// should not happen
durationMs = 15;
}
}
return durationMs;
}
void Scheduler::sleepIfRequired() {
noInterrupts();
bool queueEmpty = first == NULL;
interrupts();
SleepMode sleepMode = IDLE;
if (!queueEmpty) {
sleepMode = evaluateSleepMode();
} else {
// nothing in the queue
if (doesSleep()
#ifdef SLEEP_DELAY
&& millis() >= lastTaskFinishedMillis + SLEEP_DELAY
#endif
) {
sleepMode = SLEEP;
} else {
sleepMode = IDLE;
}
}
if (sleepMode != NO_SLEEP) {
#ifdef AWAKE_INDICATION_PIN
digitalWrite(AWAKE_INDICATION_PIN, LOW);
#endif
if (sleepMode == SLEEP) {
taskWdtDisable();
noInterrupts();
unsigned long currentSchedulerMillis = getMillis();
unsigned long firstScheduledUptimeMillis = 0;
if (first != NULL) {
firstScheduledUptimeMillis = first->scheduledUptimeMillis;
}
unsigned long maxWaitTimeMillis = 0;
if (firstScheduledUptimeMillis > currentSchedulerMillis) {
maxWaitTimeMillis = firstScheduledUptimeMillis - currentSchedulerMillis;
}
interrupts();
sleep(maxWaitTimeMillis, queueEmpty);
} else { // IDLE
yield();
}
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
#ifdef AWAKE_INDICATION_PIN
digitalWrite(AWAKE_INDICATION_PIN, HIGH);
#endif
}
}
inline Scheduler::SleepMode Scheduler::evaluateSleepMode() {
noInterrupts();
unsigned long currentSchedulerMillis = getMillis();
unsigned long firstScheduledUptimeMillis = 0;
if (first != NULL) {
firstScheduledUptimeMillis = first->scheduledUptimeMillis;
}
interrupts();
SleepMode sleepMode = NO_SLEEP;
unsigned long maxWaitTimeMillis = 0;
if (firstScheduledUptimeMillis > currentSchedulerMillis) {
maxWaitTimeMillis = firstScheduledUptimeMillis - currentSchedulerMillis;
}
if (maxWaitTimeMillis == 0) {
sleepMode = NO_SLEEP;
} else if (!doesSleep() || maxWaitTimeMillis < BUFFER_TIME
#ifdef SLEEP_DELAY
|| millis() < lastTaskFinishedMillis + SLEEP_DELAY
#endif
) {
// use IDLE for values less then BUFFER_TIME
sleepMode = IDLE;
} else {
sleepMode = SLEEP;
}
return sleepMode;
}
#ifdef ESP32
// -------------------------------------------------------------------------------------------------
void Scheduler::sleep(unsigned long durationMs, bool queueEmpty) {
bool timerWakeup;
if (durationMs > 0) {
esp_sleep_enable_timer_wakeup(durationMs * 1000L);
timerWakeup = true;
} else if (queueEmpty) {
#ifdef ESP_DEEP_SLEEP_FOR_INFINITE_SLEEP
esp_deep_sleep_start(); // does not return
#endif
timerWakeup = false;
} else {
// should not happen
esp_sleep_enable_timer_wakeup(1);
timerWakeup = true;
}
esp_light_sleep_start();
if (timerWakeup) {
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
}
}
#elif ESP8266
// -------------------------------------------------------------------------------------------------
void Scheduler::sleep(unsigned long durationMs, bool queueEmpty) {
#ifdef ESP_DEEP_SLEEP_FOR_INFINITE_SLEEP
if (queueEmpty) {
ESP.deepSleep(0); // does not return
}
#endif
if (durationMs > ESP8266_MAX_DELAY_TIME_MS) {
durationMs = ESP8266_MAX_DELAY_TIME_MS;
}
delay(durationMs);
ESP.wdtFeed();
}
#endif
// -------------------------------------------------------------------------------------------------
#endif // #ifndef LIBCALL_DEEP_SLEEP_SCHEDULER