Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_main.cpp @ b0ede265

History | View | Annotate | Download (51.863 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14

15
You should have received a copy of the GNU General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

    
19
/**
20
 * @file    aos_main.cpp
21
 * @brief   Main function.
22
 * @details Main function with SSSP and initialization,
23
 *          extendable via hooks.
24
 *
25
 * @addtogroup aos_system
26
 * @{
27
 */
28

    
29
#include <amiroos.h>
30

    
31
/*
32
 * hook to add further includes
33
 */
34
#if defined(AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER)
35
#include AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER
36
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER) */
37

    
38
/******************************************************************************/
39
/* LOCAL DEFINITIONS                                                          */
40
/******************************************************************************/
41

    
42
/**
43
 * @brief   Event mask to identify I/O events.
44
 */
45
#define IOEVENT_MASK                            EVENT_MASK(0)
46

    
47
/**
48
 * @brief   Event mask to identify OS events.
49
 */
50
#define OSEVENT_MASK                            EVENT_MASK(1)
51

    
52
/**
53
 * @brief   Event mask to idetify CAN events.
54
 */
55
#define CANEVENT_MASK                           EVENT_MASK(2)
56

    
57
/**
58
 * @brief   Event mask to idetify timeout events.
59
 */
60
#define TIMEOUTEVENT_MASK                       EVENT_MASK(3)
61

    
62
/**
63
 * @brief   Event mask to idetify signal delay events.
64
 */
65
#define DELAYEVENT_MASK                         EVENT_MASK(4)
66

    
67
#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_CAN == TRUE)) || defined(__DOXYGEN__)
68
#define SSSP_STAGE3_ENABLE                      true
69
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_CAN == TRUE) */
70
#define SSSP_STAGE3_ENABLE                      false
71
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_CAN == TRUE) */
72

    
73
#if (SSSP_STAGE3_ENABLE == true) || defined(__DOXYGEN__)
74

    
75
/**
76
 * @brief   CAN message identifier for initialization of the SSSP stack initialization sequence.
77
 */
78
#define SSSP_STACKINIT_CANMSGID_INIT            0x003
79

    
80
/**
81
 * @brief   CAN message identifier for transmitting module IDs during the SSSP stack initialization sequence.
82
 */
83
#define SSSP_STACKINIT_CANMSGID_MODULEID        0x002
84

    
85
/**
86
 * @brief   CAN message identifier for abortion of the SSSP stack initialization sequence.
87
 */
88
#define SSSP_STACKINIT_CANMSGID_ABORT           0x001
89

    
90
#endif /* (SSSP_STAGE3_ENABLE == true) */
91

    
92
#if (AMIROOS_CFG_SSSP_ENABLE != true) || defined(__DOXYGEN__)
93
/**
94
 * @brief   Default shutdown mode if SSSP is unavailable.
95
 */
96
#define AOS_SHUTDOWN_DEFAULT                    AOS_SHUTDOWN_DEEPSLEEP
97
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
98

    
99
#if (HAL_USE_CAN == TRUE) || defined(__DOXYGEN__)
100
/**
101
 * @brief   CAN message identifier for calender synchronization message.
102
 */
103
#define CALENDERSYNC_CANMSGID                   0x004
104
#endif /* (HAL_USE_CAN == TRUE) */
105

    
106
/******************************************************************************/
107
/* EXPORTED VARIABLES                                                         */
108
/******************************************************************************/
109

    
110
/******************************************************************************/
111
/* LOCAL TYPES                                                                */
112
/******************************************************************************/
113

    
114
/******************************************************************************/
115
/* LOCAL VARIABLES                                                            */
116
/******************************************************************************/
117

    
118
/**
119
 * @brief   Listener object for I/O events.
120
 */
121
static event_listener_t _eventListenerIO;
122

    
123
/**
124
 * @brief   Listener object for OS events.
125
 */
126
static event_listener_t _eventListenerOS;
127

    
128
#if defined(MODULE_HAL_PROGIF) || defined(__DOXYGEN__)
129
/**
130
 * @brief   I/O channel for the programmer interface.
131
 */
132
static AosIOChannel _stdiochannel;
133

    
134
#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
135
/**
136
 * @brief   I/O shell channel for the programmer interface.
137
 */
138
static AosShellChannel _stdshellchannel;
139
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true)*/
140
#endif /* defined(MODULE_HAL_PROGIF) */
141

    
142
/*
143
 * hook to add further static variables
144
 */
145
#if defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES)
146
AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES
147
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES) */
148

    
149
/******************************************************************************/
150
/* LOCAL FUNCTIONS                                                            */
151
/******************************************************************************/
152

    
153
/**
154
 * @brief   Prints an error message about an unexpected event.
155
 *
156
 * @param[in] mask    The event mask.
157
 * @param[in] flags   The event flags.
158
 */
159
static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags)
160
{
161
#if (AMIROOS_CFG_DBG == true)
162
  aosprintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
163
#else /* (AMIROOS_CFG_DBG == true) */
164
  (void)(mask);
165
  (void)(flags);
166
#endif /* (AMIROOS_CFG_DBG == true) */
167
  return;
168
}
169

    
170
#if (SSSP_STAGE3_ENABLE == true) || defined(__DOXYGEN__)
171
/**
172
 * @brief   Callback function to be used during SSSP stack initialization sequence.
173
 *
174
 * @param[in] par   A pointer to an @p event_source_t to be fired.
175
 */
176
static void _ssspTimerCallback(void* par)
177
{
178
  aosDbgCheck(par != NULL);
179

    
180
  chSysLockFromISR();
181
  chEvtBroadcastI((event_source_t*)par);
182
  chSysUnlockFromISR();
183

    
184
  return;
185
}
186
#endif /* (SSSP_STAGE3_ENABLE == true) */
187

    
188
/**
189
 * @brief   Helper function to serialize data.
190
 *
191
 * @param[out]  dst   Pointer to the output buffer.
192
 * @param[in]   src   Data to be serialized.
193
 * @param[in]   n     Number of bytes to serialize.
194
 */
195
inline void _serialize(uint8_t* dst, const uint64_t src, const uint8_t n)
196
{
197
  aosDbgCheck(dst != NULL);
198
  aosDbgCheck(n > 0 && n <= 8);
199

    
200
  for (uint8_t byte = 0; byte < n; ++byte) {
201
    dst[byte] = (uint8_t)((src >> (byte * 8)) & 0xFF);
202
  }
203

    
204
  return;
205
}
206

    
207
/**
208
 * @brief   Helper function to deserialize data.
209
 *
210
 * @param[in] src   Pointer to the buffer of data to be deserialzed.
211
 * @param[in] n     Number of bytes to deserialize.
212
 *
213
 * @return    The deserialized 32 bit data.
214
 */
