Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (45.046 KB)

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

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 53710ca3 Marc Rothmann
/**
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 e2d7143f Thomas Schöpping
#include <aos_system.h>
30 cb835a3e Thomas Schöpping
#include <module.h>
31 e545e620 Thomas Schöpping
32 b6b45e4c Thomas Schöpping
/*
33
 * hook to add further includes
34
 */
35 512abac1 Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER)
36
#include AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER
37 b6b45e4c Thomas Schöpping
#endif
38
39 e545e620 Thomas Schöpping
/**
40
 * @brief   Event mask to identify I/O events.
41
 */
42 6b53f6bf Thomas Schöpping
#define IOEVENT_MASK                            EVENT_MASK(0)
43 e545e620 Thomas Schöpping
44
/**
45
 * @brief   Event mask to identify OS events.
46
 */
47 6b53f6bf Thomas Schöpping
#define OSEVENT_MASK                            EVENT_MASK(1)
48 e545e620 Thomas Schöpping
49
/**
50 933df08e Thomas Schöpping
 * @brief   Event mask to idetify CAN events.
51
 */
52
#define CANEVENT_MASK                           EVENT_MASK(2)
53
54
/**
55
 * @brief   Event mask to idetify timeout events.
56
 */
57
#define TIMEOUTEVENT_MASK                       EVENT_MASK(3)
58
59
/**
60
 * @brief   Event mask to idetify signal delay events.
61
 */
62
#define DELAYEVENT_MASK                         EVENT_MASK(4)
63
64 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
65
66 933df08e Thomas Schöpping
/**
67
 * @brief   CAN message identifier for initialization of the SSSP stack initialization sequence.
68
 */
69
#define SSSP_STACKINIT_CANMSGID_INIT            0x003
70
71
/**
72
 * @brief   CAN message identifier for transmitting module IDs during the SSSP stack initialization sequence.
73
 */
74
#define SSSP_STACKINIT_CANMSGID_MODULEID        0x002
75
76
/**
77
 * @brief   CAN message identifier for abortion of the SSSP stack initialization sequence.
78
 */
79
#define SSSP_STACKINIT_CANMSGID_ABORT           0x001
80
81 1d3e002f Thomas Schöpping
#else /* AMIROOS_CFG_SSSP_ENABLE == false */
82
83
/**
84
 * @brief   Default shutdown mode if SSSP is unavailable.
85
 */
86
#define AOS_SHUTDOWN_DEFAULT                    AOS_SHUTDOWN_DEEPSLEEP
87
88
#endif /* AMIROOS_CFG_SSSP_ENABLE */
89 9ebb11a9 Thomas Schöpping
90 933df08e Thomas Schöpping
/**
91 9461fadc Thomas Schöpping
 * @brief   CAN message identifier for calender synchronization message.
92
 */
93
#define CALENDERSYNC_CANMSGID                   0x004
94
95
/**
96 e545e620 Thomas Schöpping
 * @brief   Listener object for I/O events.
97
 */
98
static event_listener_t _eventListenerIO;
99
100
/**
101
 * @brief   Listener object for OS events.
102
 */
103
static event_listener_t _eventListenerOS;
104
105
#if defined(MODULE_HAL_PROGIF) || defined(__DOXYGEN__)
106
/**
107 ba516b61 Thomas Schöpping
 * @brief   I/O channel for the programmer interface.
108 e545e620 Thomas Schöpping
 */
109 ba516b61 Thomas Schöpping
static AosIOChannel _stdiochannel;
110
111 2dd2e257 Thomas Schöpping
#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
112 ba516b61 Thomas Schöpping
/**
113
 * @brief   I/O shell channel for the programmer interface.
114
 */
115
static AosShellChannel _stdshellchannel;
116 2dd2e257 Thomas Schöpping
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true)*/
117 1d3e002f Thomas Schöpping
#endif /* defined(MODULE_HAL_PROGIF) */
118 e545e620 Thomas Schöpping
119 b6b45e4c Thomas Schöpping
/*
120
 * hook to add further static variables
121
 */
122
#if defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES)
123
AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES
124
#endif
125
126 e545e620 Thomas Schöpping
/**
127
 * @brief   Prints an error message about an unexpected event.
128
 *
129
 * @param[in] mask    The event mask.
130
 * @param[in] flags   The event flags.
131
 */
132 933df08e Thomas Schöpping
static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags)
133 e545e620 Thomas Schöpping
{
134 6b53f6bf Thomas Schöpping
#if (AMIROOS_CFG_DBG == true)
135 1e5f7648 Thomas Schöpping
  aosprintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
136 6b53f6bf Thomas Schöpping
#else
137 933df08e Thomas Schöpping
  (void)(mask);
138
  (void)(flags);
139 6b53f6bf Thomas Schöpping
#endif
140 3e1a9c79 Thomas Schöpping
  return;
141 933df08e Thomas Schöpping
}
142
143 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
144 933df08e Thomas Schöpping
/**
145
 * @brief   Callback function to be used during SSSP stack initialization sequence.
146
 *
147
 * @param[in] par   A pointer to an @p event_source_t to be fired.
148
 */
149
static void _ssspTimerCallback(void* par)
150
{
151
  aosDbgCheck(par != NULL);
152
153
  chSysLockFromISR();
154
  chEvtBroadcastI((event_source_t*)par);
155
  chSysUnlockFromISR();
156
157 e545e620 Thomas Schöpping
  return;
158
}
159 9ebb11a9 Thomas Schöpping
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
160 e545e620 Thomas Schöpping
161
/**
162 9461fadc Thomas Schöpping
 * @brief   Helper function to serialize data.
163 933df08e Thomas Schöpping
 *
164
 * @param[out]  dst   Pointer to the output buffer.
165 9461fadc Thomas Schöpping
 * @param[in]   src   Data to be serialized.
166
 * @param[in]   n     Number of bytes to serialize.
167 933df08e Thomas Schöpping
 */
168 9461fadc Thomas Schöpping
inline void _serialize(uint8_t* dst, const uint64_t src, const uint8_t n)
169 933df08e Thomas Schöpping
{
170
  aosDbgCheck(dst != NULL);
171 9461fadc Thomas Schöpping
  aosDbgCheck(n > 0 && n <= 8);
172 933df08e Thomas Schöpping
173 9461fadc Thomas Schöpping
  for (uint8_t byte = 0; byte < n; ++byte) {
174 933df08e Thomas Schöpping
    dst[byte] = (uint8_t)((src >> (byte * 8)) & 0xFF);
175
  }
176
177
  return;
178
}
179
180
/**
181 9461fadc Thomas Schöpping
 * @brief   Helper function to deserialize data.
182 933df08e Thomas Schöpping
 *
183
 * @param[in] src   Pointer to the buffer of data to be deserialzed.
184 9461fadc Thomas Schöpping
 * @param[in] n     Number of bytes to deserialize.
185 933df08e Thomas Schöpping
 *
186
 * @return    The deserialized 32 bit data.
187
 */
188 9461fadc Thomas Schöpping
inline uint64_t _deserialize(uint8_t* src, const uint8_t n)
189 933df08e Thomas Schöpping
{
190
  aosDbgCheck(src != NULL);
191 9461fadc Thomas Schöpping
  aosDbgCheck(n > 0 && n <= 8);
192 933df08e Thomas Schöpping
193 9461fadc Thomas Schöpping
  uint64_t result = 0;
194
  for (uint8_t byte = 0; byte < n; ++byte) {
195
    result |= ((uint64_t)src[byte]) << (byte * 8);
196
  }
197
198
  return result;
199
}
200
201
/**
202
 * @brief   Converter function to encode a TM value to a single unsigned 64 bit integer.
203
 *
204
 * @details Contents of the TM struct are mapped as follows:
205
 *            bits  |63     62|61      53|52    50|49         26|25     22|21     17|16     12|11      6|5       0|
206
 *            #bits |       2 |        9 |      3 |          24 |       4 |       5 |       5 |       6 |       6 |
207
 *            value |   isdst |     yday |   wday |        year |     mon |    mday |    hour |     min |     sec |
208
 *            range | special | [0, 365] | [0, 6] | [1900, ...] | [0, 11] | [1, 31] | [0, 23] | [0, 59] | [0, 61] |
209
 *          The Daylight Saving Time Flag (isdsst) is encoded as follows:
210
 *            DST not in effect         -> 0
211
 *            DST in effect             -> 1
212
 *            no information available  -> 2
213
 *
214
 * @param[in] src   Pointer to the TM struct to encode.
215
 *
216
 * @return  An unsigned 64 bit integer, which holds the encoded time value.
217
 */
