Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_main.cpp @ 3e8094a0

History | View | Annotate | Download (38.6 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2020  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
 *
23
 * @addtogroup aos_system
24
 * @{
25
 */
26

    
27
#include <amiroos.h>
28

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

    
36
/******************************************************************************/
37
/* LOCAL DEFINITIONS                                                          */
38
/******************************************************************************/
39

    
40
/**
41
 * @brief   Event mask to identify GPIO events.
42
 */
43
#define EVENTMASK_GPIO                          EVENT_MASK(0)
44

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

    
50
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
51

    
52

    
53
#if (AMIROOS_CFG_SSSP_MSI == true) || defined(__DOXYGEN__)
54

    
55
/**
56
 * @brief   Event mask to identify SSSP timeout events (MSI only).
57
 */
58
#define EVENTMASK_SSSPTIMEOUT                   EVENT_MASK(2)
59

    
60
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
61

    
62
/**
63
 * @brief   Event mask to identify SSSP delay events.
64
 */
65
#define EVENTMASK_SSSPDELAY                     EVENT_MASK(3)
66

    
67
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
68

    
69
/******************************************************************************/
70
/* EXPORTED VARIABLES                                                         */
71
/******************************************************************************/
72

    
73
/******************************************************************************/
74
/* LOCAL TYPES                                                                */
75
/******************************************************************************/
76

    
77
/******************************************************************************/
78
/* LOCAL VARIABLES                                                            */
79
/******************************************************************************/
80

    
81
/**
82
 * @brief   Listener object for GPIO events.
83
 */
84
static event_listener_t _eventListenerGPIO;
85

    
86
/**
87
 * @brief   Listener object for OS events.
88
 */
89
static event_listener_t _eventListenerOS;
90

    
91
#if defined(MODULE_HAL_PROGIF) || defined(__DOXYGEN__)
92

    
93
/**
94
 * @brief   I/O channel for the programmer interface.
95
 */
96
static AosIOChannel _stdiochannel;
97

    
98
#endif /* defined(MODULE_HAL_PROGIF) */
99

    
100
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
101

    
102
/**
103
 * @brief   I/O shell channel for the programmer interface.
104
 */
105
static AosShellChannel _stdshellchannel;
106

    
107
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
108

    
109
/*
110
 * hook to add further static variables
111
 */
112
#if defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES)
113
AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES
114
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES) */
115

    
116
/******************************************************************************/
117
/* LOCAL FUNCTIONS                                                            */
118
/******************************************************************************/
119

    
120
/**
121
 * @brief   Prints an error message about an unexpected event.
122
 *
123
 * @param[in] mask    The event mask.
124
 * @param[in] flags   The event flags.
125
 */
126
static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags)
127
{
128
#if (AMIROOS_CFG_DBG == true)
129
  aosDbgPrintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
130
#else /* (AMIROOS_CFG_DBG == true) */
131
  (void)(mask);
132
  (void)(flags);
133
#endif /* (AMIROOS_CFG_DBG == true) */
134
  return;
135
}
136

    
137
#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_RTC == TRUE)) || defined(__DOXYGEN__)
138
#if (AMIROOS_CFG_SSSP_MASTER == true) || defined(__DOXYGEN__)
139

    
140
/**
141
 * @brief   Converter function to encode a TM value to a single unsigned 64 bit integer.
142
 *
143
 * @details Contents of the TM struct are mapped as follows:
144
 *            bits  |63     62|61      53|52    50|49         26|25     22|21     17|16     12|11      6|5       0|
145
 *            #bits |       2 |        9 |      3 |          24 |       4 |       5 |       5 |       6 |       6 |
146
 *            value |   isdst |     yday |   wday |        year |     mon |    mday |    hour |     min |     sec |
147
 *            range | special | [0, 365] | [0, 6] | [1900, ...] | [0, 11] | [1, 31] | [0, 23] | [0, 59] | [0, 61] |
148
 *          The Daylight Saving Time Flag (isdsst) is encoded as follows:
149
 *            DST not in effect         -> 0
150
 *            DST in effect             -> 1
151
 *            no information available  -> 2
152
 *
153
 * @param[in] src   Pointer to the TM struct to encode.
154
 *
155
 * @return  An unsigned 64 bit integer, which holds the encoded time value.
156
 */
157
static inline uint64_t _TM2U64(struct tm* src)
158
{
159
  aosDbgCheck(src != NULL);
160

    
161
  return (((uint64_t)(src->tm_sec  & 0x0000003F) << (0))               |
162
          ((uint64_t)(src->tm_min  & 0x0000003F) << (6))               |
163
          ((uint64_t)(src->tm_hour & 0x0000001F) << (12))              |
164
          ((uint64_t)(src->tm_mday & 0x0000001F) << (17))              |
165
          ((uint64_t)(src->tm_mon  & 0x0000000F) << (22))              |
166
          ((uint64_t)(src->tm_year & 0x00FFFFFF) << (26))              |
167
          ((uint64_t)(src->tm_wday & 0x00000007) << (50))              |
168
          ((uint64_t)(src->tm_yday & 0x000001FF) << (53))              |
169
          ((uint64_t)((src->tm_isdst == 0) ? 0 : (src->tm_isdst > 0) ? 1 : 2) << (62)));
170
}
171

    
172
/**
173
 * @brief   Serializes 64 bit unsigned integer input and stores it in a byte array.
174
 * @details Serialization is performed in big-endian fashion.
175
 *
176
 * @param[out]  dst   Pointer to the output buffer.
177
 * @param[in]   src   Data to be serialized.
178
 */
