Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_main.cpp @ 849b383a

History | View | Annotate | Download (38.637 KB)

1 e545e620 Thomas Schöpping
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3 96621a83 Thomas Schöpping
Copyright (C) 2016..2020  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 c53ef0b1 Thomas Schöpping
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 933df08e Thomas Schöpping
/**
63 c53ef0b1 Thomas Schöpping
 * @brief   Event mask to identify SSSP delay events.
64 933df08e Thomas Schöpping
 */
65 c53ef0b1 Thomas Schöpping
#define EVENTMASK_SSSPDELAY                     EVENT_MASK(3)
66 933df08e Thomas Schöpping
67 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
68 9ebb11a9 Thomas Schöpping
69 f3ac1c96 Thomas Schöpping
/******************************************************************************/
70
/* EXPORTED VARIABLES                                                         */
71
/******************************************************************************/
72
73
/******************************************************************************/
74
/* LOCAL TYPES                                                                */
75
/******************************************************************************/
76
77
/******************************************************************************/
78
/* LOCAL VARIABLES                                                            */
79
/******************************************************************************/
80
81 9461fadc Thomas Schöpping
/**
82 cda14729 Thomas Schöpping
 * @brief   Listener object for GPIO events.
83 e545e620 Thomas Schöpping
 */
84 cda14729 Thomas Schöpping
static event_listener_t _eventListenerGPIO;
85 e545e620 Thomas Schöpping
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 cda14729 Thomas Schöpping
93 e545e620 Thomas Schöpping
/**
94 ba516b61 Thomas Schöpping
 * @brief   I/O channel for the programmer interface.
95 e545e620 Thomas Schöpping
 */
96 ba516b61 Thomas Schöpping
static AosIOChannel _stdiochannel;
97
98 afcf6d89 Thomas Schöpping
#endif /* defined(MODULE_HAL_PROGIF) */
99 cda14729 Thomas Schöpping
100
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
101
102 ba516b61 Thomas Schöpping
/**
103
 * @brief   I/O shell channel for the programmer interface.
104
 */
105
static AosShellChannel _stdshellchannel;
106 cda14729 Thomas Schöpping
107 47e89ebf Thomas Schöpping
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
108 e545e620 Thomas Schöpping
109 b6b45e4c Thomas Schöpping
/*
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 7de0cc90 Thomas Schöpping
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES) */
115 b6b45e4c Thomas Schöpping
116 f3ac1c96 Thomas Schöpping
/******************************************************************************/
117
/* LOCAL FUNCTIONS                                                            */
118
/******************************************************************************/
119
120 e545e620 Thomas Schöpping
/**
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 88c47fd9 Thomas Schöpping
static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags)
127 e545e620 Thomas Schöpping
{
128 6b53f6bf Thomas Schöpping
#if (AMIROOS_CFG_DBG == true)
129 bc7aed20 Thomas Schöpping
  aosDbgPrintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
130 7de0cc90 Thomas Schöpping
#else /* (AMIROOS_CFG_DBG == true) */
131 933df08e Thomas Schöpping
  (void)(mask);
132
  (void)(flags);
133 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_DBG == true) */
134 3e1a9c79 Thomas Schöpping
  return;