218
inline uint64_t _TM2U64(struct tm* src)
219
{
220
  aosDbgCheck(src != NULL);
221
222
  return (((uint64_t)(src->tm_sec  & 0x0000003F) << (0))               |
223
          ((uint64_t)(src->tm_min  & 0x0000003F) << (6))               |
224
          ((uint64_t)(src->tm_hour & 0x0000001F) << (12))              |
225
          ((uint64_t)(src->tm_mday & 0x0000001F) << (17))              |
226
          ((uint64_t)(src->tm_mon  & 0x0000000F) << (22))              |
227
          ((uint64_t)(src->tm_year & 0x00FFFFFF) << (26))              |
228
          ((uint64_t)(src->tm_wday & 0x00000007) << (50))              |
229
          ((uint64_t)(src->tm_yday & 0x000001FF) << (53))              |
230
          ((uint64_t)((src->tm_isdst == 0) ? 0 : (src->tm_isdst > 0) ? 1 : 2) << (62)));
231
}
232
233
/**
234
 * @brief   Converter functiomn to retrieve the encoded TM value from an unsigned 64 bit integer.
235
 *
236
 * @details For information on the encoding, please refer to @p _TM2U64 function.
237
 *
238
 * @param[out] dst  The TM struct to fill with the decoded values.
239
 * @param[in]  src  Unsigned 64 bit integer holding the encoded TM value.
240
 */
241
inline void _U642TM(struct tm* dst, const uint64_t src)
242
{
243
  aosDbgCheck(dst != NULL);
244
245
  dst->tm_sec  = (src >> 0)  & 0x0000003F;
246
  dst->tm_min  = (src >> 6)  & 0x0000003F;
247
  dst->tm_hour = (src >> 12) & 0x0000001F;
248
  dst->tm_mday = (src >> 17) & 0x0000001F;
249
  dst->tm_mon  = (src >> 22) & 0x0000000F;
250
  dst->tm_year = (src >> 26) & 0x00FFFFFF;
251
  dst->tm_wday = (src >> 50) & 0x00000007;
252
  dst->tm_yday = (src >> 53) & 0x000001FF;
253
  dst->tm_isdst = (((src >> 62) & 0x03) == 0) ? 0 : (((src >> 62) & 0x03) > 0) ? 1 : -1;
254
255
  return;
256 933df08e Thomas Schöpping
}
257
258 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
259 933df08e Thomas Schöpping
/**
260
 * @brief   Implementation of the SSSP module stack initialization sequence (startup phase 3).
261
 *
262
 * @return Shutdown value.
263
 * @retval AOS_SHUTDOWN_NONE      No shutdown signal received
264
 * @retval AOS_SHUTDOWN_PASSIVE   Shutdown signal received.
265
 */
266
aos_shutdown_t _ssspModuleStackInitialization(void)
267
{
268
  // local types
269
  /**
270
   * @brief   States for the internal state machine to implement SSSP startup stage 3.
271
   */
272
  typedef enum {
273
    STAGE_3_1,                  /**< Initiation of SSSP startup stage 3. */
274
    STAGE_3_2,                  /**< Starting the sequence and broadcasting the first ID. */
275
    STAGE_3_3_WAITFORFIRSTID,   /**< Waiting for first ID after initiation. */
276
    STAGE_3_3_WAITFORIDORSIG,   /**< Waiting for next ID or activation of neighbor signal. */
277
    STAGE_3_3_WAITFORID,        /**< Waiting for next ID (after the module has set its own ID). */
278
    STAGE_3_4_FINISH,           /**< Successful finish of stage 3. */
279
    STAGE_3_4_ABORT_ACTIVE,     /**< Aborting stage 3 (active). */
280
    STAGE_3_4_ABORT,            /**< Aborting stage 3 (passive). */
281
  } sssp_modulestackinitstage_t;
282
283
  typedef struct {
284
    bool loop     : 1;
285
    bool wfe      : 1;
286
    bool wfe_next : 1;
287
  } flags_t;
288
289
  // local variables
290
  aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE;
291
  sssp_modulestackinitstage_t stage = STAGE_3_1;
292
  eventmask_t eventmask = 0;
293 1d3e002f Thomas Schöpping
  eventflags_t ioflags = 0;
294 933df08e Thomas Schöpping
  event_source_t eventSourceTimeout;
295
  event_source_t eventSourceDelay;
296
  event_listener_t eventListenerTimeout;
297
  event_listener_t eventListenerDelay;
298
  event_listener_t eventListenerCan;
299
  virtual_timer_t timerTimeout;
300
  virtual_timer_t timerDelay;
301
  CANTxFrame canTxFrame;
302
  CANRxFrame canRxFrame;
303
#if (AMIROOS_CFG_SSSP_STACK_START != true) || (AMIROOS_CFG_DBG == true)
304
  aos_ssspmoduleid_t lastid = 0;
305
#endif
306
  flags_t flags;
307 1d3e002f Thomas Schöpping
  aos_timestamp_t uptime;
308 933df08e Thomas Schöpping
309
  // initialize local varibles
310
  chEvtObjectInit(&eventSourceTimeout);
311
  chEvtObjectInit(&eventSourceDelay);
312
  chVTObjectInit(&timerTimeout);
313
  chVTObjectInit(&timerDelay);
314
  canTxFrame.RTR = CAN_RTR_DATA;
315
  canTxFrame.IDE = CAN_IDE_STD;
316
  flags.loop = true;
317
  flags.wfe = false; // do not wait for events in the initial iteration of the FSM loop
318
  flags.wfe_next = true;
319
320
  // initialize system variables
321
  aos.sssp.stage = AOS_SSSP_STARTUP_3_1;
322
  aos.sssp.moduleId = 0;
323
324
  // listen to events (timout, delay, CAN receive)
325
  chEvtRegisterMask(&eventSourceTimeout, &eventListenerTimeout, TIMEOUTEVENT_MASK);
326
  chEvtRegisterMask(&eventSourceDelay, &eventListenerDelay, DELAYEVENT_MASK);
327
  chEvtRegisterMask(&MODULE_HAL_CAN.rxfull_event, &eventListenerCan, CANEVENT_MASK);
328
329
  /*
330
   * FSM in a loop.
331
   *
332
   * This is a fully event-based FSM for the module stack initialization
333
   * sequence, defined by SSSP as startup stage 3. There are five different
334
   * events that can occur at this point:
335
   *  I/O events: The input level of an input pin has changed. Such events must
336
   *              be handled differently depending on the current state. Most
337
   *              of the time, however, such events can be ignored.
338
   *  OS events:  Such events are only available after this stage completed and
339
   *              thus should never occur. However, there is an optional hook
340
   *              to handle such events, nevertheless.
341
   *  CAN events: At least one CAN message was received. Note that this event
342
   *              will only fire again if all input buffers have been cleared.
343
   *  timeouts:   If some module does not support the sequence of there is any
344
   *              issue, such a case is detected via timeouts and must be
345
   *              handled accordingly (see abort state). In some cases, it is
346
   *              possible that a timeout event occurres 'simultaneously' with
347
   *              some other event. This can be caused by several timing issues
348
   *              and is a valid situation. As a result, any other events
349
   *              should be handled before the timeout event. If the other
350
   *              events are expected and valid, this implementation requires
351
   *              the timeout event flag to be cleared explicitely. Otherwise
352
   *              it is evaluated at the end of each iteration of the loop.
353
   *  delays:     Depending on the current state, delays are required by SSSP
354
   *              for timing of the sequential activation of signals.
355
   */
356
  aosDbgPrintf("SSSP stack initialization sequence:\n");
357
  while (flags.loop) {
358
#if (AMIROOS_CFG_DBG == true)
359
    switch (stage) {
360
      case STAGE_3_1:
361
        aosDbgPrintf(">>> 3-1\n");
362
        break;
363
      case STAGE_3_2:
364
        aosDbgPrintf(">>> 3-2\n");
365
        break;
366
      case STAGE_3_3_WAITFORFIRSTID:
367
        aosDbgPrintf(">>> 3-3 (1st ID)\n");
368
        break;
369
      case STAGE_3_3_WAITFORIDORSIG:
370
        aosDbgPrintf(">>> 3-3 (ID/sig)\n");
371
        break;
372
      case STAGE_3_3_WAITFORID:
373
        aosDbgPrintf(">>> 3-3 (ID)\n");
374
        break;
375
      case STAGE_3_4_FINISH:
376
        aosDbgPrintf(">>> 3-4 (finish)\n");
377
        break;
378
      case STAGE_3_4_ABORT_ACTIVE:
379 9461fadc Thomas Schöpping
        aosDbgPrintf(">>> 3-4 (active abort)\n");
380 933df08e Thomas Schöpping
        break;
381
      case STAGE_3_4_ABORT:
382
        aosDbgPrintf(">>> 3-4 (abort)\n");
383
        break;
384
    }
385
#endif
386
387
    // reset wfe flag for the next iteration
388
    flags.wfe_next = true;
389
390 1d3e002f Thomas Schöpping
    // waiting for events (may be skipped)
391 933df08e Thomas Schöpping
    if (flags.wfe) {
392
      // wait for any event to occur
393
      aosDbgPrintf("WFE...");
394 1e5f7648 Thomas Schöpping
      eventmask = chEvtWaitAnyTimeout(ALL_EVENTS, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT));
395 933df08e Thomas Schöpping
      aosDbgPrintf("\t0x%08X", eventmask);
396
    } else {
397
      aosDbgPrintf("WFE skipped");
398 1d3e002f Thomas Schöpping
      eventmask = 0;
399 933df08e Thomas Schöpping
    }
400
    aosSysGetUptime(&uptime);
401 1d3e002f Thomas Schöpping
    aosDbgPrintf("\t%04ums\n", (uint32_t)(uptime / MICROSECONDS_PER_MILLISECOND));
402 933df08e Thomas Schöpping
403
    /*
404
     * execute some general tasks and high priority events
405
     */
406
    // no event occurred at all
407
    if ((flags.wfe) && (eventmask == 0)) {
408
      aosDbgPrintf("ERR: no evt\n");
409
      // enforce timeout event
410
      chEvtBroadcast(&eventSourceTimeout);
411
      continue;
412
    }
413
    // if an IO event occurred
414 9461fadc Thomas Schöpping
    if (eventmask & _eventListenerIO.events) {
415 933df08e Thomas Schöpping
      ioflags = chEvtGetAndClearFlags(&_eventListenerIO);
416
      aosDbgPrintf("INFO: IO evt (0x%08X)\n", ioflags);
417
      // a power-down event occurred
418
      if (ioflags & MODULE_SSSP_EVENTFLAGS_PD) {
419
        aosDbgPrintf("PD evt\n");
420
        // deactivate S and UP
421 1d3e002f Thomas Schöpping
        aosDbgPrintf("disabling S\n");
422 933df08e Thomas Schöpping
        apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
423 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_STACK_END != true)
424 1d3e002f Thomas Schöpping
        aosDbgPrintf("disabling UP\n");
425 933df08e Thomas Schöpping
        apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_OFF);
426 9ebb11a9 Thomas Schöpping
#endif
427 933df08e Thomas Schöpping
        // set shutdown flag and exit the loop
428
        shutdown = AOS_SHUTDOWN_PASSIVE;
429
        break;
430
      }