179
static inline void _serializeU64(uint8_t* dst, const uint64_t src)
180
{
181
  aosDbgCheck(dst != NULL);
182

    
183
  for (uint8_t byte = 0; byte < sizeof(uint64_t); ++byte) {
184
    dst[byte] = ((src >> ((sizeof(uint64_t) - (byte+1)) * 8)) & 0xFF);
185
  }
186

    
187
  return;
188
}
189

    
190
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
191
#if (AMIROOS_CFG_SSSP_MASTER != true) || defined(__DOXYGEN__)
192

    
193
/**
194
 * @brief   Deserialize 64 bit unsigned integer data from a buffer.
195
 * @details Data is assumed to be serialized in big-endian fashion.
196
 *
197
 * @param[in] src   Pointer to the buffer of data to be deserialzed.
198
 *
199
 * @return    Deserialized 64 bit data.
200
 */
201
static inline uint64_t _deserializeU64(uint8_t* src)
202
{
203
  aosDbgCheck(src != NULL);
204

    
205
  uint64_t result = 0;
206
  for (uint8_t byte = 0; byte < sizeof(uint64_t); ++byte) {
207
    result |= ((uint64_t)src[byte] << ((sizeof(uint64_t) - (byte+1)) * 8));
208
  }
209

    
210
  return result;
211
}
212

    
213
/**
214
 * @brief   Converter functiomn to retrieve the encoded TM value from an unsigned 64 bit integer.
215
 *
216
 * @details For information on the encoding, please refer to @p _TM2U64 function.
217
 *
218
 * @param[out] dst  The TM struct to fill with the decoded values.
219
 * @param[in]  src  Unsigned 64 bit integer holding the encoded TM value.
220
 */
221
static inline void _U642TM(struct tm* dst, const uint64_t src)
222
{
223
  aosDbgCheck(dst != NULL);
224

    
225
  dst->tm_sec  = (src >> 0)  & 0x0000003F;
226
  dst->tm_min  = (src >> 6)  & 0x0000003F;
227
  dst->tm_hour = (src >> 12) & 0x0000001F;
228
  dst->tm_mday = (src >> 17) & 0x0000001F;
229
  dst->tm_mon  = (src >> 22) & 0x0000000F;
230
  dst->tm_year = (src >> 26) & 0x00FFFFFF;
231
  dst->tm_wday = (src >> 50) & 0x00000007;
232
  dst->tm_yday = (src >> 53) & 0x000001FF;
233
  dst->tm_isdst = (((src >> 62) & 0x03) == 0) ? 0 : (((src >> 62) & 0x03) > 0) ? 1 : -1;
234

    
235
  return;
236
}
237

    
238
#endif /* (AMIROOS_CFG_SSSP_MASTER != true) */
239
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_RTC == TRUE) */
240

    
241
/**
242
 * @brief   Application entry point.
243
 */
244
int main(void)
245
{
246
  // local variables
247
  eventmask_t eventmask = 0;
248
  eventflags_t eventflags = 0;
249
  aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE;
250
#if defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES)
251
  AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES
252
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES) */
253

    
254
  /*
255
   * ##########################################################################
256
   * # system initialization                                                  #
257
   * ##########################################################################
258
   */
259

    
260
  /* hardware, kernel, and operating system initialization */
261
  // ChibiOS/HAL and custom hal additions (if any)
262
  halInit();
263
#if defined(MODULE_INIT_HAL_EXTRA)
264
  MODULE_INIT_HAL_EXTRA();
265
#endif /* defined(MODULE_INIT_HAL_EXTRA) */
266

    
267
  // ChibiOS/RT kernel and custom kernel additions (if any)
268
  chSysInit();
269
#if defined(MODULE_INIT_KERNEL_EXTRA)
270
  MODULE_INIT_KERNEL_EXTRA();
271
#endif /* defined(MODULE_INIT_KERNEL_EXTRA) */
272

    
273
  // AMiRo-OS, additional interrupts and custom OS additions (if any)
274
#if (AMIROOS_CFG_SHELL_ENABLE == true)
275
  aosSysInit(moduleShellPrompt);
276
#else /* (AMIROOS_CFG_SHELL_ENABLE == true) */
277
  aosSysInit(NULL);
278
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
279
#if defined(MODULE_INIT_INTERRUPTS)
280
  MODULE_INIT_INTERRUPTS();
281
#endif
282
#if defined(MODULE_INIT_OS_EXTRA)
283
  MODULE_INIT_OS_EXTRA();
284
#endif /* defined(MODULE_INIT_OS_EXTRA) */
285

    
286
  /* event associations */
