Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_main.cpp @ 510b93cc

History | View | Annotate | Download (34.1 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
 *
23
 * @addtogroup aos_system
24
 * @{
25
 */
26
27 3940ba8a Thomas Schöpping
#include <amiroos.h>
28 e545e620 Thomas Schöpping
29 b6b45e4c Thomas Schöpping
/*
30
 * hook to add further includes
31
 */
32 512abac1 Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER)
33
#include AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER
34 7de0cc90 Thomas Schöpping
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER) */
35 b6b45e4c Thomas Schöpping
36 f3ac1c96 Thomas Schöpping
/******************************************************************************/
37
/* LOCAL DEFINITIONS                                                          */
38
/******************************************************************************/
39
40 e545e620 Thomas Schöpping
/**
41 cda14729 Thomas Schöpping
 * @brief   Event mask to identify GPIO events.
42 e545e620 Thomas Schöpping
 */
43 cda14729 Thomas Schöpping
#define EVENTMASK_GPIO                          EVENT_MASK(0)
44 e545e620 Thomas Schöpping
45
/**
46
 * @brief   Event mask to identify OS events.
47
 */
48 cda14729 Thomas Schöpping
#define EVENTMASK_OS                            EVENT_MASK(1)
49 e545e620 Thomas Schöpping
50 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
51 933df08e Thomas Schöpping
52
/**
53 cda14729 Thomas Schöpping
 * @brief   Event mask to identify SSSP timer events.
54 933df08e Thomas Schöpping
 */
55 cda14729 Thomas Schöpping
#define EVENTMASK_SSSPTIMER                     EVENT_MASK(2)
56 933df08e Thomas Schöpping
57 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
58 9ebb11a9 Thomas Schöpping
59 f3ac1c96 Thomas Schöpping
/******************************************************************************/
60
/* EXPORTED VARIABLES                                                         */
61
/******************************************************************************/
62
63
/******************************************************************************/
64
/* LOCAL TYPES                                                                */
65
/******************************************************************************/
66
67
/******************************************************************************/
68
/* LOCAL VARIABLES                                                            */
69
/******************************************************************************/
70
71 9461fadc Thomas Schöpping
/**
72 cda14729 Thomas Schöpping
 * @brief   Listener object for GPIO events.
73 e545e620 Thomas Schöpping
 */
74 cda14729 Thomas Schöpping
static event_listener_t _eventListenerGPIO;
75 e545e620 Thomas Schöpping
76
/**
77
 * @brief   Listener object for OS events.
78
 */
79
static event_listener_t _eventListenerOS;
80
81
#if defined(MODULE_HAL_PROGIF) || defined(__DOXYGEN__)
82 cda14729 Thomas Schöpping
83 e545e620 Thomas Schöpping
/**
84 ba516b61 Thomas Schöpping
 * @brief   I/O channel for the programmer interface.
85 e545e620 Thomas Schöpping
 */
86 ba516b61 Thomas Schöpping
static AosIOChannel _stdiochannel;
87
88 cda14729 Thomas Schöpping
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
89
90
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
91
92 ba516b61 Thomas Schöpping
/**
93
 * @brief   I/O shell channel for the programmer interface.
94
 */
95
static AosShellChannel _stdshellchannel;
96 cda14729 Thomas Schöpping
97 1d3e002f Thomas Schöpping
#endif /* defined(MODULE_HAL_PROGIF) */
98 e545e620 Thomas Schöpping
99 b6b45e4c Thomas Schöpping
/*
100
 * hook to add further static variables
101
 */
102
#if defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES)
103
AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES
104 7de0cc90 Thomas Schöpping
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES) */
105 b6b45e4c Thomas Schöpping
106 f3ac1c96 Thomas Schöpping
/******************************************************************************/
107
/* LOCAL FUNCTIONS                                                            */
108
/******************************************************************************/
109
110 e545e620 Thomas Schöpping
/**
111
 * @brief   Prints an error message about an unexpected event.
112
 *
113
 * @param[in] mask    The event mask.
114
 * @param[in] flags   The event flags.
115
 */
116 933df08e Thomas Schöpping
static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags)
117 e545e620 Thomas Schöpping
{
118 6b53f6bf Thomas Schöpping
#if (AMIROOS_CFG_DBG == true)
119 1e5f7648 Thomas Schöpping
  aosprintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
120 7de0cc90 Thomas Schöpping
#else /* (AMIROOS_CFG_DBG == true) */
121 933df08e Thomas Schöpping
  (void)(mask);
122
  (void)(flags);
123 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_DBG == true) */
124 3e1a9c79 Thomas Schöpping
  return;
125 933df08e Thomas Schöpping
}
126
127 e545e620 Thomas Schöpping
/**
128 9461fadc Thomas Schöpping
 * @brief   Helper function to serialize data.
129 933df08e Thomas Schöpping
 *
130
 * @param[out]  dst   Pointer to the output buffer.
131 9461fadc Thomas Schöpping
 * @param[in]   src   Data to be serialized.
132
 * @param[in]   n     Number of bytes to serialize.
133 933df08e Thomas Schöpping
 */
