Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (38.9 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
#if (CH_CFG_USE_REGISTRY == TRUE) || defined(__DOXYGEN__)
82

    
83
/**
84
 * @brief   Name of the main thread.
85
 */
86
static const char _threadName[] = "control (main)";
87

    
88
#endif /* (CH_CFG_USE_REGISTRY == TRUE) */
89

    
90
/**
91
 * @brief   Listener object for GPIO events.
92
 */
93
static event_listener_t _eventListenerGPIO;
94

    
95
/**
96
 * @brief   Listener object for OS events.
97
 */
98
static event_listener_t _eventListenerOS;
99

    
100
#if defined(MODULE_HAL_PROGIF) || defined(__DOXYGEN__)
101

    
102
/**
103
 * @brief   I/O channel for the programmer interface.
104
 */
105
static AosIOChannel _stdiochannel;
106

    
107
#endif /* defined(MODULE_HAL_PROGIF) */
108

    
109
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
110

    
111
/**
112
 * @brief   I/O shell channel for the programmer interface.
113
 */
114
static AosShellChannel _stdshellchannel;
115

    
116
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
117

    
118
/*
119
 * hook to add further static variables
120
 */
121
#if defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES)
122
AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES
123
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES) */
124

    
125
/******************************************************************************/
126
/* LOCAL FUNCTIONS                                                            */
127
/******************************************************************************/
128

    
129
/**
130
 * @brief   Prints an error message about an unexpected event.
131
 *
132
 * @param[in] mask    The event mask.
133
 * @param[in] flags   The event flags.
134
 */
135
static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags)
136
{
137
#if (AMIROOS_CFG_DBG == true)
138
  aosDbgPrintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
139
#else /* (AMIROOS_CFG_DBG == true) */
140
  (void)(mask);
141
  (void)(flags);
142
#endif /* (AMIROOS_CFG_DBG == true) */
143
  return;
144
}
145

    
146
#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_RTC == TRUE)) || defined(__DOXYGEN__)
147
#if (AMIROOS_CFG_SSSP_MASTER == true) || defined(__DOXYGEN__)
148

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

    
170
  return (((uint64_t)(src->tm_sec  & 0x0000003F) << (0))               |
171
          ((uint64_t)(src->tm_min  & 0x0000003F) << (6))               |
172
          ((uint64_t)(src->tm_hour & 0x0000001F) << (12))              |
173
          ((uint64_t)(src->tm_mday & 0x0000001F) << (17))              |
174
          ((uint64_t)(src->tm_mon  & 0x0000000F) << (22))              |
175
          ((uint64_t)(src->tm_year & 0x00FFFFFF) << (26))              |
176
          ((uint64_t)(src->tm_wday & 0x00000007) << (50))              |
177
          ((uint64_t)(src->tm_yday & 0x000001FF) << (53))              |
178
          ((uint64_t)((src->tm_isdst == 0) ? 0 : (src->tm_isdst > 0) ? 1 : 2) << (62)));
179
}
180

    
181
/**
182
 * @brief   Serializes 64 bit unsigned integer input and stores it in a byte array.
183
 * @details Serialization is performed in big-endian fashion.
184
 *
185
 * @param[out]  dst   Pointer to the output buffer.
186
 * @param[in]   src   Data to be serialized.
187
 */
188
static inline void _serializeU64(uint8_t* dst, const uint64_t src)
189
{
190
  aosDbgCheck(dst != NULL);
191

    
192
  for (uint8_t byte = 0; byte < sizeof(uint64_t); ++byte) {
193
    dst[byte] = ((src >> ((sizeof(uint64_t) - (byte+1)) * 8)) & 0xFF);
194
  }
195

    
196
  return;
197
}
198

    
199
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
200
#if (AMIROOS_CFG_SSSP_MASTER != true) || defined(__DOXYGEN__)
201

    
202
/**
203
 * @brief   Deserialize 64 bit unsigned integer data from a buffer.
204
 * @details Data is assumed to be serialized in big-endian fashion.
205
 *
206
 * @param[in] src   Pointer to the buffer of data to be deserialzed.
207
 *
208
 * @return    Deserialized 64 bit data.
209
 */