287
#if (AMIROOS_CFG_SSSP_ENABLE == true)
288
  {
289
    eventflags_t flagsmask = AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK | moduleSsspEventflagPD() | moduleSsspEventflagS();
290
#if (AMIROOS_CFG_SSSP_MSI == true)
291
#if (AMIROOS_CFG_SSSP_STACK_START != true)
292
    flagsmask |= moduleSsspEventflagDN();
293
#endif /* (AMIROOS_CFG_SSSP_STACK_START != true) */
294
#if (AMIROOS_CFG_SSSP_STACK_END != true)
295
    flagsmask |= moduleSsspEventflagUP();
296
#endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
297
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
298
    chEvtRegisterMaskWithFlags(&aos.events.gpio, &_eventListenerGPIO, EVENTMASK_GPIO, flagsmask);
299
  }
300
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
301
  if (AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK) {
302
    chEvtRegisterMaskWithFlags(&aos.events.gpio, &_eventListenerGPIO, EVENTMASK_GPIO, AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK);
303
  }
304
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
305
  chEvtRegisterMask(&aos.events.os, &_eventListenerOS, EVENTMASK_OS);
306

    
307
#if (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_STARTUP == true)
308

    
309
  /* perform SSSP basic initialization stage */
310

    
311
#if defined(MODULE_SSSP_BASICINIT_HOOK)
312
#if defined(MODULE_SSSP_BASICINIT_HOOK_ARGS)
313
  MODULE_SSSP_BASICINIT_HOOK(MODULE_SSSP_BASICINIT_HOOK_ARGS);
314
#else /* defined(MODULE_SSSP_BASICINIT_HOOK_ARGS) */
315
  MODULE_SSSP_BASICINIT_HOOK();
316
#endif /* defined(MODULE_SSSP_BASICINIT_HOOK_ARGS) */
317
#endif /* defined(MODULE_SSSP_BASICINIT_HOOK) */
318

    
319
  // proceed to startup stage 1.2
320
#if (AMIROOS_CFG_SSSP_MASTER == true)
321
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, EVENTMASK_SSSPDELAY, &eventmask) != AOS_SUCCESS)) {
322
    /*
323
     * This code is executed if the received event was not about the delay.
324
     * The received event could be casued by any listener.
325
     */
326
    // GPIO event
327
    if (eventmask & _eventListenerGPIO.events) {
328
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
329
      // PD event
330
      if (eventflags & moduleSsspEventflagPD()) {
331
        shutdown = AOS_SHUTDOWN_PASSIVE;
332
      } else {
333
#if defined(MODULE_SSSP_STARTUP_1_1_GPIOEVENT_HOOK)
334
        MODULE_SSSP_STARTUP_1_1_GPIOEVENT_HOOK(eventmask, eventflags);
335
#else /* defined(MODULE_SSSP_STARTUP_1_1_GPIOEVENT_HOOK) */
336
        /* silently ignore any other GPIO events */
337
#endif /* defined(MODULE_SSSP_STARTUP_1_1_GPIOEVENT_HOOK) */
338
      }
339
    }
340
    // OS event
341
    else if (eventmask & _eventListenerOS.events) {
342
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
343
      _unexpectedEventError(eventmask, eventflags);
344
    }
345
    // unknown event
346
    else {
347
      _unexpectedEventError(eventmask, 0);
348
    }
349
  }
350
  if (shutdown == AOS_SHUTDOWN_NONE) {
351
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_1_2);
352
  }
353
#else /* (AMIROOS_CFG_SSSP_MASTER == true) */
354
  if (shutdown == AOS_SHUTDOWN_NONE) {
355
    aosSsspProceed(NULL, 0, NULL);
356
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_1_2);
357
  }
358
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
359

    
360
  // proceed to startup stage 1.3
361
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
362
    /*
363
     * This code is executed if the received event was not about a deactivation of the snychronization signal.
364
     * The received event could be caused by any listener.
365
     */
366
    // GPIO event
367
    if (eventmask & _eventListenerGPIO.events) {
368
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
369
      // PD event
370
      if (eventflags & moduleSsspEventflagPD()) {
371
        shutdown = AOS_SHUTDOWN_PASSIVE;
372
      } else {
373
#if defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK)
374
        MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK(eventmask, eventflags);
375
#else /* defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK) */
376
        /* silently ignore any other GPIO events */
377
#endif /* defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK) */
378
      }
379
    }
380
    // OS event
381
    else if (eventmask & _eventListenerOS.events) {
382
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
383
      _unexpectedEventError(eventmask, eventflags);
384
    }
385
    // unknown event
386
    else {
387
      _unexpectedEventError(eventmask, 0);
388
    }
389
  }
390
  if (shutdown == AOS_SHUTDOWN_NONE) {
391
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_1_3);
392
  }
393

    
394
  // proceed to startup stage 2.1
395
#if (AMIROOS_CFG_SSSP_MASTER == true)
396
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, EVENTMASK_SSSPDELAY, &eventmask) != AOS_SUCCESS)) {
397
    /*
398
     * This code is executed if the received event was not about the delay.
399
     * The received event could be caused by any listener.
400
     */
401
    // GPIO event
402
    if (eventmask & _eventListenerGPIO.events) {
403
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
404
      // PD event
405
      if (eventflags & moduleSsspEventflagPD()) {
406
        shutdown = AOS_SHUTDOWN_PASSIVE;
407
      } else {
408
#if defined(MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK)
409
        MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK(eventmask, eventflags);
410
#else /* defined(MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK) */
411
        /* silently ignore any other GPIO events */
412
#endif /* defined(MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK) */
413
      }
414
    }
415
    // OS event
416
    else if (eventmask & _eventListenerOS.events) {
417
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
418
      _unexpectedEventError(eventmask, eventflags);
419
    }
420
    // unknown event
421
    else {
422
      _unexpectedEventError(eventmask, 0);
423
    }
424
  }