135 933df08e Thomas Schöpping
}
136
137 bc7aed20 Thomas Schöpping
#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_RTC == TRUE)) || defined(__DOXYGEN__)
138
#if (AMIROOS_CFG_SSSP_MASTER == true) || defined(__DOXYGEN__)
139 23437e98 Thomas Schöpping
140 9461fadc Thomas Schöpping
/**
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 88c47fd9 Thomas Schöpping
static inline uint64_t _TM2U64(struct tm* src)
158 9461fadc Thomas Schöpping
{
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 bc7aed20 Thomas Schöpping
 * @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 88c47fd9 Thomas Schöpping
static inline void _serializeU64(uint8_t* dst, const uint64_t src)
180 bc7aed20 Thomas Schöpping
{
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 88c47fd9 Thomas Schöpping
static inline uint64_t _deserializeU64(uint8_t* src)
202 bc7aed20 Thomas Schöpping
{
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 9461fadc Thomas Schöpping
 * @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 88c47fd9 Thomas Schöpping
static inline void _U642TM(struct tm* dst, const uint64_t src)
222 9461fadc Thomas Schöpping
{
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 933df08e Thomas Schöpping
}
237
238 bc7aed20 Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_MASTER != true) */
239
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_RTC == TRUE) */
240 23437e98 Thomas Schöpping
241 933df08e Thomas Schöpping
/**
242 e545e620 Thomas Schöpping
 * @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 b6b45e4c Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES)
251
  AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES
252 7de0cc90 Thomas Schöpping
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES) */
253 e545e620 Thomas Schöpping
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 7de0cc90 Thomas Schöpping
#if defined(MODULE_INIT_HAL_EXTRA)
264 e545e620 Thomas Schöpping
  MODULE_INIT_HAL_EXTRA();
265 7de0cc90 Thomas Schöpping
#endif /* defined(MODULE_INIT_HAL_EXTRA) */
266 e545e620 Thomas Schöpping
267
  // ChibiOS/RT kernel and custom kernel additions (if any)
268
  chSysInit();
269 7de0cc90 Thomas Schöpping
#if defined(MODULE_INIT_KERNEL_EXTRA)
270 e545e620 Thomas Schöpping
  MODULE_INIT_KERNEL_EXTRA();
271 7de0cc90 Thomas Schöpping
#endif /* defined(MODULE_INIT_KERNEL_EXTRA) */
272 e545e620 Thomas Schöpping
273 3106e8cc Thomas Schöpping
  // AMiRo-OS, additional interrupts and custom OS additions (if any)
274 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_SHELL_ENABLE == true)
275 6b53f6bf Thomas Schöpping
  aosSysInit(moduleShellPrompt);
276 cda14729 Thomas Schöpping
#else /* (AMIROOS_CFG_SHELL_ENABLE == true) */
277 47e89ebf Thomas Schöpping
  aosSysInit(NULL);
278 cda14729 Thomas Schöpping
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
279 3106e8cc Thomas Schöpping
#if defined(MODULE_INIT_INTERRUPTS)
280
  MODULE_INIT_INTERRUPTS();
281
#endif
282 7de0cc90 Thomas Schöpping
#if defined(MODULE_INIT_OS_EXTRA)
283 e545e620 Thomas Schöpping
  MODULE_INIT_OS_EXTRA();
284 7de0cc90 Thomas Schöpping
#endif /* defined(MODULE_INIT_OS_EXTRA) */
285 e545e620 Thomas Schöpping
286
  /* event associations */
287 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
288 cda14729 Thomas Schöpping
  {
289 c53ef0b1 Thomas Schöpping
    eventflags_t flagsmask = AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK | moduleSsspEventflagPD() | moduleSsspEventflagS();
290 c218345a Thomas Schöpping
#if (AMIROOS_CFG_SSSP_MSI == true)
291 2920c6b7 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_STACK_START != true)
292 c53ef0b1 Thomas Schöpping
    flagsmask |= moduleSsspEventflagDN();
293 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_STACK_START != true) */
294 2920c6b7 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_STACK_END != true)
295 c53ef0b1 Thomas Schöpping
    flagsmask |= moduleSsspEventflagUP();
296 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
297 c218345a Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
298 cda14729 Thomas Schöpping
    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 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