431
      // the S signal was deactivated
432
      if (ioflags & MODULE_SSSP_EVENTFLAGS_SYNC) {
433
        apalControlGpioState_t sstate;
434
        apalControlGpioGet(&moduleSsspGpioSync, &sstate);
435 1d3e002f Thomas Schöpping
        if (sstate == APAL_GPIO_ON) {
436
          aosDbgPrintf("S evt (enabled)\n");
437
        } else {
438
          aosDbgPrintf("S evt (disabled)\n");
439 933df08e Thomas Schöpping
          // either finish or abort
440
          if ((stage == STAGE_3_3_WAITFORID) && (aos.sssp.moduleId != 0)) {
441
            stage = STAGE_3_4_FINISH;
442
          } else if (stage != STAGE_3_4_ABORT) {
443
            stage = STAGE_3_4_ABORT_ACTIVE;
444
          }
445
        }
446
      }
447
    }
448
    // an OS event occurred
449 9461fadc Thomas Schöpping
    if (eventmask & _eventListenerOS.events) {
450 933df08e Thomas Schöpping
      aosDbgPrintf("WARN: OS evt\n");
451
      // get the flags
452
      eventflags_t oseventflags = chEvtGetAndClearFlags(&_eventListenerOS);
453
      // there should be no OS events at this point
454
#ifdef MODULE_SSSP_STARTUP_3_OSEVENT_HOOK
455
      MODULE_SSSP_STARTUP_3_OSEVENT_HOOK(eventmask, eventflags);
456
#else
457
      _unexpectedEventError(eventmask, oseventflags);
458
#endif
459
    }
460
    // if a CAN event occurred
461 9461fadc Thomas Schöpping
    if ((eventmask & eventListenerCan.events)) {
462 1d3e002f Thomas Schöpping
      aosDbgPrintf("CAN evt\n");
463 933df08e Thomas Schöpping
      // fetch message
464
      if (flags.wfe) {
465
        canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canRxFrame, TIME_IMMEDIATE);
466
        aosDbgPrintf("CAN <- 0x%03X\n", canRxFrame.SID);
467
      }
468
      // identify and handle abort messgaes
469
      if (canRxFrame.SID == SSSP_STACKINIT_CANMSGID_ABORT) {
470
        stage = STAGE_3_4_ABORT;
471
      }
472
      // warn if a unexpected message was received
473
      else if ((canRxFrame.SID != SSSP_STACKINIT_CANMSGID_INIT) &&
474
               (canRxFrame.SID != SSSP_STACKINIT_CANMSGID_MODULEID)) {
475
        aosDbgPrintf("WARN: unknown msg\n");
476
      }
477
      // any further pending messages are fetched at the end of the loop
478
    }
479
    // if a timeout event occurred
480 9461fadc Thomas Schöpping
    if (eventmask & eventListenerTimeout.events) {
481 1d3e002f Thomas Schöpping
      aosDbgPrintf("timeout evt\n");
482 933df08e Thomas Schöpping
      // is handled at the end of the loop (or must be cleared by FSM)
483
    }
484
    // if a delay event occurred
485 9461fadc Thomas Schöpping
    if (eventmask & eventListenerDelay.events) {
486 1d3e002f Thomas Schöpping
      aosDbgPrintf("delay evt\n");
487 933df08e Thomas Schöpping
      // is handled by FSM
488
    }
489
490
    /*
491
     * this is the actual FSM
492
     */
493
    switch (stage) {
494
      case STAGE_3_1:
495
      {
496
        aos.sssp.stage = AOS_SSSP_STARTUP_3_1;
497
498
        // there was no event at all (skipped wfe)
499
        if (eventmask == 0 && flags.wfe == false) {
500
#if (AMIROOS_CFG_SSSP_MASTER == true)
501
          // initialize the stage by transmitting an according CAN message
502
          aosDbgPrintf("CAN -> init\n");
503
          canTxFrame.DLC = 0;
504
          canTxFrame.SID = SSSP_STACKINIT_CANMSGID_INIT;
505
          if (canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_IMMEDIATE) != MSG_OK) {
506
            chEvtBroadcast(&eventSourceTimeout);
507
            break;
508
          }
509
          // activate S
510 1d3e002f Thomas Schöpping
          aosDbgPrintf("enabling S\n");
511 933df08e Thomas Schöpping
          apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_ON);
512
#if (AMIROOS_CFG_SSSP_STACK_START == true)
513
          // proceed immediately
514
          stage = STAGE_3_2;
515
          flags.wfe_next = false;
516
#else
517
          // set the timeout timer
518 1e5f7648 Thomas Schöpping
          chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
519 933df08e Thomas Schöpping
          // proceed
520
          stage = STAGE_3_3_WAITFORFIRSTID;
521
#endif
522
#else
523
          // set the timeout timer
524 1e5f7648 Thomas Schöpping
          chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
525 933df08e Thomas Schöpping
#endif
526
        }
