Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_main.cpp @ 90e99d44

History | View | Annotate | Download (44.894 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
#include <module.h>
31

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

    
39
/**
40
 * @brief   Event mask to identify I/O events.
41
 */
42
#define IOEVENT_MASK                            EVENT_MASK(0)
43

    
44
/**
45
 * @brief   Event mask to identify OS events.
46
 */
47
#define OSEVENT_MASK                            EVENT_MASK(1)
48

    
49
/**
50
 * @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
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
65

    
66
/**
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
#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

    
90
/**
91
 * @brief   CAN message identifier for calender synchronization message.
92
 */
93
#define CALENDERSYNC_CANMSGID                   0x004
94

    
95
/**
96
 * @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
 * @brief   I/O channel for the programmer interface.
108
 */
109
static AosIOChannel _stdiochannel;
110

    
111
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
112
/**
113
 * @brief   I/O shell channel for the programmer interface.
114
 */
115
static AosShellChannel _stdshellchannel;
116
#endif /* AMIROOS_CFG_SHELL_ENABLE == true */
117
#endif /* defined(MODULE_HAL_PROGIF) */
118

    
119
/*
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
/**
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
static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags)
133
{
134
#if (AMIROOS_CFG_DBG == true)
135
  aosprintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
136
#else
137
  (void)(mask);
138
  (void)(flags);
139
#endif
140
  return;
141
}
142

    
143
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
144
/**
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
  return;
158
}
159
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
160

    
161
/**
162
 * @brief   Helper function to serialize data.
163
 *
164
 * @param[out]  dst   Pointer to the output buffer.
165
 * @param[in]   src   Data to be serialized.
166
 * @param[in]   n     Number of bytes to serialize.
167
 */
168
inline void _serialize(uint8_t* dst, const uint64_t src, const uint8_t n)
169
{
170
  aosDbgCheck(dst != NULL);
171
  aosDbgCheck(n > 0 && n <= 8);
172

    
173
  for (uint8_t byte = 0; byte < n; ++byte) {
174
    dst[byte] = (uint8_t)((src >> (byte * 8)) & 0xFF);
175
  }
176

    
177
  return;
178
}
179

    
180
/**
181
 * @brief   Helper function to deserialize data.
182
 *
183
 * @param[in] src   Pointer to the buffer of data to be deserialzed.
184
 * @param[in] n     Number of bytes to deserialize.
185
 *
186
 * @return    The deserialized 32 bit data.
187
 */
188
inline uint64_t _deserialize(uint8_t* src, const uint8_t n)
189
{
190
  aosDbgCheck(src != NULL);
191
  aosDbgCheck(n > 0 && n <= 8);
192

    
193
  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
}
257

    
258
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
259
/**
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
  eventflags_t ioflags = 0;
294
  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
  aos_timestamp_t uptime;
308

    
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
        aosDbgPrintf(">>> 3-4 (active abort)\n");
380
        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
    // waiting for events (may be skipped)
391
    if (flags.wfe) {
392
      // wait for any event to occur
393
      aosDbgPrintf("WFE...");
394
      eventmask = chEvtWaitAnyTimeout(ALL_EVENTS, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT));
395
      aosDbgPrintf("\t0x%08X", eventmask);
396
    } else {
397
      aosDbgPrintf("WFE skipped");
398
      eventmask = 0;
399
    }
400
    aosSysGetUptime(&uptime);
401
    aosDbgPrintf("\t%04ums\n", (uint32_t)(uptime / MICROSECONDS_PER_MILLISECOND));
402

    
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
    if (eventmask & _eventListenerIO.events) {
415
      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
        aosDbgPrintf("disabling S\n");
422
        apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
423
#if (AMIROOS_CFG_SSSP_STACK_END != true)
424
        aosDbgPrintf("disabling UP\n");
425
        apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_OFF);
426
#endif
427
        // 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
        if (sstate == APAL_GPIO_ON) {
436
          aosDbgPrintf("S evt (enabled)\n");
437
        } else {
438
          aosDbgPrintf("S evt (disabled)\n");
439
          // 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
    if (eventmask & _eventListenerOS.events) {
450
      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
    if ((eventmask & eventListenerCan.events)) {
462
      aosDbgPrintf("CAN evt\n");
463
      // 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
    if (eventmask & eventListenerTimeout.events) {
481
      aosDbgPrintf("timeout evt\n");
482
      // is handled at the end of the loop (or must be cleared by FSM)
483
    }
484
    // if a delay event occurred
485
    if (eventmask & eventListenerDelay.events) {
486
      aosDbgPrintf("delay evt\n");
487
      // 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
          aosDbgPrintf("enabling S\n");
511
          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
          chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
519
          // proceed
520
          stage = STAGE_3_3_WAITFORFIRSTID;
521
#endif
522
#else
523
          // set the timeout timer
524
          chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
525
#endif
526
        }
527

    
528
#if (AMIROOS_CFG_SSSP_MASTER != true)
529
        // a CAN message was received
530
        else if (eventmask & eventListenerCan.events) {
531
          // 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
            chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
540
            eventmask &= ~(eventListenerTimeout.events);
541
            // activate S
542
            aosDbgPrintf("enabling S\n");
543
            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
            chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
551
            // 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
          _serialize(canTxFrame.data8, aos.sssp.moduleId, 4);
575
          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
          aosDbgPrintf("disabling S\n");
586
          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
          chVTSet(&timerDelay, chTimeUS2I(AMIROOS_CFG_SSSP_SIGNALDELAY), _ssspTimerCallback, &eventSourceDelay);
592
#endif
593
        }
594

    
595
        // if a delay event occurred
596
        if (eventmask & eventListenerDelay.events) {
597
          // activate UP
598
          aosDbgPrintf("enabling UP\n");
599
          apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_ON);
600
          // deactivate S
601
          aosDbgPrintf("disabling S\n");
602
          apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
603
          // explicitely clear timeout event flag
604
          chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
605
          eventmask &= ~(eventListenerTimeout.events);
606
          // 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
        if (eventmask & eventListenerCan.events) {
621
          // 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
            aosDbgPrintf("ID (%u)\n", (uint32_t)_deserialize(canRxFrame.data8, 4));
627
            // validate received ID
628
            if (lastid < _deserialize(canRxFrame.data8, 4)) {
629
              // store received ID
630
              lastid = _deserialize(canRxFrame.data8, 4);
631
              // restart timeout timer
632
              chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
633
              // 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
            chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
643
            eventmask &= ~(eventListenerTimeout.events);
644
          }
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
        if (eventmask & eventListenerCan.events) {
657
          // 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
            aosDbgPrintf("ID (%u)\n", (uint32_t)_deserialize(canRxFrame.data8, 4));
663
            // validate received ID
664
            if (lastid < _deserialize(canRxFrame.data8, 4)) {
665
              // store received ID
666
              lastid = _deserialize(canRxFrame.data8, 4);
667
              // restart timeout timer
668
              chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
669
            } 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
            chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
677
            eventmask &= ~(eventListenerTimeout.events);
678
          }
679
        }
680

    
681
        // if an IO event was received (DN signal)
682
        if ((eventmask & _eventListenerIO.events) && (ioflags & MODULE_SSSP_EVENTFLAGS_DN)) {
683
          aosDbgPrintf("DN evt\n");
684
          // reset timeout timer
685
          chVTReset(&timerTimeout);
686
          chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
687
          eventmask &= ~(eventListenerTimeout.events);
688
          // 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
          _serialize(canTxFrame.data8, aos.sssp.moduleId, 4);
694
          if (canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_IMMEDIATE) != MSG_OK) {
695
            chEvtBroadcast(&eventSourceTimeout);
696
            break;
697
          }
698
          // set delay timer
699
          chVTSet(&timerDelay, chTimeUS2I(AMIROOS_CFG_SSSP_SIGNALDELAY), _ssspTimerCallback, &eventSourceDelay);
700
        }
701

    
702
        // if a delay event occurred
703
        if (eventmask & eventListenerDelay.events) {
704
#if (AMIROOS_CFG_SSSP_STACK_END != true)
705
          // activate UP
706
          aosDbgPrintf("enabling UP\n");
707
          apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_ON);
708
#endif
709
          // deactivate S
710
          aosDbgPrintf("disabling S\n");
711
          apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
712
          // reset the timeout timer
713
          chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
714
          chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
715
          eventmask &= ~(eventListenerTimeout.events);
716
          // 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
        if (eventmask & eventListenerCan.events) {
731
          // 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
            lastid = _deserialize(canRxFrame.data8, 4);
739
            aosDbgPrintf("ID (%u)\n", lastid);
740
#endif
741
            // restart timeout timer
742
            chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
743
            chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
744
            eventmask &= ~(eventListenerTimeout.events);
745
          }
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
        if ((eventmask & _eventListenerIO.events) && (ioflags & MODULE_SSSP_EVENTFLAGS_SYNC)) {
758
          // reset the timeout timer
759
          chVTReset(&timerTimeout);
760
          chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
761
          eventmask &= ~(eventListenerTimeout.events);
762
          //set the delay timer
763
          chVTSet(&timerDelay, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceDelay);
764
        }
765

    
766
        // if a CAN event was received
767
        if (eventmask & eventListenerCan.events) {
768
          // 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
            chEvtWaitAnyTimeout(eventListenerDelay.events, TIME_IMMEDIATE);
774
            eventmask &= ~(eventListenerDelay.events);
775
            // proceed
776
            stage = STAGE_3_4_ABORT;
777
          }
778
        }
779

    
780
        // if a delay timer event occurred
781
        if (eventmask & eventListenerDelay.events) {
782
          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
        eventmask &= ~(eventListenerTimeout.events);
801
        // 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
        aosDbgPrintf("disabling SYNC\n");
813
        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
        if ((eventmask & _eventListenerIO.events) && (ioflags & MODULE_SSSP_EVENTFLAGS_SYNC)) {
819
          aosDbgPrintf("sequence aborted\n");
820
          // exit the sequence
821
          flags.loop = false;
822
        }
823

    
824
        break;
825
      } /* end of STAGE_3_4_ABORT */
826
    } /* end of switch(stage) */
