-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Hackiebox.cpp
executable file
·253 lines (216 loc) · 7.73 KB
/
Hackiebox.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
#include "Hackiebox.h"
#include "crashCallback.h"
BoxConfig Config;
BoxEvents Events;
Hackiebox Box;
void _SlDrvHandleGeneralEvents(SlDeviceEvent_t *pSlDeviceEvent) {
Log.error("Received _SlDrvHandleGeneralEvents Event=%i", pSlDeviceEvent->Event);
}
int Report(const char *format, ...) {
//Workaround for defined Report in the WiFi/utility/
va_list args;
va_start(args, format);
Log.printFormat(format, args);
return 0;
}
void crash(crashSource source, uint32_t* sp) {
//Box.logStreamSse.setSsePaused(true);
Log.info("crashSource=%i, sp=%X, sp=%X", source, sp, (uint32_t)sp-0x20004000);
if (source == CRASH_TEST)
return;
Box.boxPower.feedSleepTimer();
FileFs _file;
bool _isOpen;
Log.info("Dumping SRAM 0x20004000-0x20040000 to /revvox/memdump...");
_isOpen = _file.open("/revvox/memdump", FA_CREATE_ALWAYS | FA_WRITE);
if (_isOpen) {
_file.write((void *)0x20004000, 0x3C000);
_file.close();
}
Log.info("...done");
Log.info("Dumping REGISTERS 0xE000E000-0xE000F000 to /revvox/regdump...");
_isOpen = _file.open("/revvox/regdump", FA_CREATE_ALWAYS | FA_WRITE);
if (_isOpen) {
_file.write((void *)0xE000E000, 0x1000);
_file.close();
}
Log.info("...done");
Box.logStreamMulti.flush(); //Write all buffered logs to SD/SSE
__asm__ volatile("bkpt");
Box.boxPower.hibernate();
}
void Hackiebox::setup() {
if (!watchdog_start(10)) {
watchdog_stop();
//reset box?!
}
uint32_t stackCanaries = countStackCanaries();
uint32_t heapCanaries = countHeapCanaries();
setCanaries();
inDelayTask = true;
logStreamMulti.setSlot(&logStreamSd, 0);
logStreamMulti.setSlot(&logStreamSse, 1);
Log.init(LOG_LEVEL_VERBOSE, 921600, &logStreamMulti);
Log.info("Booting Hackiebox...");
Box.boxPower.feedSleepTimer();
Log.info(" -sizes: stack=%X, heap=%X", stackStart()-stackEnd(), heapEnd()-heapStart());
Log.info(" -prev. canaries: stack=%ix, heap=%ix", stackCanaries, heapCanaries);
register_crash_callback(crash);
crashed(CRASH_TEST, 0);
Wire.begin();
boxPower.initPins();
boxPower.setSdPower(true);
boxPower.setOtherPower(true);
boxSD.begin();
Config.begin(); //SD Card needed!
ConfigStruct* config = Config.get();
watchdog_start(config->misc.watchdogSeconds);
boxPower.begin();
boxI2C.begin();
boxLEDs.begin(config->misc.swd);
boxLEDs.setAll(BoxLEDs::CRGB::White);
boxBattery.begin();
boxLEDs.setAll(BoxLEDs::CRGB::Orange);
boxEars.begin();
boxLEDs.setAll(BoxLEDs::CRGB::Yellow);
boxAccel.begin();
boxLEDs.setAll(BoxLEDs::CRGB::Pink);
boxRFID.begin();
boxLEDs.setAll(BoxLEDs::CRGB::Teal);
//boxDAC.begin();
boxLEDs.setAll(BoxLEDs::CRGB::Fuchsia);
boxCLI.begin();
Box.boxPower.feedSleepTimerSilent();
boxWiFi = WrapperWiFi(config->wifi.ssid, config->wifi.password);
boxWiFi.begin();
Box.boxPower.feedSleepTimerSilent();
webServer = WrapperWebServer();
webServer.begin();
boxPlayer = BoxPlayer();
boxPlayer.begin();
boxAccel.setName("Accelerometer");
boxBattery.setName("Battery");
boxBattery.batteryTestThread.setName("Battery.Test");
boxCLI.setName("CLI");
boxDAC.setName("DAC");
boxRFID.setName("RFID");
boxEars.setName("Ears");
boxLEDs.setName("LEDs");
boxPower.setName("Power");
boxWiFi.setName("WiFi");
webServer.setName("Webserver");
boxDAC.priority = 0;
boxRFID.priority = 1;
boxAccel.priority = 2;
boxLEDs.priority = 3;
boxEars.priority = 4;
webServer.priority = 5;
boxPower.priority = 10;
boxWiFi.priority = 50;
boxBattery.priority = 100;
boxBattery.batteryTestThread.priority = 100;
boxCLI.priority = 100;
threadController = ThreadController();
threadController.add(&boxAccel);
threadController.add(&boxBattery);
threadController.add(&boxBattery.batteryTestThread);
threadController.add(&boxCLI);
threadController.add(&boxDAC);
threadController.add(&boxEars);
threadController.add(&boxRFID);
threadController.add(&boxLEDs);
threadController.add(&boxPower);
threadController.add(&boxWiFi);
threadController.add(&webServer);
threadController.sortThreads();
Log.info("Config: %s", Config.getAsJson().c_str());
boxAccel.onRun(ThreadCallbackHandler([&]() { boxAccel.loop(); }));
boxBattery.onRun(ThreadCallbackHandler([&]() { boxBattery.loop(); }));
boxBattery.batteryTestThread.onRun(ThreadCallbackHandler([&]() { boxBattery.doBatteryTestStep(); }));
boxCLI.onRun(ThreadCallbackHandler([&]() { boxCLI.loop(); }));
boxDAC.onRun(ThreadCallbackHandler([&]() { boxDAC.loop(); }));
boxEars.onRun(ThreadCallbackHandler([&]() { boxEars.loop(); }));
boxRFID.onRun(ThreadCallbackHandler([&]() { boxRFID.loop(); }));
boxLEDs.onRun(ThreadCallbackHandler([&]() { boxLEDs.loop(); }));
boxPower.onRun(ThreadCallbackHandler([&]() { boxPower.loop(); }));
boxWiFi.onRun(ThreadCallbackHandler([&]() { boxWiFi.loop(); }));
webServer.onRun(ThreadCallbackHandler([&]() { webServer.loop(); }));
//logStreamSse.setSsePaused(false);
boxLEDs.defaultIdleAnimation();
Log.info("Hackiebox started!");
Box.boxPower.feedSleepTimer();
inDelayTask = false;
boxDAC.begin();
//Workaround, as something seems to interfere / remove the irq.
//But box now crashes!
boxDAC.i2sStartMicros = micros();
}
void Hackiebox::delayTask(uint16_t millis) {
if (!inDelayTask) {
inDelayTask = true;
BoxTimer timer;
timer.setTimer(millis);
//Work start
while (timer.isRunning()) {
delayTaskWork(timer.getTimeTillEnd());
timer.tick();
}
//Work end
inDelayTask = false;
} else {
delay(millis);
}
}
void Hackiebox::delayTaskWork(uint16_t millis) {
//delay(millis);
boxDAC.loop(millis);
//if (millis > 100)
// Log.debug("Delay %i", millis);
//boxDAC.generateZeroAudio(millis);
}
void Hackiebox::loop() {
watchdog_feed();
threadController.run();
}
bool Hackiebox::watchdog_isFed() {
return _watchdog_fed;
}
void Hackiebox::watchdog_feed() {
_watchdog_fed = true;
}
void Hackiebox::watchdog_unfeed() {
_watchdog_fed = false;
}
void watchdog_handler() {
if (Box.watchdog_isFed() || !Box.watchdog_enabled) {
MAP_WatchdogIntClear(WDT_BASE);
Box.watchdog_unfeed();
}
}
bool Hackiebox::watchdog_start(uint8_t timeoutS) {
watchdog_feed();
if (timeoutS == 0) {
watchdog_start(53);
//watchdog_stop(); // Random watchdog triggers?!
watchdog_enabled = false;
} else {
if (timeoutS > 53)
timeoutS = 53; //otherwise uint32_t of WatchdogReloadSet will overflow.
MAP_PRCMPeripheralClkEnable(PRCM_WDT, PRCM_RUN_MODE_CLK);
MAP_WatchdogUnlock(WDT_BASE);
MAP_IntPrioritySet(INT_WDT, INT_PRIORITY_LVL_1);
MAP_WatchdogStallEnable(WDT_BASE); //Allow Debugging
MAP_WatchdogIntRegister(WDT_BASE, watchdog_handler);
MAP_WatchdogReloadSet(WDT_BASE, 80000000*timeoutS);
MAP_WatchdogEnable(WDT_BASE);
watchdog_enabled = true;
}
return MAP_WatchdogRunning(WDT_BASE);
}
void Hackiebox::watchdog_stop() {
MAP_WatchdogUnlock(WDT_BASE);
MAP_WatchdogReloadSet(WDT_BASE, 0xFFFFFFFF); //set timer to high value
MAP_WatchdogIntClear(WDT_BASE);
MAP_WatchdogIntUnregister(WDT_BASE);
watchdog_enabled = false;
}