527
528
#if (AMIROOS_CFG_SSSP_MASTER != true)
529
        // a CAN message was received
530 9461fadc Thomas Schöpping
        else if (eventmask & eventListenerCan.events) {
531 933df08e Thomas Schöpping
          // if an initiation message was received
532
          if (canRxFrame.DLC == 0 &&
533
              canRxFrame.RTR == CAN_RTR_DATA &&
534
              canRxFrame.IDE == CAN_IDE_STD &&
535
              canRxFrame.SID == SSSP_STACKINIT_CANMSGID_INIT) {
536
            aosDbgPrintf("init msg\n");
537
            // reset the timeout timer and clear pending flags
538
            chVTReset(&timerTimeout);
539 9461fadc Thomas Schöpping
            chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
540
            eventmask &= ~(eventListenerTimeout.events);
541 933df08e Thomas Schöpping
            // activate S
542 1d3e002f Thomas Schöpping
            aosDbgPrintf("enabling S\n");
543 933df08e Thomas Schöpping
            apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_ON);
544
#if (AMIROOS_CFG_SSSP_STACK_START == true)
545
            // proceed
546
            stage = STAGE_3_2;
547
            flags.wfe_next = false;
548
#else
549
            // set the timeout timer
550 1e5f7648 Thomas Schöpping
            chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
551 933df08e Thomas Schöpping
            // proceed
552
            stage = STAGE_3_3_WAITFORFIRSTID;
553
#endif
554
          }
555
        }
556
#endif
557
558
        break;
559
      } /* end of STAGE_3_1 */
560
561
      case STAGE_3_2:
562
      {
563
#if (AMIROOS_CFG_SSSP_STACK_START == true)
564
        aos.sssp.stage = AOS_SSSP_STARTUP_3_2;
565
566
        // if this stage was just entered
567
        if (flags.wfe == false) {
568
          // set the module ID
569
          aos.sssp.moduleId = 1;
570
          // broadcast module ID
571
          aosDbgPrintf("CAN -> ID (%u)\n", aos.sssp.moduleId);
572
          canTxFrame.DLC = 4;
573
          canTxFrame.SID = SSSP_STACKINIT_CANMSGID_MODULEID;
574 9461fadc Thomas Schöpping
          _serialize(canTxFrame.data8, aos.sssp.moduleId, 4);
575 933df08e Thomas Schöpping
          if (canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_IMMEDIATE) != MSG_OK) {
576
            chEvtBroadcast(&eventSourceTimeout);
577
            break;
578
          }
579
#if (AMIROOS_CFG_SSSP_STACK_START != true) || (AMIROOS_CFG_DBG == true)
580
          lastid = aos.sssp.moduleId;
581
#endif
582
#if (AMIROOS_CFG_SSSP_STACK_END == true)
583
          // sequence is already over
584
          // deactivate S
585 1d3e002f Thomas Schöpping
          aosDbgPrintf("disabling S\n");
586 933df08e Thomas Schöpping
          apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
587
          // proceed
588
          stage = STAGE_3_3_WAITFORID;
589
#else
590
          // set the delay timer so the UP signal is activated later
591 1e5f7648 Thomas Schöpping
          chVTSet(&timerDelay, chTimeUS2I(AMIROOS_CFG_SSSP_SIGNALDELAY), _ssspTimerCallback, &eventSourceDelay);
592 933df08e Thomas Schöpping
#endif
593
        }
594
595
        // if a delay event occurred
596 9461fadc Thomas Schöpping
        if (eventmask & eventListenerDelay.events) {
597 933df08e Thomas Schöpping
          // activate UP
598 1d3e002f Thomas Schöpping
          aosDbgPrintf("enabling UP\n");
599 933df08e Thomas Schöpping
          apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_ON);
600
          // deactivate S
601 1d3e002f Thomas Schöpping
          aosDbgPrintf("disabling S\n");
602 933df08e Thomas Schöpping
          apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
603
          // explicitely clear timeout event flag
604 9461fadc Thomas Schöpping
          chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
605
          eventmask &= ~(eventListenerTimeout.events);
606 933df08e Thomas Schöpping
          // proceed
607
          stage = STAGE_3_3_WAITFORID;
608
        }
609
#endif
610
611
        break;
612
      } /* end of STAGE_3_2 */
613
614
      case STAGE_3_3_WAITFORFIRSTID:
615
      {
616
#if (AMIROOS_CFG_SSSP_STACK_START != true)
617
        aos.sssp.stage = AOS_SSSP_STARTUP_3_3;
618
619
        // a CAN message was received
620 9461fadc Thomas Schöpping
        if (eventmask & eventListenerCan.events) {
621 933df08e Thomas Schöpping
          // if an ID message was received
622
          if (canRxFrame.DLC == 4 &&
623
              canRxFrame.RTR == CAN_RTR_DATA &&
624
              canRxFrame.IDE == CAN_IDE_STD &&
625
              canRxFrame.SID == SSSP_STACKINIT_CANMSGID_MODULEID) {
626 9461fadc Thomas Schöpping
            aosDbgPrintf("ID (%u)\n", (uint32_t)_deserialize(canRxFrame.data8, 4));
627 933df08e Thomas Schöpping
            // validate received ID
628 9461fadc Thomas Schöpping
            if (lastid < _deserialize(canRxFrame.data8, 4)) {
629 933df08e Thomas Schöpping
              // store received ID
630 9461fadc Thomas Schöpping
              lastid = _deserialize(canRxFrame.data8, 4);
631 933df08e Thomas Schöpping
              // restart timeout timer
632 1e5f7648 Thomas Schöpping
              chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
633 933df08e Thomas Schöpping
              // proceed
634
              stage = STAGE_3_3_WAITFORIDORSIG;
635
            } else {
636
              aosDbgPrintf("ERR: invalid ID\n");
637
              // abort
638
              stage = STAGE_3_4_ABORT_ACTIVE;
639
              flags.wfe_next = false;
640
            }
641
            // explicitely clear timeout event flag
642 9461fadc Thomas Schöpping
            chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
643
            eventmask &= ~(eventListenerTimeout.events);
644 933df08e Thomas Schöpping
          }
645
        }
646
#endif
647
        break;
648
      } /* end of STAGE_3_3_WAITFORFIRSTID */
649
650
      case STAGE_3_3_WAITFORIDORSIG:
651
      {
652
#if (AMIROOS_CFG_SSSP_STACK_START != true)
653
        aos.sssp.stage = AOS_SSSP_STARTUP_3_3;
654
655
        // a CAN message was received
656 9461fadc Thomas Schöpping
        if (eventmask & eventListenerCan.events) {
657 933df08e Thomas Schöpping
          // if an ID message was received
658
          if (canRxFrame.DLC == 4 &&
659
              canRxFrame.RTR == CAN_RTR_DATA &&
660
              canRxFrame.IDE == CAN_IDE_STD &&
661
              canRxFrame.SID == SSSP_STACKINIT_CANMSGID_MODULEID) {
662 9461fadc Thomas Schöpping
            aosDbgPrintf("ID (%u)\n", (uint32_t)_deserialize(canRxFrame.data8, 4));
663 933df08e Thomas Schöpping
            // validate received ID
664 9461fadc Thomas Schöpping
            if (lastid < _deserialize(canRxFrame.data8, 4)) {
665 933df08e Thomas Schöpping
              // store received ID
666 9461fadc Thomas Schöpping
              lastid = _deserialize(canRxFrame.data8, 4);
667 933df08e Thomas Schöpping
              // restart timeout timer
668 1e5f7648 Thomas Schöpping
              chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
669 933df08e Thomas Schöpping
            } else {
670
              aosDbgPrintf("ERR: invalid ID\n");
671
              // abort
672
              stage = STAGE_3_4_ABORT_ACTIVE;
673
              flags.wfe_next = false;
674
            }
675
            // explicitely clear timeout event flag
676 9461fadc Thomas Schöpping
            chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
677
            eventmask &= ~(eventListenerTimeout.events);
678 933df08e Thomas Schöpping
          }
679
        }
680
681
        // if an IO event was received (DN signal)
682 9461fadc Thomas Schöpping
        if ((eventmask & _eventListenerIO.events) && (ioflags & MODULE_SSSP_EVENTFLAGS_DN)) {
683 1d3e002f Thomas Schöpping
          aosDbgPrintf("DN evt\n");
684 933df08e Thomas Schöpping
          // reset timeout timer
685
          chVTReset(&timerTimeout);
686 9461fadc Thomas Schöpping
          chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
687
          eventmask &= ~(eventListenerTimeout.events);
688 933df08e Thomas Schöpping
          // increment and broadcast ID
689
          aos.sssp.moduleId = lastid + 1;
690
          aosDbgPrintf("CAN -> ID (%u)\n", aos.sssp.moduleId);
691
          canTxFrame.DLC = 4;
692
          canTxFrame.SID = SSSP_STACKINIT_CANMSGID_MODULEID;
693 9461fadc Thomas Schöpping
          _serialize(canTxFrame.data8, aos.sssp.moduleId, 4);
694 933df08e Thomas Schöpping
          if (canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_IMMEDIATE) != MSG_OK) {
695
            chEvtBroadcast(&eventSourceTimeout);
696
            break;
697
          }
698
          // set delay timer
699 1e5f7648 Thomas Schöpping
          chVTSet(&timerDelay, chTimeUS2I(AMIROOS_CFG_SSSP_SIGNALDELAY), _ssspTimerCallback, &eventSourceDelay);
700 933df08e Thomas Schöpping
        }
701
702
        // if a delay event occurred
703 9461fadc Thomas Schöpping
        if (eventmask & eventListenerDelay.events) {
704 933df08e Thomas Schöpping
#if (AMIROOS_CFG_SSSP_STACK_END != true)
705
          // activate UP
706 1d3e002f Thomas Schöpping
          aosDbgPrintf("enabling UP\n");
707 933df08e Thomas Schöpping
          apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_ON);
708
#endif
709
          // deactivate S
710 1d3e002f Thomas Schöpping
          aosDbgPrintf("disabling S\n");
711 933df08e Thomas Schöpping
          apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
712
          // reset the timeout timer
713 1e5f7648 Thomas Schöpping
          chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
714 9461fadc Thomas Schöpping
          chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
715
          eventmask &= ~(eventListenerTimeout.events);
716 933df08e Thomas Schöpping
          // proceed
717
          stage = STAGE_3_3_WAITFORID;
718
        }
719
#endif
720
721
        break;
722
      } /* end of STAGE_3_3_WAITFORIDORSIG */
723
724
      case STAGE_3_3_WAITFORID:
725
      {
726
        aos.sssp.stage = AOS_SSSP_STARTUP_3_3;
727
728
#if (AMIROOS_CFG_SSSP_STACK_END != true)
729
        // a CAN message was received
730 9461fadc Thomas Schöpping
        if (eventmask & eventListenerCan.events) {
731 933df08e Thomas Schöpping
          // if an ID message was received
732
          if (canRxFrame.DLC == 4 &&
733
              canRxFrame.RTR == CAN_RTR_DATA &&
734
              canRxFrame.IDE == CAN_IDE_STD &&
735
              canRxFrame.SID == SSSP_STACKINIT_CANMSGID_MODULEID) {
736
#if (AMIROOS_CFG_SSSP_STACK_START != true) || (AMIROOS_CFG_DBG == true)
737
            // Plausibility of the received ID is not checked at this point but is done by other modules still in a previous stage.
738 9461fadc Thomas Schöpping
            lastid = _deserialize(canRxFrame.data8, 4);
739 933df08e Thomas Schöpping
            aosDbgPrintf("ID (%u)\n", lastid);
740
#endif
741
            // restart timeout timer
742 1e5f7648 Thomas Schöpping
            chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
743 9461fadc Thomas Schöpping
            chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
744
            eventmask &= ~(eventListenerTimeout.events);
745 933df08e Thomas Schöpping
          }
746
        }
747
#endif
748
749
        break;
750
      } /* end of STAGE_3_3_WAITFORID */
751
752
      case STAGE_3_4_FINISH:
753
      {
754
        aos.sssp.stage = AOS_SSSP_STARTUP_3_4;
755
756
        // if an IO event was received (S signal)
757 9461fadc Thomas Schöpping
        if ((eventmask & _eventListenerIO.events) && (ioflags & MODULE_SSSP_EVENTFLAGS_SYNC)) {
758 933df08e Thomas Schöpping
          // reset the timeout timer
759
          chVTReset(&timerTimeout);
760 9461fadc Thomas Schöpping
          chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
761
          eventmask &= ~(eventListenerTimeout.events);
762 933df08e Thomas Schöpping
          //set the delay timer
763 1e5f7648 Thomas Schöpping
          chVTSet(&timerDelay, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceDelay);
764 933df08e Thomas Schöpping
        }
765
766
        // if a CAN event was received
767 9461fadc Thomas Schöpping
        if (eventmask & eventListenerCan.events) {
768 933df08e Thomas Schöpping
          // if an abort message was received
769
          if (canRxFrame.SID == SSSP_STACKINIT_CANMSGID_ABORT) {
770
            aosDbgPrintf("abort msg\n");
771
            // reset the delay timer
772
            chVTReset(&timerDelay);
773 9461fadc Thomas Schöpping
            chEvtWaitAnyTimeout(eventListenerDelay.events, TIME_IMMEDIATE);
774
            eventmask &= ~(eventListenerDelay.events);
775 933df08e Thomas Schöpping
            // proceed
776
            stage = STAGE_3_4_ABORT;
777
          }
778
        }
779
780
        // if a delay timer event occurred
781 9461fadc Thomas Schöpping
        if (eventmask & eventListenerDelay.events) {
782 933df08e Thomas Schöpping
          aosDbgPrintf("sequence sucessful\n");
783
          // sequence finished sucessfully
784
          flags.loop = false;
785
        }
786
787
        break;
788
      } /* end of STAGE_3_4_FINISH */
789
790
      case STAGE_3_4_ABORT_ACTIVE:
791
      {
792
        aos.sssp.stage = AOS_SSSP_STARTUP_3_4;
793
794
        // emit abort message
795
        canTxFrame.DLC = 0;
796
        canTxFrame.SID = SSSP_STACKINIT_CANMSGID_ABORT;
797
        canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_INFINITE);
798
        aosDbgPrintf("CAN -> abort\n");
799
        // clear timeout flag
800 9461fadc Thomas Schöpping
        eventmask &= ~(eventListenerTimeout.events);
801 933df08e Thomas Schöpping
        // proceed immediately
802
        stage = STAGE_3_4_ABORT;
803
        flags.wfe_next = false;
804
        break;
805
      } /* end of STAGE_3_4_ABORT_ACTIVE */
806
807
      case STAGE_3_4_ABORT:
808
      {
809
        aos.sssp.stage = AOS_SSSP_STARTUP_3_4;
810
811
        // deactivate S
812 1d3e002f Thomas Schöpping
        aosDbgPrintf("disabling SYNC\n");
813 933df08e Thomas Schöpping
        apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
814
        // invalidate module ID
815
        aos.sssp.moduleId = 0;
816
817
        // if an IO event was received (S signal)
818 9461fadc Thomas Schöpping
        if ((eventmask & _eventListenerIO.events) && (ioflags & MODULE_SSSP_EVENTFLAGS_SYNC)) {
819 933df08e Thomas Schöpping
          aosDbgPrintf("sequence aborted\n");
820
          // exit the sequence
821
          flags.loop = false;
822
        }
823
824
        break;
825
      } /* end of STAGE_3_4_ABORT */