134 9461fadc Thomas Schöpping
inline void _serialize(uint8_t* dst, const uint64_t src, const uint8_t n)
135 933df08e Thomas Schöpping
{
136
  aosDbgCheck(dst != NULL);
137 9461fadc Thomas Schöpping
  aosDbgCheck(n > 0 && n <= 8);
138 933df08e Thomas Schöpping
139 9461fadc Thomas Schöpping
  for (uint8_t byte = 0; byte < n; ++byte) {
140 933df08e Thomas Schöpping
    dst[byte] = (uint8_t)((src >> (byte * 8)) & 0xFF);
141
  }
142
143
  return;
144
}
145
146
/**
147 9461fadc Thomas Schöpping
 * @brief   Helper function to deserialize data.
148 933df08e Thomas Schöpping
 *
149
 * @param[in] src   Pointer to the buffer of data to be deserialzed.
150 9461fadc Thomas Schöpping
 * @param[in] n     Number of bytes to deserialize.
151 933df08e Thomas Schöpping
 *
152
 * @return    The deserialized 32 bit data.
153
 */
154 9461fadc Thomas Schöpping
inline uint64_t _deserialize(uint8_t* src, const uint8_t n)
155 933df08e Thomas Schöpping
{
156
  aosDbgCheck(src != NULL);
157 9461fadc Thomas Schöpping
  aosDbgCheck(n > 0 && n <= 8);
158 933df08e Thomas Schöpping
159 9461fadc Thomas Schöpping
  uint64_t result = 0;
160
  for (uint8_t byte = 0; byte < n; ++byte) {
161
    result |= ((uint64_t)src[byte]) << (byte * 8);
162
  }
163
164
  return result;
165
}
166
167 23437e98 Thomas Schöpping
#if (HAL_USE_RTC == TRUE) || defined(__DOXYGEN__)
168
169 9461fadc Thomas Schöpping
/**
170
 * @brief   Converter function to encode a TM value to a single unsigned 64 bit integer.
171
 *
172
 * @details Contents of the TM struct are mapped as follows:
173
 *            bits  |63     62|61      53|52    50|49         26|25     22|21     17|16     12|11      6|5       0|
174
 *            #bits |       2 |        9 |      3 |          24 |       4 |       5 |       5 |       6 |       6 |
175
 *            value |   isdst |     yday |   wday |        year |     mon |    mday |    hour |     min |     sec |
176
 *            range | special | [0, 365] | [0, 6] | [1900, ...] | [0, 11] | [1, 31] | [0, 23] | [0, 59] | [0, 61] |
177
 *          The Daylight Saving Time Flag (isdsst) is encoded as follows:
178
 *            DST not in effect         -> 0
179
 *            DST in effect             -> 1
180
 *            no information available  -> 2
181
 *
182
 * @param[in] src   Pointer to the TM struct to encode.
183
 *
184
 * @return  An unsigned 64 bit integer, which holds the encoded time value.
185
 */
186
inline uint64_t _TM2U64(struct tm* src)
187
{
188
  aosDbgCheck(src != NULL);
189
190
  return (((uint64_t)(src->tm_sec  & 0x0000003F) << (0))               |
191
          ((uint64_t)(src->tm_min  & 0x0000003F) << (6))               |
192
          ((uint64_t)(src->tm_hour & 0x0000001F) << (12))              |
193
          ((uint64_t)(src->tm_mday & 0x0000001F) << (17))              |
194
          ((uint64_t)(src->tm_mon  & 0x0000000F) << (22))              |
195
          ((uint64_t)(src->tm_year & 0x00FFFFFF) << (26))              |
196
          ((uint64_t)(src->tm_wday & 0x00000007) << (50))              |
197
          ((uint64_t)(src->tm_yday & 0x000001FF) << (53))              |
198
          ((uint64_t)((src->tm_isdst == 0) ? 0 : (src->tm_isdst > 0) ? 1 : 2) << (62)));
199
}
200
201
/**
202
 * @brief   Converter functiomn to retrieve the encoded TM value from an unsigned 64 bit integer.
203
 *
204
 * @details For information on the encoding, please refer to @p _TM2U64 function.
205
 *
206
 * @param[out] dst  The TM struct to fill with the decoded values.
207
 * @param[in]  src  Unsigned 64 bit integer holding the encoded TM value.
208
 */
209
inline void _U642TM(struct tm* dst, const uint64_t src)
210
{
211
  aosDbgCheck(dst != NULL);
212
213
  dst->tm_sec  = (src >> 0)  & 0x0000003F;
214
  dst->tm_min  = (src >> 6)  & 0x0000003F;
215
  dst->tm_hour = (src >> 12) & 0x0000001F;
216
  dst->tm_mday = (src >> 17) & 0x0000001F;
217
  dst->tm_mon  = (src >> 22) & 0x0000000F;
218
  dst->tm_year = (src >> 26) & 0x00FFFFFF;
219
  dst->tm_wday = (src >> 50) & 0x00000007;
220
  dst->tm_yday = (src >> 53) & 0x000001FF;
221
  dst->tm_isdst = (((src >> 62) & 0x03) == 0) ? 0 : (((src >> 62) & 0x03) > 0) ? 1 : -1;
222
223
  return;
224 933df08e Thomas Schöpping
}
225
226 7de0cc90 Thomas Schöpping
#endif /* (HAL_USE_RTC == TRUE) */
227 23437e98 Thomas Schöpping
228 933df08e Thomas Schöpping
/**
229 e545e620 Thomas Schöpping
 * @brief   Application entry point.
230
 */
231
int main(void)
232
{
233
  // local variables
234
  eventmask_t eventmask = 0;
235
  eventflags_t eventflags = 0;
236
  aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE;
237 b6b45e4c Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES)
238
  AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES
239 7de0cc90 Thomas Schöpping
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES) */
240 e545e620 Thomas Schöpping
241
  /*
242
   * ##########################################################################
243
   * # system initialization                                                  #
244
   * ##########################################################################
245
   */