305 cda14729 Thomas Schöpping
  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 c53ef0b1 Thomas Schöpping
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, EVENTMASK_SSSPDELAY, &eventmask) != AOS_SUCCESS)) {
322 cda14729 Thomas Schöpping
    /*
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 c53ef0b1 Thomas Schöpping
      if (eventflags & moduleSsspEventflagPD()) {
331 cda14729 Thomas Schöpping
        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 2920c6b7 Thomas Schöpping
  }
350 cda14729 Thomas Schöpping
  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 c53ef0b1 Thomas Schöpping
    aosSsspProceed(NULL, 0, NULL);
356 cda14729 Thomas Schöpping
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_1_2);
357
  }
358
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
359 e545e620 Thomas Schöpping
360 cda14729 Thomas Schöpping
  // proceed to startup stage 1.3
361 c53ef0b1 Thomas Schöpping
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
362 cda14729 Thomas Schöpping
    /*
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 c53ef0b1 Thomas Schöpping
      if (eventflags & moduleSsspEventflagPD()) {
371 cda14729 Thomas Schöpping
        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 c53ef0b1 Thomas Schöpping
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, EVENTMASK_SSSPDELAY, &eventmask) != AOS_SUCCESS)) {
397 cda14729 Thomas Schöpping
    /*
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 c53ef0b1 Thomas Schöpping
      if (eventflags & moduleSsspEventflagPD()) {
406 cda14729 Thomas Schöpping
        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 c53ef0b1 Thomas Schöpping
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
427 cda14729 Thomas Schöpping
    /*
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 c53ef0b1 Thomas Schöpping
      if (eventflags & moduleSsspEventflagPD()) {
436 cda14729 Thomas Schöpping
        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 e545e620 Thomas Schöpping
470 4c72a54c Thomas Schöpping
  /* periphery communication interfaces initialization */
471
#if defined(MODULE_INIT_PERIPHERY_IF)
472 cda14729 Thomas Schöpping
  // module specific initialization
473 4c72a54c Thomas Schöpping
  MODULE_INIT_PERIPHERY_IF();
474
#endif /* defined(MODULE_INIT_PERIPHERY_IF) */
475 7de0cc90 Thomas Schöpping
#if defined(MODULE_HAL_PROGIF)
476 cda14729 Thomas Schöpping
  // user interface
477 ba516b61 Thomas Schöpping
  aosIOChannelInit(&_stdiochannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
478
  aosIOChannelOutputEnable(&_stdiochannel);
479
  aosIOStreamAddChannel(&aos.iostream, &_stdiochannel);
480 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_SHELL_ENABLE == true)
481 dd8738ea Thomas Schöpping
  aosShellChannelInit(&_stdshellchannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
482 ba516b61 Thomas Schöpping
  aosShellChannelInputEnable(&_stdshellchannel);
483
  aosShellChannelOutputEnable(&_stdshellchannel);
484 6b53f6bf Thomas Schöpping
  aosShellStreamAddChannel(&aos.shell.stream, &_stdshellchannel);
485 cda14729 Thomas Schöpping
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
486 7de0cc90 Thomas Schöpping
#endif /* defined(MODULE_HAL_PROGIF) */
487 e545e620 Thomas Schöpping
488 cda14729 Thomas Schöpping
#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 e545e620 Thomas Schöpping
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 96621a83 Thomas Schöpping
  aosprintf("# Copyright (C) 2016..2020  Thomas Schöpping et al.                  #\n");
502 e545e620 Thomas Schöpping
  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 cda14729 Thomas Schöpping
#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 7de40828 Thomas Schöpping
521
#if (AMIROOS_CFG_TESTS_ENABLE == true)
522
#if defined(MODULE_INIT_TESTS)
523
  MODULE_INIT_TESTS();
524 7de0cc90 Thomas Schöpping
#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 7de40828 Thomas Schöpping
529 cda14729 Thomas Schöpping
#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 e545e620 Thomas Schöpping
537 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
538 cda14729 Thomas Schöpping
539
  // proceed to startup stage 2.2
540
#if (AMIROOS_CFG_SSSP_MASTER == true)
541 c53ef0b1 Thomas Schöpping
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, EVENTMASK_SSSPDELAY, &eventmask) != AOS_SUCCESS)) {
542 e545e620 Thomas Schöpping
    /*
543 cda14729 Thomas Schöpping
     * This code is executed if the received event was not about the delay.
544
     * The received event could be caused by any listener.
545 e545e620 Thomas Schöpping
     */
546 cda14729 Thomas Schöpping
    // GPIO event
547
    if (eventmask & _eventListenerGPIO.events) {
548
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
549 9461fadc Thomas Schöpping
      // PD event
550 c53ef0b1 Thomas Schöpping
      if (eventflags & moduleSsspEventflagPD()) {
551 9461fadc Thomas Schöpping
        shutdown = AOS_SHUTDOWN_PASSIVE;
552
      } else {
553 cda14729 Thomas Schöpping
#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 9461fadc Thomas Schöpping
      }
559 e545e620 Thomas Schöpping
    }
560 9461fadc Thomas Schöpping
    // OS event
561 e545e620 Thomas Schöpping
    else if (eventmask & _eventListenerOS.events) {
562
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
563
      _unexpectedEventError(eventmask, eventflags);
564
    }
565 9461fadc Thomas Schöpping
    // unknown event
566 e545e620 Thomas Schöpping
    else {
567 933df08e Thomas Schöpping
      _unexpectedEventError(eventmask, 0);
568 e545e620 Thomas Schöpping
    }
569
  }