827

    
828
    // fetch pending CAN message (if any)
829
    if ((eventmask & eventListenerCan.events) && (canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canRxFrame, TIME_IMMEDIATE) == MSG_OK)) {
830
      aosDbgPrintf("CAN <- 0x%03X\n", canRxFrame.SID);
831
      flags.wfe_next = false;
832
    }
833

    
834
    // handle unhandled timeout events
835
    if (eventmask & eventListenerTimeout.events) {
836
      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
  } /* end of FSM loop */
845

    
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
  aosDbgPrintf("disabling UP\n");
856
  apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_OFF);
857
#endif
858
  aosDbgPrintf("disabling S\n");
859
  apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
860
  aosSysGetUptime(&uptime);
861
  aosDbgPrintf("done\t%04ums\n", (uint32_t)(uptime / MICROSECONDS_PER_MILLISECOND));
862

    
863
  return shutdown;
864
}
865
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
866

    
867
/**
868
 * @brief   Application entry point.
869
 */
870
int main(void)
871
{
872
  // local variables
873
  eventmask_t eventmask = 0;
874
  eventflags_t eventflags = 0;
875
  eventflags_t ioeventflagsmask = AMIROOS_CFG_MAIN_LOOP_IOEVENT_MASK;
876
  aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE;
877
#if defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES)
878
  AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES
879
#endif
880

    
881
  /*
882
   * ##########################################################################
883
   * # system initialization                                                  #
884
   * ##########################################################################
885
   */
886

    
887
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_0)
888
#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
  AMIROOS_CFG_MAIN_INIT_HOOK_0();