215
inline uint64_t _deserialize(uint8_t* src, const uint8_t n)
216
{
217
  aosDbgCheck(src != NULL);
218
  aosDbgCheck(n > 0 && n <= 8);
219

    
220
  uint64_t result = 0;
221
  for (uint8_t byte = 0; byte < n; ++byte) {
222
    result |= ((uint64_t)src[byte]) << (byte * 8);
223
  }
224

    
225
  return result;
226
}
227

    
228
#if (HAL_USE_RTC == TRUE) || defined(__DOXYGEN__)
229

    
230
/**
231
 * @brief   Converter function to encode a TM value to a single unsigned 64 bit integer.
232
 *
233
 * @details Contents of the TM struct are mapped as follows:
234
 *            bits  |63     62|61      53|52    50|49         26|25     22|21     17|16     12|11      6|5       0|
235
 *            #bits |       2 |        9 |      3 |          24 |       4 |       5 |       5 |       6 |       6 |
236
 *            value |   isdst |     yday |   wday |        year |     mon |    mday |    hour |     min |     sec |
237
 *            range | special | [0, 365] | [0, 6] | [1900, ...] | [0, 11] | [1, 31] | [0, 23] | [0, 59] | [0, 61] |
238
 *          The Daylight Saving Time Flag (isdsst) is encoded as follows:
239
 *            DST not in effect         -> 0
240
 *            DST in effect             -> 1
241
 *            no information available  -> 2
242
 *
243
 * @param[in] src   Pointer to the TM struct to encode.
244
 *
245
 * @return  An unsigned 64 bit integer, which holds the encoded time value.
246
 */
247
inline uint64_t _TM2U64(struct tm* src)
248
{
249
  aosDbgCheck(src != NULL);
250

    
251
  return (((uint64_t)(src->tm_sec  & 0x0000003F) << (0))               |
252
          ((uint64_t)(src->tm_min  & 0x0000003F) << (6))               |
253
          ((uint64_t)(src->tm_hour & 0x0000001F) << (12))              |
254
          ((uint64_t)(src->tm_mday & 0x0000001F) << (17))              |
255
          ((uint64_t)(src->tm_mon  & 0x0000000F) << (22))              |
256
          ((uint64_t)(src->tm_year & 0x00FFFFFF) << (26))              |
257
          ((uint64_t)(src->tm_wday & 0x00000007) << (50))              |
258
          ((uint64_t)(src->tm_yday & 0x000001FF) << (53))              |
259
          ((uint64_t)((src->tm_isdst == 0) ? 0 : (src->tm_isdst > 0) ? 1 : 2) << (62)));
260
}
261

    
262
/**
263
 * @brief   Converter functiomn to retrieve the encoded TM value from an unsigned 64 bit integer.
264
 *
265
 * @details For information on the encoding, please refer to @p _TM2U64 function.
266
 *
267
 * @param[out] dst  The TM struct to fill with the decoded values.
268
 * @param[in]  src  Unsigned 64 bit integer holding the encoded TM value.
269
 */
270
inline void _U642TM(struct tm* dst, const uint64_t src)
271
{
272
  aosDbgCheck(dst != NULL);
273

    
274
  dst->tm_sec  = (src >> 0)  & 0x0000003F;
275
  dst->tm_min  = (src >> 6)  & 0x0000003F;
276
  dst->tm_hour = (src >> 12) & 0x0000001F;
277
  dst->tm_mday = (src >> 17) & 0x0000001F;
278
  dst->tm_mon  = (src >> 22) & 0x0000000F;
279
  dst->tm_year = (src >> 26) & 0x00FFFFFF;
280
  dst->tm_wday = (src >> 50) & 0x00000007;
281
  dst->tm_yday = (src >> 53) & 0x000001FF;
282
  dst->tm_isdst = (((src >> 62) & 0x03) == 0) ? 0 : (((src >> 62) & 0x03) > 0) ? 1 : -1;
283

    
284
  return;
285
}
286

    
287
#endif /* (HAL_USE_RTC == TRUE) */
288

    
289
#if (SSSP_STAGE3_ENABLE) || defined(__DOXYGEN__)
290
/**
291
 * @brief   Implementation of the SSSP module stack initialization sequence (startup phase 3).
292
 *
293
 * @return Shutdown value.
294
 * @retval AOS_SHUTDOWN_NONE      No shutdown signal received
295
 * @retval AOS_SHUTDOWN_PASSIVE   Shutdown signal received.
296
 */