246
247
  /* hardware, kernel, and operating system initialization */
248
  // ChibiOS/HAL and custom hal additions (if any)
249
  halInit();
250 7de0cc90 Thomas Schöpping
#if defined(MODULE_INIT_HAL_EXTRA)
251 e545e620 Thomas Schöpping
  MODULE_INIT_HAL_EXTRA();
252 7de0cc90 Thomas Schöpping
#endif /* defined(MODULE_INIT_HAL_EXTRA) */
253 e545e620 Thomas Schöpping
254
  // ChibiOS/RT kernel and custom kernel additions (if any)
255
  chSysInit();
256 7de0cc90 Thomas Schöpping
#if defined(MODULE_INIT_KERNEL_EXTRA)
257 e545e620 Thomas Schöpping
  MODULE_INIT_KERNEL_EXTRA();
258 7de0cc90 Thomas Schöpping
#endif /* defined(MODULE_INIT_KERNEL_EXTRA) */
259 e545e620 Thomas Schöpping
260 3106e8cc Thomas Schöpping
  // AMiRo-OS, additional interrupts and custom OS additions (if any)
261 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_SHELL_ENABLE == true)
262 6b53f6bf Thomas Schöpping
  aosSysInit(moduleShellPrompt);
263 cda14729 Thomas Schöpping
#else /* (AMIROOS_CFG_SHELL_ENABLE == true) */
264 6b53f6bf Thomas Schöpping
  aosSysInit();
265 cda14729 Thomas Schöpping
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
266 3106e8cc Thomas Schöpping
#if defined(MODULE_INIT_INTERRUPTS)
267
  MODULE_INIT_INTERRUPTS();
268
#endif
269 7de0cc90 Thomas Schöpping
#if defined(MODULE_INIT_OS_EXTRA)
270 e545e620 Thomas Schöpping
  MODULE_INIT_OS_EXTRA();
271 7de0cc90 Thomas Schöpping
#endif /* defined(MODULE_INIT_OS_EXTRA) */
272 e545e620 Thomas Schöpping
273
  /* event associations */
274 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
275 cda14729 Thomas Schöpping
  {
276
    eventflags_t flagsmask = AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK | MODULE_SSSP_EVENTFLAG_PD | MODULE_SSSP_EVENTFLAG_S;
277 2920c6b7 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_STACK_START != true)
278 cda14729 Thomas Schöpping
    flagsmask |= MODULE_SSSP_EVENTFLAG_DN;
279 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_STACK_START != true) */
280 2920c6b7 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_STACK_END != true)
281 cda14729 Thomas Schöpping
    flagsmask |= MODULE_SSSP_EVENTFLAG_UP;
282 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
283 cda14729 Thomas Schöpping
    chEvtRegisterMaskWithFlags(&aos.events.gpio, &_eventListenerGPIO, EVENTMASK_GPIO, flagsmask);
284
  }
285
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
286
  if (AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK) {
287
    chEvtRegisterMaskWithFlags(&aos.events.gpio, &_eventListenerGPIO, EVENTMASK_GPIO, AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK);
288
  }
289 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
290 cda14729 Thomas Schöpping
  chEvtRegisterMask(&aos.events.os, &_eventListenerOS, EVENTMASK_OS);
291
292
#if (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_STARTUP == true)
293
294
  /* perform SSSP basic initialization stage */
295
296
#if defined(MODULE_SSSP_BASICINIT_HOOK)
297
#if defined(MODULE_SSSP_BASICINIT_HOOK_ARGS)
298
  MODULE_SSSP_BASICINIT_HOOK(MODULE_SSSP_BASICINIT_HOOK_ARGS);
299
#else /* defined(MODULE_SSSP_BASICINIT_HOOK_ARGS) */
300
  MODULE_SSSP_BASICINIT_HOOK();
301
#endif /* defined(MODULE_SSSP_BASICINIT_HOOK_ARGS) */
302
#endif /* defined(MODULE_SSSP_BASICINIT_HOOK) */
303
304
  // proceed to startup stage 1.2
305
#if (AMIROOS_CFG_SSSP_MASTER == true)
306
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, 0, EVENTMASK_SSSPTIMER, &eventmask) != AOS_SUCCESS)) {
307
    /*
308
     * This code is executed if the received event was not about the delay.
309
     * The received event could be casued by any listener.
310
     */
311
    // GPIO event
312
    if (eventmask & _eventListenerGPIO.events) {
313
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
314
      // PD event
315
      if (eventflags & MODULE_SSSP_EVENTFLAG_PD) {
316
        shutdown = AOS_SHUTDOWN_PASSIVE;
317
      } else {
318
#if defined(MODULE_SSSP_STARTUP_1_1_GPIOEVENT_HOOK)
319
        MODULE_SSSP_STARTUP_1_1_GPIOEVENT_HOOK(eventmask, eventflags);
320
#else /* defined(MODULE_SSSP_STARTUP_1_1_GPIOEVENT_HOOK) */
321
        /* silently ignore any other GPIO events */
322
#endif /* defined(MODULE_SSSP_STARTUP_1_1_GPIOEVENT_HOOK) */
323
      }
324
    }
325
    // OS event
326
    else if (eventmask & _eventListenerOS.events) {
327
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
328
      _unexpectedEventError(eventmask, eventflags);
329
    }
330
    // unknown event
331
    else {
332
      _unexpectedEventError(eventmask, 0);
333
    }
334 2920c6b7 Thomas Schöpping
  }
