-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSleepyPi2.cpp
More file actions
556 lines (440 loc) · 13.8 KB
/
SleepyPi2.cpp
File metadata and controls
556 lines (440 loc) · 13.8 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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* SleepyPi2.cpp - library for Sleepy Pi Board
Copyright (c) Jon Watkins 2016
http://spellfoundry.com
This is a library of functions for use with the Sleepy Pi2 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)
- pcf8523 (https://github.com/SpellFoundry/pcf8523.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
V2_0 - 27 April 2016 - Initial Modification to run Sleepy Pi 2
*/
#include "SleepyPi2.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 A6 // I/P - A/I Supply monitoring pin
#define I_MONITOR_PIN A7 // I/P - A/I Current 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
+****************************************************/
void SleepyPiClass::enableWakeupAlarm(bool enable)
{
enableAlarm(enable);
return;
}
/* **************************************************+
--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;
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
SleepyPiClass::enablePiPower(false);
pi_running = false;
}
}
return false;
}
}
/* **************************************************+
--checkPiStatus (current monitor)
Monitor the current draw of the Rpi and deteremine
whether the Raspberry Pi is running or not.
The theshold_mA is the threshold below which the
Rpi is considered shutdown in the traditional sense.
For example a Rpi3 will settle on about 75mA after
a "sudo shutdown -h now" command. As a rule of thumb
anything nelow 90mA should be considered shutdown.
Note a Pi Zero or A+ can have "active" currents as
low as 100mA
Option to Force a shutdown i.e. remove power to
the Raspberry Pi if it is not runnng.
+****************************************************/
bool SleepyPiClass::checkPiStatus(long threshold_mA, bool forceShutdownIfNotRunning)
{
float pi_current = 0.0;
// Read the RPi Current draw
pi_current = SleepyPiClass::rpiCurrent();
if(pi_current >= (float)threshold_mA)
{
// RasPi is still running
pi_running = true;
return true;
}
else
{
// Pi not running - either booting or manually shutdown
if(forceShutdownIfNotRunning == true){
// so cut the power
SleepyPiClass::enablePiPower(false);
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(void)
{
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);
digitalWrite(CMD_PI_TO_SHDWN_PIN,LOW);
}
return;
}
/* **************************************************+
--piShutdown (current monitor)
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(long threshold_mA)
{
// int handShake;
bool pi_running;
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;
pi_running = SleepyPiClass::checkPiStatus(threshold_mA,false);
while((pi_running == true) && (testTime < kFAILSAFETIME_MS)){
pi_running = SleepyPiClass::checkPiStatus(threshold_mA,false);
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);
digitalWrite(CMD_PI_TO_SHDWN_PIN,LOW);
}
return;
}
/* **************************************************+
--rtcInit
Setup the PCF8523 RTC registers with some essential
settings for Sleepy Pi functioning.
+****************************************************/
bool SleepyPiClass::rtcInit(bool reset)
{
uint8_t tmp_reg;
begin();
// Check whether the RTC is detected - will read 0xFF if not
tmp_reg = rtcReadReg(PCF8523_CONTROL_3);
if(tmp_reg == 0xFF){
//Not detected
return false;
}
if(reset){
SleepyPiClass::rtcReset();
}
// ...Disable default 32 kHz O/P on Alarm
SleepyPiClass::rtcStop_32768_Clkout();
SleepyPiClass::setBatterySwitchover();
SleepyPiClass::clearRtcInterruptFlags();
SleepyPiClass::rtcCapSelect(eCAP_12_5pF);
return true;
}
/* **************************************************+
--rtcReset
Reset the PCF8523 RTC
+****************************************************/
void SleepyPiClass::rtcReset()
{
// Rest the RTC
reset();
}
/* **************************************************+
--rtcStop_32768_Clkout
Stop the default 32khz clock being output on the
/Alarm pin
+****************************************************/
void SleepyPiClass::rtcStop_32768_Clkout()
{
// Stop the default 32kHz output on the INT1 pin as we use this
// as an alarm
stop_32768_clkout();
}
/* **************************************************+
--rtcClearInterrupts
Clear any active interrupt flags
+****************************************************/
uint8_t SleepyPiClass::rtcClearInterrupts()
{
return clearRtcInterruptFlags();
}
/* **************************************************+
--rtcIsRunning
Find out if the RTC is currently running
or stopped
+****************************************************/
uint8_t SleepyPiClass::rtcIsRunning()
{
return isrunning();
}
/* **************************************************+
--supplyVoltage
Measure the Supply voltage of the External supply
to the Sleepy Pi and return it as a scaled voltage
in Volts.
Note: This is not calibrated and should only be used
as a rough guide.
+****************************************************/
float SleepyPiClass::supplyVoltage(void)
{
int reading;
float voltage;
// Read
reading = analogRead(V_SUPPLY_PIN);
// Convert
voltage = 3.22 * (float)reading; // Raw voltage reading
// 10-bit ADC resolution = 3.3 / 1024 = 3.22mV
return voltage / 52; // Scaled to Volts
}
/* **************************************************+
--rpiCurrent
Measure the Rpi current and return it as a
scaled current in mA.
Note: This is not calibrated and should only be used
as a rough guide.
+****************************************************/
float SleepyPiClass::rpiCurrent(void)
{
int reading;
float current;
// Read
reading = analogRead(I_MONITOR_PIN);
// remove lower bit noise
if(reading <= 3)
{
reading = 0;
}
// Convert
current = 3.22 * (float)reading; // Raw current reading
// 10-bit ADC resolution = 3.3 / 1024 = 3.22mV
return current; // in mA
}
SleepyPiClass SleepyPi;