210
static inline uint64_t _deserializeU64(uint8_t* src)
211
{
212
  aosDbgCheck(src != NULL);
213

    
214
  uint64_t result = 0;
215
  for (uint8_t byte = 0; byte < sizeof(uint64_t); ++byte) {
216
    result |= ((uint64_t)src[byte] << ((sizeof(uint64_t) - (byte+1)) * 8));
217
  }
218

    
219
  return result;
220
}
221

    
222
/**
223
 * @brief   Converter functiomn to retrieve the encoded TM value from an unsigned 64 bit integer.
224
 *
225
 * @details For information on the encoding, please refer to @p _TM2U64 function.
226
 *
227
 * @param[out] dst  The TM struct to fill with the decoded values.
228
 * @param[in]  src  Unsigned 64 bit integer holding the encoded TM value.
229
 */
230
static inline void _U642TM(struct tm* dst, const uint64_t src)
231
{
232
  aosDbgCheck(dst != NULL);
233

    
234
  dst->tm_sec  = (src >> 0)  & 0x0000003F;
235
  dst->tm_min  = (src >> 6)  & 0x0000003F;
236
  dst->tm_hour = (src >> 12) & 0x0000001F;
237
  dst->tm_mday = (src >> 17) & 0x0000001F;
238
  dst->tm_mon  = (src >> 22) & 0x0000000F;
239
  dst->tm_year = (src >> 26) & 0x00FFFFFF;
240
  dst->tm_wday = (src >> 50) & 0x00000007;
241
  dst->tm_yday = (src >> 53) & 0x000001FF;
242
  dst->tm_isdst = (((src >> 62) & 0x03) == 0) ? 0 : (((src >> 62) & 0x03) > 0) ? 1 : -1;
243

    
244
  return;
245
}
246

    
247
#endif /* (AMIROOS_CFG_SSSP_MASTER != true) */
248
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_RTC == TRUE) */
249

    
250
/**
251
 * @brief   Application entry point.
252
 */
253
int main(void)
254
{
255
  // local variables
256
  eventmask_t eventmask = 0;
257
  eventflags_t eventflags = 0;
258
  aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE;
259
#if defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES)
260
  AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES
261
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES) */
262

    
263
  /*
264
   * ##########################################################################
265
   * # system initialization                                                  #
266
   * ##########################################################################
267
   */
268

    
269
  /* hardware, kernel, and operating system initialization */
270
  // ChibiOS/HAL and custom hal additions (if any)
271
  halInit();
272
#if defined(MODULE_INIT_HAL_EXTRA)
273
  MODULE_INIT_HAL_EXTRA();
274
#endif /* defined(MODULE_INIT_HAL_EXTRA) */
275

    
276
  // ChibiOS/RT kernel and custom kernel additions (if any)
277
  chSysInit();
278
#if defined(MODULE_INIT_KERNEL_EXTRA)
279
  MODULE_INIT_KERNEL_EXTRA();
280
#endif /* defined(MODULE_INIT_KERNEL_EXTRA) */
281
#if (CH_CFG_USE_REGISTRY == TRUE)
282
  currp->name = _threadName;
283
#endif /* (CH_CFG_USE_REGISTRY == TRUE) */
284

    
285
  // AMiRo-OS, additional interrupts and custom OS additions (if any)
286
#if (AMIROOS_CFG_SHELL_ENABLE == true)
287
  aosSysInit(moduleShellPrompt);
288
#else /* (AMIROOS_CFG_SHELL_ENABLE == true) */
289
  aosSysInit(NULL);
290
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
291
#if defined(MODULE_INIT_INTERRUPTS)
292
  MODULE_INIT_INTERRUPTS();
293
#endif
294
#if defined(MODULE_INIT_OS_EXTRA)
295
  MODULE_INIT_OS_EXTRA();
296
#endif /* defined(MODULE_INIT_OS_EXTRA) */
297

    
298
  /* event associations */