335 cda14729 Thomas Schöpping
  if (shutdown == AOS_SHUTDOWN_NONE) {
336
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_1_2);
337
  }
338
#else /* (AMIROOS_CFG_SSSP_MASTER == true) */
339
  if (shutdown == AOS_SHUTDOWN_NONE) {
340
    aosSsspProceed(NULL, 0, 0, NULL);
341
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_1_2);
342
  }
343
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
344 e545e620 Thomas Schöpping
345 cda14729 Thomas Schöpping
  // proceed to startup stage 1.3
346
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, MODULE_SSSP_EVENTFLAG_S, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
347
    /*
348
     * This code is executed if the received event was not about a deactivation of the snychronization signal.
349
     * The received event could be caused by any listener.
350
     */
351
    // GPIO event
352
    if (eventmask & _eventListenerGPIO.events) {
353
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
354
      // PD event
355
      if (eventflags & MODULE_SSSP_EVENTFLAG_PD) {
356
        shutdown = AOS_SHUTDOWN_PASSIVE;
357
      } else {
358
#if defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK)
359
        MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK(eventmask, eventflags);
360
#else /* defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK) */
361
        /* silently ignore any other GPIO events */
362
#endif /* defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK) */
363
      }
364
    }
365
    // OS event
366
    else if (eventmask & _eventListenerOS.events) {
367
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
368
      _unexpectedEventError(eventmask, eventflags);
369
    }
370
    // unknown event
371
    else {
372
      _unexpectedEventError(eventmask, 0);
373
    }
374
  }
375
  if (shutdown == AOS_SHUTDOWN_NONE) {
376
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_1_3);
377
  }
378
379
  // proceed to startup stage 2.1
380
#if (AMIROOS_CFG_SSSP_MASTER == true)
381
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, 0, EVENTMASK_SSSPTIMER, &eventmask) != AOS_SUCCESS)) {
382
    /*
383
     * This code is executed if the received event was not about the delay.
384
     * The received event could be caused by any listener.
385
     */
386
    // GPIO event
387
    if (eventmask & _eventListenerGPIO.events) {
388
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
389
      // PD event
390
      if (eventflags & MODULE_SSSP_EVENTFLAG_PD) {
391
        shutdown = AOS_SHUTDOWN_PASSIVE;
392
      } else {
393
#if defined(MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK)
394
        MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK(eventmask, eventflags);
395
#else /* defined(MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK) */
396
        /* silently ignore any other GPIO events */
397
#endif /* defined(MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK) */
398
      }
399
    }
400
    // OS event
401
    else if (eventmask & _eventListenerOS.events) {
402
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
403
      _unexpectedEventError(eventmask, eventflags);
404
    }
405
    // unknown event
406
    else {
407
      _unexpectedEventError(eventmask, 0);
408
    }
409
  }
410
#else /* (AMIROOS_CFG_SSSP_MASTER == true) */
411 510b93cc Thomas Schöpping
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, MODULE_SSSP_EVENTFLAG_S, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
412 cda14729 Thomas Schöpping
    /*
413
     * This code is executed if the received event was not about a deactivation of the snychronization signal.
414
     * The received event could be caused by any listener.
415
     */
416
    // GPIO event
417
    if (eventmask & _eventListenerGPIO.events) {
418
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
419
      // PD event
420
      if (eventflags & MODULE_SSSP_EVENTFLAG_PD) {
421
        shutdown = AOS_SHUTDOWN_PASSIVE;
422
      } else {
423
#if defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK)
424
        MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK(eventmask, eventflags);
425
#else /* defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK) */
426
        /* silently ignore any other GPIO events */
427
#endif /* defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK) */
428
      }
429
    }
430
    // OS event
431
    else if (eventmask & _eventListenerOS.events) {
432
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
433
      _unexpectedEventError(eventmask, eventflags);
434
    }
435
    // unknown event
436
    else {
437
      _unexpectedEventError(eventmask, 0);
438
    }
439
  }
440
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
441
  if (shutdown == AOS_SHUTDOWN_NONE) {
442
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_2_1);
443
  }
444
445
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_STARTUP == true) */
446
447
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1)
448
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS)
449
  AMIROOS_CFG_MAIN_INIT_HOOK_1(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS);
450
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS) */
451
  AMIROOS_CFG_MAIN_INIT_HOOK_1();
452
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS) */
453
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_1) */
454 e545e620 Thomas Schöpping
455 4c72a54c Thomas Schöpping
  /* periphery communication interfaces initialization */
456
#if defined(MODULE_INIT_PERIPHERY_IF)
457 cda14729 Thomas Schöpping
  // module specific initialization
458 4c72a54c Thomas Schöpping
  MODULE_INIT_PERIPHERY_IF();
459
#endif /* defined(MODULE_INIT_PERIPHERY_IF) */
460 7de0cc90 Thomas Schöpping
#if defined(MODULE_HAL_PROGIF)
461 cda14729 Thomas Schöpping
  // user interface
462 ba516b61 Thomas Schöpping
  aosIOChannelInit(&_stdiochannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
463
  aosIOChannelOutputEnable(&_stdiochannel);
464
  aosIOStreamAddChannel(&aos.iostream, &_stdiochannel);
465 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_SHELL_ENABLE == true)
466 dd8738ea Thomas Schöpping
  aosShellChannelInit(&_stdshellchannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
467 ba516b61 Thomas Schöpping
  aosShellChannelInputEnable(&_stdshellchannel);
468
  aosShellChannelOutputEnable(&_stdshellchannel);
469 6b53f6bf Thomas Schöpping
  aosShellStreamAddChannel(&aos.shell.stream, &_stdshellchannel);
470 cda14729 Thomas Schöpping
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
471 7de0cc90 Thomas Schöpping
#endif /* defined(MODULE_HAL_PROGIF) */
472 e545e620 Thomas Schöpping
473 cda14729 Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2)
474
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS)
475
  AMIROOS_CFG_MAIN_INIT_HOOK_2(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS);