425
#else /* (AMIROOS_CFG_SSSP_MASTER == true) */
426
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
427
    /*
428
     * This code is executed if the received event was not about a deactivation of the snychronization signal.
429
     * The received event could be caused by any listener.
430
     */
431
    // GPIO event
432
    if (eventmask & _eventListenerGPIO.events) {
433
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
434
      // PD event
435
      if (eventflags & moduleSsspEventflagPD()) {
436
        shutdown = AOS_SHUTDOWN_PASSIVE;
437
      } else {
438
#if defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK)
439
        MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK(eventmask, eventflags);
440
#else /* defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK) */
441
        /* silently ignore any other GPIO events */
442
#endif /* defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK) */
443
      }
444
    }
445
    // OS event
446
    else if (eventmask & _eventListenerOS.events) {
447
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
448
      _unexpectedEventError(eventmask, eventflags);
449
    }
450
    // unknown event
451
    else {
452
      _unexpectedEventError(eventmask, 0);
453
    }
454
  }
455
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
456
  if (shutdown == AOS_SHUTDOWN_NONE) {
457
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_2_1);
458
  }
459

    
460
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_STARTUP == true) */
461

    
462
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1)
463
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS)
464
  AMIROOS_CFG_MAIN_INIT_HOOK_1(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS);
465
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS) */
466
  AMIROOS_CFG_MAIN_INIT_HOOK_1();
467
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS) */
468
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_1) */
469

    
470
  /* periphery communication interfaces initialization */
471
#if defined(MODULE_INIT_PERIPHERY_IF)
472
  // module specific initialization
473
  MODULE_INIT_PERIPHERY_IF();
474
#endif /* defined(MODULE_INIT_PERIPHERY_IF) */
475
#if defined(MODULE_HAL_PROGIF)
476
  // user interface
477
  aosIOChannelInit(&_stdiochannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
478
  aosIOChannelOutputEnable(&_stdiochannel);
479
  aosIOStreamAddChannel(&aos.iostream, &_stdiochannel);
480
#if (AMIROOS_CFG_SHELL_ENABLE == true)
481
  aosShellChannelInit(&_stdshellchannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
482
  aosShellChannelInputEnable(&_stdshellchannel);
483
  aosShellChannelOutputEnable(&_stdshellchannel);
484
  aosShellStreamAddChannel(&aos.shell.stream, &_stdshellchannel);
485
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
486
#endif /* defined(MODULE_HAL_PROGIF) */
487

    
488
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2)
489
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS)
490
  AMIROOS_CFG_MAIN_INIT_HOOK_2(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS);
491
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS) */
492
  AMIROOS_CFG_MAIN_INIT_HOOK_2();
493
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS) */
494
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_2) */
495

    
496
  /* module is ready -> print welcome prompt */
497
  aosprintf("\n");
498
  aosprintf("######################################################################\n");
499
  aosprintf("# AMiRo-OS is an operating system designed for the Autonomous Mini   #\n");
500
  aosprintf("# Robot (AMiRo) platform.                                            #\n");
501
  aosprintf("# Copyright (C) 2016..2020  Thomas Schöpping et al.                  #\n");
502
  aosprintf("#                                                                    #\n");
503
  aosprintf("# This is free software; see the source for copying conditions.      #\n");
504
  aosprintf("# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR  #\n");
505
  aosprintf("# A PARTICULAR PURPOSE.                                              #\n");
506
  aosprintf("# The development of this software was supported by the Excellence   #\n");
507
  aosprintf("# Cluster EXC 227 Cognitive Interaction Technology. The Excellence   #\n");
508
  aosprintf("# Cluster EXC 227 is a grant of the Deutsche Forschungsgemeinschaft  #\n");
509
  aosprintf("# (DFG) in the context of the German Excellence Initiative.          #\n");
510
  aosprintf("######################################################################\n");
511
  aosprintf("\n");
512

    
513
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3)
514
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS)
515
  AMIROOS_CFG_MAIN_INIT_HOOK_3(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS);
516
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS) */
517
  AMIROOS_CFG_MAIN_INIT_HOOK_3();
518
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS) */
519
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3) */
520

    
521
#if (AMIROOS_CFG_TESTS_ENABLE == true)
522
#if defined(MODULE_INIT_TESTS)
523
  MODULE_INIT_TESTS();
524
#else /* defined(MODULE_INIT_TESTS) */
525
  #warning "AMIROOS_CFG_TESTS_ENABLE set to true, but MODULE_INIT_TESTS() not defined"
526
#endif /* defined(MODULE_INIT_TESTS) */
527
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
528

    
529
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4)
530
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS)
531
  AMIROOS_CFG_MAIN_INIT_HOOK_4(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS);
532
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS) */
533
  AMIROOS_CFG_MAIN_INIT_HOOK_4();