299
#if (AMIROOS_CFG_SSSP_ENABLE == true)
300
  {
301
    eventflags_t flagsmask = AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK | moduleSsspEventflagPD() | moduleSsspEventflagS();
302
#if (AMIROOS_CFG_SSSP_MSI == true)
303
#if (AMIROOS_CFG_SSSP_STACK_START != true)
304
    flagsmask |= moduleSsspEventflagDN();
305
#endif /* (AMIROOS_CFG_SSSP_STACK_START != true) */
306
#if (AMIROOS_CFG_SSSP_STACK_END != true)
307
    flagsmask |= moduleSsspEventflagUP();
308
#endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
309
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
310
    chEvtRegisterMaskWithFlags(&aos.events.gpio, &_eventListenerGPIO, EVENTMASK_GPIO, flagsmask);
311
  }
312
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
313
  if (AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK) {
314
    chEvtRegisterMaskWithFlags(&aos.events.gpio, &_eventListenerGPIO, EVENTMASK_GPIO, AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK);
315
  }
316
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
317
  chEvtRegisterMask(&aos.events.os, &_eventListenerOS, EVENTMASK_OS);
318

    
319
#if (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_STARTUP == true)
320

    
321
  /* perform SSSP basic initialization stage */
322

    
323
#if defined(MODULE_SSSP_BASICINIT_HOOK)
324
#if defined(MODULE_SSSP_BASICINIT_HOOK_ARGS)
325
  MODULE_SSSP_BASICINIT_HOOK(MODULE_SSSP_BASICINIT_HOOK_ARGS);
326
#else /* defined(MODULE_SSSP_BASICINIT_HOOK_ARGS) */
327
  MODULE_SSSP_BASICINIT_HOOK();
328
#endif /* defined(MODULE_SSSP_BASICINIT_HOOK_ARGS) */
329
#endif /* defined(MODULE_SSSP_BASICINIT_HOOK) */
330

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

    
372
  // proceed to startup stage 1.3
373
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
374
    /*
375
     * This code is executed if the received event was not about a deactivation of the snychronization signal.
376
     * The received event could be caused by any listener.
377
     */
378
    // GPIO event
379
    if (eventmask & _eventListenerGPIO.events) {
380
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
381
      // PD event
382
      if (eventflags & moduleSsspEventflagPD()) {
383
        shutdown = AOS_SHUTDOWN_PASSIVE;
384
      } else {
385
#if defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK)
386
        MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK(eventmask, eventflags);
387
#else /* defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK) */
388
        /* silently ignore any other GPIO events */
389
#endif /* defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK) */
390
      }
391
    }
392
    // OS event
393
    else if (eventmask & _eventListenerOS.events) {
394
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
395
      _unexpectedEventError(eventmask, eventflags);
396
    }
397
    // unknown event
398
    else {
399
      _unexpectedEventError(eventmask, 0);
400
    }
401
  }
402
  if (shutdown == AOS_SHUTDOWN_NONE) {
403
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_1_3);
404
  }
405

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

    
472
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_STARTUP == true) */
473

    
474
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1)
475
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS)
476
  AMIROOS_CFG_MAIN_INIT_HOOK_1(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS);
477
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS) */
478
  AMIROOS_CFG_MAIN_INIT_HOOK_1();
479
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS) */
480
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_1) */
481

    
482
  /* periphery communication interfaces initialization */
483
#if defined(MODULE_INIT_PERIPHERY_IF)
484
  // module specific initialization
485
  MODULE_INIT_PERIPHERY_IF();
486
#endif /* defined(MODULE_INIT_PERIPHERY_IF) */
487
#if defined(MODULE_HAL_PROGIF)
488
  // user interface
489
  aosIOChannelInit(&_stdiochannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
490
  aosIOChannelOutputEnable(&_stdiochannel);
491
  aosIOStreamAddChannel(&aos.iostream, &_stdiochannel);
492
#if (AMIROOS_CFG_SHELL_ENABLE == true)
493
  aosShellChannelInit(&_stdshellchannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
494
  aosShellChannelInputEnable(&_stdshellchannel);
495
  aosShellChannelOutputEnable(&_stdshellchannel);
496
  aosShellStreamAddChannel(&aos.shell.stream, &_stdshellchannel);
497
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
498
#endif /* defined(MODULE_HAL_PROGIF) */
499

    
500
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2)
501
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS)
502
  AMIROOS_CFG_MAIN_INIT_HOOK_2(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS);
