blob: eda6a62fadd16c075bb5c8ec7fcf2b21653a44af (
plain)
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
|
#include <Arduino.h>
#include <string.h>
#include <avr/wdt.h>
#include <helpers/ArduinoHelpers.h>
#include <target.h>
#include "FixedPoolPacketManager.h"
#include "FlashStore.h"
#include "PublicApp.h"
#include "UITask.h"
uint8_t mcusr_mirror __attribute__((section(".noinit")));
uint8_t wdt_stage_mirror __attribute__((section(".noinit")));
extern char __heap_start;
namespace {
const uint8_t WDT_STAGE_IDLE = 'I';
const uint8_t WDT_STAGE_SETUP = 'S';
const uint8_t WDT_STAGE_APP = 'A';
const uint8_t WDT_STAGE_UI = 'U';
const uint8_t WDT_STAGE_FLASH = 'F';
const uint8_t WDT_STAGE_POWER = 'P';
const uint8_t STACK_CANARY = 0xA5;
const uint8_t STACK_GUARD_BYTES = 64;
uint8_t* stack_canary_start = NULL;
uint8_t* stack_canary_end = NULL;
uint16_t stack_min_free = 0;
static uint8_t* currentStackPointer() {
uint16_t sp;
asm volatile(
"in %A0, __SP_L__" "\n\t"
"in %B0, __SP_H__"
: "=r"(sp));
return (uint8_t*)sp;
}
static uint16_t currentStackFree() {
uint8_t* heap_end = (uint8_t*)&__heap_start;
uint8_t* sp = currentStackPointer();
if (sp <= heap_end + STACK_GUARD_BYTES) {
return 0;
}
return (uint16_t)(sp - heap_end - STACK_GUARD_BYTES);
}
static uint16_t scanStackFree() {
if (!stack_canary_start || !stack_canary_end || stack_canary_end <= stack_canary_start) {
return 0;
}
uint8_t* heap_end = stack_canary_start;
if (heap_end >= stack_canary_end) {
return 0;
}
uint8_t* p = stack_canary_end;
while (p > heap_end && *(p - 1) != STACK_CANARY) {
--p;
}
return (uint16_t)(p - heap_end);
}
static void initStackCanary() {
uint8_t* start = (uint8_t*)&__heap_start;
uint8_t* sp = currentStackPointer();
if (sp <= start + STACK_GUARD_BYTES) {
stack_canary_start = NULL;
stack_canary_end = NULL;
stack_min_free = 0;
return;
}
stack_canary_start = start;
stack_canary_end = sp - STACK_GUARD_BYTES;
memset(stack_canary_start, STACK_CANARY, stack_canary_end - stack_canary_start);
stack_min_free = scanStackFree();
}
static void updateStackMinFree() {
uint16_t free_now = scanStackFree();
if (stack_min_free == 0 || free_now < stack_min_free) {
stack_min_free = free_now;
}
}
}
void captureResetCause(void) __attribute__((naked, used, section(".init3")));
void captureResetCause(void) {
mcusr_mirror = MCUSR;
MCUSR = 0;
wdt_disable();
}
static void setWatchdogStage(uint8_t stage) {
wdt_stage_mirror = stage;
}
extern "C" void SRXEWatchdogPoll(void) {
setWatchdogStage(WDT_STAGE_FLASH);
wdt_reset();
}
extern "C" void MeshCoreWatchdogStage(uint8_t stage) {
setWatchdogStage(stage);
}
extern "C" uint16_t MeshCoreStackFreeNow(void) {
updateStackMinFree();
return currentStackFree();
}
extern "C" uint16_t MeshCoreStackFreeMin(void) {
updateStackMinFree();
return stack_min_free;
}
static ArduinoMillis ms_clock;
static FixedPoolPacketManager<4> packet_mgr;
static FlashStore flash_store;
static PublicRTCClock rtc_clock;
static PublicApp app(radio_driver, ms_clock, packet_mgr, rtc_clock, flash_store);
static UITask ui(board, app);
#ifndef SMARTRESPONSE_BUILD_EPOCH
#define SMARTRESPONSE_BUILD_EPOCH 1715770351UL
#endif
static uint32_t initialClockTime() {
uint32_t start = SMARTRESPONSE_BUILD_EPOCH;
uint32_t latest = flash_store.getLatestTimestamp();
if (latest != 0xFFFFFFFFUL && latest >= start) {
start = latest + 1;
}
return start;
}
static void waitForPowerButtonRelease() {
while (board.isPowerButtonPressed()) {
wdt_reset();
delay(10);
}
delay(50);
}
static void sleepUntilPowerButton() {
setWatchdogStage(WDT_STAGE_POWER);
waitForPowerButtonRelease();
radio_driver.powerOff();
wdt_disable();
board.sleep(0);
wdt_enable(WDTO_4S);
app.resetRadio();
ui.redraw();
waitForPowerButtonRelease();
}
static void halt() {
while (1) {
}
}
void setup() {
wdt_disable();
uint8_t last_wdt_stage = (mcusr_mirror & _BV(WDRF)) ? wdt_stage_mirror : '-';
setWatchdogStage(WDT_STAGE_SETUP);
delay(100);
board.begin();
flash_store.begin();
rtc_clock.begin(initialClockTime());
if (!radio_init()) {
halt();
}
app.setResetCause(mcusr_mirror);
app.setWatchdogStage(last_wdt_stage);
app.begin();
ui.begin();
initStackCanary();
setWatchdogStage(WDT_STAGE_IDLE);
wdt_enable(WDTO_4S);
}
void loop() {
setWatchdogStage(WDT_STAGE_IDLE);
wdt_reset();
rtc_clock.tick();
setWatchdogStage(WDT_STAGE_APP);
app.loop();
wdt_reset();
setWatchdogStage(WDT_STAGE_UI);
ui.loop();
if (board.isPowerButtonPressed()) {
sleepUntilPowerButton();
}
updateStackMinFree();
setWatchdogStage(WDT_STAGE_IDLE);
board.idleSleep();
}
|