570 9461fadc Thomas Schöpping
  if (shutdown == AOS_SHUTDOWN_NONE) {
571 cda14729 Thomas Schöpping
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_2_2);
572 9461fadc Thomas Schöpping
  }
573 7de0cc90 Thomas Schöpping
#else /* (AMIROOS_CFG_SSSP_MASTER == true) */
574 cda14729 Thomas Schöpping
  if (shutdown == AOS_SHUTDOWN_NONE) {
575 c53ef0b1 Thomas Schöpping
    aosSsspProceed(NULL, 0, NULL);
576 cda14729 Thomas Schöpping
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_2_2);
577
  }
578
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
579 9461fadc Thomas Schöpping
580 cda14729 Thomas Schöpping
  // proceed to startup stage 3 (MSI is enabled), or to operation phase (no MSI)
581 c53ef0b1 Thomas Schöpping
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
582 cda14729 Thomas Schöpping
    /*
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 c53ef0b1 Thomas Schöpping
      if (eventflags & moduleSsspEventflagPD()) {
591 cda14729 Thomas Schöpping
        shutdown = AOS_SHUTDOWN_PASSIVE;
592 9461fadc Thomas Schöpping
      } else {
593 cda14729 Thomas Schöpping
#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 9461fadc Thomas Schöpping
      }
599
    }
600 cda14729 Thomas Schöpping
    // 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 c53ef0b1 Thomas Schöpping
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_3);
613 cda14729 Thomas Schöpping
#else /* (AMIROOS_CFG_SSSP_MSI == true) */
614
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_OPERATION);
615
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
616 9461fadc Thomas Schöpping
  }
617 933df08e Thomas Schöpping
618 cda14729 Thomas Schöpping
  /*
619 c218345a Thomas Schöpping
   * There must be no delay at this point, thus no hook allowed.
620 cda14729 Thomas Schöpping
   */
621
622
#if (AMIROOS_CFG_SSSP_MSI == true)
623 c53ef0b1 Thomas Schöpping
  {
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 cda14729 Thomas Schöpping
661 c218345a Thomas Schöpping
#if (HAL_USE_RTC == TRUE)
662
663 bc7aed20 Thomas Schöpping
  /*
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 c218345a Thomas Schöpping
716
#endif /* (HAL_USE_RTC == TRUE) */
717 cda14729 Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
718
719
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
720 e545e620 Thomas Schöpping
721
  /* completely start AMiRo-OS */
722 933df08e Thomas Schöpping
  if (shutdown == AOS_SHUTDOWN_NONE) {
723
    aosSysStart();
724
  }
725 e545e620 Thomas Schöpping
726 cda14729 Thomas Schöpping
#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 e545e620 Thomas Schöpping
734
  /*
735
   * ##########################################################################
736
   * # infinite loop                                                          #
737
   * ##########################################################################
738
   */
739
740 c218345a Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
741
  /* ignore all SSSP signal events, except for PD and configured events */
742 c53ef0b1 Thomas Schöpping
  eventflags = moduleSsspEventflagS();
743 c218345a Thomas Schöpping
#if (AMIROOS_CFG_SSSP_MSI == true)
744
#if (AMIROOS_CFG_SSSP_STACK_START != true)
745 c53ef0b1 Thomas Schöpping
  eventflags |= moduleSsspEventflagDN();
746 c218345a Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_STACK_START != true) */
747
#if (AMIROOS_CFG_SSSP_STACK_END != true)
748 c53ef0b1 Thomas Schöpping
  eventflags |= moduleSsspEventflagUP();
