forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlarmClock.h
409 lines (343 loc) · 10.3 KB
/
AlarmClock.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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef __ALARMCLOCK_H__
#define __ALARMCLOCK_H__
#include "Debug.h"
#include "Alarm.h"
#ifdef ARDUINO_ARCH_EFM32
#include "sl_sleeptimer.h"
#endif
#ifdef ARDUINO_ARCH_RP2040
#include "hardware/rtc.h"
#include "pico/stdlib.h"
#include "pico/util/datetime.h"
#endif
#ifdef ARDUINO_ARCH_EFM32
// minimum timeout for a timer
#define TIMER_MIN_TIMEOUT_MS 10
#endif
namespace as {
#ifndef TICKS_PER_SECOND
// default 100 ticks per second
#define TICKS_PER_SECOND 100UL
#endif
#define seconds2ticks(tm) ( (tm) * TICKS_PER_SECOND )
#define ticks2seconds(tm) ( (tm) / TICKS_PER_SECOND )
#define decis2ticks(tm) ( (tm) * TICKS_PER_SECOND / 10 )
#define ticks2decis(tm) ( (tm) * 10UL / TICKS_PER_SECOND )
#define centis2ticks(tm) ( (tm) * TICKS_PER_SECOND / 100 )
#define ticks2centis(tm) ( (tm) * 100UL / TICKS_PER_SECOND )
#define millis2ticks(tm) ( (tm) * TICKS_PER_SECOND / 1000 )
#define ticks2millis(tm) ( (tm) * 1000UL / TICKS_PER_SECOND )
class AlarmClock: protected Link {
Link ready;
public:
void cancel(Alarm& item);
AlarmClock& operator --();
bool isready () const {
return ready.select() != 0;
}
bool runready() {
bool worked = false;
while( runsingle()==true ) {
worked=true;
}
return worked;
}
bool runsingle() {
Alarm* a = (Alarm*) ready.unlink();
if (a != 0) {
a->active(false);
a->trigger(*this);
return true;
}
return false;
}
bool runwait () {
return runsingle();
}
void add(Alarm& item);
uint32_t get(const Alarm& item) const;
uint32_t next () const {
Alarm* n = (Alarm*)select();
return n != 0 ? n->tick : 0;
}
Alarm* first () const {
return (Alarm*)select();
}
// correct the alarms after sleep
void correct (uint32_t ticks) {
ticks--;
Alarm* n = first();
if( n != 0 ) {
uint32_t nextticks = n->tick-1;
n->tick -= nextticks < ticks ? nextticks : ticks;
}
--(*this);
}
};
#if defined ARDUINO_ARCH_EFM32
extern void callback(sl_sleeptimer_timer_handle_t *id , void *user);
extern void rtccallback(sl_sleeptimer_timer_handle_t *id , void *user);
extern uint32_t getTimeout();
#else
extern void callback(void);
extern void rtccallback(void);
#endif
class SysClock : public AlarmClock {
#ifdef ARDUINO_ARCH_ESP32
hw_timer_t * Timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
#endif
#if defined ARDUINO_ARCH_STM32
HardwareTimer* Timer = new HardwareTimer(TIMER_TONE);
#endif
#ifdef ARDUINO_ARCH_RP2040
private:
struct repeating_timer rp2040_timer;
static bool TimerHandler(__attribute__((unused)) struct repeating_timer *t) { callback(); return true; }
#endif
#ifdef ARDUINO_ARCH_EFM32
sl_sleeptimer_timer_handle_t id;
uint32_t timerTimeout;
#endif
public:
static SysClock& instance();
void init() {
#ifdef ARDUINO_ARCH_AVR
#define TIMER1_RESOLUTION 65536UL // Timer1 is 16 bit
// use Time1 on AVR
TCCR1B = _BV(WGM13); // set mode as phase and frequency correct pwm, stop the timer
TCCR1A = 0; // clear control register A
const unsigned long cycles = (F_CPU / 2000000) * (1000000 / TICKS_PER_SECOND);
unsigned short pwmPeriod;
unsigned char clockSelectBits;
if (cycles < TIMER1_RESOLUTION) {
clockSelectBits = _BV(CS10);
pwmPeriod = (unsigned short)cycles;
}
else if (cycles < TIMER1_RESOLUTION * 8) {
clockSelectBits = _BV(CS11);
pwmPeriod = cycles / 8;
}
else if (cycles < TIMER1_RESOLUTION * 64) {
clockSelectBits = _BV(CS11) | _BV(CS10);
pwmPeriod = cycles / 64;
}
else if (cycles < TIMER1_RESOLUTION * 256) {
clockSelectBits = _BV(CS12);
pwmPeriod = cycles / 256;
}
else if (cycles < TIMER1_RESOLUTION * 1024) {
clockSelectBits = _BV(CS12) | _BV(CS10);
pwmPeriod = cycles / 1024;
}
else {
clockSelectBits = _BV(CS12) | _BV(CS10);
pwmPeriod = TIMER1_RESOLUTION - 1;
}
TCNT1 = 0;
ICR1 = pwmPeriod;
TCCR1B = _BV(WGM13) | clockSelectBits;
#endif
#ifdef ARDUINO_ARCH_STM32F1
// Setup Timer2 on ARM
Timer2.setMode(TIMER_CH2,TIMER_OUTPUT_COMPARE);
Timer2.setPeriod(1000000 / TICKS_PER_SECOND); // in microseconds
Timer2.setCompare(TIMER_CH2, 1); // overflow might be small
#endif
#if defined ARDUINO_ARCH_STM32
Timer->setOverflow(1000000 / TICKS_PER_SECOND, MICROSEC_FORMAT);
Timer->attachInterrupt(callback);
#endif
#ifdef ARDUINO_ARCH_ESP32
// https://techtutorialsx.com/2017/10/07/esp32-arduino-timer-interrupts/
Timer = timerBegin(0, 80, true);
timerAlarmWrite(Timer, 1000000 / TICKS_PER_SECOND, true);
timerAttachInterrupt(Timer, &callback, true);
#endif
#ifdef ARDUINO_ARCH_EFM32
sl_sleeptimer_init();
timerTimeout = TIMER_MIN_TIMEOUT_MS;
#endif
enable();
}
void disable () {
#if defined ARDUINO_AVR_ATmega32 || defined(__AVR_ATmega128__)
TIMSK &= ~_BV(TOIE1);
#elif defined(ARDUINO_ARCH_AVR)
TIMSK1 &= ~_BV(TOIE1);
#elif defined(ARDUINO_ARCH_STM32F1)
Timer2.detachInterrupt(TIMER_CH2);
#elif defined ARDUINO_ARCH_STM32
Timer->pause();
#endif
#ifdef ARDUINO_ARCH_ESP32
if (Timer != NULL)
timerAlarmDisable(Timer);
#endif
#ifdef ARDUINO_ARCH_RP2040
cancel_repeating_timer(&rp2040_timer);
#endif
#ifdef ARDUINO_ARCH_EFM32
sl_sleeptimer_stop_timer(&id);
#endif
}
#ifdef ARDUINO_ARCH_EFM32
uint32_t getTimeout() {
return timerTimeout;
}
void enable(uint32_t ms) {
timerTimeout=ms < TIMER_MIN_TIMEOUT_MS ? TIMER_MIN_TIMEOUT_MS : ms;
//DPRINT("enable ");DDECLN(ms);
//_restart_ stops a running timer, if any exists
sl_sleeptimer_restart_timer_ms( &id, timerTimeout, callback , (void *) NULL ,0, 0 );
}
#endif
void enable () {
#if defined ARDUINO_AVR_ATmega32 || defined(__AVR_ATmega128__)
TIMSK |= _BV(TOIE1);
#elif defined(ARDUINO_ARCH_AVR)
TIMSK1 |= _BV(TOIE1);
#elif defined(ARDUINO_ARCH_STM32F1)
Timer2.attachInterrupt(TIMER_CH2,callback);
#elif defined ARDUINO_ARCH_STM32
Timer->resume();
#endif
#ifdef ARDUINO_ARCH_ESP32
if (Timer != NULL)
timerAlarmEnable(Timer);
#endif
#ifdef ARDUINO_ARCH_RP2040
if (rp2040_timer.alarm_id < 1) {
add_repeating_timer_us(10000UL, TimerHandler, NULL, &rp2040_timer);
}
#endif
#ifdef ARDUINO_ARCH_EFM32
enable(TIMER_MIN_TIMEOUT_MS);
#endif
}
#ifdef ARDUINO_ARCH_EFM32
sl_sleeptimer_timer_handle_t getTimerID() {
return id;
}
#endif
void add(Alarm& item) {
AlarmClock::add(item);
}
void add(Alarm& item,uint32_t millis) {
item.tick = (millis2ticks(millis));
add(item);
}
};
extern SysClock sysclock;
#ifndef NORTC
class RealTimeClock : public AlarmClock {
uint8_t ovrfl;
#if defined(ARDUINO_ARCH_STM32F1) && defined(_RTCLOCK_H_)
RTClock rt;
#endif
#ifdef ARDUINO_ARCH_EFM32
sl_sleeptimer_timer_handle_t id;
#endif
public:
RealTimeClock() : ovrfl(0) {}
static RealTimeClock& instance();
void init () {
#if defined ARDUINO_AVR_ATmega32
TIMSK &= ~(1<<TOIE2); //Disable timer2 interrupts
ASSR |= (1<<AS2); //Enable asynchronous mode
TCNT2 = 0; //set initial counter value
TCCR2 = (1<<CS22)|(1<<CS20); // mode normal & set prescaller 128
while (ASSR & (1<<TCN2UB)); //wait for registers update
TIFR |= (1<<TOV2); //clear interrupt flags
TIMSK |= (1<<TOIE2); //enable TOV2 interrupt
#elif defined(__AVR_ATmega128__)
TIMSK &= ~(1<<TOIE2); //Disable timer2 interrupts
ASSR |= (1<<AS0); //Enable asynchronous mode
TCNT2 = 0; //set initial counter value
TCCR2 = (1<<CS22)|(1<<CS20); // mode normal & set prescaller 128
while (ASSR & (1<<TCN0UB)); //wait for registers update
TIFR |= (1<<TOV2); //clear interrupt flags
TIMSK |= (1<<TOIE2); //enable TOV2 interrupt
#elif defined(ARDUINO_ARCH_AVR)
TIMSK2 = 0; //Disable timer2 interrupts
ASSR = (1<<AS2); //Enable asynchronous mode
TCNT2 = 0; //set initial counter value
TCCR2A = 0; // mode normal
TCCR2B = (1<<CS22)|(1<<CS20); //set prescaller 128
while (ASSR & ((1<<TCN2UB)|(1<<TCR2BUB))); //wait for registers update
TIFR2 = (1<<TOV2); //clear interrupt flags
TIMSK2 = (1<<TOIE2); //enable TOV2 interrupt
#elif defined(ARDUINO_ARCH_STM32F1) && defined(_RTCLOCK_H_)
rt = RTClock(RTCSEL_LSE);
rt.attachSecondsInterrupt(rtccallback);
#elif defined(ARDUINO_ARCH_RP2040)
datetime_t alarmT;
rtc_init();
rtc_get_datetime(&alarmT);
alarmT.min = alarmT.hour = alarmT.day = alarmT.dotw = alarmT.month = alarmT.year = -1;
alarmT.sec = 1;
rtc_set_alarm(&alarmT, rtccallback); // https://raspberrypi.github.io/pico-sdk-doxygen/group__hardware__rtc.html
#elif defined(ARDUINO_ARCH_EFM32)
sl_sleeptimer_init();
sl_sleeptimer_restart_timer_ms( &id, 1000, callback , (void *) NULL ,0, 0 );
#else
#warning "RTC not supported"
#endif
}
// return millis done of the current second
uint32_t getCurrentMillis () {
#ifdef ARDUINO_ARCH_AVR
return (TCNT2 * 1000) / 255;
#else
return 0; // not supported ???
#endif
}
uint32_t getCounter (bool resetovrflow) {
if( resetovrflow == true ) {
ovrfl = 0;
}
#ifdef ARDUINO_ARCH_AVR
return (256 * ovrfl) + TCNT2;
#elif defined(ARDUINO_ARCH_STM32F1) && defined(_RTCLOCK_H_)
return rtc_get_count();
#else
return 0;
#endif
}
void overflow () {
ovrfl++;
}
void debug () {
if( select() != 0 ) {
DDEC((uint16_t)((Alarm*)select())->tick);
DPRINT(F(" "));
}
}
void add(Alarm& item) {
AlarmClock::add(item);
}
void add(Alarm& item,uint32_t millis) {
millis += getCurrentMillis();
item.tick = (millis / 1000); // seconds to wait
add(item);
}
void add(RTCAlarm& item,uint32_t millis) {
// correct the time with the millis of the current second
millis += getCurrentMillis();
item.tick = (millis / 1000); // seconds to wait
item.millis = millis % 1000; // millis to wait
add(item);
}
};
// backward compatibility
#if not defined(ARDUINO_ARCH_STM32) && not defined(ARDUINO_ARCH_EFM32)
typedef RealTimeClock RTC;
#endif
extern RealTimeClock rtc;
#endif
}
#endif