892
#endif
893
#endif
894

    
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
#if (AMIROOS_CFG_SHELL_ENABLE == true)
926
  aosSysInit(moduleShellPrompt);
927
#else
928
  aosSysInit();
929
#endif
930
#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
#if (AMIROOS_CFG_TESTS_ENABLE == true)
943
#if defined(MODULE_INIT_TESTS)
944
  MODULE_INIT_TESTS();
945
#else
946
  #warning "MODULE_INIT_TESTS() not defined"
947
#endif
948
#endif
949

    
950
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4)
951
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS)
952
  AMIROOS_CFG_MAIN_INIT_HOOK_4(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS);
953
#else
954
  AMIROOS_CFG_MAIN_INIT_HOOK_4();
955
#endif
956
#endif
957

    
958
  /* event associations */
959
#if (AMIROOS_CFG_SSSP_ENABLE == true)
960
  ioeventflagsmask |= MODULE_SSSP_EVENTFLAGS_PD | MODULE_SSSP_EVENTFLAGS_SYNC;
961
#if (AMIROOS_CFG_SSSP_STACK_START != true)
962
  ioeventflagsmask |= MODULE_SSSP_EVENTFLAGS_DN;
963
#endif
964
#if (AMIROOS_CFG_SSSP_STACK_END != true)
965
  ioeventflagsmask |= MODULE_SSSP_EVENTFLAGS_UP;