476
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS) */
477
  AMIROOS_CFG_MAIN_INIT_HOOK_2();
478
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS) */
479
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_2) */
480 e545e620 Thomas Schöpping
481
  /* module is ready -> print welcome prompt */
482
  aosprintf("\n");
483
  aosprintf("######################################################################\n");
484
  aosprintf("# AMiRo-OS is an operating system designed for the Autonomous Mini   #\n");
485
  aosprintf("# Robot (AMiRo) platform.                                            #\n");
486 84f0ce9e Thomas Schöpping
  aosprintf("# Copyright (C) 2016..2019  Thomas Schöpping et al.                  #\n");
487 e545e620 Thomas Schöpping
  aosprintf("#                                                                    #\n");
488
  aosprintf("# This is free software; see the source for copying conditions.      #\n");
489
  aosprintf("# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR  #\n");
490
  aosprintf("# A PARTICULAR PURPOSE.                                              #\n");
491
  aosprintf("# The development of this software was supported by the Excellence   #\n");
492
  aosprintf("# Cluster EXC 227 Cognitive Interaction Technology. The Excellence   #\n");
493
  aosprintf("# Cluster EXC 227 is a grant of the Deutsche Forschungsgemeinschaft  #\n");
494
  aosprintf("# (DFG) in the context of the German Excellence Initiative.          #\n");
495
  aosprintf("######################################################################\n");
496
  aosprintf("\n");
497
498 cda14729 Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3)
499
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS)
500
  AMIROOS_CFG_MAIN_INIT_HOOK_3(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS);
501
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS) */
502
  AMIROOS_CFG_MAIN_INIT_HOOK_3();
503
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS) */
504
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_3) */
505 7de40828 Thomas Schöpping
506
#if (AMIROOS_CFG_TESTS_ENABLE == true)
507
#if defined(MODULE_INIT_TESTS)
508
  MODULE_INIT_TESTS();
509 7de0cc90 Thomas Schöpping
#else /* defined(MODULE_INIT_TESTS) */
510
  #warning "AMIROOS_CFG_TESTS_ENABLE set to true, but MODULE_INIT_TESTS() not defined"
511
#endif /* defined(MODULE_INIT_TESTS) */
512
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
513 7de40828 Thomas Schöpping
514 cda14729 Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4)
515
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS)
516
  AMIROOS_CFG_MAIN_INIT_HOOK_4(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS);
517
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS) */
518
  AMIROOS_CFG_MAIN_INIT_HOOK_4();
519
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS) */
520
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_4) */
521 e545e620 Thomas Schöpping
522 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
523 cda14729 Thomas Schöpping
524
  // proceed to startup stage 2.2
525
#if (AMIROOS_CFG_SSSP_MASTER == true)
526
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, 0, EVENTMASK_SSSPTIMER, &eventmask) != AOS_SUCCESS)) {
527 e545e620 Thomas Schöpping
    /*
528 cda14729 Thomas Schöpping
     * This code is executed if the received event was not about the delay.
529
     * The received event could be caused by any listener.
530 e545e620 Thomas Schöpping
     */
531 cda14729 Thomas Schöpping
    // GPIO event
532
    if (eventmask & _eventListenerGPIO.events) {
533
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
534 9461fadc Thomas Schöpping
      // PD event
535 cda14729 Thomas Schöpping
      if (eventflags & MODULE_SSSP_EVENTFLAG_PD) {
536 9461fadc Thomas Schöpping
        shutdown = AOS_SHUTDOWN_PASSIVE;
537
      } else {
538 cda14729 Thomas Schöpping
#if defined(MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK)
539
        MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK(eventmask, eventflags);
540
#else /* defined(MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK) */
541
        /* silently ignore any other GPIO events */
542
#endif /* defined(MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK) */
543 9461fadc Thomas Schöpping
      }
544 e545e620 Thomas Schöpping
    }
545 9461fadc Thomas Schöpping
    // OS event
546 e545e620 Thomas Schöpping
    else if (eventmask & _eventListenerOS.events) {
547
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
548
      _unexpectedEventError(eventmask, eventflags);
549
    }
550 9461fadc Thomas Schöpping
    // unknown event
551 e545e620 Thomas Schöpping
    else {
552 933df08e Thomas Schöpping
      _unexpectedEventError(eventmask, 0);
553 e545e620 Thomas Schöpping
    }
554
  }
555 9461fadc Thomas Schöpping
  if (shutdown == AOS_SHUTDOWN_NONE) {
556 cda14729 Thomas Schöpping
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_2_2);
557 9461fadc Thomas Schöpping
  }
558 7de0cc90 Thomas Schöpping
#else /* (AMIROOS_CFG_SSSP_MASTER == true) */
559 cda14729 Thomas Schöpping
  if (shutdown == AOS_SHUTDOWN_NONE) {
560
    aosSsspProceed(NULL, 0, 0, NULL);
561
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_2_2);
562
  }
563
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
564 9461fadc Thomas Schöpping
565 cda14729 Thomas Schöpping
  // proceed to startup stage 3 (MSI is enabled), or to operation phase (no MSI)