503
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS) */
504
  AMIROOS_CFG_MAIN_INIT_HOOK_2();
505
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS) */
506
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_2) */
507

    
508
  /* module is ready -> print welcome prompt */
509
  aosprintf("\n");
510
  aosprintf("######################################################################\n");
511
  aosprintf("# AMiRo-OS is an operating system designed for the Autonomous Mini   #\n");
512
  aosprintf("# Robot (AMiRo) platform.                                            #\n");
513
  aosprintf("# Copyright (C) 2016..2020  Thomas Schöpping et al.                  #\n");
514
  aosprintf("#                                                                    #\n");
515
  aosprintf("# This is free software; see the source for copying conditions.      #\n");
516
  aosprintf("# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR  #\n");
517
  aosprintf("# A PARTICULAR PURPOSE.                                              #\n");
518
  aosprintf("# The development of this software was supported by the Excellence   #\n");
519
  aosprintf("# Cluster EXC 227 Cognitive Interaction Technology. The Excellence   #\n");
520
  aosprintf("# Cluster EXC 227 is a grant of the Deutsche Forschungsgemeinschaft  #\n");
521
  aosprintf("# (DFG) in the context of the German Excellence Initiative.          #\n");
522
  aosprintf("######################################################################\n");
523
  aosprintf("\n");
524

    
525
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3)
526
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS)
527
  AMIROOS_CFG_MAIN_INIT_HOOK_3(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS);
528
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS) */
529
  AMIROOS_CFG_MAIN_INIT_HOOK_3();
530
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS) */
531
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3) */
532

    
533
#if (AMIROOS_CFG_TESTS_ENABLE == true)
534
#if defined(MODULE_INIT_TESTS)
535
  MODULE_INIT_TESTS();
536
#else /* defined(MODULE_INIT_TESTS) */
537
  #warning "AMIROOS_CFG_TESTS_ENABLE set to true, but MODULE_INIT_TESTS() not defined"
538
#endif /* defined(MODULE_INIT_TESTS) */
539
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
540

    
541
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4)
542
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS)
543
  AMIROOS_CFG_MAIN_INIT_HOOK_4(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS);
544
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS) */
545
  AMIROOS_CFG_MAIN_INIT_HOOK_4();
546
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS) */
547
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4) */
548

    
549
#if (AMIROOS_CFG_SSSP_ENABLE == true)
550

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

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

    
630
  /*
631
   * There must be no delay at this point, thus no hook allowed.
632
   */
633

    
634
#if (AMIROOS_CFG_SSSP_MSI == true)
635
  {
636
    // initialize temporary MSI data
637
    aos_ssspmsidata_t msidata;
638
    aosSsspMsiInit(&msidata, EVENTMASK_SSSPDELAY, EVENTMASK_SSSPTIMEOUT, &_eventListenerGPIO);
639

    
640
    // execute module stack initialization (MSI) but react to unrelated events as well
641
    while ((shutdown == AOS_SHUTDOWN_NONE) && (aos.sssp.stage != AOS_SSSP_STAGE_OPERATION)) {
642
      // execute MSI routine
643
      aosSsspMsi(&msidata, &eventmask);
644
      // handly event (if any)
645
      if (eventmask) {
646
        // GPIO event
647
        if (eventmask & _eventListenerGPIO.events) {
648
          eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
649
          // PD event
650
          if (eventflags & moduleSsspEventflagPD()) {
651
            shutdown = AOS_SHUTDOWN_PASSIVE;
652
          } else {
653
  #if defined(MODULE_SSSP_STARTUP_3_GPIOEVENT_HOOK)
654
            MODULE_SSSP_STARTUP_3_GPIOEVENT_HOOK(eventmask, eventflags);
655
  #else /* defined(MODULE_SSSP_STARTUP_3_GPIOEVENT_HOOK) */
656
            /* silently ignore any other GPIO events */
657
  #endif /* defined(MODULE_SSSP_STARTUP_3_GPIOEVENT_HOOK) */
658
          }
659
        }
660
        // OS event
661
        else if (eventmask & _eventListenerOS.events) {
662
          eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
663
          _unexpectedEventError(eventmask, eventflags);
664
        }
665
        // unknown event
666
        else {
667
          _unexpectedEventError(eventmask, 0);
668
        }
669
      }
670
    }
671
  }