534
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS) */
535
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4) */
536

    
537
#if (AMIROOS_CFG_SSSP_ENABLE == true)
538

    
539
  // proceed to startup stage 2.2
540
#if (AMIROOS_CFG_SSSP_MASTER == true)
541
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, EVENTMASK_SSSPDELAY, &eventmask) != AOS_SUCCESS)) {
542
    /*
543
     * This code is executed if the received event was not about the delay.
544
     * The received event could be caused by any listener.
545
     */
546
    // GPIO event
547
    if (eventmask & _eventListenerGPIO.events) {
548
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
549
      // PD event
550
      if (eventflags & moduleSsspEventflagPD()) {
551
        shutdown = AOS_SHUTDOWN_PASSIVE;
552
      } else {
553
#if defined(MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK)
554
        MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK(eventmask, eventflags);
555
#else /* defined(MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK) */
556
        /* silently ignore any other GPIO events */
557
#endif /* defined(MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK) */
558
      }
559
    }
560
    // OS event
561
    else if (eventmask & _eventListenerOS.events) {
562
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
563
      _unexpectedEventError(eventmask, eventflags);
564
    }
565
    // unknown event
566
    else {
567
      _unexpectedEventError(eventmask, 0);
568
    }
569
  }
570
  if (shutdown == AOS_SHUTDOWN_NONE) {
571
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_2_2);
572
  }
573
#else /* (AMIROOS_CFG_SSSP_MASTER == true) */
574
  if (shutdown == AOS_SHUTDOWN_NONE) {
575
    aosSsspProceed(NULL, 0, NULL);
576
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_2_2);
577
  }
578
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
579

    
580
  // proceed to startup stage 3 (MSI is enabled), or to operation phase (no MSI)
581
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
582
    /*
583
     * This code is executed if the received event was not about a deactivation of the snychronization signal.
584
     * The received event could be caused by any listener.
585
     */
586
    // GPIO event
587
    if (eventmask & _eventListenerGPIO.events) {
588
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
589
      // PD event
590
      if (eventflags & moduleSsspEventflagPD()) {
591
        shutdown = AOS_SHUTDOWN_PASSIVE;
592
      } else {
593
#if defined(MODULE_SSSP_STARTUP_2_2_GPIOEVENT_HOOK)
594
        MODULE_SSSP_STARTUP_2_2_GPIOEVENT_HOOK(eventmask, eventflags);
595
#else /* defined(MODULE_SSSP_STARTUP_2_2_GPIOEVENT_HOOK) */
596
        /* silently ignore any other GPIO events */
597
#endif /* defined(MODULE_SSSP_STARTUP_2_2_GPIOEVENT_HOOK) */
598
      }
599
    }
600
    // OS event
601
    else if (eventmask & _eventListenerOS.events) {
602
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
603
      _unexpectedEventError(eventmask, eventflags);
604
    }
605
    // unknown event
606
    else {
607
      _unexpectedEventError(eventmask, 0);
608
    }
609
  }
610
  if (shutdown == AOS_SHUTDOWN_NONE) {
611
#if (AMIROOS_CFG_SSSP_MSI == true)
612
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_3);
613
#else /* (AMIROOS_CFG_SSSP_MSI == true) */
614
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_OPERATION);
615
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
616
  }
617

    
618
  /*
619
   * There must be no delay at this point, thus no hook allowed.
620
   */
621

    
622
#if (AMIROOS_CFG_SSSP_MSI == true)
623
  {
624
    // initialize temporary MSI data
625
    aos_ssspmsidata_t msidata;
626
    aosSsspMsiInit(&msidata, EVENTMASK_SSSPDELAY, EVENTMASK_SSSPTIMEOUT, &_eventListenerGPIO);
627

    
628
    // execute module stack initialization (MSI) but react to unrelated events as well
629
    while ((shutdown == AOS_SHUTDOWN_NONE) && (aos.sssp.stage != AOS_SSSP_STAGE_OPERATION)) {
630
      // execute MSI routine
631
      aosSsspMsi(&msidata, &eventmask);
632
      // handly event (if any)
633
      if (eventmask) {
634
        // GPIO event
635
        if (eventmask & _eventListenerGPIO.events) {
636
          eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
637
          // PD event
638
          if (eventflags & moduleSsspEventflagPD()) {
639
            shutdown = AOS_SHUTDOWN_PASSIVE;
640
          } else {
641
  #if defined(MODULE_SSSP_STARTUP_3_GPIOEVENT_HOOK)
642
            MODULE_SSSP_STARTUP_3_GPIOEVENT_HOOK(eventmask, eventflags);
643
  #else /* defined(MODULE_SSSP_STARTUP_3_GPIOEVENT_HOOK) */
644
            /* silently ignore any other GPIO events */
645
  #endif /* defined(MODULE_SSSP_STARTUP_3_GPIOEVENT_HOOK) */
646
          }
647
        }
648
        // OS event
649
        else if (eventmask & _eventListenerOS.events) {
650
          eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
651
          _unexpectedEventError(eventmask, eventflags);
652
        }
653
        // unknown event
654
        else {
655
          _unexpectedEventError(eventmask, 0);
656
        }
657
      }
658
    }
659
  }