297
aos_shutdown_t _ssspModuleStackInitialization(void)
298
{
299
  // local types
300
  /**
301
   * @brief   States for the internal state machine to implement SSSP startup stage 3.
302
   */
303
  typedef enum {
304
    STAGE_3_1,                  /**< Initiation of SSSP startup stage 3. */
305
    STAGE_3_2,                  /**< Starting the sequence and broadcasting the first ID. */
306
    STAGE_3_3_WAITFORFIRSTID,   /**< Waiting for first ID after initiation. */
307
    STAGE_3_3_WAITFORIDORSIG,   /**< Waiting for next ID or activation of neighbor signal. */
308
    STAGE_3_3_WAITFORID,        /**< Waiting for next ID (after the module has set its own ID). */
309
    STAGE_3_4_FINISH,           /**< Successful finish of stage 3. */
310
    STAGE_3_4_ABORT_ACTIVE,     /**< Aborting stage 3 (active). */
311
    STAGE_3_4_ABORT,            /**< Aborting stage 3 (passive). */
312
  } sssp_modulestackinitstage_t;
313

    
314
  typedef struct {
315
    bool loop     : 1;
316
    bool wfe      : 1;
317
    bool wfe_next : 1;
318
  } flags_t;
319

    
320
  // local variables
321
  aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE;
322
  sssp_modulestackinitstage_t stage = STAGE_3_1;
323
  eventmask_t eventmask = 0;
324
  eventflags_t ioflags = 0;
325
  event_source_t eventSourceTimeout;
326
  event_source_t eventSourceDelay;
327
  event_listener_t eventListenerTimeout;
328
  event_listener_t eventListenerDelay;
329
  event_listener_t eventListenerCan;
330
  virtual_timer_t timerTimeout;
331
  virtual_timer_t timerDelay;
332
  CANTxFrame canTxFrame;
333
  CANRxFrame canRxFrame;
334
#if (AMIROOS_CFG_SSSP_STACK_START != true) || (AMIROOS_CFG_DBG == true)
335
  aos_ssspmoduleid_t lastid = 0;
336
#endif /* (AMIROOS_CFG_SSSP_STACK_START != true) || (AMIROOS_CFG_DBG == true) */
337
  flags_t flags;
338
  aos_timestamp_t uptime;
339

    
340
  // initialize local varibles
341
  chEvtObjectInit(&eventSourceTimeout);
342
  chEvtObjectInit(&eventSourceDelay);
343
  chVTObjectInit(&timerTimeout);
344
  chVTObjectInit(&timerDelay);
345
  canTxFrame.RTR = CAN_RTR_DATA;
346
  canTxFrame.IDE = CAN_IDE_STD;
347
  flags.loop = true;
348
  flags.wfe = false; // do not wait for events in the initial iteration of the FSM loop
349
  flags.wfe_next = true;
350

    
351
  // initialize system variables
352
  aos.sssp.stage = AOS_SSSP_STARTUP_3_1;
353
  aos.sssp.moduleId = 0;
354

    
355
  // listen to events (timout, delay, CAN receive)
356
  chEvtRegisterMask(&eventSourceTimeout, &eventListenerTimeout, TIMEOUTEVENT_MASK);
357
  chEvtRegisterMask(&eventSourceDelay, &eventListenerDelay, DELAYEVENT_MASK);
358
  chEvtRegisterMask(&MODULE_HAL_CAN.rxfull_event, &eventListenerCan, CANEVENT_MASK);
359

    
360
  /*
361
   * FSM in a loop.
362
   *
363
   * This is a fully event-based FSM for the module stack initialization
364
   * sequence, defined by SSSP as startup stage 3. There are five different
365
   * events that can occur at this point:
366
   *  I/O events: The input level of an input pin has changed. Such events must
367
   *              be handled differently depending on the current state. Most
368
   *              of the time, however, such events can be ignored.
369
   *  OS events:  Such events are only available after this stage completed and
370
   *              thus should never occur. However, there is an optional hook
371
   *              to handle such events, nevertheless.
372
   *  CAN events: At least one CAN message was received. Note that this event
373
   *              will only fire again if all input buffers have been cleared.
374
   *  timeouts:   If some module does not support the sequence of there is any
375
   *              issue, such a case is detected via timeouts and must be
376
   *              handled accordingly (see abort state). In some cases, it is
377
   *              possible that a timeout event occurres 'simultaneously' with
378
   *              some other event. This can be caused by several timing issues
379
   *              and is a valid situation. As a result, any other events
380
   *              should be handled before the timeout event. If the other
381
   *              events are expected and valid, this implementation requires
382
   *              the timeout event flag to be cleared explicitely. Otherwise
383
   *              it is evaluated at the end of each iteration of the loop.
384
   *  delays:     Depending on the current state, delays are required by SSSP
385
   *              for timing of the sequential activation of signals.
386
   */
387
  aosDbgPrintf("SSSP stack initialization sequence:\n");
388
  while (flags.loop) {
389
#if (AMIROOS_CFG_DBG == true)
390
    switch (stage) {
391
      case STAGE_3_1:
392
        aosDbgPrintf(">>> 3-1\n");
393
        break;
394
      case STAGE_3_2:
395
        aosDbgPrintf(">>> 3-2\n");
396
        break;
397
      case STAGE_3_3_WAITFORFIRSTID:
398
        aosDbgPrintf(">>> 3-3 (1st ID)\n");
399
        break;
400
      case STAGE_3_3_WAITFORIDORSIG:
401
        aosDbgPrintf(">>> 3-3 (ID/sig)\n");
402
        break;
403
      case STAGE_3_3_WAITFORID:
404
        aosDbgPrintf(">>> 3-3 (ID)\n");
405
        break;
406
      case STAGE_3_4_FINISH:
407
        aosDbgPrintf(">>> 3-4 (finish)\n");
408
        break;
409
      case STAGE_3_4_ABORT_ACTIVE:
410
        aosDbgPrintf(">>> 3-4 (active abort)\n");
411
        break;
412
      case STAGE_3_4_ABORT:
413
        aosDbgPrintf(">>> 3-4 (abort)\n");
414
        break;
415
    }
416
#endif /* (AMIROOS_CFG_DBG == true) */
417

    
418
    // reset wfe flag for the next iteration
419
    flags.wfe_next = true;
420

    
421
    // waiting for events (may be skipped)
422
    if (flags.wfe) {
423
      // wait for any event to occur
424
      aosDbgPrintf("WFE...");
425
      eventmask = chEvtWaitAnyTimeout(ALL_EVENTS, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT));
426
      aosDbgPrintf("\t0x%08X", eventmask);
427
    } else {
428
      aosDbgPrintf("WFE skipped");
429
      eventmask = 0;
430
    }
431
    aosSysGetUptime(&uptime);
432
    aosDbgPrintf("\t%04ums\n", (uint32_t)(uptime / MICROSECONDS_PER_MILLISECOND));
433

    
434
    /*
435
     * execute some general tasks and high priority events
436
     */
437
    // no event occurred at all
438
    if ((flags.wfe) && (eventmask == 0)) {
439
      aosDbgPrintf("ERR: no evt\n");
440
      // enforce timeout event
441
      chEvtBroadcast(&eventSourceTimeout);
442
      continue;
443
    }
444
    // if an IO event occurred
445
    if (eventmask & _eventListenerIO.events) {
446
      ioflags = chEvtGetAndClearFlags(&_eventListenerIO);
447
      aosDbgPrintf("INFO: IO evt (0x%08X)\n", ioflags);
448
      // a power-down event occurred
449
      if (ioflags & MODULE_SSSP_EVENTFLAGS_PD) {
450
        aosDbgPrintf("PD evt\n");
451
        // deactivate S and UP
452
        aosDbgPrintf("disabling S\n");
453
        apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
454
#if (AMIROOS_CFG_SSSP_STACK_END != true)
455
        aosDbgPrintf("disabling UP\n");
456
        apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_OFF);
457
#endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
458
        // set shutdown flag and exit the loop
459
        shutdown = AOS_SHUTDOWN_PASSIVE;
460
        break;
461
      }
462
      // the S signal was deactivated
463
      if (ioflags & MODULE_SSSP_EVENTFLAGS_SYNC) {
464
        apalControlGpioState_t sstate;
465
        apalControlGpioGet(&moduleSsspGpioSync, &sstate);
466
        if (sstate == APAL_GPIO_ON) {
467
          aosDbgPrintf("S evt (enabled)\n");
468
        } else {
469
          aosDbgPrintf("S evt (disabled)\n");
470
          // either finish or abort
471
          if ((stage == STAGE_3_3_WAITFORID) && (aos.sssp.moduleId != 0)) {
472
            stage = STAGE_3_4_FINISH;
473
          } else if (stage != STAGE_3_4_ABORT) {
474
            stage = STAGE_3_4_ABORT_ACTIVE;
475
          }
476
        }
477
      }
478
    }
479
    // an OS event occurred
480
    if (eventmask & _eventListenerOS.events) {
481
      aosDbgPrintf("WARN: OS evt\n");
482
      // get the flags
483
      eventflags_t oseventflags = chEvtGetAndClearFlags(&_eventListenerOS);
484
      // there should be no OS events at this point
485
#if defined(MODULE_SSSP_STARTUP_3_OSEVENT_HOOK)
486
      MODULE_SSSP_STARTUP_3_OSEVENT_HOOK(eventmask, eventflags);
487
#else /* defined(MODULE_SSSP_STARTUP_3_OSEVENT_HOOK) */
488
      _unexpectedEventError(eventmask, oseventflags);
489
#endif /* defined(MODULE_SSSP_STARTUP_3_OSEVENT_HOOK) */
490
    }
491
    // if a CAN event occurred
492
    if ((eventmask & eventListenerCan.events)) {
493
      aosDbgPrintf("CAN evt\n");
494
      // fetch message
495
      if (flags.wfe) {
496
        canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canRxFrame, TIME_IMMEDIATE);
497
        aosDbgPrintf("CAN <- 0x%03X\n", canRxFrame.SID);
498
      }
499
      // identify and handle abort messgaes
500
      if (canRxFrame.SID == SSSP_STACKINIT_CANMSGID_ABORT) {
501
        stage = STAGE_3_4_ABORT;
502
      }
503
      // warn if a unexpected message was received
504
      else if ((canRxFrame.SID != SSSP_STACKINIT_CANMSGID_INIT) &&
505
               (canRxFrame.SID != SSSP_STACKINIT_CANMSGID_MODULEID)) {
506
        aosDbgPrintf("WARN: unknown msg\n");
507
      }
508
      // any further pending messages are fetched at the end of the loop
509
    }
510
    // if a timeout event occurred
511
    if (eventmask & eventListenerTimeout.events) {
512
      aosDbgPrintf("timeout evt\n");
513
      // is handled at the end of the loop (or must be cleared by FSM)
514
    }