566
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, MODULE_SSSP_EVENTFLAG_S, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
567
    /*
568
     * This code is executed if the received event was not about a deactivation of the snychronization signal.
569
     * The received event could be caused by any listener.
570
     */
571
    // GPIO event
572
    if (eventmask & _eventListenerGPIO.events) {
573
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
574
      // PD event
575
      if (eventflags & MODULE_SSSP_EVENTFLAG_PD) {
576
        shutdown = AOS_SHUTDOWN_PASSIVE;
577 9461fadc Thomas Schöpping
      } else {
578 cda14729 Thomas Schöpping
#if defined(MODULE_SSSP_STARTUP_2_2_GPIOEVENT_HOOK)
579
        MODULE_SSSP_STARTUP_2_2_GPIOEVENT_HOOK(eventmask, eventflags);
580
#else /* defined(MODULE_SSSP_STARTUP_2_2_GPIOEVENT_HOOK) */
581
        /* silently ignore any other GPIO events */
582
#endif /* defined(MODULE_SSSP_STARTUP_2_2_GPIOEVENT_HOOK) */
583 9461fadc Thomas Schöpping
      }
584
    }
585 cda14729 Thomas Schöpping
    // OS event
586
    else if (eventmask & _eventListenerOS.events) {
587
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
588
      _unexpectedEventError(eventmask, eventflags);
589
    }
590
    // unknown event
591
    else {
592
      _unexpectedEventError(eventmask, 0);
593
    }
594
  }
595
  if (shutdown == AOS_SHUTDOWN_NONE) {
596
#if (AMIROOS_CFG_SSSP_MSI == true)
597
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_3_1);
598
#else /* (AMIROOS_CFG_SSSP_MSI == true) */
599
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_OPERATION);
600
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
601 9461fadc Thomas Schöpping
  }
602 933df08e Thomas Schöpping
603 cda14729 Thomas Schöpping
  // start the internal system uptime counter
604
  aosSysStartUptime();
605
606
  /*
607
   * There must be no delays at this point, thus no hook allowed.
608
   */
609
610
#if (AMIROOS_CFG_SSSP_MSI == true)
611
612
  //TODO
613
614
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
615
616
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
617 e545e620 Thomas Schöpping
618
  /* completely start AMiRo-OS */
619 933df08e Thomas Schöpping
  if (shutdown == AOS_SHUTDOWN_NONE) {
620
    aosSysStart();
621
  }
622 e545e620 Thomas Schöpping
623 cda14729 Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5)
624
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS)
625
  AMIROOS_CFG_MAIN_INIT_HOOK_5(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS);
626
#else /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS) */
627
  AMIROOS_CFG_MAIN_INIT_HOOK_5();
628
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS) */
629
#endif /* defined(AMIROOS_CFG_MAIN_INIT_HOOK_5) */
630 e545e620 Thomas Schöpping
631
  /*
632
   * ##########################################################################
633
   * # infinite loop                                                          #
634
   * ##########################################################################
635
   */
636
637
  // sleep until a shutdown event is received
638
  while (shutdown == AOS_SHUTDOWN_NONE) {
639
    // wait for an event
640
#if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0)
641 1e5f7648 Thomas Schöpping
    eventmask = chEvtWaitOneTimeout(ALL_EVENTS, chTimeUS2I(AMIROOS_CFG_MAIN_LOOP_TIMEOUT));
642 7de0cc90 Thomas Schöpping
#else /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0) */
643 e545e620 Thomas Schöpping
    eventmask = chEvtWaitOne(ALL_EVENTS);
644 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0) */
645 e545e620 Thomas Schöpping
646 cda14729 Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1)
647
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS)
648
    AMIROOS_CFG_MAIN_LOOP_HOOK_1(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS);
649
#else /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS) */
650
    AMIROOS_CFG_MAIN_LOOP_HOOK_1();
651
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS) */
652
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1) */
653 e545e620 Thomas Schöpping
654
    switch (eventmask) {
655 cda14729 Thomas Schöpping
      // if this was an GPIO event
656
      case EVENTMASK_GPIO:
657 e545e620 Thomas Schöpping
        // evaluate flags
658 cda14729 Thomas Schöpping
        eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
659 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
660 e545e620 Thomas Schöpping
        // PD event
661 cda14729 Thomas Schöpping
        if (eventflags & MODULE_SSSP_EVENTFLAG_PD) {
662
          aosSsspShutdownInit();
663 e545e620 Thomas Schöpping
          shutdown = AOS_SHUTDOWN_PASSIVE;
664
        }
665
        // all other events
666 cda14729 Thomas Schöpping
#if defined(MODULE_MAIN_LOOP_GPIOEVENT)
667 e545e620 Thomas Schöpping
        else {
668 cda14729 Thomas Schöpping
          MODULE_MAIN_LOOP_GPIOEVENT(eventflags);
669 e545e620 Thomas Schöpping
        }
670 cda14729 Thomas Schöpping
#endif /* defined(MODULE_MAIN_LOOP_GPIOEVENT) */
671 7de0cc90 Thomas Schöpping
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
672 cda14729 Thomas Schöpping
#if defined(MODULE_MAIN_LOOP_GPIOEVENT)
673
        MODULE_MAIN_LOOP_GPIOEVENT(eventflags);
674
#endif /* defined(MODULE_MAIN_LOOP_GPIOEVENT) */
675 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
676 e545e620 Thomas Schöpping
        break;
677
678
      // if this was an OS event
679 cda14729 Thomas Schöpping
      case EVENTMASK_OS:
680 e545e620 Thomas Schöpping
        // evaluate flags
681
        eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
682
        switch (eventflags) {
683 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_NONE)
684
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_MASK:
685
            shutdown = AOS_SHUTDOWN_ACTIVE;
686
            break;
687
#elif (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
688
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_HIBERNATE:
689 e545e620 Thomas Schöpping
            shutdown = AOS_SHUTDOWN_HIBERNATE;
690
            break;
691 cda14729 Thomas Schöpping
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_DEEPSLEEP:
692 e545e620 Thomas Schöpping
            shutdown = AOS_SHUTDOWN_DEEPSLEEP;
693
            break;
694 cda14729 Thomas Schöpping
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_TRANSPORTATION:
695 e545e620 Thomas Schöpping
            shutdown = AOS_SHUTDOWN_TRANSPORTATION;
696
            break;
697 cda14729 Thomas Schöpping
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_RESTART:
698 e545e620 Thomas Schöpping
            shutdown = AOS_SHUTDOWN_RESTART;
699
            break;
700 cda14729 Thomas Schöpping
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_PASSIVE:
701
            _unexpectedEventError(eventmask, eventflags);
702 1d3e002f Thomas Schöpping
            break;
703 cda14729 Thomas Schöpping
#endif /* (AMIROOS_CFG_BOOTLOADER == X) */
704 e545e620 Thomas Schöpping
          default:
705
            _unexpectedEventError(eventmask, eventflags);
706
            break;
707
        }