660

    
661
#if (HAL_USE_RTC == TRUE)
662

    
663
  /*
664
   * calendar synchronization
665
   *
666
   * Although calendar synchronization does not inherently rely on SSSP, this
667
   * implementation uses the moduleSsspBcbX() functions, defined by SSSP in
668
   * order to save redundant code. Furthermore, it is rather unlikely that
669
   * system wide synchronization of calendars is desired when SSSP is disabled
670
   * anyway.
671
   */
672
 if (shutdown == AOS_SHUTDOWN_NONE) {
673
#if (AMIROOS_CFG_SSSP_MASTER == true)
674
   // temporary variables
675
   struct tm t;
676
   uint8_t buffer[sizeof(uint64_t)];
677

    
678
   // get current date/time
679
   aosSysGetDateTime(&t);
680
   // encode & serialize
681
   _serializeU64(buffer, _TM2U64(&t));
682
   // transmit
683
   aosDbgPrintf("transmitting current date/time...\t");
684
   if (moduleSsspBcbTransmit(buffer, sizeof(uint64_t)) == AOS_SSSP_BCB_SUCCESS) {
685
     aosDbgPrintf("ok\n");
686
   } else {
687
     aosDbgPrintf("fail\n");
688
   }
689
#else /* (AMIROOS_CFG_SSSP_MASTER == true) */
690
   // temporary variables
691
   aos_timestamp_t start, now;
692
   aos_ssspbcbstatus_t bcbstatus;
693
   uint8_t buffer[sizeof(uint64_t)];
694
   struct tm t;
695

    
696
   // receive
697
   aosDbgPrintf("receiving current date/time...\t");
698
   aosSysGetUptime(&start);
699
   do {
700
     bcbstatus = moduleSsspBcbReceive(buffer, sizeof(uint64_t));
701
     aosSysGetUptime(&now);
702
   } while ((bcbstatus != AOS_SSSP_BCB_SUCCESS) && ((now - start) < AOS_SSSP_TIMEOUT));
703
   // if something was received
704
   if (bcbstatus == AOS_SSSP_BCB_SUCCESS) {
705
     // deserialize & decode
706
     _U642TM(&t, _deserializeU64(buffer));
707
     // set current date/time
708
     aosSysSetDateTime(&t);
709
     aosDbgPrintf("ok\n");
710
   } else {
711
     aosDbgPrintf("fail\n");
712
   }
713
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
714
 }
715

    
716
#endif /* (HAL_USE_RTC == TRUE) */
717
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
718

    
719
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
720

    
721
  /* completely start AMiRo-OS */
722
  if (shutdown == AOS_SHUTDOWN_NONE) {
723
    aosSysStart();
724
  }
725

    
726
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5)
727
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS)
728
  AMIROOS_CFG_MAIN_INIT_HOOK_5(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS);
729
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS) */
730
  AMIROOS_CFG_MAIN_INIT_HOOK_5();
731
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS) */
732
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5) */
733

    
734
  /*
735
   * ##########################################################################
736
   * # infinite loop                                                          #
737
   * ##########################################################################
738
   */
739

    
740
#if (AMIROOS_CFG_SSSP_ENABLE == true)
741
  /* ignore all SSSP signal events, except for PD and configured events */
742
  eventflags = moduleSsspEventflagS();
743
#if (AMIROOS_CFG_SSSP_MSI == true)
744
#if (AMIROOS_CFG_SSSP_STACK_START != true)
745
  eventflags |= moduleSsspEventflagDN();
746
#endif /* (AMIROOS_CFG_SSSP_STACK_START != true) */
747
#if (AMIROOS_CFG_SSSP_STACK_END != true)
748
  eventflags |= moduleSsspEventflagUP();
749
#endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
750
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
751
  eventflags &= ~((eventflags_t)AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK);
752
  _eventListenerGPIO.wflags &= ~eventflags;
753
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
754

    
755
  // sleep until a shutdown event is received
756
  while (shutdown == AOS_SHUTDOWN_NONE) {
757
    // wait for an event
758
#if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0)
759
    eventmask = chEvtWaitOneTimeout(ALL_EVENTS, chTimeUS2I(AMIROOS_CFG_MAIN_LOOP_TIMEOUT));
760
#else /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0) */
761
    eventmask = chEvtWaitOne(ALL_EVENTS);
762
#endif /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0) */
763

    
764
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1)
765
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS)
766
    AMIROOS_CFG_MAIN_LOOP_HOOK_1(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS);
767
#else /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS) */
768
    AMIROOS_CFG_MAIN_LOOP_HOOK_1();
