Statistics
| Branch: | Tag: | Revision:

amiro-os / os / core / src / aos_main.cpp @ b6b45e4c

History | View | Annotate | Download (13.5 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2018  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14

15
You should have received a copy of the GNU General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

    
19
#include <hal.h>
20
#include <ch.h>
21
#include <amiroos.h>
22

    
23
/*
24
 * hook to add further includes
25
 */
26
#if defined(AMIROOS_CFG_MAIN_EXTRA_INCLUDE)
27
AMIROOS_CFG_MAIN_EXTRA_INCLUDE
28
#endif
29

    
30
/**
31
 * @brief   Event mask to identify I/O events.
32
 */
33
#define IOEVENT_MASK                            EVENT_MASK(0)
34

    
35
/**
36
 * @brief   Event mask to identify OS events.
37
 */
38
#define OSEVENT_MASK                            EVENT_MASK(1)
39

    
40
/**
41
 * @brief   Listener object for I/O events.
42
 */
43
static event_listener_t _eventListenerIO;
44

    
45
/**
46
 * @brief   Listener object for OS events.
47
 */
48
static event_listener_t _eventListenerOS;
49

    
50
#if defined(MODULE_HAL_PROGIF) || defined(__DOXYGEN__)
51
/**
52
 * @brief   I/O channel for the programmer interface.
53
 */
54
static AosIOChannel _stdiochannel;
55

    
56
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
57
/**
58
 * @brief   I/O shell channel for the programmer interface.
59
 */
60
static AosShellChannel _stdshellchannel;
61
#endif
62
#endif
63

    
64
/*
65
 * hook to add further static variables
66
 */
67
#if defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES)
68
AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES
69
#endif
70

    
71
/**
72
 * @brief   Prints an error message about an unexpected event.
73
 *
74
 * @param[in] mask    The event mask.
75
 * @param[in] flags   The event flags.
76
 */
77
static inline void _unexpectedEventError(eventmask_t mask, eventflags_t flags)
78
{
79
  aosprintf("unexpected/unknown event recieved. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
80
  return;
81
}
82

    
83
/**
84
 * @brief   Application entry point.
85
 */
86
int main(void)
87
{
88
  // local variables
89
  eventmask_t eventmask = 0;
90
  eventflags_t eventflags = 0;
91
  aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE;
92
#if defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES)
93
  AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES
94
#endif
95

    
96
  /*
97
   * ##########################################################################
98
   * # system initialization                                                  #
99
   * ##########################################################################
100
   */
101

    
102
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_0)
103
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_0_ARGS)
104
  AMIROOS_CFG_MAIN_INIT_HOOK_0(AMIROOS_CFG_MAIN_INIT_HOOK_0_ARGS);
105
#else
106
  AMIROOS_CFG_MAIN_INIT_HOOK_0();
107
#endif
108
#endif
109

    
110
  /* hardware, kernel, and operating system initialization */
111
  // ChibiOS/HAL and custom hal additions (if any)
112
  halInit();
113
#ifdef MODULE_INIT_HAL_EXTRA
114
  MODULE_INIT_HAL_EXTRA();
115
#endif
116

    
117
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1)
118
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS)
119
  AMIROOS_CFG_MAIN_INIT_HOOK_1(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS);
120
#else
121
  AMIROOS_CFG_MAIN_INIT_HOOK_1();
122
#endif
123
#endif
124

    
125
  // ChibiOS/RT kernel and custom kernel additions (if any)
126
  chSysInit();
127
#ifdef MODULE_INIT_KERNEL_EXTRA
128
  MODULE_INIT_KERNEL_EXTRA();
129
#endif
130

    
131
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2)
132
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS)
133
  AMIROOS_CFG_MAIN_INIT_HOOK_2(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS);
134
#else
135
  AMIROOS_CFG_MAIN_INIT_HOOK_2();
136
#endif
137
#endif
138

    
139
  // AMiRo-OS and custom OS additions (if any)
140
  aosSysInit(&MODULE_HAL_EXT,
141
             &moduleHalExtConfig,
142
             &moduleSsspPd,
143
             &moduleSsspSync,
144
             MODULE_OS_IOEVENTFLAGS_SYSPD,
145
             MODULE_OS_IOEVENTFLAGS_SYSSYNC,
146
             moduleShellPrompt);
147
#ifdef MODULE_INIT_OS_EXTRA
148
  MODULE_INIT_OS_EXTRA();
149
#endif
150

    
151
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3)
152
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS)
153
  AMIROOS_CFG_MAIN_INIT_HOOK_3(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS);
154
#else
155
  AMIROOS_CFG_MAIN_INIT_HOOK_3();
156
#endif
157
#endif
158

    
159
#if (AMIROOS_CFG_TESTS_ENABLE == true)
160
#if defined(MODULE_INIT_TESTS)
161
  MODULE_INIT_TESTS();
162
#else
163
  #warning "MODULE_INIT_TESTS not defined"
164
#endif
165
#endif
166

    
167
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4)
168
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS)
169
  AMIROOS_CFG_MAIN_INIT_HOOK_4(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS);