672

    
673
#if (HAL_USE_RTC == TRUE)
674

    
675
  /*
676
   * calendar synchronization
677
   *
678
   * Although calendar synchronization does not inherently rely on SSSP, this
679
   * implementation uses the moduleSsspBcbX() functions, defined by SSSP in
680
   * order to save redundant code. Furthermore, it is rather unlikely that
681
   * system wide synchronization of calendars is desired when SSSP is disabled
682
   * anyway.
683
   */
684
 if (shutdown == AOS_SHUTDOWN_NONE) {
685
#if (AMIROOS_CFG_SSSP_MASTER == true)
686
   // temporary variables
687
   struct tm t;
688
   uint8_t buffer[sizeof(uint64_t)];
689

    
690
   // get current date/time
691
   aosSysGetDateTime(&t);
692
   // encode & serialize
693
   _serializeU64(buffer, _TM2U64(&t));
694
   // transmit
695
   aosDbgPrintf("transmitting current date/time...\t");
696
   if (moduleSsspBcbTransmit(buffer, sizeof(uint64_t)) == AOS_SSSP_BCB_SUCCESS) {
697
     aosDbgPrintf("ok\n");
698
   } else {
699
     aosDbgPrintf("fail\n");
700
   }
701
#else /* (AMIROOS_CFG_SSSP_MASTER == true) */
702
   // temporary variables
703
   aos_timestamp_t start, now;
704
   aos_ssspbcbstatus_t bcbstatus;
705
   uint8_t buffer[sizeof(uint64_t)];
706
   struct tm t;
707

    
708
   // receive
709
   aosDbgPrintf("receiving current date/time...\t");
710
   aosSysGetUptime(&start);
711
   do {
712
     bcbstatus = moduleSsspBcbReceive(buffer, sizeof(uint64_t));
713
     aosSysGetUptime(&now);
714
   } while ((bcbstatus != AOS_SSSP_BCB_SUCCESS) && ((now - start) < AOS_SSSP_TIMEOUT));
715
   // if something was received
716
   if (bcbstatus == AOS_SSSP_BCB_SUCCESS) {
717
     // deserialize & decode
718
     _U642TM(&t, _deserializeU64(buffer));
719
     // set current date/time
720
     aosSysSetDateTime(&t);
721
     aosDbgPrintf("ok\n");
722
   } else {
723
     aosDbgPrintf("fail\n");
724
   }
725
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
726
 }
727

    
728
#endif /* (HAL_USE_RTC == TRUE) */
729
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
730

    
731
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
732

    
733
  /* completely start AMiRo-OS */
734
  if (shutdown == AOS_SHUTDOWN_NONE) {
735
    aosSysStart();
736
  }
737

    
738
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5)
739
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS)
740
  AMIROOS_CFG_MAIN_INIT_HOOK_5(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS);
741
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS) */
742
  AMIROOS_CFG_MAIN_INIT_HOOK_5();
743
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS) */
744
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5) */
745

    
746
  /*
747
   * ##########################################################################
748
   * # infinite loop                                                          #
749
   * ##########################################################################
750
   */
751

    
752
#if (AMIROOS_CFG_SSSP_ENABLE == true)
753
  /* ignore all SSSP signal events, except for PD and configured events */
754
  eventflags = moduleSsspEventflagS();
755
#if (AMIROOS_CFG_SSSP_MSI == true)
756
#if (AMIROOS_CFG_SSSP_STACK_START != true)
757
  eventflags |= moduleSsspEventflagDN();