826 1d3e002f Thomas Schöpping
    } /* end of switch(stage) */
827 933df08e Thomas Schöpping
828
    // fetch pending CAN message (if any)
829 9461fadc Thomas Schöpping
    if ((eventmask & eventListenerCan.events) && (canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canRxFrame, TIME_IMMEDIATE) == MSG_OK)) {
830 933df08e Thomas Schöpping
      aosDbgPrintf("CAN <- 0x%03X\n", canRxFrame.SID);
831
      flags.wfe_next = false;
832
    }
833
834
    // handle unhandled timeout events
835 9461fadc Thomas Schöpping
    if (eventmask & eventListenerTimeout.events) {
836 933df08e Thomas Schöpping
      aosDbgPrintf("ERR: timeout evt\n");
837
      // abort
838
      flags.wfe_next = false;
839
      stage = STAGE_3_4_ABORT_ACTIVE;
840
    }
841
842
    // apply wfe value for next iteration
843
    flags.wfe = flags.wfe_next;
844 1e5f7648 Thomas Schöpping
  } /* end of FSM loop */
845 933df08e Thomas Schöpping
846
  // unregister all events (timeout, delay, CAN receive)
847
  chEvtUnregister(&eventSourceTimeout, &eventListenerTimeout);
848
  chEvtUnregister(&eventSourceDelay, &eventListenerDelay);
849
  chEvtUnregister(&MODULE_HAL_CAN.rxfull_event, &eventListenerCan);
850
  // clear any pending events (timeout, delay, CAN receive)
851
  chEvtWaitAllTimeout(TIMEOUTEVENT_MASK | DELAYEVENT_MASK | CANEVENT_MASK, TIME_IMMEDIATE);
852
853
  // reset all control signals
854
#if (AMIROOS_CFG_SSSP_STACK_END != true)
855 1d3e002f Thomas Schöpping
  aosDbgPrintf("disabling UP\n");
856 933df08e Thomas Schöpping
  apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_OFF);
857
#endif
858 1d3e002f Thomas Schöpping
  aosDbgPrintf("disabling S\n");
859 933df08e Thomas Schöpping
  apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
860 1d3e002f Thomas Schöpping
  aosSysGetUptime(&uptime);
861
  aosDbgPrintf("done\t%04ums\n", (uint32_t)(uptime / MICROSECONDS_PER_MILLISECOND));
862 933df08e Thomas Schöpping
863
  return shutdown;
864
}
865 9ebb11a9 Thomas Schöpping
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
866 933df08e Thomas Schöpping
867
/**
868 e545e620 Thomas Schöpping
 * @brief   Application entry point.
869
 */
870
int main(void)
871
{
872
  // local variables
873
  eventmask_t eventmask = 0;
874
  eventflags_t eventflags = 0;
875 e2377d9e Thomas Schöpping
  eventflags_t ioeventflagsmask = AMIROOS_CFG_MAIN_LOOP_IOEVENT_MASK;
876 e545e620 Thomas Schöpping
  aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE;
877 b6b45e4c Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES)
878
  AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES
879 e545e620 Thomas Schöpping
#endif
880
881
  /*
882
   * ##########################################################################
883
   * # system initialization                                                  #
884
   * ##########################################################################
885
   */
886
887
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_0)
888 b6b45e4c Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_0_ARGS)
889
  AMIROOS_CFG_MAIN_INIT_HOOK_0(AMIROOS_CFG_MAIN_INIT_HOOK_0_ARGS);
890
#else
891 e545e620 Thomas Schöpping
  AMIROOS_CFG_MAIN_INIT_HOOK_0();
892
#endif
893 b6b45e4c Thomas Schöpping
#endif
894 e545e620 Thomas Schöpping
895
  /* hardware, kernel, and operating system initialization */
896
  // ChibiOS/HAL and custom hal additions (if any)
897
  halInit();
898
#ifdef MODULE_INIT_HAL_EXTRA
899
  MODULE_INIT_HAL_EXTRA();
900
#endif
901
902
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1)
903
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS)
904
  AMIROOS_CFG_MAIN_INIT_HOOK_1(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS);
905
#else
906
  AMIROOS_CFG_MAIN_INIT_HOOK_1();
907
#endif
908
#endif
909
910
  // ChibiOS/RT kernel and custom kernel additions (if any)
911
  chSysInit();
912
#ifdef MODULE_INIT_KERNEL_EXTRA
913
  MODULE_INIT_KERNEL_EXTRA();
914
#endif
915
916
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2)
917
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS)
918
  AMIROOS_CFG_MAIN_INIT_HOOK_2(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS);
919
#else
920
  AMIROOS_CFG_MAIN_INIT_HOOK_2();
921
#endif
922
#endif
923
924
  // AMiRo-OS and custom OS additions (if any)
925 2dd2e257 Thomas Schöpping
#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true)
926 6b53f6bf Thomas Schöpping
  aosSysInit(moduleShellPrompt);
927
#else
928
  aosSysInit();
929
#endif
930 e545e620 Thomas Schöpping
#ifdef MODULE_INIT_OS_EXTRA
931
  MODULE_INIT_OS_EXTRA();
932
#endif
933
934
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3)
935
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS)
936
  AMIROOS_CFG_MAIN_INIT_HOOK_3(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS);
937
#else
938
  AMIROOS_CFG_MAIN_INIT_HOOK_3();
939
#endif
940
#endif
941
942
  /* event associations */
943 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
944 e2377d9e Thomas Schöpping
  ioeventflagsmask |= MODULE_SSSP_EVENTFLAGS_PD | MODULE_SSSP_EVENTFLAGS_SYNC;
945 2920c6b7 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_STACK_START != true)
946 e2377d9e Thomas Schöpping
  ioeventflagsmask |= MODULE_SSSP_EVENTFLAGS_DN;
947 2920c6b7 Thomas Schöpping
#endif
948
#if (AMIROOS_CFG_SSSP_STACK_END != true)
949 e2377d9e Thomas Schöpping
  ioeventflagsmask |= MODULE_SSSP_EVENTFLAGS_UP;
950 1e5f7648 Thomas Schöpping
#endif
951 1d3e002f Thomas Schöpping
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
952 e2377d9e Thomas Schöpping
  if (ioeventflagsmask != 0) {
953
    chEvtRegisterMaskWithFlags(&aos.events.io, &_eventListenerIO, IOEVENT_MASK, ioeventflagsmask);
954 2920c6b7 Thomas Schöpping
  }
955 6b53f6bf Thomas Schöpping
  chEvtRegisterMask(&aos.events.os, &_eventListenerOS, OSEVENT_MASK);
956 e545e620 Thomas Schöpping
957 7de40828 Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4)
958
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS)
959
  AMIROOS_CFG_MAIN_INIT_HOOK_4(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS);
960 e545e620 Thomas Schöpping
#else
961 7de40828 Thomas Schöpping
  AMIROOS_CFG_MAIN_INIT_HOOK_4();
962 e545e620 Thomas Schöpping
#endif
963
#endif
964
965
  /* periphery communication initialization */
966
  // module specific initialization (if any)
967
#ifdef MODULE_INIT_PERIPHERY_COMM
968
  MODULE_INIT_PERIPHERY_COMM();
969
#endif
970 7de40828 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
971
  // CAN (mandatory)
972
  canStart(&MODULE_HAL_CAN, &moduleHalCanConfig);
973
#endif
974 e545e620 Thomas Schöpping
  // user interface (if any)
