-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
BoxPower.cpp
96 lines (84 loc) · 2.79 KB
/
BoxPower.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
#include "BoxPower.h"
#include "Hackiebox.h"
#include <driverlib/prcm.h>
void BoxPower::initPins() {
pinMode(58, OUTPUT); //SD Power pin
pinMode(61, OUTPUT); //Audio, Accelerometer, RFID, LED Blue / Red?
}
void BoxPower::begin() {
_sleepMinutes = Config.get()->battery.sleepMinutes;
_lastFeed = millis();
setInterval(5000);
Log.info("Init BoxPower, sleepMinutes=%i", _sleepMinutes);
}
void BoxPower::loop() {
uint32_t millis_tmp = millis();
uint32_t deltaMinutes = (millis_tmp - _lastFeed) / (1000 * 60);
if (_sleepMinutes > 0 && deltaMinutes >= _sleepMinutes) {
Log.verbose("millis_tmp=%l, _lastFeed=%l, deltaMinutes=%l", millis_tmp, _lastFeed, deltaMinutes);
Events.handlePowerEvent(PowerEvent::BOX_IDLE);
}
}
void BoxPower::feedSleepTimer() {
feedSleepTimerSilent();
Log.verbose("Sleep timer rst, _lastFeed=%l, Mem: free(str/ptr/cny/end) Stack: %ib(%X/%X/%X/%X), Heap: %ib(%X/%X/%X/%X)",
_lastFeed,
freeStackMemory(), (uint32_t)stackStart()-0x20004000, (uint32_t)stackPointer()-0x20004000, (uint32_t)getFirstStackCanary()-0x20004000, (uint32_t)stackEnd()-0x20004000,
freeHeapMemory(), (uint32_t)heapStart()-0x20004000, (uint32_t)heapPointer()-0x20004000, (uint32_t)getFirstHeapCanary()-0x20004000, (uint32_t)heapEnd()-0x20004000
);
uint32_t stackCanaries = countStackCanaries();
uint32_t heapCanaries = countHeapCanaries();
if (stackCanaries < 10 || heapCanaries < 10) {
Log.error("!!! Canaries low !!! Stack=%l, Heap=%l", stackCanaries, heapCanaries);
}
}
void BoxPower::feedSleepTimerSilent() {
_lastFeed = millis();
Box.watchdog_feed();
}
void BoxPower::_preparePowerDown() {
Log.info("Prepare power down...");
//TODO
//smartconfig down
Box.logStreamMulti.flush();
Box.boxPlayer.stop();
setSdPower(false);
setOtherPower(false);
Box.boxLEDs.setAllBool(false);
Serial.end();
Box.watchdog_stop();
}
void BoxPower::reset() {
Events.handlePowerEvent(PowerEvent::PRE_RESET);
_preparePowerDown();
PRCMMCUReset(true);
}
void BoxPower::hibernate() {
Events.handlePowerEvent(PowerEvent::PRE_HIBERNATE);
_preparePowerDown();
//enable ear wakeup interrupt
PRCMHibernateWakeupSourceEnable(PRCM_HIB_GPIO2 | PRCM_HIB_GPIO4);
//TODO
//Utils_SpiFlashDeepPowerDown();
PRCMHibernateEnter();
}
void BoxPower::setSdPower(bool power) {
digitalWrite(58, !power);
}
void BoxPower::setOtherPower(bool power) {
digitalWrite(61, power);
if (power) {
//RESET Chips
pinMode(62, OUTPUT);
digitalWrite(62, LOW);
Box.delayTask(1);
digitalWrite(62, HIGH);
Box.delayTask(10);
}
}
bool BoxPower::getSdPower() {
return !digitalRead(58);
}
bool BoxPower::getOtherPower() {
return digitalRead(61);
}