/
system.c
457 lines (387 loc) · 9.76 KB
/
system.c
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
/*
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
*
* Crazyflie control firmware
*
* Copyright (C) 2011-2012 Bitcraze AB
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, in version 3.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* system.c - Top level module implementation
*/
#define DEBUG_MODULE "SYS"
#include <stdbool.h>
/* FreeRtos includes */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "debug.h"
#include "version.h"
#include "config.h"
#include "param.h"
#include "log.h"
#include "ledseq.h"
#include "pm.h"
#include "config.h"
#include "system.h"
#include "platform.h"
#include "storage.h"
#include "configblock.h"
#include "worker.h"
#include "freeRTOSdebug.h"
#include "uart_syslink.h"
#include "uart1.h"
#include "uart2.h"
#include "comm.h"
#include "stabilizer.h"
#include "commander.h"
#include "console.h"
#include "usblink.h"
#include "mem.h"
#include "proximity.h"
#include "watchdog.h"
#include "queuemonitor.h"
#include "buzzer.h"
#include "sound.h"
#include "sysload.h"
#include "estimator_kalman.h"
#include "deck.h"
#include "extrx.h"
#include "app.h"
#include "static_mem.h"
#include "peer_localization.h"
#include "cfassert.h"
#include "i2cdev.h"
#include "autoconf.h"
#ifndef CONFIG_MOTORS_START_DISARMED
#define ARM_INIT true
#else
#define ARM_INIT false
#endif
/* Private variable */
static bool selftestPassed;
static bool armed = ARM_INIT;
static bool forceArm;
static uint8_t dumpAssertInfo = 0;
static bool isInit;
static char nrf_version[16];
STATIC_MEM_TASK_ALLOC(systemTask, SYSTEM_TASK_STACKSIZE);
/* System wide synchronisation */
xSemaphoreHandle canStartMutex;
static StaticSemaphore_t canStartMutexBuffer;
/* Private functions */
static void systemTask(void *arg);
/* Public functions */
void systemLaunch(void)
{
STATIC_MEM_TASK_CREATE(systemTask, systemTask, SYSTEM_TASK_NAME, NULL, SYSTEM_TASK_PRI);
}
// This must be the first module to be initialized!
void systemInit(void)
{
if(isInit)
return;
canStartMutex = xSemaphoreCreateMutexStatic(&canStartMutexBuffer);
xSemaphoreTake(canStartMutex, portMAX_DELAY);
usblinkInit();
sysLoadInit();
/* Initialized here so that DEBUG_PRINT (buffered) can be used early */
debugInit();
crtpInit();
consoleInit();
DEBUG_PRINT("----------------------------\n");
DEBUG_PRINT("%s is up and running!\n", platformConfigGetDeviceTypeName());
if (V_PRODUCTION_RELEASE) {
DEBUG_PRINT("Production release %s\n", V_STAG);
} else {
DEBUG_PRINT("Build %s:%s (%s) %s\n", V_SLOCAL_REVISION,
V_SREVISION, V_STAG, (V_MODIFIED)?"MODIFIED":"CLEAN");
}
DEBUG_PRINT("I am 0x%08X%08X%08X and I have %dKB of flash!\n",
*((int*)(MCU_ID_ADDRESS+8)), *((int*)(MCU_ID_ADDRESS+4)),
*((int*)(MCU_ID_ADDRESS+0)), *((short*)(MCU_FLASH_SIZE_ADDRESS)));
configblockInit();
storageInit();
workerInit();
adcInit();
ledseqInit();
pmInit();
buzzerInit();
peerLocalizationInit();
#ifdef CONFIG_APP_ENABLE
appInit();
#endif
isInit = true;
}
bool systemTest()
{
bool pass=isInit;
pass &= ledseqTest();
pass &= pmTest();
pass &= workerTest();
pass &= buzzerTest();
return pass;
}
/* Private functions implementation */
void systemTask(void *arg)
{
bool pass = true;
ledInit();
ledSet(CHG_LED, 1);
#ifdef CONFIG_DEBUG_QUEUE_MONITOR
queueMonitorInit();
#endif
#ifdef CONFIG_DEBUG_PRINT_ON_UART1
uart1Init(115200);
#endif
initUsecTimer();
i2cdevInit(I2C3_DEV);
i2cdevInit(I2C1_DEV);
//Init the high-levels modules
systemInit();
commInit();
commanderInit();
StateEstimatorType estimator = anyEstimator;
#ifdef CONFIG_ESTIMATOR_KALMAN_ENABLE
estimatorKalmanTaskInit();
#endif
deckInit();
estimator = deckGetRequiredEstimator();
stabilizerInit(estimator);
if (deckGetRequiredLowInterferenceRadioMode() && platformConfigPhysicalLayoutAntennasAreClose())
{
platformSetLowInterferenceRadioMode();
}
soundInit();
memInit();
#ifdef PROXIMITY_ENABLED
proximityInit();
#endif
systemRequestNRFVersion();
//Test the modules
DEBUG_PRINT("About to run tests in system.c.\n");
if (systemTest() == false) {
pass = false;
DEBUG_PRINT("system [FAIL]\n");
}
if (configblockTest() == false) {
pass = false;
DEBUG_PRINT("configblock [FAIL]\n");
}
if (storageTest() == false) {
pass = false;
DEBUG_PRINT("storage [FAIL]\n");
}
if (commTest() == false) {
pass = false;
DEBUG_PRINT("comm [FAIL]\n");
}
if (commanderTest() == false) {
pass = false;
DEBUG_PRINT("commander [FAIL]\n");
}
if (stabilizerTest() == false) {
pass = false;
DEBUG_PRINT("stabilizer [FAIL]\n");
}
#ifdef CONFIG_ESTIMATOR_KALMAN_ENABLE
if (estimatorKalmanTaskTest() == false) {
pass = false;
DEBUG_PRINT("estimatorKalmanTask [FAIL]\n");
}
#endif
if (deckTest() == false) {
pass = false;
DEBUG_PRINT("deck [FAIL]\n");
}
if (soundTest() == false) {
pass = false;
DEBUG_PRINT("sound [FAIL]\n");
}
if (memTest() == false) {
pass = false;
DEBUG_PRINT("mem [FAIL]\n");
}
if (watchdogNormalStartTest() == false) {
pass = false;
DEBUG_PRINT("watchdogNormalStart [FAIL]\n");
}
if (cfAssertNormalStartTest() == false) {
pass = false;
DEBUG_PRINT("cfAssertNormalStart [FAIL]\n");
}
if (peerLocalizationTest() == false) {
pass = false;
DEBUG_PRINT("peerLocalization [FAIL]\n");
}
//Start the firmware
if(pass)
{
DEBUG_PRINT("Self test passed!\n");
selftestPassed = 1;
systemStart();
soundSetEffect(SND_STARTUP);
ledseqRun(&seq_alive);
ledseqRun(&seq_testPassed);
}
else
{
selftestPassed = 0;
if (systemTest())
{
while(1)
{
ledseqRun(&seq_testFailed);
vTaskDelay(M2T(2000));
// System can be forced to start by setting the param to 1 from the cfclient
if (selftestPassed)
{
DEBUG_PRINT("Start forced.\n");
systemStart();
break;
}
}
}
else
{
ledInit();
ledSet(SYS_LED, true);
}
}
DEBUG_PRINT("Free heap: %d bytes\n", xPortGetFreeHeapSize());
workerLoop();
//Should never reach this point!
while(1)
vTaskDelay(portMAX_DELAY);
}
/* Global system variables */
void systemStart()
{
xSemaphoreGive(canStartMutex);
#ifndef DEBUG
watchdogInit();
#endif
}
void systemWaitStart(void)
{
//This permits to guarantee that the system task is initialized before other
//tasks waits for the start event.
while(!isInit)
vTaskDelay(2);
xSemaphoreTake(canStartMutex, portMAX_DELAY);
xSemaphoreGive(canStartMutex);
}
void systemSetArmed(bool val)
{
armed = val;
}
bool systemIsArmed()
{
return armed || forceArm;
}
void systemRequestShutdown()
{
SyslinkPacket slp;
slp.type = SYSLINK_PM_ONOFF_SWITCHOFF;
slp.length = 0;
syslinkSendPacket(&slp);
}
void systemRequestNRFVersion()
{
SyslinkPacket slp;
slp.type = SYSLINK_SYS_NRF_VERSION;
slp.length = 0;
syslinkSendPacket(&slp);
}
void systemSyslinkReceive(SyslinkPacket *slp)
{
if (slp->type == SYSLINK_SYS_NRF_VERSION)
{
size_t len = slp->length - 2;
if (sizeof(nrf_version) - 1 <= len) {
len = sizeof(nrf_version) - 1;
}
memcpy(&nrf_version, &slp->data[0], len);
DEBUG_PRINT("NRF51 version: %s\n", nrf_version);
}
}
void vApplicationIdleHook( void )
{
static uint32_t tickOfLatestWatchdogReset = M2T(0);
portTickType tickCount = xTaskGetTickCount();
if (tickCount - tickOfLatestWatchdogReset > M2T(WATCHDOG_RESET_PERIOD_MS))
{
tickOfLatestWatchdogReset = tickCount;
watchdogReset();
}
if (dumpAssertInfo != 0) {
printAssertSnapshotData();
dumpAssertInfo = 0;
}
// Enter sleep mode. Does not work when debugging chip with SWD.
// Currently saves about 20mA STM32F405 current consumption (~30%).
#ifndef DEBUG
{ __asm volatile ("wfi"); }
#endif
}
/**
* This parameter group contain read-only parameters pertaining to the CPU
* in the Crazyflie.
*
* These could be used to identify an unique quad.
*/
PARAM_GROUP_START(cpu)
/**
* @brief Size in kB of the device flash memory
*/
PARAM_ADD_CORE(PARAM_UINT16 | PARAM_RONLY, flash, MCU_FLASH_SIZE_ADDRESS)
/**
* @brief Byte `0 - 3` of device unique id
*/
PARAM_ADD_CORE(PARAM_UINT32 | PARAM_RONLY, id0, MCU_ID_ADDRESS+0)
/**
* @brief Byte `4 - 7` of device unique id
*/
PARAM_ADD_CORE(PARAM_UINT32 | PARAM_RONLY, id1, MCU_ID_ADDRESS+4)
/**
* @brief Byte `8 - 11` of device unique id
*/
PARAM_ADD_CORE(PARAM_UINT32 | PARAM_RONLY, id2, MCU_ID_ADDRESS+8)
PARAM_GROUP_STOP(cpu)
PARAM_GROUP_START(system)
/**
* @brief All tests passed when booting
*/
PARAM_ADD_CORE(PARAM_INT8 | PARAM_RONLY, selftestPassed, &selftestPassed)
/**
* @brief Set to nonzero to force system to be armed
*/
PARAM_ADD(PARAM_INT8 | PARAM_PERSISTENT, forceArm, &forceArm)
/**
* @brief Set to nonzero to trigger dump of assert information to the log.
*/
PARAM_ADD(PARAM_UINT8, assertInfo, &dumpAssertInfo)
PARAM_GROUP_STOP(system)
/**
* System loggable variables to check different system states.
*/
LOG_GROUP_START(sys)
/**
* @brief If zero, arming system is preventing motors to start
*/
LOG_ADD(LOG_INT8, armed, &armed)
LOG_GROUP_STOP(sys)