975
#ifdef MODULE_HAL_PROGIF
976 ba516b61 Thomas Schöpping
  aosIOChannelInit(&_stdiochannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
977
  aosIOChannelOutputEnable(&_stdiochannel);
978
  aosIOStreamAddChannel(&aos.iostream, &_stdiochannel);
979 2dd2e257 Thomas Schöpping
#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true)
980 dd8738ea Thomas Schöpping
  aosShellChannelInit(&_stdshellchannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
981 ba516b61 Thomas Schöpping
  aosShellChannelInputEnable(&_stdshellchannel);
982
  aosShellChannelOutputEnable(&_stdshellchannel);
983 6b53f6bf Thomas Schöpping
  aosShellStreamAddChannel(&aos.shell.stream, &_stdshellchannel);
984 ba516b61 Thomas Schöpping
#endif
985 e545e620 Thomas Schöpping
#endif
986
987 7de40828 Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5)
988
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS)
989
  AMIROOS_CFG_MAIN_INIT_HOOK_5(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS);
990 e545e620 Thomas Schöpping
#else
991 7de40828 Thomas Schöpping
  AMIROOS_CFG_MAIN_INIT_HOOK_5();
992 e545e620 Thomas Schöpping
#endif
993
#endif
994
995
  /* module is ready -> print welcome prompt */
996
  aosprintf("\n");
997
  aosprintf("######################################################################\n");
998
  aosprintf("# AMiRo-OS is an operating system designed for the Autonomous Mini   #\n");
999
  aosprintf("# Robot (AMiRo) platform.                                            #\n");
1000 84f0ce9e Thomas Schöpping
  aosprintf("# Copyright (C) 2016..2019  Thomas Schöpping et al.                  #\n");
1001 e545e620 Thomas Schöpping
  aosprintf("#                                                                    #\n");
1002
  aosprintf("# This is free software; see the source for copying conditions.      #\n");
1003
  aosprintf("# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR  #\n");
1004
  aosprintf("# A PARTICULAR PURPOSE.                                              #\n");
1005
  aosprintf("# The development of this software was supported by the Excellence   #\n");
1006
  aosprintf("# Cluster EXC 227 Cognitive Interaction Technology. The Excellence   #\n");
1007
  aosprintf("# Cluster EXC 227 is a grant of the Deutsche Forschungsgemeinschaft  #\n");
1008
  aosprintf("# (DFG) in the context of the German Excellence Initiative.          #\n");
1009
  aosprintf("######################################################################\n");
1010
  aosprintf("\n");
1011
1012 7de40828 Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_6)
1013
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS)
1014
  AMIROOS_CFG_MAIN_INIT_HOOK_6(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS);
1015
#else
1016
  AMIROOS_CFG_MAIN_INIT_HOOK_6();
1017
#endif
1018
#endif
1019
1020
#if (AMIROOS_CFG_TESTS_ENABLE == true)
1021
#if defined(MODULE_INIT_TESTS)
1022
  MODULE_INIT_TESTS();
1023
#else
1024
  #warning "MODULE_INIT_TESTS() not defined"
1025
#endif
1026
#endif
1027
1028 e545e620 Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_7)
1029
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS)
1030
  AMIROOS_CFG_MAIN_INIT_HOOK_7(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS);
1031
#else
1032
  AMIROOS_CFG_MAIN_INIT_HOOK_7();
1033
#endif
1034
#endif
1035
1036 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1037 6b53f6bf Thomas Schöpping
  /* SSSP startup OS synchronization phase (end of startup stage 2) */
1038 9461fadc Thomas Schöpping
  while ((shutdown == AOS_SHUTDOWN_NONE) && (eventmask = aosSysSsspStartupOsInitSyncCheck(&_eventListenerIO)) != 0) {
1039 e545e620 Thomas Schöpping
    /*
1040
     * This code is executed if the received event was not about the SYS_SYNC control signal.
1041
     * The returned event could be caused by any listener (not only the argument).
1042
     */
1043 9461fadc Thomas Schöpping
    // IO event
1044 e545e620 Thomas Schöpping
    if (eventmask & _eventListenerIO.events) {
1045
      eventflags = chEvtGetAndClearFlags(&_eventListenerIO);
1046 9461fadc Thomas Schöpping
      // PD event
1047
      if (eventflags & MODULE_SSSP_EVENTFLAGS_PD) {
1048
        shutdown = AOS_SHUTDOWN_PASSIVE;
1049
      } else {
1050 933df08e Thomas Schöpping
#ifdef MODULE_SSSP_STARTUP_2_2_IOEVENT_HOOK
1051 9461fadc Thomas Schöpping
        MODULE_SSSP_STARTUP_2_2_IOEVENT_HOOK(eventmask, eventflags);
1052 e545e620 Thomas Schöpping
#else
1053 9461fadc Thomas Schöpping
        // ignore any other IO events
1054 e545e620 Thomas Schöpping
#endif
1055 9461fadc Thomas Schöpping
      }
1056 e545e620 Thomas Schöpping
    }
1057 9461fadc Thomas Schöpping
    // OS event
1058 e545e620 Thomas Schöpping
    else if (eventmask & _eventListenerOS.events) {
1059
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
1060
      _unexpectedEventError(eventmask, eventflags);
1061
    }
1062 9461fadc Thomas Schöpping
    // unknown event
1063 e545e620 Thomas Schöpping
    else {
1064 933df08e Thomas Schöpping
      _unexpectedEventError(eventmask, 0);
1065 e545e620 Thomas Schöpping
    }
1066
  }
1067
1068 933df08e Thomas Schöpping
  /*
1069
   * There must be no delays at this point, thus no hook is allowed.
1070
   */
1071
1072
  /* SSSP startup stage 3 (module stack initialization) */
1073 9461fadc Thomas Schöpping
  if (shutdown == AOS_SHUTDOWN_NONE) {
1074
    shutdown = _ssspModuleStackInitialization();
1075
  }
1076 9ebb11a9 Thomas Schöpping
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
1077 9461fadc Thomas Schöpping
1078
  /*
1079
   * There must be no delays at this point, thus no hook is allowed.
1080
   */
1081
1082 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1083
  /* synchronize calendars */
1084 9461fadc Thomas Schöpping
  if (shutdown == AOS_SHUTDOWN_NONE) {
1085
#if (AMIROOS_CFG_SSSP_MASTER == true)
1086
    CANTxFrame frame;
1087
    struct tm t;
1088
    uint64_t encoded;
1089
1090
    frame.DLC = 8;
1091
    frame.RTR = CAN_RTR_DATA;
1092
    frame.IDE = CAN_IDE_STD;
1093
    frame.SID = CALENDERSYNC_CANMSGID;
1094
1095
    aosDbgPrintf("transmitting current date/time...\t");
1096
    // get current date & time
1097
    aosSysGetDateTime(&t);
1098
    // encode
1099
    encoded = _TM2U64(&t);
1100
    // serialize
1101
    _serialize(frame.data8, encoded, 8);
1102
    // transmit
1103
    canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &frame, TIME_IMMEDIATE);
1104
1105
    aosDbgPrintf("done\n");
1106 9ebb11a9 Thomas Schöpping
#else /* AMIROOS_CFG_SSSP_MASTER == false */
1107 9461fadc Thomas Schöpping
    CANRxFrame frame;
1108
    uint64_t encoded;
1109
    struct tm t;
1110
1111
    aosDbgPrintf("receiving current date/time...\t");
1112
    // receive message
1113 0278542c Thomas Schöpping
#if (AMIROOS_CFG_DBG == true)
1114
    // increase timeout in debug mode due to additional delays introduced by the many prinf() calls
1115
    if (canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &frame, chTimeUS2I(10 * AOS_SYSTEM_SSSP_TIMEOUT)) == MSG_OK) {
1116
#else
1117 1e5f7648 Thomas Schöpping
    if (canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &frame, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT)) == MSG_OK) {
1118 0278542c Thomas Schöpping
#endif
1119 9461fadc Thomas Schöpping
      // validate message
1120
      if (frame.DLC == 8 &&
1121
          frame.RTR == CAN_RTR_DATA &&
1122
          frame.IDE == CAN_IDE_STD &&
1123
          frame.SID == CALENDERSYNC_CANMSGID) {
1124
        // deserialize
1125
        encoded = _deserialize(frame.data8, 8);
1126
        // decode
1127
        _U642TM(&t, encoded);
1128
        // set current date & time
1129
        aosSysSetDateTime(&t);
1130
        aosDbgPrintf("success\n");
1131
      } else {
1132
        aosDbgPrintf("fail (invalid message)\n");
1133
      }
1134
    } else {
1135
      aosDbgPrintf("fail (timeout)\n");
1136
    }