769
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS) */
770
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1) */
771

    
772
    switch (eventmask) {
773
      // if this was an GPIO event
774
      case EVENTMASK_GPIO:
775
        // evaluate flags
776
        eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
777
#if (AMIROOS_CFG_SSSP_ENABLE == true)
778
        // PD event
779
        if (eventflags & moduleSsspEventflagPD()) {
780
          aosSsspShutdownInit(false);
781
          shutdown = AOS_SHUTDOWN_PASSIVE;
782
        }
783
        // all other events
784
#if defined(MODULE_MAIN_LOOP_GPIOEVENT)
785
        else {
786
          MODULE_MAIN_LOOP_GPIOEVENT(eventflags);
787
        }
788
#endif /* defined(MODULE_MAIN_LOOP_GPIOEVENT) */
789
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
790
#if defined(MODULE_MAIN_LOOP_GPIOEVENT)
791
        MODULE_MAIN_LOOP_GPIOEVENT(eventflags);
792
#endif /* defined(MODULE_MAIN_LOOP_GPIOEVENT) */
793
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
794
        break;
795

    
796
      // if this was an OS event
797
      case EVENTMASK_OS:
798
        // evaluate flags
799
        eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
800
        switch (eventflags) {
801
#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_NONE)
802
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_MASK:
803
            shutdown = AOS_SHUTDOWN_ACTIVE;
804
            break;
805
#elif (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
806
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_HIBERNATE:
807
            shutdown = AOS_SHUTDOWN_HIBERNATE;
808
            break;
809
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_DEEPSLEEP:
810
            shutdown = AOS_SHUTDOWN_DEEPSLEEP;
811
            break;
812
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_TRANSPORTATION:
813
            shutdown = AOS_SHUTDOWN_TRANSPORTATION;
814
            break;
815
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_RESTART:
816
            shutdown = AOS_SHUTDOWN_RESTART;
817
            break;
818
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_PASSIVE:
819
            _unexpectedEventError(eventmask, eventflags);
820
            break;
821
#endif /* (AMIROOS_CFG_BOOTLOADER == X) */
822
          default:
823
            _unexpectedEventError(eventmask, eventflags);
824
            break;
825
        }
826
        break;
827

    
828
      // if this was any other event (should be impossible to occur)
829
      default:
830
        eventflags = 0;
831
#if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT == 0)
832
        _unexpectedEventError(eventmask, eventflags);
833
#endif /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT == 0) */
834
        break;
835
    }
836

    
837
#if (AMIROOS_CFG_SSSP_ENABLE == true)
838

    
839
    /*
840
     * Re-anable the S signal GPIO event.
841
     * Note that events for the optional signals UP and DN are not enabled as they are not utilized during SSSP shutdown phase.
842
     */
843
    chSysLock();
844
    _eventListenerGPIO.wflags |= moduleSsspEventflagS();
845
    chSysUnlock();
846

    
847
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
848

    
849
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2)
850
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2_ARGS)
851
    AMIROOS_CFG_MAIN_LOOP_HOOK_2(AMIROOS_CFG_MAIN_LOOP_HOOK_2_ARGS);
852
#else /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2_ARGS) */
853
    AMIROOS_CFG_MAIN_LOOP_HOOK_2();
854
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2_ARGS) */
855
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2) */
856
  }
857

    
858
  /*
859
   * ##########################################################################
860
   * # system shutdown                                                        #
861
   * ##########################################################################
862
   */
863

    
864
  aosDbgAssert(shutdown != AOS_SHUTDOWN_NONE);
865

    
866
  // initialize/acknowledge shutdown
867
  aosSysShutdownInit(shutdown);
868
#if (AMIROOS_CFG_SSSP_ENABLE == true)
869
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_1_2);
870
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
871

    
872
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1)
873
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS)
874
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS);
875
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS) */
876
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1();
877
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS) */
878
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1) */
879

    
880
  // wait for all threads to terminate
881
  aosSysStop();
882

    
883
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2)
884
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS)
885
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS);
886
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS) */
887
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2();
888
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS) */
889
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2) */
890

    
891
  // deinitialize system
892
  aosSysDeinit();
893

    
894
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3)
895
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS)
896
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS);
897
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS) */
898
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3();
899
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS) */
900
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3) */
901

    
902
  /* stop all periphery communication interfaces */
903
#if defined(MODULE_SHUTDOWN_PERIPHERY_IF)
904
  MODULE_SHUTDOWN_PERIPHERY_IF();
905
#endif /* defined(MODULE_SHUTDOWN_PERIPHERY_IF) */
906

    
907
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4)
908
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS)
909
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS);
910
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS) */
911
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4();
912
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS) */
913
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4) */
914

    
915
#if (AMIROOS_CFG_SSSP_ENABLE == true)
916

    
917
  // proceed to SSSP shutdown stage 1.3
918
  aosSsspProceed(NULL, 0, NULL);
919
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_1_3);
920

    
921
#if (AMIROOS_CFG_SSSP_SHUTDOWN == true)
922

    
923
  /* AMiRo-OS performs SSSP shutdown phase
924
   * NOTE:
925
   * The initiating module could deactivate PD at this point to indicate a restart rather than a shutdown request.
926
   * AMiRo-OS does not support restart though, thus the signal is not changed.
927
   */
928

    
929
  // proceed to SSSP shutdown stage 2.1