515
    // if a delay event occurred
516
    if (eventmask & eventListenerDelay.events) {
517
      aosDbgPrintf("delay evt\n");
518
      // is handled by FSM
519
    }
520

    
521
    /*
522
     * this is the actual FSM
523
     */
524
    switch (stage) {
525
      case STAGE_3_1:
526
      {
527
        aos.sssp.stage = AOS_SSSP_STARTUP_3_1;
528

    
529
        // there was no event at all (skipped wfe)
530
        if (eventmask == 0 && flags.wfe == false) {
531
#if (AMIROOS_CFG_SSSP_MASTER == true)
532
          // initialize the stage by transmitting an according CAN message
533
          aosDbgPrintf("CAN -> init\n");
534
          canTxFrame.DLC = 0;
535
          canTxFrame.SID = SSSP_STACKINIT_CANMSGID_INIT;
536
          if (canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_IMMEDIATE) != MSG_OK) {
537
            chEvtBroadcast(&eventSourceTimeout);
538
            break;
539
          }
540
          // activate S
541
          aosDbgPrintf("enabling S\n");
542
          apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_ON);
543
#if (AMIROOS_CFG_SSSP_STACK_START == true)
544
          // proceed immediately
545
          stage = STAGE_3_2;
546
          flags.wfe_next = false;
547
#else /* (AMIROOS_CFG_SSSP_STACK_START == true) */
548
          // set the timeout timer
549
          chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
550
          // proceed
551
          stage = STAGE_3_3_WAITFORFIRSTID;
552
#endif /* (AMIROOS_CFG_SSSP_STACK_START == true) */
553
#else /* (AMIROOS_CFG_SSSP_MASTER == true) */
554
          // set the timeout timer
555
          chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
556
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
557
        }
558

    
559
#if (AMIROOS_CFG_SSSP_MASTER != true)
560
        // a CAN message was received
561
        else if (eventmask & eventListenerCan.events) {
562
          // if an initiation message was received
563
          if (canRxFrame.DLC == 0 &&
564
              canRxFrame.RTR == CAN_RTR_DATA &&
565
              canRxFrame.IDE == CAN_IDE_STD &&
566
              canRxFrame.SID == SSSP_STACKINIT_CANMSGID_INIT) {
567
            aosDbgPrintf("init msg\n");
568
            // reset the timeout timer and clear pending flags
569
            chVTReset(&timerTimeout);
570
            chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
571
            eventmask &= ~(eventListenerTimeout.events);
572
            // activate S
573
            aosDbgPrintf("enabling S\n");
574
            apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_ON);
575
#if (AMIROOS_CFG_SSSP_STACK_START == true)
576
            // proceed
577
            stage = STAGE_3_2;
578
            flags.wfe_next = false;
579
#else /* (AMIROOS_CFG_SSSP_STACK_START == true) */
580
            // set the timeout timer
581
            chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
582
            // proceed
583
            stage = STAGE_3_3_WAITFORFIRSTID;
584
#endif /* (AMIROOS_CFG_SSSP_STACK_START == true) */
585
          }
586
        }
587
#endif /* (AMIROOS_CFG_SSSP_MASTER != true) */
588

    
589
        break;
590
      } /* end of STAGE_3_1 */
591

    
592
      case STAGE_3_2:
593
      {
594
#if (AMIROOS_CFG_SSSP_STACK_START == true)
595
        aos.sssp.stage = AOS_SSSP_STARTUP_3_2;
596

    
597
        // if this stage was just entered
598
        if (flags.wfe == false) {
599
          // set the module ID
600
          aos.sssp.moduleId = 1;
601
          // broadcast module ID
602
          aosDbgPrintf("CAN -> ID (%u)\n", aos.sssp.moduleId);
603
          canTxFrame.DLC = 4;
604
          canTxFrame.SID = SSSP_STACKINIT_CANMSGID_MODULEID;
605
          _serialize(canTxFrame.data8, aos.sssp.moduleId, 4);
606
          if (canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_IMMEDIATE) != MSG_OK) {
607
            chEvtBroadcast(&eventSourceTimeout);
608
            break;
609
          }
610
#if (AMIROOS_CFG_DBG == true)
611
          lastid = aos.sssp.moduleId;
612
#endif /* (AMIROOS_CFG_DBG == true) */
613
#if (AMIROOS_CFG_SSSP_STACK_END == true)
614
          // sequence is already over
615
          // deactivate S
616
          aosDbgPrintf("disabling S\n");
617
          apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
618
          // proceed
619
          stage = STAGE_3_3_WAITFORID;
620
#else /* (AMIROOS_CFG_SSSP_STACK_END == true) */
621
          // set the delay timer so the UP signal is activated later
622
          chVTSet(&timerDelay, chTimeUS2I(AMIROOS_CFG_SSSP_SIGNALDELAY), _ssspTimerCallback, &eventSourceDelay);
623
#endif /* (AMIROOS_CFG_SSSP_STACK_END == true) */
624
        }
625

    
626
        // if a delay event occurred
627
        if (eventmask & eventListenerDelay.events) {
628
          // activate UP
629
          aosDbgPrintf("enabling UP\n");
630
          apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_ON);
631
          // deactivate S
632
          aosDbgPrintf("disabling S\n");
633
          apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
634
          // explicitely clear timeout event flag
635
          chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
636
          eventmask &= ~(eventListenerTimeout.events);
637
          // proceed
638
          stage = STAGE_3_3_WAITFORID;
639
        }
640
#endif /* (AMIROOS_CFG_SSSP_STACK_START == true) */
641

    
642
        break;
643
      } /* end of STAGE_3_2 */
644

    
645
      case STAGE_3_3_WAITFORFIRSTID:
646
      {
647
#if (AMIROOS_CFG_SSSP_STACK_START != true)
648
        aos.sssp.stage = AOS_SSSP_STARTUP_3_3;
649

    
650
        // a CAN message was received
651
        if (eventmask & eventListenerCan.events) {
652
          // if an ID message was received
653
          if (canRxFrame.DLC == 4 &&
654
              canRxFrame.RTR == CAN_RTR_DATA &&
655
              canRxFrame.IDE == CAN_IDE_STD &&
656
              canRxFrame.SID == SSSP_STACKINIT_CANMSGID_MODULEID) {
657
            aosDbgPrintf("ID (%u)\n", (uint32_t)_deserialize(canRxFrame.data8, 4));
658
            // validate received ID
659
            if (lastid < _deserialize(canRxFrame.data8, 4)) {
660
              // store received ID
661
              lastid = _deserialize(canRxFrame.data8, 4);
662
              // restart timeout timer
663
              chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
664
              // proceed
665
              stage = STAGE_3_3_WAITFORIDORSIG;
666
            } else {
667
              aosDbgPrintf("ERR: invalid ID\n");
668
              // abort
669
              stage = STAGE_3_4_ABORT_ACTIVE;
670
              flags.wfe_next = false;
671
            }
672
            // explicitely clear timeout event flag
673
            chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
674
            eventmask &= ~(eventListenerTimeout.events);
675
          }
676
        }
677
#endif /* (AMIROOS_CFG_SSSP_STACK_START != true) */
678
        break;
679
      } /* end of STAGE_3_3_WAITFORFIRSTID */
680

    
681
      case STAGE_3_3_WAITFORIDORSIG:
