amiro-os / os / core / src / main.c @ 42f1b573
History | View | Annotate | Download (13.014 KB)
1 | e545e620 | Thomas Schöpping | /*
|
---|---|---|---|
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 | * @brief Event mask to identify I/O events.
|
||
25 | */
|
||
26 | #define IOEVENT_MASK EVENT_MASK(0) |
||
27 | |||
28 | /**
|
||
29 | * @brief Event mask to identify OS events.
|
||
30 | */
|
||
31 | #define OSEVENT_MASK EVENT_MASK(1) |
||
32 | |||
33 | /**
|
||
34 | * @brief Listener object for I/O events.
|
||
35 | */
|
||
36 | static event_listener_t _eventListenerIO;
|
||
37 | |||
38 | /**
|
||
39 | * @brief Listener object for OS events.
|
||
40 | */
|
||
41 | static event_listener_t _eventListenerOS;
|
||
42 | |||
43 | #if defined(MODULE_HAL_PROGIF) || defined(__DOXYGEN__)
|
||
44 | /**
|
||
45 | ba516b61 | Thomas Schöpping | * @brief I/O channel for the programmer interface.
|
46 | e545e620 | Thomas Schöpping | */
|
47 | ba516b61 | Thomas Schöpping | static AosIOChannel _stdiochannel;
|
48 | |||
49 | /**
|
||
50 | * @brief I/O shell channel for the programmer interface.
|
||
51 | */
|
||
52 | static AosShellChannel _stdshellchannel;
|
||
53 | e545e620 | Thomas Schöpping | #endif
|
54 | |||
55 | /**
|
||
56 | * @brief Prints an error message about an unexpected event.
|
||
57 | *
|
||
58 | * @param[in] mask The event mask.
|
||
59 | * @param[in] flags The event flags.
|
||
60 | */
|
||
61 | static inline void _unexpectedEventError(eventmask_t mask, eventflags_t flags) |
||
62 | { |
||
63 | aosprintf("unexpected/unknown event recieved. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
|
||
64 | return;
|
||
65 | } |
||
66 | |||
67 | /**
|
||
68 | * @brief Application entry point.
|
||
69 | */
|
||
70 | int main(void) |
||
71 | { |
||
72 | // local variables
|
||
73 | eventmask_t eventmask = 0;
|
||
74 | eventflags_t eventflags = 0;
|
||
75 | aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE; |
||
76 | #if defined(AMIROOS_CFG_MAIN_EXTRA_VARIABLES)
|
||
77 | AMIROOS_CFG_MAIN_EXTRA_VARIABLES |
||
78 | #endif
|
||
79 | |||
80 | /*
|
||
81 | * ##########################################################################
|
||
82 | * # system initialization #
|
||
83 | * ##########################################################################
|
||
84 | */
|
||
85 | |||
86 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_0)
|
||
87 | AMIROOS_CFG_MAIN_INIT_HOOK_0(); |
||
88 | #endif
|
||
89 | |||
90 | /* hardware, kernel, and operating system initialization */
|
||
91 | // ChibiOS/HAL and custom hal additions (if any)
|
||
92 | halInit(); |
||
93 | #ifdef MODULE_INIT_HAL_EXTRA
|
||
94 | MODULE_INIT_HAL_EXTRA(); |
||
95 | #endif
|
||
96 | |||
97 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1)
|
||
98 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS)
|
||
99 | AMIROOS_CFG_MAIN_INIT_HOOK_1(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS); |
||
100 | #else
|
||
101 | AMIROOS_CFG_MAIN_INIT_HOOK_1(); |
||
102 | #endif
|
||
103 | #endif
|
||
104 | |||
105 | // ChibiOS/RT kernel and custom kernel additions (if any)
|
||
106 | chSysInit(); |
||
107 | #ifdef MODULE_INIT_KERNEL_EXTRA
|
||
108 | MODULE_INIT_KERNEL_EXTRA(); |
||
109 | #endif
|
||
110 | |||
111 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2)
|
||
112 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS)
|
||
113 | AMIROOS_CFG_MAIN_INIT_HOOK_2(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS); |
||
114 | #else
|
||
115 | AMIROOS_CFG_MAIN_INIT_HOOK_2(); |
||
116 | #endif
|
||
117 | #endif
|
||
118 | |||
119 | // AMiRo-OS and custom OS additions (if any)
|
||
120 | aosSysInit(&MODULE_HAL_EXT, |
||
121 | &moduleHalExtConfig, |
||
122 | &moduleSsspPd, |
||
123 | &moduleSsspSync, |
||
124 | MODULE_OS_IOEVENTFLAGS_SYSPD, |
||
125 | MODULE_OS_IOEVENTFLAGS_SYSSYNC, |
||
126 | moduleShellPrompt); |
||
127 | #ifdef MODULE_INIT_OS_EXTRA
|
||
128 | MODULE_INIT_OS_EXTRA(); |
||
129 | #endif
|
||
130 | |||
131 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3)
|
||
132 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS)
|
||
133 | AMIROOS_CFG_MAIN_INIT_HOOK_3(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS); |
||
134 | #else
|
||
135 | AMIROOS_CFG_MAIN_INIT_HOOK_3(); |
||
136 | #endif
|
||
137 | #endif
|
||
138 | |||
139 | #if (AMIROOS_CFG_TESTS_ENABLE == true) |
||
140 | #if defined(MODULE_INIT_TESTS)
|
||
141 | MODULE_INIT_TESTS(); |
||
142 | #else
|
||
143 | ba516b61 | Thomas Schöpping | #warning "MODULE_INIT_TESTS not defined" |
144 | e545e620 | Thomas Schöpping | #endif
|
145 | #endif
|
||
146 | |||
147 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4)
|
||
148 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS)
|
||
149 | AMIROOS_CFG_MAIN_INIT_HOOK_4(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS); |
||
150 | #else
|
||
151 | AMIROOS_CFG_MAIN_INIT_HOOK_4(); |
||
152 | #endif
|
||
153 | #endif
|
||
154 | |||
155 | /* event associations */
|
||
156 | chEvtRegisterMask(&aos.events.io.source, &_eventListenerIO, IOEVENT_MASK); |
||
157 | chEvtRegisterMask(&aos.events.os.source, &_eventListenerOS, OSEVENT_MASK); |
||
158 | |||
159 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5)
|
||
160 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS)
|
||
161 | AMIROOS_CFG_MAIN_INIT_HOOK_5(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS); |
||
162 | #else
|
||
163 | AMIROOS_CFG_MAIN_INIT_HOOK_5(); |
||
164 | #endif
|
||
165 | #endif
|
||
166 | |||
167 | /* periphery communication initialization */
|
||
168 | // CAN (mandatory)
|
||
169 | canStart(&MODULE_HAL_CAN, &moduleHalCanConfig); |
||
170 | // module specific initialization (if any)
|
||
171 | #ifdef MODULE_INIT_PERIPHERY_COMM
|
||
172 | MODULE_INIT_PERIPHERY_COMM(); |
||
173 | #endif
|
||
174 | // user interface (if any)
|
||
175 | #ifdef MODULE_HAL_PROGIF
|
||
176 | ba516b61 | Thomas Schöpping | aosIOChannelInit(&_stdiochannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF); |
177 | aosIOChannelOutputEnable(&_stdiochannel); |
||
178 | aosIOStreamAddChannel(&aos.iostream, &_stdiochannel); |
||
179 | #if (AMIROOS_CFG_SHELL_ENABLE == true) |
||
180 | aosShellChannelInit(&_stdshellchannel, &_stdiochannel); |
||
181 | aosShellChannelInputEnable(&_stdshellchannel); |
||
182 | aosShellChannelOutputEnable(&_stdshellchannel); |
||
183 | aosShellStreamAddChannel(&aos.shell->stream, &_stdshellchannel); |
||
184 | #endif
|
||
185 | e545e620 | Thomas Schöpping | #endif
|
186 | |||
187 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_6)
|
||
188 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS)
|
||
189 | AMIROOS_CFG_MAIN_INIT_HOOK_6(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS); |
||
190 | #else
|
||
191 | AMIROOS_CFG_MAIN_INIT_HOOK_6(); |
||
192 | #endif
|
||
193 | #endif
|
||
194 | |||
195 | /* module is ready -> print welcome prompt */
|
||
196 | aosprintf("\n");
|
||
197 | aosprintf("######################################################################\n");
|
||
198 | aosprintf("# AMiRo-OS is an operating system designed for the Autonomous Mini #\n");
|
||
199 | aosprintf("# Robot (AMiRo) platform. #\n");
|
||
200 | aosprintf("# Copyright (C) 2016..2018 Thomas Schöpping et al. #\n");
|
||
201 | aosprintf("# #\n");
|
||
202 | aosprintf("# This is free software; see the source for copying conditions. #\n");
|
||
203 | aosprintf("# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR #\n");
|
||
204 | aosprintf("# A PARTICULAR PURPOSE. #\n");
|
||
205 | aosprintf("# The development of this software was supported by the Excellence #\n");
|
||
206 | aosprintf("# Cluster EXC 227 Cognitive Interaction Technology. The Excellence #\n");
|
||
207 | aosprintf("# Cluster EXC 227 is a grant of the Deutsche Forschungsgemeinschaft #\n");
|
||
208 | aosprintf("# (DFG) in the context of the German Excellence Initiative. #\n");
|
||
209 | aosprintf("######################################################################\n");
|
||
210 | aosprintf("\n");
|
||
211 | |||
212 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_7)
|
||
213 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS)
|
||
214 | AMIROOS_CFG_MAIN_INIT_HOOK_7(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS); |
||
215 | #else
|
||
216 | AMIROOS_CFG_MAIN_INIT_HOOK_7(); |
||
217 | #endif
|
||
218 | #endif
|
||
219 | |||
220 | /* SSSP startup outro (end of startup stage 2) synchronization */
|
||
221 | while ((eventmask = aosSysSsspStartupOsInitSyncCheck(&_eventListenerIO)) != 0) { |
||
222 | /*
|
||
223 | * This code is executed if the received event was not about the SYS_SYNC control signal.
|
||
224 | * The returned event could be caused by any listener (not only the argument).
|
||
225 | */
|
||
226 | // unexpected IO events
|
||
227 | if (eventmask & _eventListenerIO.events) {
|
||
228 | eventflags = chEvtGetAndClearFlags(&_eventListenerIO); |
||
229 | #ifdef MODULE_SSP_STARTUP_OUTRO_IO_EVENT
|
||
230 | MODULE_SSP_STARTUP_OUTRO_IO_EVENT(eventmask, eventflags); |
||
231 | #else
|
||
232 | _unexpectedEventError(eventmask, eventflags); |
||
233 | #endif
|
||
234 | } |
||
235 | // unexpected OS event
|
||
236 | else if (eventmask & _eventListenerOS.events) { |
||
237 | eventflags = chEvtGetAndClearFlags(&_eventListenerOS); |
||
238 | _unexpectedEventError(eventmask, eventflags); |
||
239 | } |
||
240 | #if (AMIROOS_CFG_DBG == true) |
||
241 | // unknown event (must never occur, thus disabled for release builds)
|
||
242 | else {
|
||
243 | eventflags = 0;
|
||
244 | _unexpectedEventError(eventmask, eventflags); |
||
245 | } |
||
246 | #endif
|
||
247 | } |
||
248 | |||
249 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_8)
|
||
250 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS)
|
||
251 | AMIROOS_CFG_MAIN_INIT_HOOK_8(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS); |
||
252 | #else
|
||
253 | AMIROOS_CFG_MAIN_INIT_HOOK_8(); |
||
254 | #endif
|
||
255 | #endif
|
||
256 | |||
257 | /* completely start AMiRo-OS */
|
||
258 | aosSysStart(); |
||
259 | |||
260 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_9)
|
||
261 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS)
|
||
262 | AMIROOS_CFG_MAIN_INIT_HOOK_9(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS); |
||
263 | #else
|
||
264 | AMIROOS_CFG_MAIN_INIT_HOOK_9(); |
||
265 | #endif
|
||
266 | #endif
|
||
267 | |||
268 | /*
|
||
269 | * ##########################################################################
|
||
270 | * # infinite loop #
|
||
271 | * ##########################################################################
|
||
272 | */
|
||
273 | |||
274 | // sleep until a shutdown event is received
|
||
275 | while (shutdown == AOS_SHUTDOWN_NONE) {
|
||
276 | // wait for an event
|
||
277 | #if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0) |
||
278 | eventmask = chEvtWaitOneTimeout(ALL_EVENTS, US2ST_LLD(AMIROOS_CFG_MAIN_LOOP_TIMEOUT)); |
||
279 | #else
|
||
280 | eventmask = chEvtWaitOne(ALL_EVENTS); |
||
281 | #endif
|
||
282 | |||
283 | #if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0)
|
||
284 | #if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS)
|
||
285 | AMIROOS_CFG_MAIN_LOOP_HOOK_0(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS); |
||
286 | #else
|
||
287 | AMIROOS_CFG_MAIN_LOOP_HOOK_0(); |
||
288 | #endif
|
||
289 | #endif
|
||
290 | |||
291 | switch (eventmask) {
|
||
292 | // if this was an I/O event
|
||
293 | case IOEVENT_MASK:
|
||
294 | // evaluate flags
|
||
295 | eventflags = chEvtGetAndClearFlags(&_eventListenerIO); |
||
296 | // PD event
|
||
297 | if (eventflags & MODULE_OS_IOEVENTFLAGS_SYSPD) {
|
||
298 | shutdown = AOS_SHUTDOWN_PASSIVE; |
||
299 | } |
||
300 | // all other events
|
||
301 | #ifdef MODULE_MAIN_LOOP_IO_EVENT
|
||
302 | else {
|
||
303 | MODULE_MAIN_LOOP_IO_EVENT(eventmask, eventflags); |
||
304 | } |
||
305 | #endif
|
||
306 | break;
|
||
307 | |||
308 | // if this was an OS event
|
||
309 | case OSEVENT_MASK:
|
||
310 | // evaluate flags
|
||
311 | eventflags = chEvtGetAndClearFlags(&_eventListenerOS); |
||
312 | switch (eventflags) {
|
||
313 | case AOS_SYSTEM_EVENTFLAGS_HIBERNATE:
|
||
314 | shutdown = AOS_SHUTDOWN_HIBERNATE; |
||
315 | break;
|
||
316 | case AOS_SYSTEM_EVENTFLAGS_DEEPSLEEP:
|
||
317 | shutdown = AOS_SHUTDOWN_DEEPSLEEP; |
||
318 | break;
|
||
319 | case AOS_SYSTEM_EVENTFLAGS_TRANSPORTATION:
|
||
320 | shutdown = AOS_SHUTDOWN_TRANSPORTATION; |
||
321 | break;
|
||
322 | case AOS_SYSTEM_EVENTFLAGS_RESTART:
|
||
323 | shutdown = AOS_SHUTDOWN_RESTART; |
||
324 | break;
|
||
325 | default:
|
||
326 | _unexpectedEventError(eventmask, eventflags); |
||
327 | break;
|
||
328 | } |
||
329 | break;
|
||
330 | |||
331 | // if this was any other event (should be impossible to occur)
|
||
332 | default:
|
||
333 | eventflags = 0;
|
||
334 | _unexpectedEventError(eventmask, eventflags); |
||
335 | break;
|
||
336 | } |
||
337 | |||
338 | #if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1)
|
||
339 | #if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS)
|
||
340 | AMIROOS_CFG_MAIN_LOOP_HOOK_1(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS); |
||
341 | #else
|
||
342 | AMIROOS_CFG_MAIN_LOOP_HOOK_1(); |
||
343 | #endif
|
||
344 | #endif
|
||
345 | } |
||
346 | |||
347 | /*
|
||
348 | * ##########################################################################
|
||
349 | * # system shutdown #
|
||
350 | * ##########################################################################
|
||
351 | */
|
||
352 | |||
353 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0)
|
||
354 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS)
|
||
355 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS); |
||
356 | #else
|
||
357 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0(); |
||
358 | #endif
|
||
359 | #endif
|
||
360 | |||
361 | // initialize/acknowledge shutdown
|
||
362 | aosSysShutdownInit(shutdown); |
||
363 | |||
364 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1)
|
||
365 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS)
|
||
366 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS); |
||
367 | #else
|
||
368 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1(); |
||
369 | #endif
|
||
370 | #endif
|
||
371 | |||
372 | // stop system threads
|
||
373 | aosSysStop(); |
||
374 | |||
375 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2)
|
||
376 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS)
|
||
377 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS); |
||
378 | #else
|
||
379 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2(); |
||
380 | #endif
|
||
381 | #endif
|
||
382 | |||
383 | // deinitialize system
|
||
384 | aosSysDeinit(); |
||
385 | |||
386 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3)
|
||
387 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS)
|
||
388 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS); |
||
389 | #else
|
||
390 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3(); |
||
391 | #endif
|
||
392 | #endif
|
||
393 | |||
394 | /* stop all periphery communication */
|
||
395 | // CAN (mandatory)
|
||
396 | canStop(&MODULE_HAL_CAN); |
||
397 | #ifdef MODULE_SHUTDOWN_PERIPHERY_COMM
|
||
398 | MODULE_SHUTDOWN_PERIPHERY_COMM(); |
||
399 | #endif
|
||
400 | |||
401 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4)
|
||
402 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS)
|
||
403 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS); |
||
404 | #else
|
||
405 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4(); |
||
406 | #endif
|
||
407 | #endif
|
||
408 | |||
409 | // finally hand over to bootloader
|
||
410 | aosSysShutdownFinal(&MODULE_HAL_EXT, shutdown); |
||
411 | |||
412 | /*
|
||
413 | * ##########################################################################
|
||
414 | * # after shutdown/restart #
|
||
415 | * ##########################################################################
|
||
416 | *
|
||
417 | * NOTE: This code will not be executed, since the bootloader callbacks will stop/restart the MCU.
|
||
418 | * It is included nevertheless for the sake of completeness and to explicitely indicate,
|
||
419 | * which subsystems should NOT be shut down.
|
||
420 | */
|
||
421 | |||
422 | // return an error, since this code should not be executed
|
||
423 | return -1; |
||
424 | } |