749 c218345a Thomas Schöpping
#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 e545e620 Thomas Schöpping
  // 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 1e5f7648 Thomas Schöpping
    eventmask = chEvtWaitOneTimeout(ALL_EVENTS, chTimeUS2I(AMIROOS_CFG_MAIN_LOOP_TIMEOUT));
760 7de0cc90 Thomas Schöpping
#else /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0) */
761 e545e620 Thomas Schöpping
    eventmask = chEvtWaitOne(ALL_EVENTS);
762 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0) */
763 e545e620 Thomas Schöpping
764 cda14729 Thomas Schöpping
#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 e545e620 Thomas Schöpping
772
    switch (eventmask) {
773 cda14729 Thomas Schöpping
      // if this was an GPIO event
774
      case EVENTMASK_GPIO:
775 e545e620 Thomas Schöpping
        // evaluate flags
776 cda14729 Thomas Schöpping
        eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
777 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
778 e545e620 Thomas Schöpping
        // PD event
779 c53ef0b1 Thomas Schöpping
        if (eventflags & moduleSsspEventflagPD()) {
780 c218345a Thomas Schöpping
          aosSsspShutdownInit(false);
781 e545e620 Thomas Schöpping
          shutdown = AOS_SHUTDOWN_PASSIVE;
782
        }
783
        // all other events
784 cda14729 Thomas Schöpping
#if defined(MODULE_MAIN_LOOP_GPIOEVENT)
785 e545e620 Thomas Schöpping
        else {
786 cda14729 Thomas Schöpping
          MODULE_MAIN_LOOP_GPIOEVENT(eventflags);
787 e545e620 Thomas Schöpping
        }
788 cda14729 Thomas Schöpping
#endif /* defined(MODULE_MAIN_LOOP_GPIOEVENT) */
789 7de0cc90 Thomas Schöpping
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
790 cda14729 Thomas Schöpping
#if defined(MODULE_MAIN_LOOP_GPIOEVENT)
791
        MODULE_MAIN_LOOP_GPIOEVENT(eventflags);
792
#endif /* defined(MODULE_MAIN_LOOP_GPIOEVENT) */
793 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
794 e545e620 Thomas Schöpping
        break;
795
796
      // if this was an OS event
797 cda14729 Thomas Schöpping
      case EVENTMASK_OS:
798 e545e620 Thomas Schöpping
        // evaluate flags
799
        eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
800
        switch (eventflags) {
801 cda14729 Thomas Schöpping
#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 e545e620 Thomas Schöpping
            shutdown = AOS_SHUTDOWN_HIBERNATE;
808
            break;
809 cda14729 Thomas Schöpping
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_DEEPSLEEP:
810 e545e620 Thomas Schöpping
            shutdown = AOS_SHUTDOWN_DEEPSLEEP;
811
            break;
812 cda14729 Thomas Schöpping
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_TRANSPORTATION:
813 e545e620 Thomas Schöpping
            shutdown = AOS_SHUTDOWN_TRANSPORTATION;
814
            break;
815 cda14729 Thomas Schöpping
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_RESTART:
816 e545e620 Thomas Schöpping
            shutdown = AOS_SHUTDOWN_RESTART;
817
            break;
818 cda14729 Thomas Schöpping
          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_PASSIVE:
819
            _unexpectedEventError(eventmask, eventflags);
820 1d3e002f Thomas Schöpping
            break;
821 cda14729 Thomas Schöpping
#endif /* (AMIROOS_CFG_BOOTLOADER == X) */
822 e545e620 Thomas Schöpping
          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 41fc7088 Thomas Schöpping
#if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT == 0)
832 e545e620 Thomas Schöpping
        _unexpectedEventError(eventmask, eventflags);
833 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_MAIN_LOOP_TIMEOUT == 0) */
834 e545e620 Thomas Schöpping
        break;
835
    }