682
      {
683
#if (AMIROOS_CFG_SSSP_STACK_START != true)
684
        aos.sssp.stage = AOS_SSSP_STARTUP_3_3;
685

    
686
        // a CAN message was received
687
        if (eventmask & eventListenerCan.events) {
688
          // if an ID message was received
689
          if (canRxFrame.DLC == 4 &&
690
              canRxFrame.RTR == CAN_RTR_DATA &&
691
              canRxFrame.IDE == CAN_IDE_STD &&
692
              canRxFrame.SID == SSSP_STACKINIT_CANMSGID_MODULEID) {
693
            aosDbgPrintf("ID (%u)\n", (uint32_t)_deserialize(canRxFrame.data8, 4));
694
            // validate received ID
695
            if (lastid < _deserialize(canRxFrame.data8, 4)) {
696
              // store received ID
697
              lastid = _deserialize(canRxFrame.data8, 4);
698
              // restart timeout timer
699
              chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
700
            } else {
701
              aosDbgPrintf("ERR: invalid ID\n");
702
              // abort
703
              stage = STAGE_3_4_ABORT_ACTIVE;
704
              flags.wfe_next = false;
705
            }
706
            // explicitely clear timeout event flag
707
            chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
708
            eventmask &= ~(eventListenerTimeout.events);
709
          }
710
        }
711

    
712
        // if an IO event was received (DN signal)
713
        if ((eventmask & _eventListenerIO.events) && (ioflags & MODULE_SSSP_EVENTFLAGS_DN)) {
714
          aosDbgPrintf("DN evt\n");
715
          // reset timeout timer
716
          chVTReset(&timerTimeout);
717
          chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
718
          eventmask &= ~(eventListenerTimeout.events);
719
          // increment and broadcast ID
720
          aos.sssp.moduleId = lastid + 1;
721
          aosDbgPrintf("CAN -> ID (%u)\n", aos.sssp.moduleId);
722
          canTxFrame.DLC = 4;
723
          canTxFrame.SID = SSSP_STACKINIT_CANMSGID_MODULEID;
724
          _serialize(canTxFrame.data8, aos.sssp.moduleId, 4);
725
          if (canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_IMMEDIATE) != MSG_OK) {
726
            chEvtBroadcast(&eventSourceTimeout);
727
            break;
728
          }
729
          // set delay timer
730
          chVTSet(&timerDelay, chTimeUS2I(AMIROOS_CFG_SSSP_SIGNALDELAY), _ssspTimerCallback, &eventSourceDelay);
731
        }
732

    
733
        // if a delay event occurred
734
        if (eventmask & eventListenerDelay.events) {
735
#if (AMIROOS_CFG_SSSP_STACK_END != true)
736
          // activate UP
737
          aosDbgPrintf("enabling UP\n");
738
          apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_ON);
739
#endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
740
          // deactivate S
741
          aosDbgPrintf("disabling S\n");
742
          apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
743
          // reset the timeout timer
744
          chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
745
          chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
746
          eventmask &= ~(eventListenerTimeout.events);
747
          // proceed
748
          stage = STAGE_3_3_WAITFORID;
749
        }
750
#endif /* (AMIROOS_CFG_SSSP_STACK_START != true) */
751

    
752
        break;
753
      } /* end of STAGE_3_3_WAITFORIDORSIG */
754

    
755
      case STAGE_3_3_WAITFORID:
756
      {
757
        aos.sssp.stage = AOS_SSSP_STARTUP_3_3;
758

    
759
#if (AMIROOS_CFG_SSSP_STACK_END != true)
760
        // a CAN message was received
761
        if (eventmask & eventListenerCan.events) {
762
          // if an ID message was received
763
          if (canRxFrame.DLC == 4 &&
764
              canRxFrame.RTR == CAN_RTR_DATA &&
765
              canRxFrame.IDE == CAN_IDE_STD &&
766
              canRxFrame.SID == SSSP_STACKINIT_CANMSGID_MODULEID) {
767
#if (AMIROOS_CFG_SSSP_STACK_START != true) || (AMIROOS_CFG_DBG == true)
768
            // Plausibility of the received ID is not checked at this point but is done by other modules still in a previous stage.
769
            lastid = _deserialize(canRxFrame.data8, 4);
770
            aosDbgPrintf("ID (%u)\n", lastid);
771
#endif /* (AMIROOS_CFG_SSSP_STACK_START != true) || (AMIROOS_CFG_DBG == true) */
772
            // restart timeout timer
773
            chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
774
            chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
775
            eventmask &= ~(eventListenerTimeout.events);
776
          }
777
        }
778
#endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
779

    
780
        break;
781
      } /* end of STAGE_3_3_WAITFORID */
782

    
783
      case STAGE_3_4_FINISH:
784
      {
785
        aos.sssp.stage = AOS_SSSP_STARTUP_3_4;
786

    
787
        // if an IO event was received (S signal)
788
        if ((eventmask & _eventListenerIO.events) && (ioflags & MODULE_SSSP_EVENTFLAGS_SYNC)) {
789
          // reset the timeout timer
790
          chVTReset(&timerTimeout);
791
          chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
792
          eventmask &= ~(eventListenerTimeout.events);
793
          //set the delay timer
794
          chVTSet(&timerDelay, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceDelay);
795
        }
796

    
797
        // if a CAN event was received
798
        if (eventmask & eventListenerCan.events) {
799
          // if an abort message was received
800
          if (canRxFrame.SID == SSSP_STACKINIT_CANMSGID_ABORT) {
801
            aosDbgPrintf("abort msg\n");
802
            // reset the delay timer
803
            chVTReset(&timerDelay);
804
            chEvtWaitAnyTimeout(eventListenerDelay.events, TIME_IMMEDIATE);
805
            eventmask &= ~(eventListenerDelay.events);
806
            // proceed
807
            stage = STAGE_3_4_ABORT;
808
          }
809
        }
810

    
811
        // if a delay timer event occurred
812
        if (eventmask & eventListenerDelay.events) {
813
          aosDbgPrintf("sequence sucessful\n");
814
          // sequence finished sucessfully
815
          flags.loop = false;
816
        }
817

    
818
        break;
819
      } /* end of STAGE_3_4_FINISH */
820

    
821
      case STAGE_3_4_ABORT_ACTIVE:
822
      {
823
        aos.sssp.stage = AOS_SSSP_STARTUP_3_4;
824

    
825
        // emit abort message
826
        canTxFrame.DLC = 0;
827
        canTxFrame.SID = SSSP_STACKINIT_CANMSGID_ABORT;
828
        canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_INFINITE);
829
        aosDbgPrintf("CAN -> abort\n");
830
        // clear timeout flag
831
        eventmask &= ~(eventListenerTimeout.events);
832
        // proceed immediately
833
        stage = STAGE_3_4_ABORT;
834
        flags.wfe_next = false;
835
        break;
836
      } /* end of STAGE_3_4_ABORT_ACTIVE */
837

    
838
      case STAGE_3_4_ABORT:
839
      {
840
        aos.sssp.stage = AOS_SSSP_STARTUP_3_4;
841

    
842
        // deactivate S
843
        aosDbgPrintf("disabling SYNC\n");
844
        apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
845
        // invalidate module ID
846
        aos.sssp.moduleId = 0;
847

    
848
        // if an IO event was received (S signal)
849
        if ((eventmask & _eventListenerIO.events) && (ioflags & MODULE_SSSP_EVENTFLAGS_SYNC)) {
850
          aosDbgPrintf("sequence aborted\n");
851
          // exit the sequence
852
          flags.loop = false;
853
        }
854

    
855
        break;
856
      } /* end of STAGE_3_4_ABORT */