758
#endif /* (AMIROOS_CFG_SSSP_STACK_START != true) */
759
#if (AMIROOS_CFG_SSSP_STACK_END != true)
760
  eventflags |= moduleSsspEventflagUP();
761
#endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
762
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
763
  eventflags &= ~((eventflags_t)AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK);
764
  _eventListenerGPIO.wflags &= ~eventflags;
765
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
766

    
767
  // sleep until a shutdown event is received
768
  while (shutdown == AOS_SHUTDOWN_NONE) {
769
    // wait for an event
770
#if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0)
771
    eventmask = chEvtWaitOneTimeout(ALL_EVENTS, chTimeUS2I(AMIROOS_CFG_MAIN_LOOP_TIMEOUT));
772
#else /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0) */
773
    eventmask = chEvtWaitOne(ALL_EVENTS);
774
#endif /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0) */
775

    
776
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1)
777
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS)
778
    AMIROOS_CFG_MAIN_LOOP_HOOK_1(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS);
779
#else /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS) */
780
    AMIROOS_CFG_MAIN_LOOP_HOOK_1();
781
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS) */
782
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1) */
783

    
784
    switch (eventmask) {
785
      // if this was an GPIO event
786
      case EVENTMASK_GPIO:
787
        // evaluate flags
788
        eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
789
#if (AMIROOS_CFG_SSSP_ENABLE == true)
790
        // PD event
791
        if (eventflags & moduleSsspEventflagPD()) {
792
          aosSsspShutdownInit(false);
793
          shutdown = AOS_SHUTDOWN_PASSIVE;
794
        }
795
        // all other events
796
#if defined(MODULE_MAIN_LOOP_GPIOEVENT)
797
        else {
798
          MODULE_MAIN_LOOP_GPIOEVENT(eventflags);
799
        }
800
#endif /* defined(MODULE_MAIN_LOOP_GPIOEVENT) */
801
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
802
#if defined(MODULE_MAIN_LOOP_GPIOEVENT)
803
        MODULE_MAIN_LOOP_GPIOEVENT(eventflags);
804
#endif /* defined(MODULE_MAIN_LOOP_GPIOEVENT) */
805
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
806
        break;
807

    
808
      // if this was an OS event
809
      case EVENTMASK_OS:
810
        // evaluate flags
811
        eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
812
        switch (eventflags) {
813
#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_NONE)
814
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_MASK:
815
            shutdown = AOS_SHUTDOWN_ACTIVE;
816
            break;
817
#elif (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
818
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_HIBERNATE:
819
            shutdown = AOS_SHUTDOWN_HIBERNATE;
820
            break;
821
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_DEEPSLEEP:
822
            shutdown = AOS_SHUTDOWN_DEEPSLEEP;
823
            break;
824
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_TRANSPORTATION:
825
            shutdown = AOS_SHUTDOWN_TRANSPORTATION;
826
            break;
827
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_RESTART:
828
            shutdown = AOS_SHUTDOWN_RESTART;
829
            break;
830
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_PASSIVE:
831
            _unexpectedEventError(eventmask, eventflags);
832
            break;
833
#endif /* (AMIROOS_CFG_BOOTLOADER == X) */
834
          default:
835
            _unexpectedEventError(eventmask, eventflags);
836
            break;
837
        }
838
        break;
839

    
840
      // if this was any other event (should be impossible to occur)
841
      default:
842
        eventflags = 0;
843
#if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT == 0)
844
        _unexpectedEventError(eventmask, eventflags);
845
#endif /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT == 0) */
846
        break;
847
    }
848

    
849
#if (AMIROOS_CFG_SSSP_ENABLE == true)
850

    
851
    /*
852
     * Re-anable the S signal GPIO event.
853
     * Note that events for the optional signals UP and DN are not enabled as they are not utilized during SSSP shutdown phase.
854
     */
855
    chSysLock();
856
    _eventListenerGPIO.wflags |= moduleSsspEventflagS();
857
    chSysUnlock();
858

    
859
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
860

    
861
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2)
862
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2_ARGS)
863
    AMIROOS_CFG_MAIN_LOOP_HOOK_2(AMIROOS_CFG_MAIN_LOOP_HOOK_2_ARGS);