836
837 c218345a Thomas Schöpping
#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 c53ef0b1 Thomas Schöpping
    _eventListenerGPIO.wflags |= moduleSsspEventflagS();
845 c218345a Thomas Schöpping
    chSysUnlock();
846
847
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
848
849 cda14729 Thomas Schöpping
#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 e545e620 Thomas Schöpping
  }
857
858
  /*
859
   * ##########################################################################
860
   * # system shutdown                                                        #
861
   * ##########################################################################
862
   */
863
864 cda14729 Thomas Schöpping
  aosDbgAssert(shutdown != AOS_SHUTDOWN_NONE);
865 e545e620 Thomas Schöpping
866
  // initialize/acknowledge shutdown
867
  aosSysShutdownInit(shutdown);
868 cda14729 Thomas Schöpping
#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 e545e620 Thomas Schöpping
872
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1)
873
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS)
874 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS);
875 7de0cc90 Thomas Schöpping
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS) */
876 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1();
877 7de0cc90 Thomas Schöpping
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS) */
878
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1) */
879 e545e620 Thomas Schöpping
880 cda14729 Thomas Schöpping
  // wait for all threads to terminate
881 e545e620 Thomas Schöpping
  aosSysStop();
882
883
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2)
884
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS)
885 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS);
886 7de0cc90 Thomas Schöpping
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS) */
887 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2();
888 7de0cc90 Thomas Schöpping
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS) */
889
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2) */
890 e545e620 Thomas Schöpping
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 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS);
897 7de0cc90 Thomas Schöpping
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS) */
898 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3();
899 7de0cc90 Thomas Schöpping
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS) */
900
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3) */
901 e545e620 Thomas Schöpping
902 4c72a54c Thomas Schöpping
  /* 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 e545e620 Thomas Schöpping
907
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4)
908
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS)
909 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS);
910 7de0cc90 Thomas Schöpping
#else /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS) */
911 cda14729 Thomas Schöpping
  AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4();
912 7de0cc90 Thomas Schöpping
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS) */
913
#endif /* defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4) */
914 e545e620 Thomas Schöpping
915 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true)
916
917
  // proceed to SSSP shutdown stage 1.3
918 c53ef0b1 Thomas Schöpping
  aosSsspProceed(NULL, 0, NULL);
919 cda14729 Thomas Schöpping
  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 c53ef0b1 Thomas Schöpping
  while (aosSsspProceed(&_eventListenerGPIO, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS) {
931 cda14729 Thomas Schöpping
    /*
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 c53ef0b1 Thomas Schöpping
  aosSsspProceed(NULL, 0, &eventmask);
958 cda14729 Thomas Schöpping
  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 c53ef0b1 Thomas Schöpping
    while ((eventmask = aosSsspShutdownWaitForIdentifierPulse(&_eventListenerGPIO, moduleSsspEventflagS(), EVENTMASK_SSSPDELAY, &identifier)) != EVENTMASK_SSSPDELAY) {
978 cda14729 Thomas Schöpping
      // 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 c53ef0b1 Thomas Schöpping
  aosSsspProceed(NULL, 0, NULL);
1004 cda14729 Thomas Schöpping
  aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_2_3);
1005 e545e620 Thomas Schöpping
1006
  /*
1007 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.
1008 e545e620 Thomas Schöpping
   */
1009
1010 cda14729 Thomas Schöpping
  // 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 e545e620 Thomas Schöpping
}
1056 53710ca3 Marc Rothmann
1057 f3ac1c96 Thomas Schöpping
/******************************************************************************/
1058
/* EXPORTED FUNCTIONS                                                         */
1059
/******************************************************************************/
1060
1061 53710ca3 Marc Rothmann
/** @} */