857
    } /* end of switch(stage) */
858

    
859
    // fetch pending CAN message (if any)
860
    if ((eventmask & eventListenerCan.events) && (canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canRxFrame, TIME_IMMEDIATE) == MSG_OK)) {
861
      aosDbgPrintf("CAN <- 0x%03X\n", canRxFrame.SID);
862
      flags.wfe_next = false;
863
    }
864

    
865
    // handle unhandled timeout events
866
    if (eventmask & eventListenerTimeout.events) {
867
      aosDbgPrintf("ERR: timeout evt\n");
868
      // abort
869
      flags.wfe_next = false;
870
      stage = STAGE_3_4_ABORT_ACTIVE;
871
    }
872

    
873
    // apply wfe value for next iteration
874
    flags.wfe = flags.wfe_next;
875
  } /* end of FSM loop */
876

    
877
  // unregister all events (timeout, delay, CAN receive)
878
  chEvtUnregister(&eventSourceTimeout, &eventListenerTimeout);
879
  chEvtUnregister(&eventSourceDelay, &eventListenerDelay);
880
  chEvtUnregister(&MODULE_HAL_CAN.rxfull_event, &eventListenerCan);
881
  // clear any pending events (timeout, delay, CAN receive)
882
  chEvtWaitAllTimeout(TIMEOUTEVENT_MASK | DELAYEVENT_MASK | CANEVENT_MASK, TIME_IMMEDIATE);
883

    
884
  // reset all control signals
885
#if (AMIROOS_CFG_SSSP_STACK_END != true)
886
  aosDbgPrintf("disabling UP\n");
887
  apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_OFF);
888
#endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
889
  aosDbgPrintf("disabling S\n");
890
  apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
891
  aosSysGetUptime(&uptime);
892
  aosDbgPrintf("done\t%04ums\n", (uint32_t)(uptime / MICROSECONDS_PER_MILLISECOND));
893

    
894
  return shutdown;
895
}
896
#endif /* (SSSP_STAGE3_ENABLE == true) */
897

    
898
/**
899
 * @brief   Application entry point.
900
 */
901
int main(void)
902
{
903
  // local variables
904
  eventmask_t eventmask = 0;
905
  eventflags_t eventflags = 0;
906
  eventflags_t ioeventflagsmask = AMIROOS_CFG_MAIN_LOOP_IOEVENT_MASK;
907
  aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE;
908
#if defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES)
909
  AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES
910
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES) */
911

    
912
  /*
913
   * ##########################################################################
914
   * # system initialization                                                  #
915
   * ##########################################################################
916
   */
917

    
918
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_0)
919
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_0_ARGS)
920
  AMIROOS_CFG_MAIN_INIT_HOOK_0(AMIROOS_CFG_MAIN_INIT_HOOK_0_ARGS);
921
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_0_ARGS) */
922
  AMIROOS_CFG_MAIN_INIT_HOOK_0();
923
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_0_ARGS) */
924
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_0) */
925

    
926
  /* hardware, kernel, and operating system initialization */
927
  // ChibiOS/HAL and custom hal additions (if any)
928
  halInit();
929
#if defined(MODULE_INIT_HAL_EXTRA)
930
  MODULE_INIT_HAL_EXTRA();
931
#endif /* defined(MODULE_INIT_HAL_EXTRA) */
932

    
933
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1)
934
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS)
935
  AMIROOS_CFG_MAIN_INIT_HOOK_1(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS);
936
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS) */
937
  AMIROOS_CFG_MAIN_INIT_HOOK_1();
938
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS) */
939
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_1) */
940

    
941
  // ChibiOS/RT kernel and custom kernel additions (if any)
942
  chSysInit();
943
#if defined(MODULE_INIT_KERNEL_EXTRA)
944
  MODULE_INIT_KERNEL_EXTRA();
945
#endif /* defined(MODULE_INIT_KERNEL_EXTRA) */
946

    
947
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2)
948
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS)
949
  AMIROOS_CFG_MAIN_INIT_HOOK_2(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS);
950
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS) */
951
  AMIROOS_CFG_MAIN_INIT_HOOK_2();
952
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS) */
953
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_2) */
954

    
955
  // AMiRo-OS and custom OS additions (if any)
956
#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true)
957
  aosSysInit(moduleShellPrompt);
958
#else /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) */
959
  aosSysInit();
960
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) */
961
#if defined(MODULE_INIT_OS_EXTRA)
962
  MODULE_INIT_OS_EXTRA();
963
#endif /* defined(MODULE_INIT_OS_EXTRA) */
964

    
965
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3)
966
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS)
967
  AMIROOS_CFG_MAIN_INIT_HOOK_3(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS);
968
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS) */
969
  AMIROOS_CFG_MAIN_INIT_HOOK_3();
970
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS) */
971
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3) */
972

    
973
  /* event associations */
974
#if (AMIROOS_CFG_SSSP_ENABLE == true)
975
  ioeventflagsmask |= MODULE_SSSP_EVENTFLAGS_PD | MODULE_SSSP_EVENTFLAGS_SYNC;
976
#if (AMIROOS_CFG_SSSP_STACK_START != true)
977
  ioeventflagsmask |= MODULE_SSSP_EVENTFLAGS_DN;
978
#endif /* (AMIROOS_CFG_SSSP_STACK_START != true) */
979
#if (AMIROOS_CFG_SSSP_STACK_END != true)
980
  ioeventflagsmask |= MODULE_SSSP_EVENTFLAGS_UP;
981
#endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
982
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
983
  if (ioeventflagsmask != 0) {
984
    chEvtRegisterMaskWithFlags(&aos.events.io, &_eventListenerIO, IOEVENT_MASK, ioeventflagsmask);
985
  }
986
  chEvtRegisterMask(&aos.events.os, &_eventListenerOS, OSEVENT_MASK);
987

    
988
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4)
989
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS)
990
  AMIROOS_CFG_MAIN_INIT_HOOK_4(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS);
991
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS) */
992
  AMIROOS_CFG_MAIN_INIT_HOOK_4();
993
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS) */
994
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4) */
995

    
996
  /* periphery communication initialization */
997
  // module specific initialization (if any)
998
#if defined(MODULE_INIT_PERIPHERY_COMM)
999
  MODULE_INIT_PERIPHERY_COMM();
1000
#endif /* defined(MODULE_INIT_PERIPHERY_COMM) */
1001
#if (SSSP_STAGE3_ENABLE == true )
1002
  // CAN
1003
  if (MODULE_HAL_CAN.state == CAN_STOP) {
1004
    canStart(&MODULE_HAL_CAN, &moduleHalCanConfig);
1005
  }
1006
#endif /* (SSSP_STAGE3_ENABLE == true) */
1007
  // user interface (if any)