864
#else /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2_ARGS) */
865
    AMIROOS_CFG_MAIN_LOOP_HOOK_2();
866
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2_ARGS) */
867
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2) */
868
  }
869

    
870
  /*
871
   * ##########################################################################
872
   * # system shutdown                                                        #
873
   * ##########################################################################
874
   */
875

    
876
  aosDbgAssert(shutdown != AOS_SHUTDOWN_NONE);
877

    
878
  // initialize/acknowledge shutdown
879
  aosSysShutdownInit(shutdown);
880
#if (AMIROOS_CFG_SSSP_ENABLE == true)
881
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_1_2);
882
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
883

    
884
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1)
885
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS)
886
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS);
887
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS) */
888
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1();
889
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS) */
890
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1) */
891

    
892
  // wait for all threads to terminate
893
  aosSysStop();
894

    
895
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2)
896
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS)
897
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS);
898
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS) */
899
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2();
900
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS) */
901
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2) */
902

    
903
  // deinitialize system
904
  aosSysDeinit();
905

    
906
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3)
907
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS)
908
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS);
909
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS) */
910
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3();
911
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS) */
912
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3) */
913

    
914
  /* stop all periphery communication interfaces */
915
#if defined(MODULE_SHUTDOWN_PERIPHERY_IF)
916
  MODULE_SHUTDOWN_PERIPHERY_IF();
917
#endif /* defined(MODULE_SHUTDOWN_PERIPHERY_IF) */
918

    
919
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4)
920
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS)
921
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS);
922
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS) */
923
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4();
924
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS) */
925
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4) */
926

    
927
#if (AMIROOS_CFG_SSSP_ENABLE == true)
928

    
929
  // proceed to SSSP shutdown stage 1.3
930
  aosSsspProceed(NULL, 0, NULL);
931
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_1_3);
932

    
933
#if (AMIROOS_CFG_SSSP_SHUTDOWN == true)
934

    
935
  /* AMiRo-OS performs SSSP shutdown phase
936
   * NOTE:
937
   * The initiating module could deactivate PD at this point to indicate a restart rather than a shutdown request.
938
   * AMiRo-OS does not support restart though, thus the signal is not changed.
939
   */
940

    
941
  // proceed to SSSP shutdown stage 2.1
942
  while (aosSsspProceed(&_eventListenerGPIO, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS) {
943
    /*
944
     * This code is executed if the received event was not about a deactivation of the synchronization signal.
945
     * The received event could be caused by any listener.
946
     */
947
    // GPIO event
948
    if (eventmask & _eventListenerGPIO.events) {
949
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
950
#if defined(MODULE_SSSP_SHUTDOWN_1_3_GPIOEVENT_HOOK)
951
        MODULE_SSSP_SHUTDOWN_1_3_GPIOEVENT_HOOK(eventmask, eventflags);
952
#else /* defined(MODULE_SSSP_SHUTDOWN_1_3_GPIOEVENT_HOOK) */
953
        /* silently ignore any other GPIO events */
954
#endif /* defined(MODULE_SSSP_SHUTDOWN_1_3_GPIOEVENT_HOOK) */
955
    }
956
    // OS event
957
    else if (eventmask & _eventListenerOS.events) {
958
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
959
      _unexpectedEventError(eventmask, eventflags);
960
    }
961
    // unknown event
962
    else {
963
      _unexpectedEventError(eventmask, 0);
964
    }
965
  }
966
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_2_1);
967

    
968
  // proceed to SSSP shutdown stage 2.2
969
  aosSsspProceed(NULL, 0, &eventmask);
970
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_2_2);
971
  if (shutdown == AOS_SHUTDOWN_PASSIVE) {
972
    aosDbgPrintf("%s request received\n", eventmask ? "shutdown" : "restart");
973
  }
974
  /* NOTE:
975
   * Actually the call of aosSsspProceed() returned an indicator, whether a shutdown or restart has been initiated.
976
   * Since AMiRo-OS does not support restart, this return value is ignored and shutdown is assumed.
977
   */
978

    
979
  // the initiating module broadcasts the shutdown identifier
