forked from brabl2/narcoleptic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNarcoleptic.cpp
330 lines (302 loc) · 7.17 KB
/
Narcoleptic.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
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
/**
* Narcoleptic - A sleep library for Arduino
* Copyright (C) 2010 Peter Knight (Cathedrow)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/common.h>
//#include <util/delay.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
#include "Narcoleptic.h"
#if NARCOLEPTIC_CALIBRATION_ENABLE
uint32_t watchdogTime_us = 16000;
#endif
uint32_t millisCounter = 0;
SIGNAL(WDT_vect) {
wdt_disable();
wdt_reset();
#ifdef WDTCSR
WDTCSR &= ~_BV(WDIE);
#else
WDTCR &= ~_BV(WDIE);
#endif
}
void NarcolepticClass::sleep(uint8_t wdt_period,uint8_t sleep_mode) {
#ifdef BODSE
// Turn off BOD in sleep (picopower devices only)
MCUCR |= _BV(BODSE);
MCUCR |= _BV(BODS);
#endif
MCUSR = 0;
#ifdef WDTCSR
WDTCSR &= ~_BV(WDE);
WDTCSR = _BV(WDIF) | _BV(WDIE) | _BV(WDCE);
#else
WDTCR &= ~_BV(WDE);
WDTCR = _BV(WDIF) | _BV(WDIE) | _BV(WDCE);
#endif
wdt_enable(wdt_period);
wdt_reset();
#ifdef WDTCSR
WDTCSR |= _BV(WDIE);
#else
WDTCR |= _BV(WDIE);
#endif
set_sleep_mode(sleep_mode);
// Disable all interrupts
uint8_t SREGcopy = SREG; cli();
#ifdef EECR
uint8_t EECRcopy = EECR; EECR &= ~_BV(EERIE);
#endif
#ifdef EIMSK
uint8_t EIMSKcopy = EIMSK; EIMSK = 0;
#endif
#ifdef PCMSK0
uint8_t PCMSK0copy = PCMSK0; PCMSK0 = 0;
#endif
#ifdef PCMSK1
uint8_t PCMSK1copy = PCMSK1; PCMSK1 = 0;
#endif
#ifdef PCMSK2
uint8_t PCMSK2copy = PCMSK2; PCMSK2 = 0;
#endif
#ifdef TIMSK0
uint8_t TIMSK0copy = TIMSK0; TIMSK0 = 0;
#endif
#ifdef TIMSK1
uint8_t TIMSK1copy = TIMSK1; TIMSK1 = 0;
#endif
#ifdef TIMSK2
uint8_t TIMSK2copy = TIMSK2; TIMSK2 = 0;
#endif
#ifdef SPCR
uint8_t SPCRcopy = SPCR; SPCR &= ~_BV(SPIE);
#endif
#ifdef UCSR0B
uint8_t UCSR0Bcopy = UCSR0B; UCSR0B &= ~(_BV(RXCIE0) | _BV(TXCIE0) | _BV(UDRIE0));
#endif
#ifdef TWCR
uint8_t TWCRcopy = TWCR; TWCR &= ~_BV(TWIE);
#endif
#ifdef ACSR
uint8_t ACSRcopy = ACSR; ACSR &= ~_BV(ACIE);
#endif
#ifdef ADCSRA
uint8_t ADCSRAcopy = ADCSRA; ADCSRA &= ~_BV(ADIE);
#endif
#if defined(SPMCSR) && defined(SPMIE)
uint8_t SPMCSRcopy = SPMCSR; SPMCSR &= ~_BV(SPMIE);
#endif
sei();
sleep_mode(); // here the device is actually put to sleep!!
wdt_disable(); // first thing after waking from sleep: disable watchdog...
// Reenable all interrupts
#if defined(SPMCSR) && defined(SPMIE)
SPMCSR = SPMCSRcopy;
#endif
#ifdef ADCSRA
ADCSRA = ADCSRAcopy;
#endif
#ifdef ACSR
ACSR = ACSRcopy;
#endif
#ifdef TWCR
TWCR = TWCRcopy;
#endif
#ifdef UCSR0B
UCSR0B = UCSR0Bcopy;
#endif
#ifdef SPCR
SPCR = SPCRcopy;
#endif
#ifdef TIMSK2
TIMSK2 = TIMSK2copy;
#endif
#ifdef TIMSK1
TIMSK1 = TIMSK1copy;
#endif
#ifdef TIMSK0
TIMSK0 = TIMSK0copy;
#endif
#ifdef PCMSK2
PCMSK2 = PCMSK2copy;
#endif
#ifdef PCMSK1
PCMSK1 = PCMSK1copy;
#endif
#ifdef PCMSK0
PCMSK0 = PCMSK0copy;
#endif
#ifdef EIMSK
EIMSK = EIMSKcopy;
#endif
#ifdef EECR
EECR = EECRcopy;
#endif
SREG = SREGcopy;
#ifdef WDTCSR
WDTCSR &= ~_BV(WDIE);
#else
WDTCR &= ~_BV(WDIE);
#endif
}
void NarcolepticClass::delay(uint32_t milliseconds) {
millisCounter += milliseconds;
#if NARCOLEPTIC_CALIBRATION_ENABLE
uint32_t microseconds = milliseconds * 1000L;
calibrate();
if (microseconds > watchdogTime_us) {
microseconds -= watchdogTime_us;
uint32_t sleep_periods = microseconds / watchdogTime_us;
#else
uint32_t sleep_periods = milliseconds / 16;
#endif
while (sleep_periods >= 512) {
sleep(WDTO_8S,SLEEP_MODE_PWR_DOWN);
sleep_periods -= 512;
}
if (sleep_periods & 256) sleep(WDTO_4S,SLEEP_MODE_PWR_DOWN);
if (sleep_periods & 128) sleep(WDTO_2S,SLEEP_MODE_PWR_DOWN);
if (sleep_periods & 64) sleep(WDTO_1S,SLEEP_MODE_PWR_DOWN);
if (sleep_periods & 32) sleep(WDTO_500MS,SLEEP_MODE_PWR_DOWN);
if (sleep_periods & 16) sleep(WDTO_250MS,SLEEP_MODE_PWR_DOWN);
if (sleep_periods & 8) sleep(WDTO_120MS,SLEEP_MODE_PWR_DOWN);
if (sleep_periods & 4) sleep(WDTO_60MS,SLEEP_MODE_PWR_DOWN);
if (sleep_periods & 2) sleep(WDTO_30MS,SLEEP_MODE_PWR_DOWN);
if (sleep_periods & 1) sleep(WDTO_15MS,SLEEP_MODE_PWR_DOWN);
#if NARCOLEPTIC_CALIBRATION_ENABLE
}
#endif
}
#if NARCOLEPTIC_CALIBRATION_ENABLE
void NarcolepticClass::calibrate() {
// Calibration needs Timer 1. Ensure it is powered up.
uint8_t PRRcopy = PRR;
PRR &= ~_BV(PRTIM1);
uint8_t TCCR1Bcopy = TCCR1B;
TCCR1B &= ~(_BV(CS12) | _BV(CS11) | _BV(CS10)); // Stop clock immediately
// Capture Timer 1 state
uint8_t TCCR1Acopy = TCCR1A;
uint16_t TCNT1copy = TCNT1;
uint16_t OCR1Acopy = OCR1A;
uint16_t OCR1Bcopy = OCR1B;
uint16_t ICR1copy = ICR1;
uint8_t TIMSK1copy = TIMSK1;
uint8_t TIFR1copy = TIFR1;
// Configure as simple count-up timer
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
TIMSK1 = 0;
TIFR1 = 0;
// Set clock to /64 (16ms should take approx. 4000 cycles at 16MHz clock)
TCCR1B = _BV(CS11) | _BV(CS10);
sleep(WDTO_15MS,SLEEP_MODE_IDLE);
uint16_t watchdogDuration = TCNT1;
TCCR1B = 0; // Stop clock immediately
// Restore Timer 1
TIFR1 = TIFR1copy;
TIMSK1 = TIMSK1copy;
ICR1 = ICR1copy;
OCR1B = OCR1Bcopy;
OCR1A = OCR1Acopy;
TCNT1 = TCNT1copy;
TCCR1A = TCCR1Acopy;
TCCR1B = TCCR1Bcopy;
// Restore power reduction state
PRR = PRRcopy;
watchdogTime_us = watchdogDuration * (64 * 1000000 / F_CPU); // should be approx. 16000
}
#endif
uint32_t NarcolepticClass::millis() {
return millisCounter;
}
void NarcolepticClass::disableWire() {
#ifdef PRTWI
PRR |= _BV(PRTWI);
#endif
#ifdef PRUSI
PRR |= _BV(PRUSI);
#endif
}
void NarcolepticClass::disableTimer2() {
#ifdef PRTIM2
PRR |= _BV(PRTIM2);
#endif
}
void NarcolepticClass::disableTimer1() {
PRR |= _BV(PRTIM1);
}
void NarcolepticClass::disableMillis() {
PRR |= _BV(PRTIM0);
}
void NarcolepticClass::disableSerial() {
#ifdef PRUSART0
PRR |= _BV(PRUSART0);
#endif
#ifdef PRUSART
PRR |= _BV(PRUSART);
#endif
}
void NarcolepticClass::disableADC() {
#ifdef PRADC
PRR |= _BV(PRADC);
#endif
}
void NarcolepticClass::disableSPI() {
#ifdef PRSPI
PRR |= _BV(PRSPI);
#endif
}
void NarcolepticClass::enableWire() {
#ifdef PRTWI
PRR &= ~_BV(PRTWI);
#endif
#ifdef PRUSI
PRR &= ~_BV(PRUSI);
#endif
}
void NarcolepticClass::enableTimer2() {
#ifdef PRTIM2
PRR &= ~_BV(PRTIM2);
#endif
}
void NarcolepticClass::enableTimer1() {
PRR &= ~_BV(PRTIM1);
}
void NarcolepticClass::enableMillis() {
PRR &= ~_BV(PRTIM0);
}
void NarcolepticClass::enableSerial() {
#ifdef PRUSART0
PRR &= ~_BV(PRUSART0);
#endif
#ifdef PRUSART
PRR &= ~_BV(PRUSART);
#endif
}
void NarcolepticClass::enableADC() {
#ifdef PRADC
PRR &= ~_BV(PRADC);
#endif
}
void NarcolepticClass::enableSPI() {
#ifdef PRSPI
PRR &= ~_BV(PRSPI);
#endif
}
NarcolepticClass Narcoleptic;