1008
#if defined(MODULE_HAL_PROGIF)
1009
  aosIOChannelInit(&_stdiochannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
1010
  aosIOChannelOutputEnable(&_stdiochannel);
1011
  aosIOStreamAddChannel(&aos.iostream, &_stdiochannel);
1012
#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true)
1013
  aosShellChannelInit(&_stdshellchannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
1014
  aosShellChannelInputEnable(&_stdshellchannel);
1015
  aosShellChannelOutputEnable(&_stdshellchannel);
1016
  aosShellStreamAddChannel(&aos.shell.stream, &_stdshellchannel);
1017
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) */
1018
#endif /* defined(MODULE_HAL_PROGIF) */
1019

    
1020
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5)
1021
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS)
1022
  AMIROOS_CFG_MAIN_INIT_HOOK_5(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS);
1023
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS) */
1024
  AMIROOS_CFG_MAIN_INIT_HOOK_5();
1025
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS) */
1026
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5) */
1027

    
1028
  /* module is ready -> print welcome prompt */
1029
  aosprintf("\n");
1030
  aosprintf("######################################################################\n");
1031
  aosprintf("# AMiRo-OS is an operating system designed for the Autonomous Mini   #\n");
1032
  aosprintf("# Robot (AMiRo) platform.                                            #\n");
1033
  aosprintf("# Copyright (C) 2016..2019  Thomas Schöpping et al.                  #\n");
1034
  aosprintf("#                                                                    #\n");
1035
  aosprintf("# This is free software; see the source for copying conditions.      #\n");
1036
  aosprintf("# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR  #\n");
1037
  aosprintf("# A PARTICULAR PURPOSE.                                              #\n");
1038
  aosprintf("# The development of this software was supported by the Excellence   #\n");
1039
  aosprintf("# Cluster EXC 227 Cognitive Interaction Technology. The Excellence   #\n");
1040
  aosprintf("# Cluster EXC 227 is a grant of the Deutsche Forschungsgemeinschaft  #\n");
1041
  aosprintf("# (DFG) in the context of the German Excellence Initiative.          #\n");
1042
  aosprintf("######################################################################\n");
1043
  aosprintf("\n");
1044

    
1045
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_6)
1046
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS)
1047
  AMIROOS_CFG_MAIN_INIT_HOOK_6(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS);
1048
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS) */
1049
  AMIROOS_CFG_MAIN_INIT_HOOK_6();
1050
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS) */
1051
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_6) */
1052

    
1053
#if (AMIROOS_CFG_TESTS_ENABLE == true)
1054
#if defined(MODULE_INIT_TESTS)
1055
  MODULE_INIT_TESTS();
1056
#else /* defined(MODULE_INIT_TESTS) */
1057
  #warning "AMIROOS_CFG_TESTS_ENABLE set to true, but MODULE_INIT_TESTS() not defined"
1058
#endif /* defined(MODULE_INIT_TESTS) */
1059
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
1060

    
1061
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_7)
1062
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS)
1063
  AMIROOS_CFG_MAIN_INIT_HOOK_7(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS);
1064
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS) */
1065
  AMIROOS_CFG_MAIN_INIT_HOOK_7();
1066
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS) */
1067
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_7) */
1068

    
1069
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1070
  /* SSSP startup OS synchronization phase (end of startup stage 2) */
1071
  while ((shutdown == AOS_SHUTDOWN_NONE) && (eventmask = aosSysSsspStartupOsInitSyncCheck(&_eventListenerIO)) != 0) {
1072
    /*
1073
     * This code is executed if the received event was not about the SYS_SYNC control signal.
1074
     * The returned event could be caused by any listener (not only the argument).
1075
     */
1076
    // IO event
1077
    if (eventmask & _eventListenerIO.events) {
1078
      eventflags = chEvtGetAndClearFlags(&_eventListenerIO);
1079
      // PD event
1080
      if (eventflags & MODULE_SSSP_EVENTFLAGS_PD) {
1081
        shutdown = AOS_SHUTDOWN_PASSIVE;
1082
      } else {
1083
#if defined(MODULE_SSSP_STARTUP_2_2_IOEVENT_HOOK)
1084
        MODULE_SSSP_STARTUP_2_2_IOEVENT_HOOK(eventmask, eventflags);
1085
#else /* defined(MODULE_SSSP_STARTUP_2_2_IOEVENT_HOOK) */
1086
        // ignore any other IO events
1087
#endif /* defined(MODULE_SSSP_STARTUP_2_2_IOEVENT_HOOK) */
1088
      }
1089
    }
1090
    // OS event
1091
    else if (eventmask & _eventListenerOS.events) {
1092
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
1093
      _unexpectedEventError(eventmask, eventflags);
1094
    }
1095
    // unknown event
1096
    else {
1097
      _unexpectedEventError(eventmask, 0);
1098
    }
1099
  }
1100

    
1101
#if (HAL_USE_CAN == TRUE)
1102

    
1103
  /*
1104
   * There must be no delays at this point, thus no hook is allowed.
1105
   */
1106

    
1107
  /* SSSP startup stage 3 (module stack initialization) */
1108
  if (shutdown == AOS_SHUTDOWN_NONE) {
1109
    shutdown = _ssspModuleStackInitialization();
1110
  }
1111

    
1112
  /*
1113
   * There must be no delays at this point, thus no hook is allowed.
1114
   */
1115

    
1116
#if (HAL_USE_RTC == TRUE)
1117
  /* synchronize calendars */
1118
  if (shutdown == AOS_SHUTDOWN_NONE) {
1119
#if (AMIROOS_CFG_SSSP_MASTER == true)
1120
    CANTxFrame frame;
1121
    struct tm t;
1122
    uint64_t encoded;
1123

    
1124
    frame.DLC = 8;
1125
    frame.RTR = CAN_RTR_DATA;
1126
    frame.IDE = CAN_IDE_STD;
1127
    frame.SID = CALENDERSYNC_CANMSGID;
1128

    
1129
    aosDbgPrintf("transmitting current date/time...\t");
1130
    // get current date & time
1131
    aosSysGetDateTime(&t);
1132
    // encode
1133
    encoded = _TM2U64(&t);
1134
    // serialize
1135
    _serialize(frame.data8, encoded, 8);
1136
    // transmit
1137
    canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &frame, TIME_IMMEDIATE);
1138

    
1139
    aosDbgPrintf("done\n");
1140
#else /* (AMIROOS_CFG_SSSP_MASTER == true) */
1141
    CANRxFrame frame;
1142
    uint64_t encoded;
1143
    struct tm t;
1144

    
1145
    aosDbgPrintf("receiving current date/time...\t");
1146
    // receive message
1147
#if (AMIROOS_CFG_DBG == true)
1148
    // increase timeout in debug mode due to additional delays introduced by the many prinf() calls
1149
    if (canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &frame, chTimeUS2I(10 * AOS_SYSTEM_SSSP_TIMEOUT)) == MSG_OK) {
1150
#else /* (AMIROOS_CFG_DBG == true) */
1151
    if (canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &frame, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT)) == MSG_OK) {
1152
#endif /* (AMIROOS_CFG_DBG == true) */
1153
      // validate message
1154
      if (frame.DLC == 8 &&
1155
          frame.RTR == CAN_RTR_DATA &&
1156
          frame.IDE == CAN_IDE_STD &&
1157
          frame.SID == CALENDERSYNC_CANMSGID) {
1158
        // deserialize
1159
        encoded = _deserialize(frame.data8, 8);
1160
        // decode
1161
        _U642TM(&t, encoded);
1162
        // set current date & time
1163
        aosSysSetDateTime(&t);
1164
        aosDbgPrintf("success\n");
1165
      } else {
1166
        aosDbgPrintf("fail (invalid message)\n");
1167
      }
1168
    } else {
1169
      aosDbgPrintf("fail (timeout)\n");
1170
    }