980
  if (shutdown != AOS_SHUTDOWN_PASSIVE) {
981
    // since AMiRo-OS does not support multiple ways to shutdown or restart, the special identifier 0 is broadcasted.
982
    aosSsspShutdownBroadcastIdentifier(0);
983
  }
984
  // passive modules receive the broadcasted identifier
985
  else {
986
    unsigned int identifier = 0;
987

    
988
    // receive the identifier, which specifies the type of shutdown/restart
989
    while ((eventmask = aosSsspShutdownWaitForIdentifierPulse(&_eventListenerGPIO, moduleSsspEventflagS(), EVENTMASK_SSSPDELAY, &identifier)) != EVENTMASK_SSSPDELAY) {
990
      // GPIO event
991
      if (eventmask & _eventListenerGPIO.events) {
992
        eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
993
#if defined(MODULE_SSSP_SHUTDOWN_2_2_GPIOEVENT_HOOK)
994
        MODULE_SSSP_SHUTDOWN_2_2_GPIOEVENT_HOOK(eventmask, eventflags);
995
#else /* defined(MODULE_SSSP_SHUTDOWN_2_2_GPIOEVENT_HOOK) */
996
        /* silently ignore any other GPIO events */
997
#endif /* defined(MODULE_SSSP_SHUTDOWN_2_2_GPIOEVENT_HOOK) */
998
        /* silently ignore any GPIO events */
999
      }
1000
      // OS event
1001
      else if (eventmask & _eventListenerOS.events) {
1002
        eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
1003
        _unexpectedEventError(eventmask, eventflags);
1004
      }
1005
      // unknown event
1006
      else {
1007
        _unexpectedEventError(eventmask, 0);
1008
      }
1009
    }
1010

    
1011
    // Since AMiRo-OS does not support multiple ways to shutdown or restart, the identifier is ignored.
1012
  }
1013

    
1014
  // proceed to SSSP shutdown stage 2.3
1015
  aosSsspProceed(NULL, 0, NULL);
1016
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_2_3);
1017

    
1018
  /*
1019
   * Since AMiRo-OS does only support one basic shutdown and no restart at all, the final shutdown is kept very simple.
1020
   */
1021

    
1022
  // disable interrupts
1023
  irqDeinit();
1024
  chSysDisable();
1025

    
1026
  chThdExit(MSG_OK);
1027
  return MSG_OK;
1028

    
1029
#else /* (AMIROOS_CFG_SSSP_SHUTDOWN == true) */
1030

    
1031
  /* hand over to the bootloader to perform SSSP shutdown phase */
1032
  aosSysShutdownToBootloader(shutdown);
1033

    
1034
  aosDbgAssertMsg(false, "AMiRo-OS must not proceed to this point!");
1035
  chThdExit(MSG_RESET);
1036
  return MSG_RESET;
1037

    
1038
#endif /* (AMIROOS_CFG_SSSP_SHUTDOWN == true) */
1039

    
1040
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
1041

    
1042
  /*
1043
   * Depending on the configured bootloader, trivial shutdown is either executed by AMiRo-OS or by that bootloader.
1044
   */
1045
#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_NONE)
1046

    
1047
  /* AMiRo-OS performs trivial shutdown */
1048

    
1049
  // disable interrupts
1050
  irqDeinit();
1051
  chSysDisable();
1052

    
1053
  chThdExit(MSG_OK);
1054
  return MSG_OK;
1055

    
1056
#elif (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
1057

    
1058
  /* AMiRo-BLT performns shutdown */
1059
  aosSysShutdownToBootloader(shutdown);
1060

    
1061
  aosDbgAssertMsg(false, "AMiRo-OS must not proceed to this point!");
1062
  chThdExit(MSG_RESET);
1063
  return MSG_RESET;
1064

    
1065
#endif /* (AMIROOS_CFG_BOOTLOADER == X) */
1066
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
1067
}
1068

    
1069
/******************************************************************************/
1070
/* EXPORTED FUNCTIONS                                                         */
1071
/******************************************************************************/
1072

    
1073
/** @} */