1137 9ebb11a9 Thomas Schöpping
#endif /* AMIROOS_CFG_SSSP_MASTER == false */
1138 9461fadc Thomas Schöpping
    aosDbgPrintf("\n");
1139
  }
1140 9ebb11a9 Thomas Schöpping
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
1141 933df08e Thomas Schöpping
1142 e545e620 Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_8)
1143
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS)
1144
  AMIROOS_CFG_MAIN_INIT_HOOK_8(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS);
1145
#else
1146
  AMIROOS_CFG_MAIN_INIT_HOOK_8();
1147
#endif
1148
#endif
1149
1150
  /* completely start AMiRo-OS */
1151 933df08e Thomas Schöpping
  if (shutdown == AOS_SHUTDOWN_NONE) {
1152
    aosSysStart();
1153
  }
1154 e545e620 Thomas Schöpping
1155
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_9)
1156
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS)
1157
  AMIROOS_CFG_MAIN_INIT_HOOK_9(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS);
1158
#else
1159
  AMIROOS_CFG_MAIN_INIT_HOOK_9();
1160
#endif
1161
#endif
1162
1163
  /*
1164
   * ##########################################################################
1165
   * # infinite loop                                                          #
1166
   * ##########################################################################
1167
   */
1168
1169
  // sleep until a shutdown event is received
1170
  while (shutdown == AOS_SHUTDOWN_NONE) {
1171
    // wait for an event
1172
#if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0)
1173 1e5f7648 Thomas Schöpping
    eventmask = chEvtWaitOneTimeout(ALL_EVENTS, chTimeUS2I(AMIROOS_CFG_MAIN_LOOP_TIMEOUT));
1174 e545e620 Thomas Schöpping
#else
1175
    eventmask = chEvtWaitOne(ALL_EVENTS);
1176
#endif
1177
1178
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0)
1179
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS)
1180
    AMIROOS_CFG_MAIN_LOOP_HOOK_0(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS);
1181
#else
1182
    AMIROOS_CFG_MAIN_LOOP_HOOK_0();
1183
#endif
1184
#endif
1185
1186
    switch (eventmask) {
1187
      // if this was an I/O event
1188 6b53f6bf Thomas Schöpping
      case IOEVENT_MASK:
1189 e545e620 Thomas Schöpping
        // evaluate flags
1190
        eventflags = chEvtGetAndClearFlags(&_eventListenerIO);
1191 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1192 e545e620 Thomas Schöpping
        // PD event
1193 6b53f6bf Thomas Schöpping
        if (eventflags & MODULE_SSSP_EVENTFLAGS_PD) {
1194 e545e620 Thomas Schöpping
          shutdown = AOS_SHUTDOWN_PASSIVE;
1195
        }
1196
        // all other events
1197
#ifdef MODULE_MAIN_LOOP_IO_EVENT
1198
        else {
1199 0963cf6c Thomas Schöpping
          MODULE_MAIN_LOOP_IO_EVENT(eventflags);
1200 e545e620 Thomas Schöpping
        }
1201
#endif
1202 41fc7088 Thomas Schöpping
#else /* AMIROOS_CFG_SSSP_ENABLE == false */
1203
#ifdef MODULE_MAIN_LOOP_IO_EVENT
1204 0963cf6c Thomas Schöpping
        MODULE_MAIN_LOOP_IO_EVENT(eventflags);
1205 41fc7088 Thomas Schöpping
#endif
1206
#endif /* AMIROOS_CFG_SSSP_ENABLE */
1207 e545e620 Thomas Schöpping
        break;
1208
1209
      // if this was an OS event
1210 6b53f6bf Thomas Schöpping
      case OSEVENT_MASK:
1211 e545e620 Thomas Schöpping
        // evaluate flags
1212
        eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
1213
        switch (eventflags) {
1214 1d3e002f Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1215 e545e620 Thomas Schöpping
          case AOS_SYSTEM_EVENTFLAGS_HIBERNATE:
1216
            shutdown = AOS_SHUTDOWN_HIBERNATE;
1217
            break;
1218
          case AOS_SYSTEM_EVENTFLAGS_DEEPSLEEP:
1219
            shutdown = AOS_SHUTDOWN_DEEPSLEEP;
1220
            break;
1221
          case AOS_SYSTEM_EVENTFLAGS_TRANSPORTATION:
1222
            shutdown = AOS_SHUTDOWN_TRANSPORTATION;
1223
            break;
1224
          case AOS_SYSTEM_EVENTFLAGS_RESTART:
1225
            shutdown = AOS_SHUTDOWN_RESTART;
1226
            break;
1227 1d3e002f Thomas Schöpping
#else /* AMIROOS_CFG_SSSP_ENABLE == false */
1228
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN:
1229
            shutdown = AOS_SHUTDOWN_DEFAULT;
1230
            break;
1231
#endif /* AMIROOS_CFG_SSSP_ENABLE */
1232 e545e620 Thomas Schöpping
          default:
1233
            _unexpectedEventError(eventmask, eventflags);
1234
            break;
1235
        }
1236
        break;
1237
1238
      // if this was any other event (should be impossible to occur)
1239
      default:
1240
        eventflags = 0;
1241 41fc7088 Thomas Schöpping
#if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT == 0)
1242 e545e620 Thomas Schöpping
        _unexpectedEventError(eventmask, eventflags);
1243 41fc7088 Thomas Schöpping
#endif
1244 e545e620 Thomas Schöpping
        break;
1245
    }
1246
1247
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1)
1248
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS)
1249
    AMIROOS_CFG_MAIN_LOOP_HOOK_1(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS);
1250
#else
1251
    AMIROOS_CFG_MAIN_LOOP_HOOK_1();
1252
#endif
1253
#endif
1254
  }
1255
1256
  /*
1257
   * ##########################################################################
1258
   * # system shutdown                                                        #
1259
   * ##########################################################################
1260
   */
1261
1262
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0)
1263
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS)
1264
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS);
1265
#else
1266
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0();
1267
#endif
1268
#endif
1269
1270
  // initialize/acknowledge shutdown
1271
  aosSysShutdownInit(shutdown);
1272
1273
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1)
1274
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS)
1275
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS);
1276
#else
1277
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1();
1278
#endif
1279
#endif
1280
1281
  // stop system threads
1282
  aosSysStop();
1283
1284
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2)
1285
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS)
1286
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS);
1287
#else
1288
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2();
1289
#endif
1290
#endif
1291
1292
  // deinitialize system
1293
  aosSysDeinit();
1294
1295
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3)
1296
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS)
1297
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS);
1298
#else
1299
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3();
1300
#endif
1301
#endif
1302
1303
  /* stop all periphery communication */
1304 90e99d44 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1305 e545e620 Thomas Schöpping
  // CAN (mandatory)
1306
  canStop(&MODULE_HAL_CAN);
1307 90e99d44 Thomas Schöpping
#endif
1308 e545e620 Thomas Schöpping
#ifdef MODULE_SHUTDOWN_PERIPHERY_COMM
1309
  MODULE_SHUTDOWN_PERIPHERY_COMM();
1310
#endif
1311
1312
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4)
1313
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS)
1314
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS);
1315
#else
1316
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4();
1317
#endif
1318
#endif
1319
1320
  // finally hand over to bootloader
1321 1e5f7648 Thomas Schöpping
  aosSysShutdownFinal(shutdown);
1322 e545e620 Thomas Schöpping
1323
  /*
1324
   * ##########################################################################
1325
   * # after shutdown/restart                                                 #
1326
   * ##########################################################################
1327
   *
1328
   * NOTE: This code will not be executed, since the bootloader callbacks will stop/restart the MCU.
1329
   *       It is included nevertheless for the sake of completeness and to explicitely indicate,
1330
   *       which subsystems should NOT be shut down.
1331
   */
1332
1333
  // return an error, since this code should not be executed
1334
  return -1;
1335
}
1336 53710ca3 Marc Rothmann
1337
/** @} */