966
#endif
967
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
968
  if (ioeventflagsmask != 0) {
969
    chEvtRegisterMaskWithFlags(&aos.events.io, &_eventListenerIO, IOEVENT_MASK, ioeventflagsmask);
970
  }
971
  chEvtRegisterMask(&aos.events.os, &_eventListenerOS, OSEVENT_MASK);
972

    
973
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5)
974
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS)
975
  AMIROOS_CFG_MAIN_INIT_HOOK_5(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS);
976
#else
977
  AMIROOS_CFG_MAIN_INIT_HOOK_5();
978
#endif
979
#endif
980

    
981
  /* periphery communication initialization */
982
#if (AMIROOS_CFG_SSSP_ENABLE == true)
983
  // CAN (mandatory)
984
  canStart(&MODULE_HAL_CAN, &moduleHalCanConfig);
985
#endif
986
  // module specific initialization (if any)
987
#ifdef MODULE_INIT_PERIPHERY_COMM
988
  MODULE_INIT_PERIPHERY_COMM();
989
#endif
990
  // user interface (if any)
991
#ifdef MODULE_HAL_PROGIF
992
  aosIOChannelInit(&_stdiochannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
993
  aosIOChannelOutputEnable(&_stdiochannel);
994
  aosIOStreamAddChannel(&aos.iostream, &_stdiochannel);
995
#if (AMIROOS_CFG_SHELL_ENABLE == true)
996
  aosShellChannelInit(&_stdshellchannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
997
  aosShellChannelInputEnable(&_stdshellchannel);
998
  aosShellChannelOutputEnable(&_stdshellchannel);
999
  aosShellStreamAddChannel(&aos.shell.stream, &_stdshellchannel);
1000
#endif
1001
#endif
1002

    
1003
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_6)
1004
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS)
1005
  AMIROOS_CFG_MAIN_INIT_HOOK_6(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS);
1006
#else
1007
  AMIROOS_CFG_MAIN_INIT_HOOK_6();
1008
#endif
1009
#endif
1010

    
1011
  /* module is ready -> print welcome prompt */
1012
  aosprintf("\n");
1013
  aosprintf("######################################################################\n");
1014
  aosprintf("# AMiRo-OS is an operating system designed for the Autonomous Mini   #\n");
1015
  aosprintf("# Robot (AMiRo) platform.                                            #\n");
1016
  aosprintf("# Copyright (C) 2016..2019  Thomas Schöpping et al.                  #\n");
1017
  aosprintf("#                                                                    #\n");
1018
  aosprintf("# This is free software; see the source for copying conditions.      #\n");
1019
  aosprintf("# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR  #\n");
1020
  aosprintf("# A PARTICULAR PURPOSE.                                              #\n");
1021
  aosprintf("# The development of this software was supported by the Excellence   #\n");
1022
  aosprintf("# Cluster EXC 227 Cognitive Interaction Technology. The Excellence   #\n");
1023
  aosprintf("# Cluster EXC 227 is a grant of the Deutsche Forschungsgemeinschaft  #\n");
1024
  aosprintf("# (DFG) in the context of the German Excellence Initiative.          #\n");
1025
  aosprintf("######################################################################\n");
1026
  aosprintf("\n");
