-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSleepyPi.cpp
More file actions
331 lines (269 loc) · 8.96 KB
/
SleepyPi.cpp
File metadata and controls
331 lines (269 loc) · 8.96 KB
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
/*
* SleepyPi.cpp - library for Sleepy Pi Board
Copyright (c) Jon Watkins 2013
http://spellfoundry.com
This is a library of functions ofr use with the Sleepy Pi Power
Management board for Raspberry Pi.
NOTE
=======
This library is dependent on the following libraries which must also
be present in the libraries directory:
- lowpower (http://www.rocketscream.com/blog/2011/07/04/lightweight-low-power-arduino-library/)
(https://github.com/rocketscream/Low-Power)
- ds1374RTC (https://github.com/SpellFoundry/DS1374RTC.git)
License
=======
The library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Releases
========
V1_0 - 23 Aug 2013 - Initial release
*/
#include "SleepyPi.h"
#define ENABLE_PI_PWR_PIN 16 // PC2 - O/P take high to enable the RaspPi - Active High
#define ENABLE_EXT_PWR_PIN 4 // PD4 - O/P take high to enable the External Supplies
#define CMD_PI_TO_SHDWN_PIN 17 // PC3 - 0/P Handshake to request the Pi to shutdown - Active high
#define PI_IS_RUNNING 7 // PD7 - I/P Handshake to show that the Pi is running - Active High
#define V_SUPPLY_PIN 20 // I/P - A/I Supply monitoring pin
#define POWER_BUTTON_PIN 3 // PD3 - I/P User Power-on Button (INT1) - Active Low
#define ALARM_PIN 2 // PD2 - I/P Pin that pulses when the alarm has expired (INT0) - Active Low
// Constructors
SleepyPiClass::SleepyPiClass()
{
RTCConfig_t rtc_config;
// Reset flags
simulationMode = false;
simPiOn = false;
pi_running = false;
power_on = false;
ext_power_on = false;
// **** Configure Power supplies ***
// ...Configure Pi Power
pinMode(ENABLE_PI_PWR_PIN, OUTPUT);
SleepyPiClass::enablePiPower(false); // ***** RasPi is Off at Startup ***** //
// ...Configure Ext Expansion Power
pinMode(ENABLE_EXT_PWR_PIN, OUTPUT);
SleepyPiClass::enableExtPower(false); // ***** Expansion Power is Off at Startup ***** //
// Change if RasPi is to be powered on at boot
// **** Configure I/O *****
// ...Configure Pi Shutdown handshake to PI
pinMode(CMD_PI_TO_SHDWN_PIN, OUTPUT);
digitalWrite(CMD_PI_TO_SHDWN_PIN,LOW); // Don't command to shutdown
// ...Configure Pi Shutdown handshake from PI - goes high when Pi is running
pinMode(PI_IS_RUNNING, INPUT);
// ...Initialize the button pin as a input: also can be used as an interrupt INT1
pinMode(POWER_BUTTON_PIN, INPUT);
// ...Initialize the alarm as a input: also can be used as an interrupt INT0
pinMode(ALARM_PIN, INPUT);
#ifdef DEBUG_MESSAGES
int handShake;
// initialize serial communication:
Serial.begin(9600);
// Display Status
//read the state of the Pi
handShake = digitalRead(PI_IS_RUNNING);
if(handShake > 0){
Serial.println("Handshake I/P high");
}
else {
Serial.println("Handshake I/P low");
}
Serial.println("Pi Shutdown O/P low");
Serial.println("Pi Power O/P low");
#endif
}
// PUBLIC FUNCTIONS
/* **************************************************+
--enableExtPower
Switch on / off the power to the external
expansion pins.
+****************************************************/
void SleepyPiClass::enableExtPower(bool enable)
{
if(simulationMode == true){
if(enable == true){
// Turn on the External Expansion power
// digitalWrite(ENABLE_EXT_PWR_PIN,HIGH);
ext_power_on = true;
}
else {
// Turn off the External Expansion power
// digitalWrite(ENABLE_EXT_PWR_PIN,LOW);
ext_power_on = false;
}
}
else {
if(enable == true){
// Turn on the External Expansion power
digitalWrite(ENABLE_EXT_PWR_PIN,HIGH);
ext_power_on = true;
}
else {
// Turn off the External Expansion power
digitalWrite(ENABLE_EXT_PWR_PIN,LOW);
ext_power_on = false;
}
}
return;
}
/* **************************************************+
--enablePiPower
Switch on / off the power to the Raspberry Pi.
+****************************************************/
void SleepyPiClass::enablePiPower(bool enable)
{
if(simulationMode == true){
if(enable == true){
// Turn on the Pi
// digitalWrite(ENABLE_PI_PWR_PIN,HIGH);
power_on = true;
}
else {
// Turn off the Pi
// digitalWrite(ENABLE_PI_PWR_PIN,LOW);
power_on = false;
}
}
else {
if(enable == true){
// Turn on the Pi
digitalWrite(ENABLE_PI_PWR_PIN,HIGH);
power_on = true;
}
else {
// Turn off the Pi
digitalWrite(ENABLE_PI_PWR_PIN,LOW);
power_on = false;
}
}
return;
}
/* **************************************************+
--enableWakeupAlarm
Setup and enable the RTC Alarm. This will output
a pulse on the /INT pin of the RTC which is recieved
as a low going pulse on the INT0 pin of the ATMEGA
+****************************************************/
bool SleepyPiClass::enableWakeupAlarm(void)
{
RTCConfig_t rtc_config;
rtc_config.disableOsc = false; // Enable / Disable the RTC OScillator
rtc_config.enableCTR = true; // Enable / Disable the Watchdog or Alarm Counters
rtc_config.CTRType = eALARM; // Select either the Watchdog or the Alarm as the Counter
rtc_config.enableSQW = false; // Enable / Disable the square wave output
rtc_config.WDToutput = eINT_PIN; // Select where the reset pulse goes when the WDT expires
rtc_config.SQWRate = e1Hz; // Selects the frequency of the square wave output
rtc_config.enableAlarmInt = true; // Enable / Disable the Alarm for setting the INT pin when it expires
return DS1374RTC::setConfig(rtc_config);
// TODO do we need to attach an interrupt here?
}
/* **************************************************+
--StartPiShutdown
Set the handshake lines so that the Raspberry Pi
is given notice to begin shutting down.
+****************************************************/
void SleepyPiClass::StartPiShutdown(void)
{
if(simulationMode == true){
// Serial.println("StartPiShutdown()");
power_on = false;
ext_power_on = false;
}
else {
// Command the Sleepy Pi to shutdown
digitalWrite(CMD_PI_TO_SHDWN_PIN,HIGH);
}
return;
}
/* **************************************************+
--checkPiStatus
Monitor the handshake lines and determine whether
the Raspberry Pi is running or not.
Option to Force a shutdown i.e. remove power to
the Raspberry Pi if it is not handshaking.
+****************************************************/
bool SleepyPiClass::checkPiStatus(bool forceShutdownIfNotRunning)
{
int handShake;
if(simulationMode == true){
handShake = power_on;
pi_running = true;
}
else {
handShake = digitalRead(PI_IS_RUNNING);
}
if(handShake > 0) {
// RasPi is still running
pi_running = true;
delay(50);
return true;
}
else{
// Pi not handshaking - either booting or manually shutdown
if(forceShutdownIfNotRunning == true){
// Pi not running - has it been on?
if(pi_running == true){
// Pi has been running and now isn't
// so cut the power
delay(2000);
SleepyPiClass::enablePiPower(false);
// Remove any Commands for the Sleepy Pi to shutdown
digitalWrite(CMD_PI_TO_SHDWN_PIN,LOW);
pi_running = false;
}
}
return false;
}
}
/* **************************************************+
--piShutdown
Set the Handshake lines to command a shutdown of
the Raspberry Pi. Then wait for the Raspberry Pi
to shutdown by monitoring the handshake line that
indicates that the Raspberry Pi is running. Once
the RPi has shutdown remove the power after a
small guard interval.
Option to Force a shutdown i.e. remove power to
the Raspberry Pi if it is not handshaking.
+****************************************************/
void SleepyPiClass::piShutdown(bool forceShutdown)
{
int handShake;
unsigned long timeStart, timeNow, testTime;
// Command the Sleepy Pi to shutdown
if(simulationMode == true){
// Serial.println("piShutdown()");
// Switch off the Pi
delay(5000);
SleepyPiClass::enablePiPower(false);
}
else {
digitalWrite(CMD_PI_TO_SHDWN_PIN,HIGH);
// Wait for the Pi to shutdown
timeStart = millis();
testTime = 0;
handShake = digitalRead(PI_IS_RUNNING);
while((handShake > 0) && (testTime < kFAILSAFETIME_MS)){
handShake = digitalRead(PI_IS_RUNNING);
delay(50);
timeNow = millis();
testTime = timeNow - timeStart;
}
// Switch off the Pi
delay(5000); // delay to make sure the Pi has finished shutting down
SleepyPiClass::enablePiPower(false);
// Remove Command for the Sleepy Pi to shutdown
digitalWrite(CMD_PI_TO_SHDWN_PIN,LOW);
}
return;
}
SleepyPiClass SleepyPi;