forked from dastcvi/StratoCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStratoCore.cpp
325 lines (266 loc) · 8.94 KB
/
StratoCore.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
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
/*
* StratoCore.cpp
* Author: Alex St. Clair
* Created: June 2019
*
* This file implements an Arduino library (C++ class) with core functionality
* for LASP Strateole payload interface boards
*/
#include "StratoCore.h"
#include <TimeLib.h>
#include <Watchdog_t4.h>
WDT_T4<WDT1> wdt; // Use Watchdog Timer1 on Teensy 4.1
StratoCore::StratoCore(Stream * zephyr_serial, Instrument_t instrument, Stream * dbg_serial)
#ifndef XMLDEBUG
: zephyrTX(zephyr_serial, instrument)
#else
: zephyrTX(zephyr_serial, instrument, dbg_serial)
#endif
, zephyrRX(zephyr_serial, instrument)
{
inst_mode = MODE_STANDBY; // always boot to standby
new_inst_mode = MODE_STANDBY;
inst_substate = MODE_ENTRY; // substate starts as mode entry
RA_ack_flag = NO_ACK;
S_ack_flag = NO_ACK;
TM_ack_flag = NO_ACK;
time_valid = false;
debug_serial = dbg_serial; // located in StratoGroundPort
last_zephyr = now();
tcs_remaining = 0;
}
void StratoCore::InitializeCore()
{
if (!StartSD()) {
log_error("StratoCore unable to start SD card");
} else {
log_nominal("StratoCore started SD card");
}
InitializeWatchdog();
}
// initialize the watchdog using the 1kHz LPO clock source to achieve a 10s WDOG
void StratoCore::InitializeWatchdog()
{
WDT_timings_t config;
//config.trigger = 5; /* in seconds, 0->128 for watchdog warning */
config.timeout = 10; /* in seconds, 0->128 for watchdog reboot */
//config.callback = myCallback;
wdt.begin(config);
if (wdt.expired()) {
ZephyrLogCrit("Reset caused by watchdog");
}
// if ((RCM_SRS0 & RCM_SRS0_WDOG) != 0) {
// ZephyrLogCrit("Reset caused by watchdog");
// }
// noInterrupts(); // disable interrupts
// // unlock
// WDOG_UNLOCK = WDOG_UNLOCK_SEQ1; // unlock access to WDOG registers
// WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
// delayMicroseconds(1);
// WDOG_PRESC = 0; // no prescaling of clock
// WDOG_TOVALH = 0x0000; // upper bits set to 0
// WDOG_TOVALL = 0x2710; // 10000 counter at 1 kHz => 10s WDOG period
// // in one write, enable the watchdog using the 1kHz LPO clock source
// WDOG_STCTRLH = 0x01D1;
// interrupts(); // enable interrupts
}
void StratoCore::KickWatchdog()
{
// noInterrupts();
// WDOG_REFRESH = 0xA602;
// WDOG_REFRESH = 0xB480;
// interrupts();
wdt.feed();
}
void StratoCore::RunMode()
{
// check for a new mode
if (inst_mode != new_inst_mode) {
// call the last mode after setting the substate to exit
inst_substate = MODE_EXIT;
(this->*(mode_array[inst_mode]))();
// clear any scheduled items from the old mode
scheduler.ClearSchedule();
// update the mode and set the substate to entry
inst_mode = new_inst_mode;
inst_substate = MODE_ENTRY;
}
// run the current mode
(this->*(mode_array[inst_mode]))();
}
void StratoCore::RunRouter()
{
// process as many messages as are available
while (zephyrRX.GetNewMessage()) {
RouteRXMessage(zephyrRX.zephyr_message);
}
// handle one TC per loop
if (tcs_remaining > 0) {
tcs_remaining--;
NextTelecommand();
}
// check for Zephyr no contact timeout
if (now() > last_zephyr + ZEPHYR_TIMEOUT) {
if(now() > last_timeout_warning + LOST_COMMS_FREQ) {
ZephyrLogCrit("Zephyr comm loss timeout");
last_timeout_warning = now();
new_inst_mode = MODE_SAFETY;
}
}
}
void StratoCore::RouteRXMessage(ZephyrMessage_t message)
{
switch (message) {
case IM:
new_inst_mode = zephyrRX.zephyr_mode;
zephyrTX.IMAck(true);
break;
case GPS:
UpdateTime();
break;
case SW:
// set the substate to shutdown and let the mode functions handle it
inst_substate = MODE_SHUTDOWN;
break;
case TC:
if (tcs_remaining > 0) ZephyrLogWarn("TC received too quickly! Last TC overwritten");
tcs_remaining = zephyrRX.num_tcs;
// we've successfully parsed the TC
zephyrTX.TCAck(true);
break;
case SAck:
S_ack_flag = (zephyrRX.zephyr_ack == 1) ? ACK : NAK;
break;
case RAAck:
RA_ack_flag = (zephyrRX.zephyr_ack == 1) ? ACK : NAK;
break;
case TMAck:
TM_ack_flag = (zephyrRX.zephyr_ack == 1) ? ACK : NAK;
break;
case NO_ZEPHYR_MSG:
break;
default:
log_error("Unknown message to route");
break;
}
last_zephyr = now();
}
void StratoCore::RunScheduler()
{
uint8_t scheduled_action = scheduler.CheckSchedule();
while (scheduled_action != NO_SCHEDULED_ACTION) {
ActionHandler(scheduled_action);
scheduled_action = scheduler.CheckSchedule();
}
}
void StratoCore::ZephyrLogFine(const char * log_info)
{
if (NULL == log_info) return;
debug_serial->print("Zephyr-FINE: ");
debug_serial->println(log_info);
zephyrTX.TM_String(FINE, log_info);
TM_ack_flag = NO_ACK;
}
void StratoCore::ZephyrLogWarn(const char * log_info)
{
if (NULL == log_info) return;
debug_serial->print("Zephyr-WARN: ");
debug_serial->println(log_info);
zephyrTX.TM_String(WARN, log_info);
TM_ack_flag = NO_ACK;
}
void StratoCore::ZephyrLogCrit(const char * log_info)
{
if (NULL == log_info) return;
debug_serial->print("Zephyr-CRIT: ");
debug_serial->println(log_info);
zephyrTX.TM_String(CRIT, log_info);
TM_ack_flag = NO_ACK;
}
void StratoCore::SendTMBuffer()
{
// use only the first flag to report the motion
zephyrTX.setStateDetails(1, "TM buffer as requested");
zephyrTX.setStateFlagValue(1, FINE);
zephyrTX.setStateFlagValue(2, NOMESS);
zephyrTX.setStateFlagValue(3, NOMESS);
TM_ack_flag = NO_ACK;
zephyrTX.TM();
}
bool StratoCore::WriteFileTM(const char * file_prefix)
{
char filename[64] = {0};
uint8_t * tm_buffer = NULL;
uint16_t tm_size = 0;
if (NULL == file_prefix) return false;
// prep the filename and check it was created correctly
if (63 < snprintf(filename, 64, "%s_%lld.dat", file_prefix, now())) return false;
// get a pointer to the TM buffer and its size
tm_size = zephyrTX.getTmBuffer(&tm_buffer);
bool success = FileWrite(filename, (const char *) tm_buffer, tm_size);
return success;
}
void StratoCore::UpdateTime()
{
int32_t before, new_time, difference;
TimeElements new_time_elements;
char gps_string[100] = {0};
new_time_elements.Hour = zephyrRX.zephyr_gps.hour;
new_time_elements.Minute = zephyrRX.zephyr_gps.minute;
new_time_elements.Second = zephyrRX.zephyr_gps.second;
new_time_elements.Day = zephyrRX.zephyr_gps.day;
new_time_elements.Month = zephyrRX.zephyr_gps.month;
new_time_elements.Year = (uint8_t) (zephyrRX.zephyr_gps.year - 1970);
before = now();
new_time = makeTime(new_time_elements);
difference = new_time - before;
// if the time difference is greater than the configured maximum, update
if (difference > MAX_TIME_DRIFT || difference < -MAX_TIME_DRIFT) {
log_nominal("Correcting time drift");
noInterrupts();
setTime(new_time);
interrupts();
scheduler.UpdateScheduleTime(difference);
}
time_valid = true;
snprintf(gps_string, 100, "%u:%u:%u %u/%u/%u, SZA: %f", new_time_elements.Hour, new_time_elements.Minute, new_time_elements.Second,
new_time_elements.Month, new_time_elements.Day, new_time_elements.Year + 1970, zephyrRX.zephyr_gps.solar_zenith_angle);
log_nominal(gps_string);
}
void StratoCore::NextTelecommand()
{
TCParseStatus_t tc_status = zephyrRX.GetTelecommand();
switch (tc_status) {
case READ_TC:
// check for generic TCs before routing to the instrument
switch (zephyrRX.zephyr_tc) {
case NULL_TELECOMMAND:
log_nominal("Null telecommand");
break;
case RESET_INST:
zephyrTX.TCAck(true);
delay(100);
SCB_AIRCR = 0x5FA0004; // write the reset key and bit to the ARM AIRCR register
break;
case GETTMBUFFER:
SendTMBuffer();
break;
case SENDSTATE:
snprintf(log_array, LOG_ARRAY_SIZE, "Current mode: %u, substate: %u", inst_mode, inst_substate);
ZephyrLogFine(log_array);
break;
default:
TCHandler(zephyrRX.zephyr_tc);
break;
}
break;
case TC_ERROR:
snprintf(log_array, LOG_ARRAY_SIZE, "Bad command at TC position %u", zephyrRX.curr_tc);
ZephyrLogWarn(log_array);
break;
case NO_TCs:
default:
tcs_remaining = 0;
break;
}
}