Statistics
| Branch: | Tag: | Revision:

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

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