Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (52.005 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, additional interrupts 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_INTERRUPTS)
962
  MODULE_INIT_INTERRUPTS();
963
#endif
964
#if defined(MODULE_INIT_OS_EXTRA)
965
  MODULE_INIT_OS_EXTRA();
966
#endif /* defined(MODULE_INIT_OS_EXTRA) */
967

    
968
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3)
969
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS)
970
  AMIROOS_CFG_MAIN_INIT_HOOK_3(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS);
971
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS) */
972
  AMIROOS_CFG_MAIN_INIT_HOOK_3();
973
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS) */
974
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3) */
975

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

    
991
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4)
992
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS)
993
  AMIROOS_CFG_MAIN_INIT_HOOK_4(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS);
994
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS) */
995
  AMIROOS_CFG_MAIN_INIT_HOOK_4();
996
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS) */
997
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4) */
998

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

    
1023
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5)
1024
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS)
1025
  AMIROOS_CFG_MAIN_INIT_HOOK_5(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS);
1026
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS) */
1027
  AMIROOS_CFG_MAIN_INIT_HOOK_5();
1028
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS) */
1029
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5) */
1030

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

    
1048
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_6)
1049
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS)
1050
  AMIROOS_CFG_MAIN_INIT_HOOK_6(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS);
1051
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS) */
1052
  AMIROOS_CFG_MAIN_INIT_HOOK_6();
1053
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS) */
1054
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_6) */
1055

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

    
1064
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_7)
1065
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS)
1066
  AMIROOS_CFG_MAIN_INIT_HOOK_7(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS);
1067
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS) */
1068
  AMIROOS_CFG_MAIN_INIT_HOOK_7();
1069
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS) */
1070
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_7) */
1071

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

    
1104
#if (HAL_USE_CAN == TRUE)
1105

    
1106
  /*
1107
   * There must be no delays at this point, thus no hook is allowed.
1108
   */
1109

    
1110
  /* SSSP startup stage 3 (module stack initialization) */
1111
  if (shutdown == AOS_SHUTDOWN_NONE) {
1112
    shutdown = _ssspModuleStackInitialization();
1113
  }
1114

    
1115
  /*
1116
   * There must be no delays at this point, thus no hook is allowed.
1117
   */
1118

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

    
1127
    frame.DLC = 8;
1128
    frame.RTR = CAN_RTR_DATA;
1129
    frame.IDE = CAN_IDE_STD;
1130
    frame.SID = CALENDERSYNC_CANMSGID;
1131

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

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

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

    
1181
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_8)
1182
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS)
1183
  AMIROOS_CFG_MAIN_INIT_HOOK_8(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS);
1184
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS) */
1185
  AMIROOS_CFG_MAIN_INIT_HOOK_8();
1186
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS) */
1187
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_8) */
1188

    
1189
  /* completely start AMiRo-OS */
1190
  if (shutdown == AOS_SHUTDOWN_NONE) {
1191
    aosSysStart();
1192
  }
1193

    
1194
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_9)
1195
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS)
1196
  AMIROOS_CFG_MAIN_INIT_HOOK_9(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS);
1197
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS) */
1198
  AMIROOS_CFG_MAIN_INIT_HOOK_9();
1199
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS) */
1200
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_9) */
1201

    
1202
  /*
1203
   * ##########################################################################
1204
   * # infinite loop                                                          #
1205
   * ##########################################################################
1206
   */
1207

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

    
1217
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0)
1218
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS)
1219
    AMIROOS_CFG_MAIN_LOOP_HOOK_0(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS);
1220
#else /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS) */
1221
    AMIROOS_CFG_MAIN_LOOP_HOOK_0();
1222
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS) */
1223
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0) */
1224

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

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

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

    
1286
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1)
1287
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS)
1288
    AMIROOS_CFG_MAIN_LOOP_HOOK_1(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS);
1289
#else /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS) */
1290
    AMIROOS_CFG_MAIN_LOOP_HOOK_1();
1291
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS) */
1292
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1) */
1293
  }
1294

    
1295
  /*
1296
   * ##########################################################################
1297
   * # system shutdown                                                        #
1298
   * ##########################################################################
1299
   */
1300

    
1301
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0)
1302
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS)
1303
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS);
1304
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS) */
1305
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0();
1306
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS) */
1307
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0) */
1308

    
1309
  // initialize/acknowledge shutdown
1310
  aosSysShutdownInit(shutdown);
1311

    
1312
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1)
1313
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS)
1314
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS);
1315
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS) */
1316
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1();
1317
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS) */
1318
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1) */
1319

    
1320
  // stop system threads
1321
  aosSysStop();
1322

    
1323
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2)
1324
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS)
1325
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS);
1326
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS) */
1327
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2();
1328
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS) */
1329
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2) */
1330

    
1331
  // deinitialize system
1332
  aosSysDeinit();
1333

    
1334
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3)
1335
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS)
1336
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS);
1337
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS) */
1338
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3();
1339
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS) */
1340
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3) */
1341

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

    
1352
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4)
1353
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS)
1354
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS);
1355
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS) */
1356
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4();
1357
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS) */
1358
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4) */
1359

    
1360
  // finally hand over to bootloader
1361
  aosSysShutdownFinal(shutdown);
1362

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

    
1373
  // return an error, since this code should not be executed
1374
  return -1;
1375
}
1376

    
1377
/******************************************************************************/
1378
/* EXPORTED FUNCTIONS                                                         */
1379
/******************************************************************************/
1380

    
1381
/** @} */