170
#else
171
  AMIROOS_CFG_MAIN_INIT_HOOK_4();
172
#endif
173
#endif
174

    
175
  /* event associations */
176
  chEvtRegisterMask(&aos.events.io.source, &_eventListenerIO, IOEVENT_MASK);
177
  chEvtRegisterMask(&aos.events.os.source, &_eventListenerOS, OSEVENT_MASK);
178

    
179
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5)
180
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS)
181
  AMIROOS_CFG_MAIN_INIT_HOOK_5(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS);
182
#else
183
  AMIROOS_CFG_MAIN_INIT_HOOK_5();
184
#endif
185
#endif
186

    
187
  /* periphery communication initialization */
188
  // CAN (mandatory)
189
  canStart(&MODULE_HAL_CAN, &moduleHalCanConfig);
190
  // module specific initialization (if any)
191
#ifdef MODULE_INIT_PERIPHERY_COMM
192
  MODULE_INIT_PERIPHERY_COMM();
193
#endif
194
  // user interface (if any)
195
#ifdef MODULE_HAL_PROGIF
196
  aosIOChannelInit(&_stdiochannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
197
  aosIOChannelOutputEnable(&_stdiochannel);
198
  aosIOStreamAddChannel(&aos.iostream, &_stdiochannel);
199
#if (AMIROOS_CFG_SHELL_ENABLE == true)
200
  aosShellChannelInit(&_stdshellchannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF);
201
  aosShellChannelInputEnable(&_stdshellchannel);
202
  aosShellChannelOutputEnable(&_stdshellchannel);
203
  aosShellStreamAddChannel(&aos.shell->stream, &_stdshellchannel);
204
#endif
205
#endif
206

    
207
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_6)
208
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS)
209
  AMIROOS_CFG_MAIN_INIT_HOOK_6(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS);
210
#else
211
  AMIROOS_CFG_MAIN_INIT_HOOK_6();
212
#endif
213
#endif
214

    
215
  /* module is ready -> print welcome prompt */
216
  aosprintf("\n");
217
  aosprintf("######################################################################\n");
218
  aosprintf("# AMiRo-OS is an operating system designed for the Autonomous Mini   #\n");
219
  aosprintf("# Robot (AMiRo) platform.                                            #\n");
220
  aosprintf("# Copyright (C) 2016..2018  Thomas Schöpping et al.                  #\n");
221
  aosprintf("#                                                                    #\n");
222
  aosprintf("# This is free software; see the source for copying conditions.      #\n");
223
  aosprintf("# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR  #\n");
224
  aosprintf("# A PARTICULAR PURPOSE.                                              #\n");
225
  aosprintf("# The development of this software was supported by the Excellence   #\n");
226
  aosprintf("# Cluster EXC 227 Cognitive Interaction Technology. The Excellence   #\n");
227
  aosprintf("# Cluster EXC 227 is a grant of the Deutsche Forschungsgemeinschaft  #\n");
228
  aosprintf("# (DFG) in the context of the German Excellence Initiative.          #\n");
229
  aosprintf("######################################################################\n");
230
  aosprintf("\n");
231

    
232
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_7)
233
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS)
234
  AMIROOS_CFG_MAIN_INIT_HOOK_7(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS);
235
#else
236
  AMIROOS_CFG_MAIN_INIT_HOOK_7();
237
#endif
238
#endif
239

    
240
  /* SSSP startup outro (end of startup stage 2) synchronization */
241
  while ((eventmask = aosSysSsspStartupOsInitSyncCheck(&_eventListenerIO)) != 0) {
242
    /*
243
     * This code is executed if the received event was not about the SYS_SYNC control signal.
244
     * The returned event could be caused by any listener (not only the argument).
245
     */
246
    // unexpected IO events
247
    if (eventmask & _eventListenerIO.events) {
248
      eventflags = chEvtGetAndClearFlags(&_eventListenerIO);
249
#ifdef MODULE_SSP_STARTUP_OUTRO_IO_EVENT
250
      MODULE_SSP_STARTUP_OUTRO_IO_EVENT(eventmask, eventflags);
251
#else
252
      _unexpectedEventError(eventmask, eventflags);
253
#endif
254
    }
255
    // unexpected OS event
256
    else if (eventmask & _eventListenerOS.events) {
257
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
258
      _unexpectedEventError(eventmask, eventflags);
259
    }
260
#if (AMIROOS_CFG_DBG == true)
261
    // unknown event (must never occur, thus disabled for release builds)
262
    else {
263
      eventflags = 0;
264
      _unexpectedEventError(eventmask, eventflags);
265
    }
266
#endif
267
  }
268

    
269
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_8)
270
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS)
271
  AMIROOS_CFG_MAIN_INIT_HOOK_8(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS);
272
#else
273
  AMIROOS_CFG_MAIN_INIT_HOOK_8();
274
#endif
275
#endif
276

    
277
  /* completely start AMiRo-OS */
278
  aosSysStart();