930
  while (aosSsspProceed(&_eventListenerGPIO, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS) {
931
    /*
932
     * This code is executed if the received event was not about a deactivation of the synchronization signal.
933
     * The received event could be caused by any listener.
934
     */
935
    // GPIO event
936
    if (eventmask & _eventListenerGPIO.events) {
937
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
938
#if defined(MODULE_SSSP_SHUTDOWN_1_3_GPIOEVENT_HOOK)
939
        MODULE_SSSP_SHUTDOWN_1_3_GPIOEVENT_HOOK(eventmask, eventflags);
940
#else /* defined(MODULE_SSSP_SHUTDOWN_1_3_GPIOEVENT_HOOK) */
941
        /* silently ignore any other GPIO events */
942
#endif /* defined(MODULE_SSSP_SHUTDOWN_1_3_GPIOEVENT_HOOK) */
943
    }
944
    // OS event
945
    else if (eventmask & _eventListenerOS.events) {
946
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
947
      _unexpectedEventError(eventmask, eventflags);
948
    }
949
    // unknown event
950
    else {
951
      _unexpectedEventError(eventmask, 0);
952
    }
953
  }
954
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_2_1);
955

    
956
  // proceed to SSSP shutdown stage 2.2
957
  aosSsspProceed(NULL, 0, &eventmask);
958
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_2_2);
959
  if (shutdown == AOS_SHUTDOWN_PASSIVE) {
960
    aosDbgPrintf("%s request received\n", eventmask ? "shutdown" : "restart");
961
  }
962
  /* NOTE:
963
   * Actually the call of aosSsspProceed() returned an indicator, whether a shutdown or restart has been initiated.
964
   * Since AMiRo-OS does not support restart, this return value is ignored and shutdown is assumed.
965
   */
966

    
967
  // the initiating module broadcasts the shutdown identifier
968
  if (shutdown != AOS_SHUTDOWN_PASSIVE) {
969
    // since AMiRo-OS does not support multiple ways to shutdown or restart, the special identifier 0 is broadcasted.
970
    aosSsspShutdownBroadcastIdentifier(0);
971
  }
972
  // passive modules receive the broadcasted identifier
973
  else {
974
    unsigned int identifier = 0;
975

    
976
    // receive the identifier, which specifies the type of shutdown/restart
977
    while ((eventmask = aosSsspShutdownWaitForIdentifierPulse(&_eventListenerGPIO, moduleSsspEventflagS(), EVENTMASK_SSSPDELAY, &identifier)) != EVENTMASK_SSSPDELAY) {
978
      // GPIO event
979
      if (eventmask & _eventListenerGPIO.events) {
980
        eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
981
#if defined(MODULE_SSSP_SHUTDOWN_2_2_GPIOEVENT_HOOK)
982
        MODULE_SSSP_SHUTDOWN_2_2_GPIOEVENT_HOOK(eventmask, eventflags);
983
#else /* defined(MODULE_SSSP_SHUTDOWN_2_2_GPIOEVENT_HOOK) */
984
        /* silently ignore any other GPIO events */
985
#endif /* defined(MODULE_SSSP_SHUTDOWN_2_2_GPIOEVENT_HOOK) */
986
        /* silently ignore any GPIO events */
987
      }
988
      // OS event
989
      else if (eventmask & _eventListenerOS.events) {
990
        eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
991
        _unexpectedEventError(eventmask, eventflags);
992
      }
993
      // unknown event
994
      else {
995
        _unexpectedEventError(eventmask, 0);
996
      }
997
    }
998

    
999
    // Since AMiRo-OS does not support multiple ways to shutdown or restart, the identifier is ignored.
1000
  }
1001

    
1002
  // proceed to SSSP shutdown stage 2.3
1003
  aosSsspProceed(NULL, 0, NULL);
1004
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_2_3);
1005

    
1006
  /*
1007
   * Since AMiRo-OS does only support one basic shutdown and no restart at all, the final shutdown is kept very simple.
1008
   */
1009

    
1010
  // disable interrupts
1011
  irqDeinit();
1012
  chSysDisable();
1013

    
1014
  chThdExit(MSG_OK);
1015
  return MSG_OK;
1016

    
1017
#else /* (AMIROOS_CFG_SSSP_SHUTDOWN == true) */
1018

    
1019
  /* hand over to the bootloader to perform SSSP shutdown phase */
1020
  aosSysShutdownToBootloader(shutdown);
1021

    
1022
  aosDbgAssertMsg(false, "AMiRo-OS must not proceed to this point!");
1023
  chThdExit(MSG_RESET);
1024
  return MSG_RESET;
1025

    
1026
#endif /* (AMIROOS_CFG_SSSP_SHUTDOWN == true) */
1027

    
1028
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
1029

    
1030
  /*
1031
   * Depending on the configured bootloader, trivial shutdown is either executed by AMiRo-OS or by that bootloader.
1032
   */
1033
#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_NONE)
1034

    
1035
  /* AMiRo-OS performs trivial shutdown */
1036

    
1037
  // disable interrupts
1038
  irqDeinit();
1039
  chSysDisable();
1040

    
1041
  chThdExit(MSG_OK);
1042
  return MSG_OK;
1043

    
1044
#elif (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
1045

    
1046
  /* AMiRo-BLT performns shutdown */
1047
  aosSysShutdownToBootloader(shutdown);
1048

    
1049
  aosDbgAssertMsg(false, "AMiRo-OS must not proceed to this point!");
1050
  chThdExit(MSG_RESET);
1051
  return MSG_RESET;
1052

    
1053
#endif /* (AMIROOS_CFG_BOOTLOADER == X) */
1054
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
1055
}
1056

    
1057
/******************************************************************************/
1058
/* EXPORTED FUNCTIONS                                                         */
1059
/******************************************************************************/
1060

    
1061
/** @} */