forked from n0m1/Sleep_n0m1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sleep_n0m1.cpp
243 lines (211 loc) · 6.65 KB
/
Sleep_n0m1.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
/************************************************************************************
*
* Name : Sleep_n0m1.cpp
* Author : Noah Shibley / NoMi Design
* Date : July 10th 2011
* Version : 0.1
* Notes : Some of this code comes from "Cloudy" on the arduino forum
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1292898715
*
* Copyright (c) 2012 NoMi Design (http://n0m1.com) All right reserved.
*
* This file is part of Triggertrap. See Triggertrap.com for more information.
*
* Triggertrap 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.
*
* Triggertrap 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 Triggertrap. If not, see <http://www.gnu.org/licenses/>.
*
***********************************************************************************/
#include "Sleep_n0m1.h"
Sleep* Sleep::pSleep = 0;
Sleep::Sleep()
{
pSleep = this; //the ptr points to this object
timeSleep = 0; // total time due to sleep
calibv = 1.0; // ratio of real clock with WDT clock
byte isrcalled = 0; // WDT vector flag
sleepCycleCount = 0;
sleepCycleInterval = 100;
}
/********************************************************************
*
* setSleepMode
*
********************************************************************/
void Sleep::setSleepMode(int mode)
{
sleepMode_ = mode;
}
/********************************************************************
*
* calibrateTime
*
********************************************************************/
void Sleep::calibrateTime(unsigned long sleepTime, boolean &abortCycle) {
// timer0 continues to run in idle sleep mode
set_sleep_mode(SLEEP_MODE_IDLE);
long tt1=millis();
sleepWDT(sleepTime,abortCycle);
long tt2=millis();
calibv = (float) sleepTime/(tt2-tt1);
//Serial.println(calibv);
}
/********************************************************************
*
* WDTMillis
*
********************************************************************/
unsigned long Sleep::WDTMillis() {
return millis()+timeSleep;
}
/********************************************************************
*
* sleepNow
*
********************************************************************/
void Sleep::sleepInterrupt(int interrupt,int mode) {
if(mode == FALLING || mode == LOW)
{
int pin = interrupt + 2; //will fail on the mega
pinMode (pin, INPUT);
digitalWrite (pin, HIGH);
}
set_sleep_mode(sleepMode_);
sleep_enable();
attachInterrupt(interrupt,sleepHandler,mode);
sei(); //make sure interrupts are on!
sleep_mode();
//----------------------------- ZZZZZZ sleeping here----------------------
sleep_disable(); //disable sleep, awake now
detachInterrupt(interrupt);
}
/********************************************************************
*
* sleepDelay
*
********************************************************************/
void Sleep::sleepDelay(unsigned long sleepTime){
boolean abortCycle = false;
sleepDelay(sleepTime,abortCycle);
}
/********************************************************************
*
* sleepDelay
*
********************************************************************/
void Sleep::sleepDelay(unsigned long sleepTime, boolean &abortCycle) {
ADCSRA &= ~(1<<ADEN); // adc off
// PRR = 0xEF; // modules off
++sleepCycleCount;
sleepCycleCount = sleepCycleCount % sleepCycleInterval; //recalibrate every interval cycles
if(sleepCycleCount == 1)
{
calibrateTime(sleepTime,abortCycle);
}
else
{
set_sleep_mode(sleepMode_);
int trem = sleepWDT(sleepTime*calibv,abortCycle);
timeSleep += (sleepTime-trem);
}
// PRR = 0x00; //modules on
ADCSRA |= (1<<ADEN); // adc on
}
/********************************************************************
*
* sleepWDT
*
********************************************************************/
int Sleep::sleepWDT(unsigned long remainTime, boolean &abortCycle) {
#if defined(WDP3)
byte WDTps = 9; // WDT Prescaler value, 9 = 8192ms
#else
byte WDTps = 7; // WDT Prescaler value, 7 = 2048ms
#endif
isrcalled = 0;
sleep_enable();
while(remainTime > 0) {
//work out next prescale unit to use
while ((0x10<<WDTps) > remainTime && WDTps > 0) {
WDTps--;
}
// send prescaler mask to WDT_On
WDT_On((WDTps & 0x08 ? (1<<WDP3) : 0x00) | (WDTps & 0x07));
isrcalled=0;
while (isrcalled==0 && abortCycle == false) {
#if defined(__AVR_ATmega328P__)
// turn bod off
MCUCR |= (1<<BODS) | (1<<BODSE);
MCUCR &= ~(1<<BODSE); // must be done right before sleep
#endif
sleep_cpu(); // sleep here
}
// calculate remaining time
remainTime -= (0x10<<WDTps);
if ((long) remainTime < 0 ) {remainTime = 0;} //check for unsigned underflow, by converting to signed
}
sleep_disable();
return remainTime;
}
/********************************************************************
*
* WDT_On
*
********************************************************************/
void Sleep::WDT_On(byte psMask)
{
// prepare timed sequence first
byte ps = (psMask | (1<<WDIE)) & ~(1<<WDE);
cli();
wdt_reset();
/* Clear WDRF in MCUSR */
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCSR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCSR = ps;
sei();
}
/********************************************************************
*
* WDT_Off
*
********************************************************************/
void Sleep::WDT_Off() {
cli();
wdt_reset();
/* Clear WDRF in MCUSR */
MCUSR &= ~(1<<WDRF);
/* Write logical one to WDCE and WDE */
/* Keep old prescaler setting to prevent unintentional time-out */
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* Turn off WDT */
WDTCSR = 0x00;
sei();
}
/********************************************************************
*
* sleepHandler ISR
*
********************************************************************/
void sleepHandler(void)
{
}
/********************************************************************
*
* WDT ISR
*
********************************************************************/
ISR(WDT_vect) {
Sleep::pSleep->WDT_Off();
Sleep::pSleep->isrcalled=1;
}