Statistics
| Branch: | Tag: | Revision:

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

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