708
        break;
709
710
      // if this was any other event (should be impossible to occur)
711
      default:
712
        eventflags = 0;
713 41fc7088 Thomas Schöpping
#if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT == 0)
714 e545e620 Thomas Schöpping
        _unexpectedEventError(eventmask, eventflags);
715 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT == 0) */
716 e545e620 Thomas Schöpping
        break;
717
    }
718
719 cda14729 Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2)
720
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2_ARGS)
721
    AMIROOS_CFG_MAIN_LOOP_HOOK_2(AMIROOS_CFG_MAIN_LOOP_HOOK_2_ARGS);
722
#else /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2_ARGS) */
723
    AMIROOS_CFG_MAIN_LOOP_HOOK_2();
724
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2_ARGS) */
725
#endif /* defined(AMIROOS_CFG_MAIN_LOOP_HOOK_2) */
726 e545e620 Thomas Schöpping
  }
727
728
  /*
729
   * ##########################################################################
730
   * # system shutdown                                                        #
731
   * ##########################################################################
732
   */
733
734 cda14729 Thomas Schöpping
  aosDbgAssert(shutdown != AOS_SHUTDOWN_NONE);
735 e545e620 Thomas Schöpping
736
  // initialize/acknowledge shutdown
737
  aosSysShutdownInit(shutdown);
738 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
739
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_1_2);
740
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
741 e545e620 Thomas Schöpping
742
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1)
743
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS)
744 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS);
745 7de0cc90 Thomas Schöpping
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS) */
746 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1();
747 7de0cc90 Thomas Schöpping
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS) */
748
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1) */
749 e545e620 Thomas Schöpping
750 cda14729 Thomas Schöpping
  // wait for all threads to terminate
751 e545e620 Thomas Schöpping
  aosSysStop();
752
753
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2)
754
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS)
755 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS);
756 7de0cc90 Thomas Schöpping
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS) */
757 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2();
758 7de0cc90 Thomas Schöpping
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS) */
759
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2) */
760 e545e620 Thomas Schöpping
761
  // deinitialize system
762
  aosSysDeinit();
763
764
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3)
765
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS)
766 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS);
767 7de0cc90 Thomas Schöpping
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS) */
768 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3();
769 7de0cc90 Thomas Schöpping
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS) */
770
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3) */
771 e545e620 Thomas Schöpping
772 4c72a54c Thomas Schöpping
  /* stop all periphery communication interfaces */
773
#if defined(MODULE_SHUTDOWN_PERIPHERY_IF)
774
  MODULE_SHUTDOWN_PERIPHERY_IF();
775
#endif /* defined(MODULE_SHUTDOWN_PERIPHERY_IF) */
776 e545e620 Thomas Schöpping
777
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4)
778
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS)
779 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS);
780 7de0cc90 Thomas Schöpping
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS) */
781 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4();
782 7de0cc90 Thomas Schöpping
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS) */
783
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4) */
784 e545e620 Thomas Schöpping
785 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
786
787
  // proceed to SSSP shutdown stage 1.3
788
  aosSsspProceed(NULL, 0, 0, NULL);
789
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_1_3);
790
791
#if (AMIROOS_CFG_SSSP_SHUTDOWN == true)
792
793
  /* AMiRo-OS performs SSSP shutdown phase
794
   * NOTE:
795
   * The initiating module could deactivate PD at this point to indicate a restart rather than a shutdown request.
796
   * AMiRo-OS does not support restart though, thus the signal is not changed.
797
   */
798
799
  // proceed to SSSP shutdown stage 2.1
