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