279

    
280
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_9)
281
#if defined(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS)
282
  AMIROOS_CFG_MAIN_INIT_HOOK_9(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS);
283
#else
284
  AMIROOS_CFG_MAIN_INIT_HOOK_9();
285
#endif
286
#endif
287

    
288
  /*
289
   * ##########################################################################
290
   * # infinite loop                                                          #
291
   * ##########################################################################
292
   */
293

    
294
  // sleep until a shutdown event is received
295
  while (shutdown == AOS_SHUTDOWN_NONE) {
296
    // wait for an event
297
#if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0)
298
    eventmask = chEvtWaitOneTimeout(ALL_EVENTS, US2ST_LLD(AMIROOS_CFG_MAIN_LOOP_TIMEOUT));
299
#else
300
    eventmask = chEvtWaitOne(ALL_EVENTS);
301
#endif
302

    
303
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0)
304
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS)
305
    AMIROOS_CFG_MAIN_LOOP_HOOK_0(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS);
306
#else
307
    AMIROOS_CFG_MAIN_LOOP_HOOK_0();
308
#endif
309
#endif
310

    
311
    switch (eventmask) {
312
      // if this was an I/O event
313
      case IOEVENT_MASK:
314
        // evaluate flags
315
        eventflags = chEvtGetAndClearFlags(&_eventListenerIO);
316
        // PD event
317
        if (eventflags & MODULE_OS_IOEVENTFLAGS_SYSPD) {
318
          shutdown = AOS_SHUTDOWN_PASSIVE;
319
        }
320
        // all other events
321
#ifdef MODULE_MAIN_LOOP_IO_EVENT
322
        else {
323
          MODULE_MAIN_LOOP_IO_EVENT(eventmask, eventflags);
324
        }
325
#endif
326
        break;
327

    
328
      // if this was an OS event
329
      case OSEVENT_MASK:
330
        // evaluate flags
331
        eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
332
        switch (eventflags) {
333
          case AOS_SYSTEM_EVENTFLAGS_HIBERNATE:
334
            shutdown = AOS_SHUTDOWN_HIBERNATE;
335
            break;
336
          case AOS_SYSTEM_EVENTFLAGS_DEEPSLEEP:
337
            shutdown = AOS_SHUTDOWN_DEEPSLEEP;
338
            break;
339
          case AOS_SYSTEM_EVENTFLAGS_TRANSPORTATION:
340
            shutdown = AOS_SHUTDOWN_TRANSPORTATION;
341
            break;
342
          case AOS_SYSTEM_EVENTFLAGS_RESTART:
343
            shutdown = AOS_SHUTDOWN_RESTART;
344
            break;
345
          default:
346
            _unexpectedEventError(eventmask, eventflags);
347
            break;
348
        }
349
        break;
350

    
351
      // if this was any other event (should be impossible to occur)
352
      default:
353
        eventflags = 0;
354
        _unexpectedEventError(eventmask, eventflags);
355
        break;
356
    }
357

    
358
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1)
359
#if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS)
360
    AMIROOS_CFG_MAIN_LOOP_HOOK_1(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS);
361
#else
362
    AMIROOS_CFG_MAIN_LOOP_HOOK_1();
363
#endif
364
#endif
365
  }
366

    
367
  /*
368
   * ##########################################################################
369
   * # system shutdown                                                        #
370
   * ##########################################################################
371
   */
372

    
373
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0)
374
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS)
375
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS);
376
#else
377
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0();
378
#endif
379
#endif
380

    
381
  // initialize/acknowledge shutdown
382
  aosSysShutdownInit(shutdown);
383

    
384
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1)
385
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS)
386
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS);
387
#else
388
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1();
389
#endif
390
#endif
391

    
392
  // stop system threads
393
  aosSysStop();
394

    
395
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2)
396
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS)
397
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS);
398
#else
399
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2();
400
#endif
401
#endif
402

    
403
  // deinitialize system
404
  aosSysDeinit();
405

    
406
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3)
407
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS)
408
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS);
409
#else
410
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3();
411
#endif
412
#endif
413

    
414
  /* stop all periphery communication */
415
  // CAN (mandatory)
416
  canStop(&MODULE_HAL_CAN);
417
#ifdef MODULE_SHUTDOWN_PERIPHERY_COMM
418
  MODULE_SHUTDOWN_PERIPHERY_COMM();
419
#endif
420

    
421
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4)
422
#if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS)
423
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS);
424
#else
425
    AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4();
426
#endif
427
#endif
428

    
429
  // finally hand over to bootloader
430
  aosSysShutdownFinal(&MODULE_HAL_EXT, shutdown);
431

    
432
  /*
433
   * ##########################################################################
434
   * # after shutdown/restart                                                 #
435
   * ##########################################################################
436
   *
437
   * NOTE: This code will not be executed, since the bootloader callbacks will stop/restart the MCU.
438
   *       It is included nevertheless for the sake of completeness and to explicitely indicate,
439
   *       which subsystems should NOT be shut down.
440
   */
441

    
442
  // return an error, since this code should not be executed
443
  return -1;
444
}