800
  while (aosSsspProceed(&_eventListenerGPIO, MODULE_SSSP_EVENTFLAG_S, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS) {
801
    /*
802
     * This code is executed if the received event was not about a deactivation of the synchronization signal.
803
     * The received event could be caused by any listener.
804
     */
805
    // GPIO event
806
    if (eventmask & _eventListenerGPIO.events) {
807
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
808
#if defined(MODULE_SSSP_SHUTDOWN_1_3_GPIOEVENT_HOOK)
809
        MODULE_SSSP_SHUTDOWN_1_3_GPIOEVENT_HOOK(eventmask, eventflags);
810
#else /* defined(MODULE_SSSP_SHUTDOWN_1_3_GPIOEVENT_HOOK) */
811
        /* silently ignore any other GPIO events */
812
#endif /* defined(MODULE_SSSP_SHUTDOWN_1_3_GPIOEVENT_HOOK) */
813
    }
814
    // OS event
815
    else if (eventmask & _eventListenerOS.events) {
816
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
817
      _unexpectedEventError(eventmask, eventflags);
818
    }
819
    // unknown event
820
    else {
821
      _unexpectedEventError(eventmask, 0);
822
    }
823
  }
824
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_2_1);
825
826
  // proceed to SSSP shutdown stage 2.2
827
  aosSsspProceed(NULL, 0, 0, &eventmask);
828
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_2_2);
829
  if (shutdown == AOS_SHUTDOWN_PASSIVE) {
830
    aosDbgPrintf("%s request received\n", eventmask ? "shutdown" : "restart");
831
  }
832
  /* NOTE:
833
   * Actually the call of aosSsspProceed() returned an indicator, whether a shutdown or restart has been initiated.
834
   * Since AMiRo-OS does not support restart, this return value is ignored and shutdown is assumed.
835
   */
836
837
  // the initiating module broadcasts the shutdown identifier
838
  if (shutdown != AOS_SHUTDOWN_PASSIVE) {
839
    // since AMiRo-OS does not support multiple ways to shutdown or restart, the special identifier 0 is broadcasted.
840
    aosSsspShutdownBroadcastIdentifier(0);
841
  }
842
  // passive modules receive the broadcasted identifier
843
  else {
844
    unsigned int identifier = 0;
845
846
    // receive the identifier, which specifies the type of shutdown/restart
847
    while ((eventmask = aosSsspShutdownWaitForIdentifierPulse(&_eventListenerGPIO, MODULE_SSSP_EVENTFLAG_S, EVENTMASK_SSSPTIMER, &identifier)) != EVENTMASK_SSSPTIMER) {
848
      // GPIO event
849
      if (eventmask & _eventListenerGPIO.events) {
850
        eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
851
#if defined(MODULE_SSSP_SHUTDOWN_2_2_GPIOEVENT_HOOK)
852
        MODULE_SSSP_SHUTDOWN_2_2_GPIOEVENT_HOOK(eventmask, eventflags);
853
#else /* defined(MODULE_SSSP_SHUTDOWN_2_2_GPIOEVENT_HOOK) */
854
        /* silently ignore any other GPIO events */
855
#endif /* defined(MODULE_SSSP_SHUTDOWN_2_2_GPIOEVENT_HOOK) */
856
        /* silently ignore any GPIO events */
857
      }
858
      // OS event
859
      else if (eventmask & _eventListenerOS.events) {
860
        eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
861
        _unexpectedEventError(eventmask, eventflags);
862
      }
863
      // unknown event
864
      else {
865
        _unexpectedEventError(eventmask, 0);
866
      }
867
    }
868
869
    // Since AMiRo-OS does not support multiple ways to shutdown or restart, the identifier is ignored.
870
  }
871
872
  // proceed to SSSP shutdown stage 2.3
873
  aosSsspProceed(NULL, 0, 0, NULL);
874
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_2_3);
875 e545e620 Thomas Schöpping
876
  /*
877 cda14729 Thomas Schöpping
   * Since AMiRo-OS does only support one basic shutdown and no restart at all, the final shutdown is kept very simple.
878 e545e620 Thomas Schöpping
   */
879
880 cda14729 Thomas Schöpping
  // disable interrupts
881
  irqDeinit();
882
  chSysDisable();
883
884
  chThdExit(MSG_OK);
885
  return MSG_OK;
886
887
#else /* (AMIROOS_CFG_SSSP_SHUTDOWN == true) */
888
889
  /* hand over to the bootloader to perform SSSP shutdown phase */
890
  aosSysShutdownToBootloader(shutdown);
891
892
  aosDbgAssertMsg(false, "AMiRo-OS must not proceed to this point!");
893
  chThdExit(MSG_RESET);
894
  return MSG_RESET;
895
896
#endif /* (AMIROOS_CFG_SSSP_SHUTDOWN == true) */
897
898
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
899
900
  /*
901
   * Depending on the configured bootloader, trivial shutdown is either executed by AMiRo-OS or by that bootloader.
902
   */
903
#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_NONE)
904
905
  /* AMiRo-OS performs trivial shutdown */
906
907
  // disable interrupts
908
  irqDeinit();
909
  chSysDisable();
910
911
  chThdExit(MSG_OK);
912
  return MSG_OK;
913
914
#elif (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
915
916
  /* AMiRo-BLT performns shutdown */
917
  aosSysShutdownToBootloader(shutdown);
918
919
  aosDbgAssertMsg(false, "AMiRo-OS must not proceed to this point!");
920
  chThdExit(MSG_RESET);
921
  return MSG_RESET;
922
923
#endif /* (AMIROOS_CFG_BOOTLOADER == X) */
924
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
925 e545e620 Thomas Schöpping
}
926 53710ca3 Marc Rothmann
927 f3ac1c96 Thomas Schöpping
/******************************************************************************/
928
/* EXPORTED FUNCTIONS                                                         */
929
/******************************************************************************/
930
931 53710ca3 Marc Rothmann
/** @} */