1171
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
1172
    aosDbgPrintf("\n");
1173
  }
1174
#endif /* (HAL_USE_RTC == TRUE) */
1175
#endif /* (HAL_USE_CAN == TRUE) */
1176
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
1177

    
1178
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_8)
1179
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS)
1180
  AMIROOS_CFG_MAIN_INIT_HOOK_8(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS);
1181
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS) */
1182
  AMIROOS_CFG_MAIN_INIT_HOOK_8();
1183
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS) */
1184
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_8) */
1185

    
1186
  /* completely start AMiRo-OS */
1187
  if (shutdown == AOS_SHUTDOWN_NONE) {
1188
    aosSysStart();
1189
  }
1190

    
1191
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_9)
1192
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS)
1193
  AMIROOS_CFG_MAIN_INIT_HOOK_9(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS);
1194
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS) */
1195
  AMIROOS_CFG_MAIN_INIT_HOOK_9();
1196
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS) */
1197
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_9) */
1198

    
1199
  /*
1200
   * ##########################################################################
1201
   * # infinite loop                                                          #
1202
   * ##########################################################################
1203
   */
1204

    
1205
  // sleep until a shutdown event is received
1206
  while (shutdown == AOS_SHUTDOWN_NONE) {
1207
    // wait for an event
1208
#if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0)
1209
    eventmask = chEvtWaitOneTimeout(ALL_EVENTS, chTimeUS2I(AMIROOS_CFG_MAIN_LOOP_TIMEOUT));
1210
#else /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0) */
1211
    eventmask = chEvtWaitOne(ALL_EVENTS);
1212
#endif /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0) */
1213

    
1214
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0)
1215
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS)
1216
    AMIROOS_CFG_MAIN_LOOP_HOOK_0(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS);
1217
#else /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS) */
1218
    AMIROOS_CFG_MAIN_LOOP_HOOK_0();
1219
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS) */
1220
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0) */
1221

    
1222
    switch (eventmask) {
1223
      // if this was an I/O event
1224
      case IOEVENT_MASK:
1225
        // evaluate flags
1226
        eventflags = chEvtGetAndClearFlags(&_eventListenerIO);
1227
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1228
        // PD event
1229
        if (eventflags & MODULE_SSSP_EVENTFLAGS_PD) {
1230
          shutdown = AOS_SHUTDOWN_PASSIVE;
1231
        }
1232
        // all other events
1233
#if defined(MODULE_MAIN_LOOP_IO_EVENT)
1234
        else {
1235
          MODULE_MAIN_LOOP_IO_EVENT(eventflags);
1236
        }
1237
#endif /* defined(MODULE_MAIN_LOOP_IO_EVENT) */
1238
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
1239
#if defined(MODULE_MAIN_LOOP_IO_EVENT)
1240
        MODULE_MAIN_LOOP_IO_EVENT(eventflags);
1241
#endif /* defined(MODULE_MAIN_LOOP_IO_EVENT) */
1242
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
1243
        break;
1244

    
1245
      // if this was an OS event
1246
      case OSEVENT_MASK:
1247
        // evaluate flags
1248
        eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
1249
        switch (eventflags) {
1250
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1251
          case AOS_SYSTEM_EVENTFLAGS_HIBERNATE:
1252
            shutdown = AOS_SHUTDOWN_HIBERNATE;
1253
            break;
1254
          case AOS_SYSTEM_EVENTFLAGS_DEEPSLEEP:
1255
            shutdown = AOS_SHUTDOWN_DEEPSLEEP;
1256
            break;
1257
          case AOS_SYSTEM_EVENTFLAGS_TRANSPORTATION:
1258
            shutdown = AOS_SHUTDOWN_TRANSPORTATION;
1259
            break;
1260
          case AOS_SYSTEM_EVENTFLAGS_RESTART:
1261
            shutdown = AOS_SHUTDOWN_RESTART;
1262
            break;
1263
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
1264
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN:
1265
            shutdown = AOS_SHUTDOWN_DEFAULT;
1266
            break;
1267
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
1268
          default:
1269
            _unexpectedEventError(eventmask, eventflags);
1270
            break;
1271
        }
1272
        break;
1273

    
1274
      // if this was any other event (should be impossible to occur)
1275
      default:
1276
        eventflags = 0;
1277
#if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT == 0)
1278
        _unexpectedEventError(eventmask, eventflags);
1279
#endif /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT == 0) */
1280
        break;
1281
    }
1282

    
1283
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1)
1284
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS)
1285
    AMIROOS_CFG_MAIN_LOOP_HOOK_1(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS);
1286
#else /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS) */
1287
    AMIROOS_CFG_MAIN_LOOP_HOOK_1();
1288
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS) */
1289
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1) */
1290
  }
1291

    
1292
  /*
1293
   * ##########################################################################
1294
   * # system shutdown                                                        #
1295
   * ##########################################################################
1296
   */
1297

    
1298
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0)
1299
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS)
1300
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS);
1301
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS) */
1302
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0();
1303
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS) */
1304
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0) */
1305

    
1306
  // initialize/acknowledge shutdown
1307
  aosSysShutdownInit(shutdown);
1308

    
1309
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1)
1310
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS)
1311
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS);
1312
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS) */
1313
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1();
1314
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS) */
1315
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1) */
1316

    
1317
  // stop system threads
1318
  aosSysStop();
1319

    
1320
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2)
1321
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS)
1322
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS);
1323
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS) */
1324
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2();
1325
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS) */
1326
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2) */
1327

    
1328
  // deinitialize system
1329
  aosSysDeinit();
1330

    
1331
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3)
1332
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS)
1333
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS);
1334
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS) */
1335
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3();
1336
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS) */
1337
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3) */
1338

    
1339
  /* stop all periphery communication */
1340
#if defined(MODULE_SHUTDOWN_PERIPHERY_COMM)
1341
  MODULE_SHUTDOWN_PERIPHERY_COMM();
1342
#endif /* defined(MODULE_SHUTDOWN_PERIPHERY_COMM) */
1343
#if (HAL_USE_CAN == TRUE)
1344
  if (MODULE_HAL_CAN.state != CAN_STOP) {
1345
    canStop(&MODULE_HAL_CAN);
1346
  }
1347
#endif /* (HAL_USE_CAN == TRUE) */
1348

    
1349
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4)
1350
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS)
1351
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS);
1352
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS) */
1353
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4();
1354
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS) */
1355
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4) */
1356

    
1357
  // finally hand over to bootloader
1358
  aosSysShutdownFinal(shutdown);
1359

    
1360
  /*
1361
   * ##########################################################################
1362
   * # after shutdown/restart                                                 #
1363
   * ##########################################################################
1364
   *
1365
   * NOTE: This code will not be executed, since the bootloader callbacks will stop/restart the MCU.
1366
   *       It is included nevertheless for the sake of completeness and to explicitely indicate,
1367
   *       which subsystems should NOT be shut down.
1368
   */
1369

    
1370
  // return an error, since this code should not be executed
1371
  return -1;
1372
}
1373

    
1374
/******************************************************************************/
1375
/* EXPORTED FUNCTIONS                                                         */
1376
/******************************************************************************/
1377

    
1378
/** @} */