-
Notifications
You must be signed in to change notification settings - Fork 71
/
Activity.h
472 lines (410 loc) · 13.4 KB
/
Activity.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
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
331
332
333
334
335
336
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
382
383
384
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef __ACTIVITY_H__
#define __ACTIVITY_H__
#include "Debug.h"
#include <AlarmClock.h>
#include <Radio.h>
#if defined(ARDUINO_ARCH_AVR) && ! ( defined(ARDUINO_AVR_ATmega32) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega128__))
#include <LowPower.h>
#endif
#if defined ARDUINO_ARCH_STM32
#include "STM32LowPower.h"
#endif
/*#if defined ARDUINO_ARCH_STM32 && defined STM32L1xx
#include "low_power.h"
#include "rtc.h"
#include <time.h>
typedef void (*voidFuncPtrVoid)(void);
static void rtcmatch(void*) {
//DPRINT('.');
}
static class STM32L1xx_LowPower {
alarm_t myalarm;
public:
STM32L1xx_LowPower() {};
void begin(void) {
RTC_SetClockSource(LSI_CLOCK);
RTC_setPrediv(36, 0);
RTC_init(HOUR_FORMAT_24, LSI_CLOCK, true);
LowPower_init();
}
void idle(uint32_t millis = 0) {
if (millis > 0) programRtcWakeUp(millis);
LowPower_sleep(PWR_MAINREGULATOR_ON);
}
void sleep(uint32_t millis = 0) {
if (millis > 0) programRtcWakeUp(millis);
LowPower_sleep(PWR_LOWPOWERREGULATOR_ON);
}
void deepSleep(uint32_t millis = 0) {
if (millis > 0) programRtcWakeUp(millis);
LowPower_stop(_serial);
detachAlarmCallback(myalarm);
}
void shutdown(uint32_t millis = 0) {
if (millis > 0) programRtcWakeUp(millis);
LowPower_shutdown();
}
void programRtcWakeUp(uint32_t millis) {
uint32_t _subSeconds; hourAM_PM_t p;
uint8_t _day, _hour, _min, _sec, _dmy;
RTC_GetTime(&_hour, &_min, &_sec, &_subSeconds, &p);
RTC_GetDate(&_dmy, &_dmy, &_day, &_dmy);
// tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst
struct tm tm = { _sec, _min, _hour, _day, 0, 100, 0, 0, -1 };
time_t tmp = mktime(&tm);
tmp += millis;
struct tm* ptm = gmtime(&tmp);
//DPRINT(millis); DPRINT(", "); DPRINTLN((uint32_t)tmp);
//DPRINT(tm.tm_mday); DPRINT(':'); DPRINT(tm.tm_hour); DPRINT(':'); DPRINT(tm.tm_min); DPRINT(':'); DPRINTLN(tm.tm_sec);
//DPRINT(ptm->tm_mday); DPRINT(':'); DPRINT(ptm->tm_hour); DPRINT(':'); DPRINT(ptm->tm_min); DPRINT(':'); DPRINTLN(ptm->tm_sec);
RTC_StartAlarm(myalarm, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, _subSeconds, p, 15);
attachAlarmCallback(rtcmatch, NULL, myalarm);
}
void attachInterruptWakeup(uint32_t pin, voidFuncPtrVoid callback, uint32_t mode) {
attachInterrupt(pin, callback, mode);
LowPower_EnableWakeUpPin(pin, mode);
}
private:
serial_t* _serial; // Serial for wakeup from deep sleep
} LowPower;
#endif*/
#ifdef ARDUINO_ARCH_ESP32
#include "esp_sleep.h"
#endif
namespace as {
#if defined(ARDUINO_ARCH_AVR) && ! (defined(ARDUINO_AVR_ATmega32) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega128__))
template <bool ENABLETIMER2=false, bool ENABLEADC=false, bool ENABLETWI=false>
class Idle {
public:
static void waitSerial () {
// DPRINT(F("Go sleep - ")); DHEXLN((uint16_t)sysclock.next());
Serial.flush();
while (!(UCSR0A & (1 << UDRE0))) { // Wait for empty transmit buffer
UCSR0A |= 1 << TXC0; // mark transmission not complete
}
while (!(UCSR0A & (1 << TXC0))); // Wait for the transmission to complete
}
template <class Hal>
static void powerSave (__attribute__((unused)) Hal& hal) {
#if defined __AVR_ATmega644P__ || defined (__AVR_ATmega1284P__)
LowPower.idle(SLEEP_FOREVER,ENABLEADC==true?ADC_ON:ADC_OFF,ENABLETIMER2==false?TIMER2_OFF:TIMER2_ON,TIMER1_ON,TIMER0_OFF,SPI_ON,USART1_OFF,USART0_ON,ENABLETWI==true?TWI_ON:TWI_OFF);
#elif defined __AVR_ATmega2560__
//there is an issue, so you have to manual change something in Low-Power.cpp: https://github.com/rocketscream/Low-Power/issues/30#issuecomment-336801240
LowPower.idle(SLEEP_FOREVER,ENABLEADC==true?ADC_ON:ADC_OFF, TIMER5_OFF, TIMER4_OFF, TIMER3_OFF,ENABLETIMER2==false?TIMER2_OFF:TIMER2_ON, TIMER1_ON, TIMER0_OFF, SPI_ON, USART3_OFF,USART2_OFF, USART1_OFF, USART0_ON, ENABLETWI==true?TWI_ON:TWI_OFF);
#else
LowPower.idle(SLEEP_FOREVER,ENABLEADC==true?ADC_ON:ADC_OFF,ENABLETIMER2==false?TIMER2_OFF:TIMER2_ON,TIMER1_ON,TIMER0_OFF,SPI_ON,USART0_ON,ENABLETWI==true?TWI_ON:TWI_OFF);
#endif
}
};
template <bool ENABLETIMER2=false>
class Sleep : public Idle<ENABLETIMER2> {
public:
static uint32_t doSleep (uint32_t ticks) {
uint32_t offset = 0;
period_t sleeptime = SLEEP_FOREVER;
if( ticks > seconds2ticks(8) ) { offset = seconds2ticks(8); sleeptime = SLEEP_8S; }
// else if( ticks > seconds2ticks(4) ) { offset = seconds2ticks(4); sleeptime = SLEEP_4S; }
// else if( ticks > seconds2ticks(2) ) { offset = seconds2ticks(2); sleeptime = SLEEP_2S; }
else if( ticks > seconds2ticks(1) ) { offset = seconds2ticks(1); sleeptime = SLEEP_1S; }
// else if( ticks > millis2ticks(500) ) { offset = millis2ticks(500); sleeptime = SLEEP_500MS; }
// else if( ticks > millis2ticks(250) ) { offset = millis2ticks(250); sleeptime = SLEEP_250MS; }
else if( ticks > millis2ticks(120) ) { offset = millis2ticks(120); sleeptime = SLEEP_120MS; }
// else if( ticks > millis2ticks(60) ) { offset = millis2ticks(60); sleeptime = SLEEP_60MS; }
// else if( ticks > millis2ticks(30) ) { offset = millis2ticks(30); sleeptime = SLEEP_30MS; }
else if( ticks > millis2ticks(15) ) { offset = millis2ticks(15); sleeptime = SLEEP_15MS; }
if( ENABLETIMER2 == false ) {
LowPower.powerDown(sleeptime,ADC_OFF,BOD_OFF);
}
else {
LowPower.powerExtStandby(sleeptime,ADC_OFF,BOD_OFF,TIMER2_ON);
}
return offset;
}
template <class Hal>
static void powerSave (Hal& hal) {
sysclock.disable();
uint32_t ticks = sysclock.next();
if( sysclock.isready() == false ) {
if( ticks == 0 || ticks > millis2ticks(15) ) {
hal.setIdle();
uint32_t offset = doSleep(ticks);
hal.unsetIdle();
sysclock.correct(offset);
sysclock.enable();
}
else{
sysclock.enable();
//Idle<ENABLETIMER2>::powerSave(hal);
}
}
else {
sysclock.enable();
}
}
};
#ifndef NORTC
class SleepRTC : public Idle<true> {
public:
static uint32_t doSleep (uint32_t ticks) {
uint32_t offset = 0;
period_t sleeptime = SLEEP_FOREVER;
if( ticks > seconds2ticks(1) ) { sleeptime = SLEEP_FOREVER; }
// else if( ticks > millis2ticks(500) ) { sleeptime = SLEEP_500MS; }
// else if( ticks > millis2ticks(250) ) { sleeptime = SLEEP_250MS; }
else if( ticks > millis2ticks(120) ) { sleeptime = SLEEP_120MS; }
// else if( ticks > millis2ticks(60) ) { sleeptime = SLEEP_60MS; }
// else if( ticks > millis2ticks(30) ) { sleeptime = SLEEP_30MS; }
else if( ticks > millis2ticks(15) ) { sleeptime = SLEEP_15MS; }
uint32_t c1 = rtc.getCounter(true);
LowPower.powerSave(sleeptime,ADC_OFF,BOD_OFF,TIMER2_ON);
uint32_t c2 = rtc.getCounter(false);
offset = (c2 - c1) * seconds2ticks(1) / 256;
// DHEX(ticks);DPRINT(" ");DHEX(c1);DPRINT(":");DHEX(c2);DPRINT(" ");DHEXLN(offset);
return min(ticks,offset);
}
template <class Hal>
static void powerSave (Hal& hal) {
sysclock.disable();
uint32_t ticks = sysclock.next();
if( sysclock.isready() == false ) {
if( ticks == 0 || ticks > millis2ticks(15) ) {
hal.setIdle();
uint32_t offset = doSleep(ticks);
hal.unsetIdle();
sysclock.correct(offset);
sysclock.enable();
}
else{
sysclock.enable();
Idle<true>::powerSave(hal);
}
}
else {
sysclock.enable();
}
}
};
#endif // NORTC
#endif
#if defined ARDUINO_ARCH_STM32
// more time to spend here
template <bool ENABLETIMER2 = false, bool ENABLEADC = false>
class Idle {
public:
static void waitSerial() {
//DPRINT(F("Go sleep - ")); DHEXLN((uint16_t)sysclock.next());
DSERIAL.flush(); // waits for the transmission of outgoing serial data to complete
}
template <class Hal>
static void powerSave(__attribute__((unused)) Hal& hal) {
// ENABLEADC == true ? ADC_ON : ADC_OFF, ENABLETIMER2 == false ? TIMER2_OFF : TIMER2_ON, TIMER1_ON, TIMER0_OFF, SPI_ON, USART0_ON, TWI_OFF);
LowPower.idle();
}
};
template <bool ENABLETIMER2 = false>
class Sleep : public Idle<ENABLETIMER2> {
public:
static uint32_t doSleep(uint32_t ticks) {
uint32_t sleeptime = 0;
// limit the max sleeptime to 8 seconds
if (ticks > seconds2ticks(8)) ticks = seconds2ticks(8);
sleeptime = ticks2millis(ticks);
// ADC_OFF, BOD_OFF, TIMER_OFF
//DPRINT(millis2ticks(sleeptime)); delay(500);
LowPower.deepSleep(sleeptime);
return millis2ticks(sleeptime-1);
}
template <class Hal>
static void powerSave(Hal& hal) {
sysclock.disable();
uint32_t ticks = sysclock.next();
if (sysclock.isready() == false) {
if (ticks == 0 || ticks > millis2ticks(15)) {
hal.setIdle();
uint32_t offset = doSleep(ticks);
hal.unsetIdle();
sysclock.correct(offset);
sysclock.enable();
} else {
sysclock.enable();
//Idle<ENABLETIMER2>::powerSave(hal);
}
} else {
sysclock.enable();
}
}
};
#endif
#if defined ARDUINO_ARCH_EFM32
class Sleep {
public:
static void waitSerial() {
//TODO: Serial.flush leads crashes when entering EM2
//Serial.flush();
}
template <class Hal>
static void powerSave(Hal& hal) {
uint32_t priMask = __get_PRIMASK();
uint32_t sysTickCtrl = SysTick->CTRL;
/* mask all IRQs, disable SysTick IRQ at the source, clear&disable SysTick IRQ in NVIC*/
__set_PRIMASK(1);
SysTick->CTRL &= ~0x03; // clear TICKINT & ENABLE
NVIC_ClearPendingIRQ(SysTick_IRQn);
NVIC_DisableIRQ(SysTick_IRQn);
EMU_EnterEM2(true);
SysTick->CTRL = sysTickCtrl;
__set_PRIMASK(priMask);
uint32_t ticks = sysclock.next();
if (sysclock.isready() == false) {
sysclock.disable();
hal.setIdle();
sysclock.enable(ticks2millis(ticks));
sysclock.correct(ticks == 0 ? 0 : ticks -1);
} else {
hal.unsetIdle();
sysclock.enable();
}
}
};
#endif
class Activity : public Alarm {
volatile bool awake;
public:
Activity () : Alarm(0), awake(false) {
async(true);
#if defined ARDUINO_ARCH_STM32
LowPower.begin();
#endif
}
virtual ~Activity () {}
virtual void trigger (__attribute__((unused)) AlarmClock& clock) {
awake = false;
}
// do not sleep for time in ticks
void stayAwake (uint32_t time) {
uint32_t old = sysclock.get(*this);
if( old < time ) {
awake = true;
sysclock.cancel(*this);
tick = time;
sysclock.add(*this);
}
}
bool stayAwake () const {
return awake;
}
template <class Saver,class Hal>
void savePower (Hal& hal) {
if( awake == false ) {
#ifndef NDEBUG
Saver::waitSerial();
#endif
Saver::powerSave(hal);
}
else {
// ensure radio is up and running
hal.wakeup();
}
}
template <class Hal>
void sleepForever (Hal& hal) {
hal.setIdle();
while( true ) {
#if defined(ARDUINO_ARCH_AVR) && ! (defined(ARDUINO_AVR_ATmega32) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega128__))
#ifndef NDEBUG
Idle<>::waitSerial();
#endif
LowPower.powerDown(SLEEP_FOREVER,ADC_OFF,BOD_OFF);
#endif
#if defined ARDUINO_ARCH_STM32
#ifndef NDEBUG
Idle<>::waitSerial();
#endif
//DPRINTLN("shutdown");
LowPower.shutdown(0);
#endif
#if defined(ARDUINO_ARCH_EFM32)
while (1) {
EMU_EnterEM2(false);
}
#endif
}
}
};
template <class HalType>
class BurstDetector : public Alarm {
bool burst;
HalType& hal;
public:
BurstDetector (HalType& h) : Alarm(millis2ticks(250)), burst(false), hal(h) {}
virtual ~BurstDetector () {}
virtual void trigger (AlarmClock& clock) {
uint32_t next = millis2ticks(250);
if( hal.activity.stayAwake() == false ) {
bool detect = hal.radio.detectBurst();
if( detect == true ) {
if( burst == false ) {
burst = true;
next = millis2ticks(30);
// DPRINTLN("1");
}
else {
burst = false;
hal.activity.stayAwake(millis2ticks(500));
// DPRINTLN("2");
}
}
else {
burst = false;
}
}
set(next);
clock.add(*this);
}
void enable(AlarmClock& clock) {
clock.add(*this);
}
};
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_RP2040)
class Sleep {
public:
static uint32_t doSleep (uint32_t ticks) {
uint32_t sleeptime = ticks2millis(ticks);
#ifdef ARDUINO_ARCH_RP2040
sleep_ms(sleeptime); // https://raspberrypi.github.io/pico-sdk-doxygen/group__sleep.html
#else
esp_sleep_enable_timer_wakeup(sleeptime * 100000);
esp_light_sleep_start();
#endif
return ticks;
}
static void waitSerial() {Serial.flush();};
template <class Hal>
static void powerSave (Hal& hal) {
sysclock.disable();
uint32_t ticks = sysclock.next();
if( sysclock.isready() == false ) {
if( ticks == 0 || ticks > millis2ticks(15) ) {
hal.radio.setIdle();
uint32_t offset = doSleep(ticks);
sysclock.correct(offset);
sysclock.enable();
}
else{
sysclock.enable();
// Idle<ENABLETIMER2>::powerSave(hal);
powerSave(hal);
}
}
else {
sysclock.enable();
}
}
};
#endif
}
#endif