1027

    
1028
#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
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1037
  /* SSSP startup OS synchronization phase (end of startup stage 2) */
1038
  while ((shutdown == AOS_SHUTDOWN_NONE) && (eventmask = aosSysSsspStartupOsInitSyncCheck(&_eventListenerIO)) != 0) {
1039
    /*
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
    // IO event
1044
    if (eventmask & _eventListenerIO.events) {
1045
      eventflags = chEvtGetAndClearFlags(&_eventListenerIO);
1046
      // PD event
1047
      if (eventflags & MODULE_SSSP_EVENTFLAGS_PD) {
1048
        shutdown = AOS_SHUTDOWN_PASSIVE;
1049
      } else {
1050
#ifdef MODULE_SSSP_STARTUP_2_2_IOEVENT_HOOK
1051
        MODULE_SSSP_STARTUP_2_2_IOEVENT_HOOK(eventmask, eventflags);
1052
#else
1053
        // ignore any other IO events
1054
#endif
1055
      }
1056
    }
1057
    // OS event
1058
    else if (eventmask & _eventListenerOS.events) {
1059
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
1060
      _unexpectedEventError(eventmask, eventflags);
1061
    }
1062
    // unknown event
1063
    else {
1064
      _unexpectedEventError(eventmask, 0);
1065
    }
1066
  }
1067

    
1068
  /*
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
  if (shutdown == AOS_SHUTDOWN_NONE) {
1074
    shutdown = _ssspModuleStackInitialization();
1075
  }
1076
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
1077

    
1078
  /*
1079
   * There must be no delays at this point, thus no hook is allowed.
1080
   */
1081

    
1082
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1083
  /* synchronize calendars */
1084
  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
#else /* AMIROOS_CFG_SSSP_MASTER == false */
1107
    CANRxFrame frame;
1108
    uint64_t encoded;
1109
    struct tm t;
1110

    
1111
    aosDbgPrintf("receiving current date/time...\t");
1112
    // receive message
1113
#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
    if (canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &frame, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT)) == MSG_OK) {
1118
#endif
1119
      // 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
#endif /* AMIROOS_CFG_SSSP_MASTER == false */
1138
    aosDbgPrintf("\n");
1139
  }
1140
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
1141

    
1142
#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
  if (shutdown == AOS_SHUTDOWN_NONE) {
1152
    aosSysStart();
1153
  }
1154

    
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
    eventmask = chEvtWaitOneTimeout(ALL_EVENTS, chTimeUS2I(AMIROOS_CFG_MAIN_LOOP_TIMEOUT));
1174
#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
      case IOEVENT_MASK:
1189
        // evaluate flags
1190
        eventflags = chEvtGetAndClearFlags(&_eventListenerIO);
1191
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1192
        // PD event
1193
        if (eventflags & MODULE_SSSP_EVENTFLAGS_PD) {
1194
          shutdown = AOS_SHUTDOWN_PASSIVE;
1195
        }
1196
        // all other events
1197
#ifdef MODULE_MAIN_LOOP_IO_EVENT
1198
        else {
1199
          MODULE_MAIN_LOOP_IO_EVENT(eventflags);
1200
        }
1201
#endif
1202
#else /* AMIROOS_CFG_SSSP_ENABLE == false */
1203
#ifdef MODULE_MAIN_LOOP_IO_EVENT
1204
        MODULE_MAIN_LOOP_IO_EVENT(eventflags);
1205
#endif
1206
#endif /* AMIROOS_CFG_SSSP_ENABLE */
1207
        break;
1208

    
1209
      // if this was an OS event
1210
      case OSEVENT_MASK:
1211
        // evaluate flags
1212
        eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
1213
        switch (eventflags) {
1214
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1215
          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
#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
          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
#if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT == 0)
1242
        _unexpectedEventError(eventmask, eventflags);
1243
#endif
1244
        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
#if (AMIROOS_CFG_SSSP_ENABLE == true)
1305
  // CAN (mandatory)
1306
  canStop(&MODULE_HAL_CAN);
1307
#endif
1308
#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
  aosSysShutdownFinal(shutdown);
1322

    
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

    
1337
/** @} */