amiro-os / core / src / aos_main.cpp @ f3b3fe09
History | View | Annotate | Download (47.383 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 | 53710ca3 | Marc Rothmann | /**
|
20 | * @file aos_main.cpp
|
||
21 | * @brief Main function.
|
||
22 | * @details Main function with SSSP and initialization,
|
||
23 | * extendable via hooks.
|
||
24 | *
|
||
25 | * @addtogroup aos_system
|
||
26 | * @{
|
||
27 | */
|
||
28 | |||
29 | e545e620 | Thomas Schöpping | #include <amiroos.h> |
30 | cb835a3e | Thomas Schöpping | #include <module.h> |
31 | e545e620 | Thomas Schöpping | |
32 | b6b45e4c | Thomas Schöpping | /*
|
33 | * hook to add further includes
|
||
34 | */
|
||
35 | 512abac1 | Thomas Schöpping | #if defined(AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER)
|
36 | #include AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER
|
||
37 | b6b45e4c | Thomas Schöpping | #endif
|
38 | |||
39 | e545e620 | Thomas Schöpping | /**
|
40 | * @brief Event mask to identify I/O events.
|
||
41 | */
|
||
42 | 6b53f6bf | Thomas Schöpping | #define IOEVENT_MASK EVENT_MASK(0) |
43 | e545e620 | Thomas Schöpping | |
44 | /**
|
||
45 | * @brief Event mask to identify OS events.
|
||
46 | */
|
||
47 | 6b53f6bf | Thomas Schöpping | #define OSEVENT_MASK EVENT_MASK(1) |
48 | e545e620 | Thomas Schöpping | |
49 | /**
|
||
50 | 933df08e | Thomas Schöpping | * @brief Event mask to idetify CAN events.
|
51 | */
|
||
52 | #define CANEVENT_MASK EVENT_MASK(2) |
||
53 | |||
54 | /**
|
||
55 | * @brief Event mask to idetify timeout events.
|
||
56 | */
|
||
57 | #define TIMEOUTEVENT_MASK EVENT_MASK(3) |
||
58 | |||
59 | /**
|
||
60 | * @brief Event mask to idetify signal delay events.
|
||
61 | */
|
||
62 | #define DELAYEVENT_MASK EVENT_MASK(4) |
||
63 | |||
64 | 1ef74af5 | Simon Welzel | #if (AMIROOS_CFG_SSSP_ENABLE == true) |
65 | |||
66 | 933df08e | Thomas Schöpping | /**
|
67 | * @brief CAN message identifier for initialization of the SSSP stack initialization sequence.
|
||
68 | */
|
||
69 | #define SSSP_STACKINIT_CANMSGID_INIT 0x003 |
||
70 | |||
71 | /**
|
||
72 | * @brief CAN message identifier for transmitting module IDs during the SSSP stack initialization sequence.
|
||
73 | */
|
||
74 | #define SSSP_STACKINIT_CANMSGID_MODULEID 0x002 |
||
75 | |||
76 | /**
|
||
77 | * @brief CAN message identifier for abortion of the SSSP stack initialization sequence.
|
||
78 | */
|
||
79 | #define SSSP_STACKINIT_CANMSGID_ABORT 0x001 |
||
80 | |||
81 | 1ef74af5 | Simon Welzel | #endif
|
82 | |||
83 | 933df08e | Thomas Schöpping | /**
|
84 | 9461fadc | Thomas Schöpping | * @brief CAN message identifier for calender synchronization message.
|
85 | */
|
||
86 | #define CALENDERSYNC_CANMSGID 0x004 |
||
87 | |||
88 | /**
|
||
89 | e545e620 | Thomas Schöpping | * @brief Listener object for I/O events.
|
90 | */
|
||
91 | static event_listener_t _eventListenerIO;
|
||
92 | |||
93 | /**
|
||
94 | * @brief Listener object for OS events.
|
||
95 | */
|
||
96 | static event_listener_t _eventListenerOS;
|
||
97 | |||
98 | #if defined(MODULE_HAL_PROGIF) || defined(__DOXYGEN__)
|
||
99 | /**
|
||
100 | ba516b61 | Thomas Schöpping | * @brief I/O channel for the programmer interface.
|
101 | e545e620 | Thomas Schöpping | */
|
102 | ba516b61 | Thomas Schöpping | static AosIOChannel _stdiochannel;
|
103 | |||
104 | b8268050 | Thomas Schöpping | #if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__) |
105 | ba516b61 | Thomas Schöpping | /**
|
106 | * @brief I/O shell channel for the programmer interface.
|
||
107 | */
|
||
108 | static AosShellChannel _stdshellchannel;
|
||
109 | e545e620 | Thomas Schöpping | #endif
|
110 | b8268050 | Thomas Schöpping | #endif
|
111 | e545e620 | Thomas Schöpping | |
112 | b6b45e4c | Thomas Schöpping | /*
|
113 | * hook to add further static variables
|
||
114 | */
|
||
115 | #if defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES)
|
||
116 | AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES |
||
117 | #endif
|
||
118 | |||
119 | e545e620 | Thomas Schöpping | /**
|
120 | * @brief Prints an error message about an unexpected event.
|
||
121 | *
|
||
122 | * @param[in] mask The event mask.
|
||
123 | * @param[in] flags The event flags.
|
||
124 | */
|
||
125 | 933df08e | Thomas Schöpping | static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags) |
126 | e545e620 | Thomas Schöpping | { |
127 | 6b53f6bf | Thomas Schöpping | #if (AMIROOS_CFG_DBG == true) |
128 | 85e62aaf | Simon Welzel | aosprintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
|
129 | 6b53f6bf | Thomas Schöpping | #else
|
130 | 85e62aaf | Simon Welzel | (void)(mask);
|
131 | (void)(flags);
|
||
132 | 6b53f6bf | Thomas Schöpping | #endif
|
133 | 85e62aaf | Simon Welzel | return;
|
134 | 933df08e | Thomas Schöpping | } |
135 | |||
136 | 1ef74af5 | Simon Welzel | |
137 | #if (AMIROOS_CFG_SSSP_ENABLE == true) |
||
138 | 933df08e | Thomas Schöpping | /**
|
139 | * @brief Callback function to be used during SSSP stack initialization sequence.
|
||
140 | *
|
||
141 | * @param[in] par A pointer to an @p event_source_t to be fired.
|
||
142 | */
|
||
143 | static void _ssspTimerCallback(void* par) |
||
144 | { |
||
145 | 85e62aaf | Simon Welzel | aosDbgCheck(par != NULL);
|
146 | 933df08e | Thomas Schöpping | |
147 | 85e62aaf | Simon Welzel | chSysLockFromISR(); |
148 | chEvtBroadcastI((event_source_t*)par); |
||
149 | chSysUnlockFromISR(); |
||
150 | 933df08e | Thomas Schöpping | |
151 | 85e62aaf | Simon Welzel | return;
|
152 | e545e620 | Thomas Schöpping | } |
153 | 1ef74af5 | Simon Welzel | #endif
|
154 | e545e620 | Thomas Schöpping | /**
|
155 | 9461fadc | Thomas Schöpping | * @brief Helper function to serialize data.
|
156 | 933df08e | Thomas Schöpping | *
|
157 | * @param[out] dst Pointer to the output buffer.
|
||
158 | 9461fadc | Thomas Schöpping | * @param[in] src Data to be serialized.
|
159 | * @param[in] n Number of bytes to serialize.
|
||
160 | 933df08e | Thomas Schöpping | */
|
161 | 9461fadc | Thomas Schöpping | inline void _serialize(uint8_t* dst, const uint64_t src, const uint8_t n) |
162 | 933df08e | Thomas Schöpping | { |
163 | 85e62aaf | Simon Welzel | aosDbgCheck(dst != NULL);
|
164 | aosDbgCheck(n > 0 && n <= 8); |
||
165 | 933df08e | Thomas Schöpping | |
166 | 85e62aaf | Simon Welzel | for (uint8_t byte = 0; byte < n; ++byte) { |
167 | dst[byte] = (uint8_t)((src >> (byte * 8)) & 0xFF); |
||
168 | } |
||
169 | 933df08e | Thomas Schöpping | |
170 | 85e62aaf | Simon Welzel | return;
|
171 | 933df08e | Thomas Schöpping | } |
172 | |||
173 | /**
|
||
174 | 9461fadc | Thomas Schöpping | * @brief Helper function to deserialize data.
|
175 | 933df08e | Thomas Schöpping | *
|
176 | * @param[in] src Pointer to the buffer of data to be deserialzed.
|
||
177 | 9461fadc | Thomas Schöpping | * @param[in] n Number of bytes to deserialize.
|
178 | 933df08e | Thomas Schöpping | *
|
179 | * @return The deserialized 32 bit data.
|
||
180 | */
|
||
181 | 9461fadc | Thomas Schöpping | inline uint64_t _deserialize(uint8_t* src, const uint8_t n) |
182 | 933df08e | Thomas Schöpping | { |
183 | 85e62aaf | Simon Welzel | aosDbgCheck(src != NULL);
|
184 | aosDbgCheck(n > 0 && n <= 8); |
||
185 | 933df08e | Thomas Schöpping | |
186 | 85e62aaf | Simon Welzel | uint64_t result = 0;
|
187 | for (uint8_t byte = 0; byte < n; ++byte) { |
||
188 | result |= ((uint64_t)src[byte]) << (byte * 8);
|
||
189 | } |
||
190 | 9461fadc | Thomas Schöpping | |
191 | 85e62aaf | Simon Welzel | return result;
|
192 | 9461fadc | Thomas Schöpping | } |
193 | |||
194 | /**
|
||
195 | * @brief Converter function to encode a TM value to a single unsigned 64 bit integer.
|
||
196 | *
|
||
197 | * @details Contents of the TM struct are mapped as follows:
|
||
198 | * bits |63 62|61 53|52 50|49 26|25 22|21 17|16 12|11 6|5 0|
|
||
199 | * #bits | 2 | 9 | 3 | 24 | 4 | 5 | 5 | 6 | 6 |
|
||
200 | * value | isdst | yday | wday | year | mon | mday | hour | min | sec |
|
||
201 | * range | special | [0, 365] | [0, 6] | [1900, ...] | [0, 11] | [1, 31] | [0, 23] | [0, 59] | [0, 61] |
|
||
202 | * The Daylight Saving Time Flag (isdsst) is encoded as follows:
|
||
203 | * DST not in effect -> 0
|
||
204 | * DST in effect -> 1
|
||
205 | * no information available -> 2
|
||
206 | *
|
||
207 | * @param[in] src Pointer to the TM struct to encode.
|
||
208 | *
|
||
209 | * @return An unsigned 64 bit integer, which holds the encoded time value.
|
||
210 | */
|
||
211 | inline uint64_t _TM2U64(struct tm* src) |
||
212 | { |
||
213 | 85e62aaf | Simon Welzel | aosDbgCheck(src != NULL);
|
214 | |||
215 | return (((uint64_t)(src->tm_sec & 0x0000003F) << (0)) | |
||
216 | ((uint64_t)(src->tm_min & 0x0000003F) << (6)) | |
||
217 | ((uint64_t)(src->tm_hour & 0x0000001F) << (12)) | |
||
218 | ((uint64_t)(src->tm_mday & 0x0000001F) << (17)) | |
||
219 | ((uint64_t)(src->tm_mon & 0x0000000F) << (22)) | |
||
220 | ((uint64_t)(src->tm_year & 0x00FFFFFF) << (26)) | |
||
221 | ((uint64_t)(src->tm_wday & 0x00000007) << (50)) | |
||
222 | ((uint64_t)(src->tm_yday & 0x000001FF) << (53)) | |
||
223 | ((uint64_t)((src->tm_isdst == 0) ? 0 : (src->tm_isdst > 0) ? 1 : 2) << (62))); |
||
224 | 9461fadc | Thomas Schöpping | } |
225 | |||
226 | /**
|
||
227 | * @brief Converter functiomn to retrieve the encoded TM value from an unsigned 64 bit integer.
|
||
228 | *
|
||
229 | * @details For information on the encoding, please refer to @p _TM2U64 function.
|
||
230 | *
|
||
231 | * @param[out] dst The TM struct to fill with the decoded values.
|
||
232 | * @param[in] src Unsigned 64 bit integer holding the encoded TM value.
|
||
233 | */
|
||
234 | inline void _U642TM(struct tm* dst, const uint64_t src) |
||
235 | { |
||
236 | 85e62aaf | Simon Welzel | aosDbgCheck(dst != NULL);
|
237 | |||
238 | dst->tm_sec = (src >> 0) & 0x0000003F; |
||
239 | dst->tm_min = (src >> 6) & 0x0000003F; |
||
240 | dst->tm_hour = (src >> 12) & 0x0000001F; |
||
241 | dst->tm_mday = (src >> 17) & 0x0000001F; |
||
242 | dst->tm_mon = (src >> 22) & 0x0000000F; |
||
243 | dst->tm_year = (src >> 26) & 0x00FFFFFF; |
||
244 | dst->tm_wday = (src >> 50) & 0x00000007; |
||
245 | dst->tm_yday = (src >> 53) & 0x000001FF; |
||
246 | dst->tm_isdst = (((src >> 62) & 0x03) == 0) ? 0 : (((src >> 62) & 0x03) > 0) ? 1 : -1; |
||
247 | |||
248 | return;
|
||
249 | 933df08e | Thomas Schöpping | } |
250 | 1ef74af5 | Simon Welzel | #if (AMIROOS_CFG_SSSP_ENABLE == true) |
251 | 933df08e | Thomas Schöpping | /**
|
252 | * @brief Implementation of the SSSP module stack initialization sequence (startup phase 3).
|
||
253 | *
|
||
254 | * @return Shutdown value.
|
||
255 | * @retval AOS_SHUTDOWN_NONE No shutdown signal received
|
||
256 | * @retval AOS_SHUTDOWN_PASSIVE Shutdown signal received.
|
||
257 | */
|
||
258 | aos_shutdown_t _ssspModuleStackInitialization(void)
|
||
259 | { |
||
260 | 85e62aaf | Simon Welzel | // local types
|
261 | /**
|
||
262 | 933df08e | Thomas Schöpping | * @brief States for the internal state machine to implement SSSP startup stage 3.
|
263 | */
|
||
264 | 85e62aaf | Simon Welzel | typedef enum { |
265 | STAGE_3_1, /**< Initiation of SSSP startup stage 3. */
|
||
266 | STAGE_3_2, /**< Starting the sequence and broadcasting the first ID. */
|
||
267 | STAGE_3_3_WAITFORFIRSTID, /**< Waiting for first ID after initiation. */
|
||
268 | STAGE_3_3_WAITFORIDORSIG, /**< Waiting for next ID or activation of neighbor signal. */
|
||
269 | STAGE_3_3_WAITFORID, /**< Waiting for next ID (after the module has set its own ID). */
|
||
270 | STAGE_3_4_FINISH, /**< Successful finish of stage 3. */
|
||
271 | STAGE_3_4_ABORT_ACTIVE, /**< Aborting stage 3 (active). */
|
||
272 | STAGE_3_4_ABORT, /**< Aborting stage 3 (passive). */
|
||
273 | } sssp_modulestackinitstage_t; |
||
274 | |||
275 | typedef struct { |
||
276 | bool loop : 1; |
||
277 | bool wfe : 1; |
||
278 | bool wfe_next : 1; |
||
279 | } flags_t; |
||
280 | |||
281 | // local variables
|
||
282 | aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE; |
||
283 | sssp_modulestackinitstage_t stage = STAGE_3_1; |
||
284 | eventmask_t eventmask = 0;
|
||
285 | eventflags_t ioflags; |
||
286 | event_source_t eventSourceTimeout; |
||
287 | event_source_t eventSourceDelay; |
||
288 | event_listener_t eventListenerTimeout; |
||
289 | event_listener_t eventListenerDelay; |
||
290 | event_listener_t eventListenerCan; |
||
291 | virtual_timer_t timerTimeout; |
||
292 | virtual_timer_t timerDelay; |
||
293 | CANTxFrame canTxFrame; |
||
294 | CANRxFrame canRxFrame; |
||
295 | 933df08e | Thomas Schöpping | #if (AMIROOS_CFG_SSSP_STACK_START != true) || (AMIROOS_CFG_DBG == true) |
296 | 85e62aaf | Simon Welzel | aos_ssspmoduleid_t lastid = 0;
|
297 | #endif
|
||
298 | flags_t flags; |
||
299 | |||
300 | // initialize local varibles
|
||
301 | chEvtObjectInit(&eventSourceTimeout); |
||
302 | chEvtObjectInit(&eventSourceDelay); |
||
303 | chVTObjectInit(&timerTimeout); |
||
304 | chVTObjectInit(&timerDelay); |
||
305 | canTxFrame.RTR = CAN_RTR_DATA; |
||
306 | canTxFrame.IDE = CAN_IDE_STD; |
||
307 | flags.loop = true;
|
||
308 | flags.wfe = false; // do not wait for events in the initial iteration of the FSM loop |
||
309 | flags.wfe_next = true;
|
||
310 | |||
311 | // initialize system variables
|
||
312 | aos.sssp.stage = AOS_SSSP_STARTUP_3_1; |
||
313 | aos.sssp.moduleId = 0;
|
||
314 | |||
315 | // listen to events (timout, delay, CAN receive)
|
||
316 | chEvtRegisterMask(&eventSourceTimeout, &eventListenerTimeout, TIMEOUTEVENT_MASK); |
||
317 | chEvtRegisterMask(&eventSourceDelay, &eventListenerDelay, DELAYEVENT_MASK); |
||
318 | chEvtRegisterMask(&MODULE_HAL_CAN.rxfull_event, &eventListenerCan, CANEVENT_MASK); |
||
319 | |||
320 | /*
|
||
321 | 933df08e | Thomas Schöpping | * FSM in a loop.
|
322 | *
|
||
323 | * This is a fully event-based FSM for the module stack initialization
|
||
324 | * sequence, defined by SSSP as startup stage 3. There are five different
|
||
325 | * events that can occur at this point:
|
||
326 | * I/O events: The input level of an input pin has changed. Such events must
|
||
327 | * be handled differently depending on the current state. Most
|
||
328 | * of the time, however, such events can be ignored.
|
||
329 | * OS events: Such events are only available after this stage completed and
|
||
330 | * thus should never occur. However, there is an optional hook
|
||
331 | * to handle such events, nevertheless.
|
||
332 | * CAN events: At least one CAN message was received. Note that this event
|
||
333 | * will only fire again if all input buffers have been cleared.
|
||
334 | * timeouts: If some module does not support the sequence of there is any
|
||
335 | * issue, such a case is detected via timeouts and must be
|
||
336 | * handled accordingly (see abort state). In some cases, it is
|
||
337 | * possible that a timeout event occurres 'simultaneously' with
|
||
338 | * some other event. This can be caused by several timing issues
|
||
339 | * and is a valid situation. As a result, any other events
|
||
340 | * should be handled before the timeout event. If the other
|
||
341 | * events are expected and valid, this implementation requires
|
||
342 | * the timeout event flag to be cleared explicitely. Otherwise
|
||
343 | * it is evaluated at the end of each iteration of the loop.
|
||
344 | * delays: Depending on the current state, delays are required by SSSP
|
||
345 | * for timing of the sequential activation of signals.
|
||
346 | */
|
||
347 | 85e62aaf | Simon Welzel | aosDbgPrintf("SSSP stack initialization sequence:\n");
|
348 | while (flags.loop) {
|
||
349 | 933df08e | Thomas Schöpping | #if (AMIROOS_CFG_DBG == true) |
350 | 85e62aaf | Simon Welzel | switch (stage) {
|
351 | case STAGE_3_1:
|
||
352 | aosDbgPrintf(">>> 3-1\n");
|
||
353 | break;
|
||
354 | case STAGE_3_2:
|
||
355 | aosDbgPrintf(">>> 3-2\n");
|
||
356 | break;
|
||
357 | case STAGE_3_3_WAITFORFIRSTID:
|
||
358 | aosDbgPrintf(">>> 3-3 (1st ID)\n");
|
||
359 | break;
|
||
360 | case STAGE_3_3_WAITFORIDORSIG:
|
||
361 | aosDbgPrintf(">>> 3-3 (ID/sig)\n");
|
||
362 | break;
|
||
363 | case STAGE_3_3_WAITFORID:
|
||
364 | aosDbgPrintf(">>> 3-3 (ID)\n");
|
||
365 | break;
|
||
366 | case STAGE_3_4_FINISH:
|
||
367 | aosDbgPrintf(">>> 3-4 (finish)\n");
|
||
368 | break;
|
||
369 | case STAGE_3_4_ABORT_ACTIVE:
|
||
370 | aosDbgPrintf(">>> 3-4 (active abort)\n");
|
||
371 | break;
|
||
372 | case STAGE_3_4_ABORT:
|
||
373 | aosDbgPrintf(">>> 3-4 (abort)\n");
|
||
374 | break;
|
||
375 | } |
||
376 | 933df08e | Thomas Schöpping | #endif
|
377 | |||
378 | 85e62aaf | Simon Welzel | // reset wfe flag for the next iteration
|
379 | flags.wfe_next = true;
|
||
380 | 933df08e | Thomas Schöpping | |
381 | 85e62aaf | Simon Welzel | // waiting for events may be skipped
|
382 | if (flags.wfe) {
|
||
383 | // wait for any event to occur
|
||
384 | aosDbgPrintf("WFE...");
|
||
385 | eventmask = chEvtWaitAnyTimeout(ALL_EVENTS, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT)); |
||
386 | aosDbgPrintf("\t0x%08X", eventmask);
|
||
387 | } else {
|
||
388 | aosDbgPrintf("WFE skipped");
|
||
389 | } |
||
390 | aos_timestamp_t uptime; |
||
391 | aosSysGetUptime(&uptime); |
||
392 | aosDbgPrintf("\t%04ums\n", (uint32_t)(uptime / 1000)); |
||
393 | 933df08e | Thomas Schöpping | |
394 | 85e62aaf | Simon Welzel | /*
|
395 | 933df08e | Thomas Schöpping | * execute some general tasks and high priority events
|
396 | */
|
||
397 | 85e62aaf | Simon Welzel | // no event occurred at all
|
398 | if ((flags.wfe) && (eventmask == 0)) { |
||
399 | aosDbgPrintf("ERR: no evt\n");
|
||
400 | // enforce timeout event
|
||
401 | chEvtBroadcast(&eventSourceTimeout); |
||
402 | continue;
|
||
403 | 933df08e | Thomas Schöpping | } |
404 | 85e62aaf | Simon Welzel | // if an IO event occurred
|
405 | if (eventmask & _eventListenerIO.events) {
|
||
406 | ioflags = chEvtGetAndClearFlags(&_eventListenerIO); |
||
407 | aosDbgPrintf("INFO: IO evt (0x%08X)\n", ioflags);
|
||
408 | // a power-down event occurred
|
||
409 | if (ioflags & MODULE_SSSP_EVENTFLAGS_PD) {
|
||
410 | aosDbgPrintf("PD evt\n");
|
||
411 | // deactivate S and UP
|
||
412 | apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF); |
||
413 | #if (AMIROOS_CFG_SSSP_STACK_END != true) |
||
414 | apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_OFF); |
||
415 | #endif
|
||
416 | // set shutdown flag and exit the loop
|
||
417 | shutdown = AOS_SHUTDOWN_PASSIVE; |
||
418 | break;
|
||
419 | } |
||
420 | // the S signal was deactivated
|
||
421 | if (ioflags & MODULE_SSSP_EVENTFLAGS_SYNC) {
|
||
422 | apalControlGpioState_t sstate; |
||
423 | apalControlGpioGet(&moduleSsspGpioSync, &sstate); |
||
424 | if (sstate == APAL_GPIO_OFF) {
|
||
425 | aosDbgPrintf("-S evt\n");
|
||
426 | // either finish or abort
|
||
427 | if ((stage == STAGE_3_3_WAITFORID) && (aos.sssp.moduleId != 0)) { |
||
428 | stage = STAGE_3_4_FINISH; |
||
429 | } else if (stage != STAGE_3_4_ABORT) { |
||
430 | stage = STAGE_3_4_ABORT_ACTIVE; |
||
431 | } |
||
432 | } |
||
433 | } |
||
434 | } |
||
435 | // an OS event occurred
|
||
436 | if (eventmask & _eventListenerOS.events) {
|
||
437 | aosDbgPrintf("WARN: OS evt\n");
|
||
438 | // get the flags
|
||
439 | eventflags_t oseventflags = chEvtGetAndClearFlags(&_eventListenerOS); |
||
440 | // there should be no OS events at this point
|
||
441 | 933df08e | Thomas Schöpping | #ifdef MODULE_SSSP_STARTUP_3_OSEVENT_HOOK
|
442 | 85e62aaf | Simon Welzel | MODULE_SSSP_STARTUP_3_OSEVENT_HOOK(eventmask, eventflags); |
443 | 933df08e | Thomas Schöpping | #else
|
444 | 85e62aaf | Simon Welzel | _unexpectedEventError(eventmask, oseventflags); |
445 | 933df08e | Thomas Schöpping | #endif
|
446 | 85e62aaf | Simon Welzel | } |
447 | // if a CAN event occurred
|
||
448 | if ((eventmask & eventListenerCan.events)) {
|
||
449 | // fetch message
|
||
450 | if (flags.wfe) {
|
||
451 | canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canRxFrame, TIME_IMMEDIATE); |
||
452 | aosDbgPrintf("CAN <- 0x%03X\n", canRxFrame.SID);
|
||
453 | } |
||
454 | // identify and handle abort messgaes
|
||
455 | if (canRxFrame.SID == SSSP_STACKINIT_CANMSGID_ABORT) {
|
||
456 | stage = STAGE_3_4_ABORT; |
||
457 | } |
||
458 | // warn if a unexpected message was received
|
||
459 | else if ((canRxFrame.SID != SSSP_STACKINIT_CANMSGID_INIT) && |
||
460 | (canRxFrame.SID != SSSP_STACKINIT_CANMSGID_MODULEID)) { |
||
461 | aosDbgPrintf("WARN: unknown msg\n");
|
||
462 | } |
||
463 | // any further pending messages are fetched at the end of the loop
|
||
464 | } |
||
465 | // if a timeout event occurred
|
||
466 | if (eventmask & eventListenerTimeout.events) {
|
||
467 | // is handled at the end of the loop (or must be cleared by FSM)
|
||
468 | } |
||
469 | // if a delay event occurred
|
||
470 | if (eventmask & eventListenerDelay.events) {
|
||
471 | // is handled by FSM
|
||
472 | } |
||
473 | 933df08e | Thomas Schöpping | |
474 | 85e62aaf | Simon Welzel | /*
|
475 | 933df08e | Thomas Schöpping | * this is the actual FSM
|
476 | */
|
||
477 | 85e62aaf | Simon Welzel | switch (stage) {
|
478 | case STAGE_3_1:
|
||
479 | { |
||
480 | aos.sssp.stage = AOS_SSSP_STARTUP_3_1; |
||
481 | 933df08e | Thomas Schöpping | |
482 | 85e62aaf | Simon Welzel | // there was no event at all (skipped wfe)
|
483 | if (eventmask == 0 && flags.wfe == false) { |
||
484 | 933df08e | Thomas Schöpping | #if (AMIROOS_CFG_SSSP_MASTER == true) |
485 | 85e62aaf | Simon Welzel | // initialize the stage by transmitting an according CAN message
|
486 | aosDbgPrintf("CAN -> init\n");
|
||
487 | canTxFrame.DLC = 0;
|
||
488 | canTxFrame.SID = SSSP_STACKINIT_CANMSGID_INIT; |
||
489 | if (canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_IMMEDIATE) != MSG_OK) {
|
||
490 | chEvtBroadcast(&eventSourceTimeout); |
||
491 | break;
|
||
492 | } |
||
493 | // activate S
|
||
494 | aosDbgPrintf("S+\n");
|
||
495 | apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_ON); |
||
496 | 933df08e | Thomas Schöpping | #if (AMIROOS_CFG_SSSP_STACK_START == true) |
497 | 85e62aaf | Simon Welzel | // proceed immediately
|
498 | stage = STAGE_3_2; |
||
499 | flags.wfe_next = false;
|
||
500 | 933df08e | Thomas Schöpping | #else
|
501 | 85e62aaf | Simon Welzel | // set the timeout timer
|
502 | chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout); |
||
503 | // proceed
|
||
504 | stage = STAGE_3_3_WAITFORFIRSTID; |
||
505 | 933df08e | Thomas Schöpping | #endif
|
506 | #else
|
||
507 | 85e62aaf | Simon Welzel | // set the timeout timer
|
508 | chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout); |
||
509 | 933df08e | Thomas Schöpping | #endif
|
510 | 85e62aaf | Simon Welzel | } |
511 | 933df08e | Thomas Schöpping | |
512 | #if (AMIROOS_CFG_SSSP_MASTER != true) |
||
513 | 85e62aaf | Simon Welzel | // a CAN message was received
|
514 | else if (eventmask & eventListenerCan.events) { |
||
515 | // if an initiation message was received
|
||
516 | if (canRxFrame.DLC == 0 && |
||
517 | canRxFrame.RTR == CAN_RTR_DATA && |
||
518 | canRxFrame.IDE == CAN_IDE_STD && |
||
519 | canRxFrame.SID == SSSP_STACKINIT_CANMSGID_INIT) { |
||
520 | aosDbgPrintf("init msg\n");
|
||
521 | // reset the timeout timer and clear pending flags
|
||
522 | chVTReset(&timerTimeout); |
||
523 | chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE); |
||
524 | eventmask &= ~(eventListenerTimeout.events); |
||
525 | // activate S
|
||
526 | aosDbgPrintf("S+\n");
|
||
527 | apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_ON); |
||
528 | 933df08e | Thomas Schöpping | #if (AMIROOS_CFG_SSSP_STACK_START == true) |
529 | 85e62aaf | Simon Welzel | // proceed
|
530 | stage = STAGE_3_2; |
||
531 | flags.wfe_next = false;
|
||
532 | 933df08e | Thomas Schöpping | #else
|
533 | 85e62aaf | Simon Welzel | // set the timeout timer
|
534 | chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout); |
||
535 | // proceed
|
||
536 | stage = STAGE_3_3_WAITFORFIRSTID; |
||
537 | 933df08e | Thomas Schöpping | #endif
|
538 | 85e62aaf | Simon Welzel | } |
539 | } |
||
540 | 933df08e | Thomas Schöpping | #endif
|
541 | |||
542 | 85e62aaf | Simon Welzel | break;
|
543 | } /* end of STAGE_3_1 */
|
||
544 | 933df08e | Thomas Schöpping | |
545 | 85e62aaf | Simon Welzel | case STAGE_3_2:
|
546 | { |
||
547 | 933df08e | Thomas Schöpping | #if (AMIROOS_CFG_SSSP_STACK_START == true) |
548 | 85e62aaf | Simon Welzel | aos.sssp.stage = AOS_SSSP_STARTUP_3_2; |
549 | |||
550 | // if this stage was just entered
|
||
551 | if (flags.wfe == false) { |
||
552 | // set the module ID
|
||
553 | aos.sssp.moduleId = 1;
|
||
554 | // broadcast module ID
|
||
555 | aosDbgPrintf("CAN -> ID (%u)\n", aos.sssp.moduleId);
|
||
556 | canTxFrame.DLC = 4;
|
||
557 | canTxFrame.SID = SSSP_STACKINIT_CANMSGID_MODULEID; |
||
558 | _serialize(canTxFrame.data8, aos.sssp.moduleId, 4);
|
||
559 | if (canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_IMMEDIATE) != MSG_OK) {
|
||
560 | chEvtBroadcast(&eventSourceTimeout); |
||
561 | break;
|
||
562 | } |
||
563 | 933df08e | Thomas Schöpping | #if (AMIROOS_CFG_SSSP_STACK_START != true) || (AMIROOS_CFG_DBG == true) |
564 | 85e62aaf | Simon Welzel | lastid = aos.sssp.moduleId; |
565 | 933df08e | Thomas Schöpping | #endif
|
566 | #if (AMIROOS_CFG_SSSP_STACK_END == true) |
||
567 | 85e62aaf | Simon Welzel | // sequence is already over
|
568 | // deactivate S
|
||
569 | aosDbgPrintf("S-\n");
|
||
570 | apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF); |
||
571 | // proceed
|
||
572 | stage = STAGE_3_3_WAITFORID; |
||
573 | 933df08e | Thomas Schöpping | #else
|
574 | 85e62aaf | Simon Welzel | // set the delay timer so the UP signal is activated later
|
575 | chVTSet(&timerDelay, chTimeUS2I(AMIROOS_CFG_SSSP_SIGNALDELAY), _ssspTimerCallback, &eventSourceDelay); |
||
576 | 933df08e | Thomas Schöpping | #endif
|
577 | 85e62aaf | Simon Welzel | } |
578 | 933df08e | Thomas Schöpping | |
579 | 85e62aaf | Simon Welzel | // if a delay event occurred
|
580 | if (eventmask & eventListenerDelay.events) {
|
||
581 | // activate UP
|
||
582 | aosDbgPrintf("UP+\n");
|
||
583 | apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_ON); |
||
584 | // deactivate S
|
||
585 | aosDbgPrintf("S-\n");
|
||
586 | apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF); |
||
587 | // explicitely clear timeout event flag
|
||
588 | chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE); |
||
589 | eventmask &= ~(eventListenerTimeout.events); |
||
590 | // proceed
|
||
591 | stage = STAGE_3_3_WAITFORID; |
||
592 | } |
||
593 | 933df08e | Thomas Schöpping | #endif
|
594 | |||
595 | 85e62aaf | Simon Welzel | break;
|
596 | } /* end of STAGE_3_2 */
|
||
597 | 933df08e | Thomas Schöpping | |
598 | 85e62aaf | Simon Welzel | case STAGE_3_3_WAITFORFIRSTID:
|
599 | { |
||
600 | 933df08e | Thomas Schöpping | #if (AMIROOS_CFG_SSSP_STACK_START != true) |
601 | 85e62aaf | Simon Welzel | aos.sssp.stage = AOS_SSSP_STARTUP_3_3; |
602 | |||
603 | // a CAN message was received
|
||
604 | if (eventmask & eventListenerCan.events) {
|
||
605 | // if an ID message was received
|
||
606 | if (canRxFrame.DLC == 4 && |
||
607 | canRxFrame.RTR == CAN_RTR_DATA && |
||
608 | canRxFrame.IDE == CAN_IDE_STD && |
||
609 | canRxFrame.SID == SSSP_STACKINIT_CANMSGID_MODULEID) { |
||
610 | aosDbgPrintf("ID (%u)\n", (uint32_t)_deserialize(canRxFrame.data8, 4)); |
||
611 | // validate received ID
|
||
612 | if (lastid < _deserialize(canRxFrame.data8, 4)) { |
||
613 | // store received ID
|
||
614 | lastid = _deserialize(canRxFrame.data8, 4);
|
||
615 | // restart timeout timer
|
||
616 | chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout); |
||
617 | // proceed
|
||
618 | stage = STAGE_3_3_WAITFORIDORSIG; |
||
619 | } else {
|
||
620 | aosDbgPrintf("ERR: invalid ID\n");
|
||
621 | // abort
|
||
622 | stage = STAGE_3_4_ABORT_ACTIVE; |
||
623 | flags.wfe_next = false;
|
||
624 | } |
||
625 | // explicitely clear timeout event flag
|
||
626 | chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE); |
||
627 | eventmask &= ~(eventListenerTimeout.events); |
||
628 | } |
||
629 | 933df08e | Thomas Schöpping | } |
630 | #endif
|
||
631 | 85e62aaf | Simon Welzel | break;
|
632 | } /* end of STAGE_3_3_WAITFORFIRSTID */
|
||
633 | 933df08e | Thomas Schöpping | |
634 | 85e62aaf | Simon Welzel | case STAGE_3_3_WAITFORIDORSIG:
|
635 | { |
||
636 | 933df08e | Thomas Schöpping | #if (AMIROOS_CFG_SSSP_STACK_START != true) |
637 | 85e62aaf | Simon Welzel | aos.sssp.stage = AOS_SSSP_STARTUP_3_3; |
638 | |||
639 | // a CAN message was received
|
||
640 | if (eventmask & eventListenerCan.events) {
|
||
641 | // if an ID message was received
|
||
642 | if (canRxFrame.DLC == 4 && |
||
643 | canRxFrame.RTR == CAN_RTR_DATA && |
||
644 | canRxFrame.IDE == CAN_IDE_STD && |
||
645 | canRxFrame.SID == SSSP_STACKINIT_CANMSGID_MODULEID) { |
||
646 | aosDbgPrintf("ID (%u)\n", (uint32_t)_deserialize(canRxFrame.data8, 4)); |
||
647 | // validate received ID
|
||
648 | if (lastid < _deserialize(canRxFrame.data8, 4)) { |
||
649 | // store received ID
|
||
650 | lastid = _deserialize(canRxFrame.data8, 4);
|
||
651 | // restart timeout timer
|
||
652 | chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout); |
||
653 | } else {
|
||
654 | aosDbgPrintf("ERR: invalid ID\n");
|
||
655 | // abort
|
||
656 | stage = STAGE_3_4_ABORT_ACTIVE; |
||
657 | flags.wfe_next = false;
|
||
658 | } |
||
659 | // explicitely clear timeout event flag
|
||
660 | chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE); |
||
661 | eventmask &= ~(eventListenerTimeout.events); |
||
662 | } |
||
663 | 933df08e | Thomas Schöpping | } |
664 | |||
665 | 85e62aaf | Simon Welzel | // if an IO event was received (DN signal)
|
666 | if ((eventmask & _eventListenerIO.events) && (ioflags & MODULE_SSSP_EVENTFLAGS_DN)) {
|
||
667 | aosDbgPrintf("DN <-\n");
|
||
668 | // reset timeout timer
|
||
669 | chVTReset(&timerTimeout); |
||
670 | chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE); |
||
671 | eventmask &= ~(eventListenerTimeout.events); |
||
672 | // increment and broadcast ID
|
||
673 | aos.sssp.moduleId = lastid + 1;
|
||
674 | aosDbgPrintf("CAN -> ID (%u)\n", aos.sssp.moduleId);
|
||
675 | canTxFrame.DLC = 4;
|
||
676 | canTxFrame.SID = SSSP_STACKINIT_CANMSGID_MODULEID; |
||
677 | _serialize(canTxFrame.data8, aos.sssp.moduleId, 4);
|
||
678 | if (canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_IMMEDIATE) != MSG_OK) {
|
||
679 | chEvtBroadcast(&eventSourceTimeout); |
||
680 | break;
|
||
681 | } |
||
682 | // set delay timer
|
||
683 | chVTSet(&timerDelay, chTimeUS2I(AMIROOS_CFG_SSSP_SIGNALDELAY), _ssspTimerCallback, &eventSourceDelay); |
||
684 | } |
||
685 | 933df08e | Thomas Schöpping | |
686 | 85e62aaf | Simon Welzel | // if a delay event occurred
|
687 | if (eventmask & eventListenerDelay.events) {
|
||
688 | 933df08e | Thomas Schöpping | #if (AMIROOS_CFG_SSSP_STACK_END != true) |
689 | 85e62aaf | Simon Welzel | // activate UP
|
690 | aosDbgPrintf("UP+\n");
|
||
691 | apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_ON); |
||
692 | #endif
|
||
693 | // deactivate S
|
||
694 | aosDbgPrintf("S-\n");
|
||
695 | apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF); |
||
696 | // reset the timeout timer
|
||
697 | chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout); |
||
698 | chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE); |
||
699 | eventmask &= ~(eventListenerTimeout.events); |
||
700 | // proceed
|
||
701 | stage = STAGE_3_3_WAITFORID; |
||
702 | } |
||
703 | 933df08e | Thomas Schöpping | #endif
|
704 | |||
705 | 85e62aaf | Simon Welzel | break;
|
706 | } /* end of STAGE_3_3_WAITFORIDORSIG */
|
||
707 | 933df08e | Thomas Schöpping | |
708 | 85e62aaf | Simon Welzel | case STAGE_3_3_WAITFORID:
|
709 | { |
||
710 | aos.sssp.stage = AOS_SSSP_STARTUP_3_3; |
||
711 | 933df08e | Thomas Schöpping | |
712 | #if (AMIROOS_CFG_SSSP_STACK_END != true) |
||
713 | 85e62aaf | Simon Welzel | // a CAN message was received
|
714 | if (eventmask & eventListenerCan.events) {
|
||
715 | // if an ID message was received
|
||
716 | if (canRxFrame.DLC == 4 && |
||
717 | canRxFrame.RTR == CAN_RTR_DATA && |
||
718 | canRxFrame.IDE == CAN_IDE_STD && |
||
719 | canRxFrame.SID == SSSP_STACKINIT_CANMSGID_MODULEID) { |
||
720 | 933df08e | Thomas Schöpping | #if (AMIROOS_CFG_SSSP_STACK_START != true) || (AMIROOS_CFG_DBG == true) |
721 | 85e62aaf | Simon Welzel | // Plausibility of the received ID is not checked at this point but is done by other modules still in a previous stage.
|
722 | lastid = _deserialize(canRxFrame.data8, 4);
|
||
723 | aosDbgPrintf("ID (%u)\n", lastid);
|
||
724 | #endif
|
||
725 | // restart timeout timer
|
||
726 | chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout); |
||
727 | chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE); |
||
728 | eventmask &= ~(eventListenerTimeout.events); |
||
729 | } |
||
730 | } |
||
731 | 933df08e | Thomas Schöpping | #endif
|
732 | |||
733 | 85e62aaf | Simon Welzel | break;
|
734 | } /* end of STAGE_3_3_WAITFORID */
|
||
735 | |||
736 | case STAGE_3_4_FINISH:
|
||
737 | { |
||
738 | aos.sssp.stage = AOS_SSSP_STARTUP_3_4; |
||
739 | |||
740 | // if an IO event was received (S signal)
|
||
741 | if ((eventmask & _eventListenerIO.events) && (ioflags & MODULE_SSSP_EVENTFLAGS_SYNC)) {
|
||
742 | // reset the timeout timer
|
||
743 | chVTReset(&timerTimeout); |
||
744 | chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE); |
||
745 | eventmask &= ~(eventListenerTimeout.events); |
||
746 | //set the delay timer
|
||
747 | chVTSet(&timerDelay, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceDelay); |
||
748 | } |
||
749 | 933df08e | Thomas Schöpping | |
750 | 85e62aaf | Simon Welzel | // if a CAN event was received
|
751 | if (eventmask & eventListenerCan.events) {
|
||
752 | // if an abort message was received
|
||
753 | if (canRxFrame.SID == SSSP_STACKINIT_CANMSGID_ABORT) {
|
||
754 | aosDbgPrintf("abort msg\n");
|
||
755 | // reset the delay timer
|
||
756 | chVTReset(&timerDelay); |
||
757 | chEvtWaitAnyTimeout(eventListenerDelay.events, TIME_IMMEDIATE); |
||
758 | eventmask &= ~(eventListenerDelay.events); |
||
759 | // proceed
|
||
760 | stage = STAGE_3_4_ABORT; |
||
761 | } |
||
762 | } |
||
763 | 933df08e | Thomas Schöpping | |
764 | 85e62aaf | Simon Welzel | // if a delay timer event occurred
|
765 | if (eventmask & eventListenerDelay.events) {
|
||
766 | aosDbgPrintf("sequence sucessful\n");
|
||
767 | // sequence finished sucessfully
|
||
768 | flags.loop = false;
|
||
769 | } |
||
770 | 933df08e | Thomas Schöpping | |
771 | 85e62aaf | Simon Welzel | break;
|
772 | } /* end of STAGE_3_4_FINISH */
|
||
773 | |||
774 | case STAGE_3_4_ABORT_ACTIVE:
|
||
775 | { |
||
776 | aos.sssp.stage = AOS_SSSP_STARTUP_3_4; |
||
777 | |||
778 | // emit abort message
|
||
779 | canTxFrame.DLC = 0;
|
||
780 | canTxFrame.SID = SSSP_STACKINIT_CANMSGID_ABORT; |
||
781 | canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_INFINITE); |
||
782 | aosDbgPrintf("CAN -> abort\n");
|
||
783 | // clear timeout flag
|
||
784 | eventmask &= ~(eventListenerTimeout.events); |
||
785 | // proceed immediately
|
||
786 | 933df08e | Thomas Schöpping | stage = STAGE_3_4_ABORT; |
787 | 85e62aaf | Simon Welzel | flags.wfe_next = false;
|
788 | break;
|
||
789 | } /* end of STAGE_3_4_ABORT_ACTIVE */
|
||
790 | |||
791 | case STAGE_3_4_ABORT:
|
||
792 | { |
||
793 | aos.sssp.stage = AOS_SSSP_STARTUP_3_4; |
||
794 | |||
795 | // deactivate S
|
||
796 | aosDbgPrintf("S-\n");
|
||
797 | apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF); |
||
798 | // invalidate module ID
|
||
799 | aos.sssp.moduleId = 0;
|
||
800 | |||
801 | // if an IO event was received (S signal)
|
||
802 | if ((eventmask & _eventListenerIO.events) && (ioflags & MODULE_SSSP_EVENTFLAGS_SYNC)) {
|
||
803 | aosDbgPrintf("sequence aborted\n");
|
||
804 | // exit the sequence
|
||
805 | flags.loop = false;
|
||
806 | } |
||
807 | 933df08e | Thomas Schöpping | |
808 | 85e62aaf | Simon Welzel | break;
|
809 | } /* end of STAGE_3_4_ABORT */
|
||
810 | 933df08e | Thomas Schöpping | } |
811 | |||
812 | 85e62aaf | Simon Welzel | // fetch pending CAN message (if any)
|
813 | if ((eventmask & eventListenerCan.events) && (canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canRxFrame, TIME_IMMEDIATE) == MSG_OK)) {
|
||
814 | aosDbgPrintf("CAN <- 0x%03X\n", canRxFrame.SID);
|
||
815 | flags.wfe_next = false;
|
||
816 | 933df08e | Thomas Schöpping | } |
817 | |||
818 | 85e62aaf | Simon Welzel | // handle unhandled timeout events
|
819 | if (eventmask & eventListenerTimeout.events) {
|
||
820 | aosDbgPrintf("ERR: timeout evt\n");
|
||
821 | // abort
|
||
822 | flags.wfe_next = false;
|
||
823 | stage = STAGE_3_4_ABORT_ACTIVE; |
||
824 | } |
||
825 | 933df08e | Thomas Schöpping | |
826 | 85e62aaf | Simon Welzel | // apply wfe value for next iteration
|
827 | flags.wfe = flags.wfe_next; |
||
828 | } /* end of FSM loop */
|
||
829 | aosDbgPrintf("\n");
|
||
830 | 933df08e | Thomas Schöpping | |
831 | 85e62aaf | Simon Welzel | // unregister all events (timeout, delay, CAN receive)
|
832 | chEvtUnregister(&eventSourceTimeout, &eventListenerTimeout); |
||
833 | chEvtUnregister(&eventSourceDelay, &eventListenerDelay); |
||
834 | chEvtUnregister(&MODULE_HAL_CAN.rxfull_event, &eventListenerCan); |
||
835 | // clear any pending events (timeout, delay, CAN receive)
|
||
836 | chEvtWaitAllTimeout(TIMEOUTEVENT_MASK | DELAYEVENT_MASK | CANEVENT_MASK, TIME_IMMEDIATE); |
||
837 | 933df08e | Thomas Schöpping | |
838 | 85e62aaf | Simon Welzel | // reset all control signals
|
839 | 933df08e | Thomas Schöpping | #if (AMIROOS_CFG_SSSP_STACK_END != true) |
840 | 85e62aaf | Simon Welzel | apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_OFF); |
841 | 933df08e | Thomas Schöpping | #endif
|
842 | 85e62aaf | Simon Welzel | apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF); |
843 | 933df08e | Thomas Schöpping | |
844 | 85e62aaf | Simon Welzel | return shutdown;
|
845 | 933df08e | Thomas Schöpping | } |
846 | 1ef74af5 | Simon Welzel | #endif
|
847 | 933df08e | Thomas Schöpping | /**
|
848 | e545e620 | Thomas Schöpping | * @brief Application entry point.
|
849 | */
|
||
850 | int main(void) |
||
851 | { |
||
852 | 85e62aaf | Simon Welzel | // local variables
|
853 | eventmask_t eventmask = 0;
|
||
854 | eventflags_t eventflags = 0;
|
||
855 | aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE; |
||
856 | b6b45e4c | Thomas Schöpping | #if defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES)
|
857 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES |
858 | #endif
|
||
859 | |||
860 | /*
|
||
861 | * ##########################################################################
|
||
862 | * # system initialization #
|
||
863 | * ##########################################################################
|
||
864 | */
|
||
865 | |||
866 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_0)
|
||
867 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_0_ARGS)
|
||
868 | AMIROOS_CFG_MAIN_INIT_HOOK_0(AMIROOS_CFG_MAIN_INIT_HOOK_0_ARGS); |
||
869 | b6b45e4c | Thomas Schöpping | #else
|
870 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_0(); |
871 | e545e620 | Thomas Schöpping | #endif
|
872 | b6b45e4c | Thomas Schöpping | #endif
|
873 | e545e620 | Thomas Schöpping | |
874 | 85e62aaf | Simon Welzel | /* hardware, kernel, and operating system initialization */
|
875 | // ChibiOS/HAL and custom hal additions (if any)
|
||
876 | halInit(); |
||
877 | e545e620 | Thomas Schöpping | #ifdef MODULE_INIT_HAL_EXTRA
|
878 | 85e62aaf | Simon Welzel | MODULE_INIT_HAL_EXTRA(); |
879 | e545e620 | Thomas Schöpping | #endif
|
880 | |||
881 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1)
|
||
882 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS)
|
||
883 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_1(AMIROOS_CFG_MAIN_INIT_HOOK_1_ARGS); |
884 | e545e620 | Thomas Schöpping | #else
|
885 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_1(); |
886 | e545e620 | Thomas Schöpping | #endif
|
887 | #endif
|
||
888 | |||
889 | 85e62aaf | Simon Welzel | // ChibiOS/RT kernel and custom kernel additions (if any)
|
890 | chSysInit(); |
||
891 | e545e620 | Thomas Schöpping | #ifdef MODULE_INIT_KERNEL_EXTRA
|
892 | 85e62aaf | Simon Welzel | MODULE_INIT_KERNEL_EXTRA(); |
893 | e545e620 | Thomas Schöpping | #endif
|
894 | |||
895 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2)
|
||
896 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS)
|
||
897 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_2(AMIROOS_CFG_MAIN_INIT_HOOK_2_ARGS); |
898 | e545e620 | Thomas Schöpping | #else
|
899 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_2(); |
900 | e545e620 | Thomas Schöpping | #endif
|
901 | #endif
|
||
902 | |||
903 | 85e62aaf | Simon Welzel | // AMiRo-OS and custom OS additions (if any)
|
904 | 6b53f6bf | Thomas Schöpping | #if (AMIROOS_CFG_SHELL_ENABLE == true) |
905 | 85e62aaf | Simon Welzel | aosSysInit(moduleShellPrompt); |
906 | 6b53f6bf | Thomas Schöpping | #else
|
907 | 85e62aaf | Simon Welzel | aosSysInit(); |
908 | 6b53f6bf | Thomas Schöpping | #endif
|
909 | e545e620 | Thomas Schöpping | #ifdef MODULE_INIT_OS_EXTRA
|
910 | 85e62aaf | Simon Welzel | MODULE_INIT_OS_EXTRA(); |
911 | e545e620 | Thomas Schöpping | #endif
|
912 | |||
913 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3)
|
||
914 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS)
|
||
915 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_3(AMIROOS_CFG_MAIN_INIT_HOOK_3_ARGS); |
916 | e545e620 | Thomas Schöpping | #else
|
917 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_3(); |
918 | e545e620 | Thomas Schöpping | #endif
|
919 | #endif
|
||
920 | |||
921 | #if (AMIROOS_CFG_TESTS_ENABLE == true) |
||
922 | 9461fadc | Thomas Schöpping | #if defined(MODULE_INIT_TESTS)
|
923 | 85e62aaf | Simon Welzel | MODULE_INIT_TESTS(); |
924 | 9461fadc | Thomas Schöpping | #else
|
925 | 85e62aaf | Simon Welzel | #warning "MODULE_INIT_TESTS not defined" |
926 | 9461fadc | Thomas Schöpping | #endif
|
927 | e545e620 | Thomas Schöpping | #endif
|
928 | |||
929 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4)
|
||
930 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS)
|
||
931 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_4(AMIROOS_CFG_MAIN_INIT_HOOK_4_ARGS); |
932 | e545e620 | Thomas Schöpping | #else
|
933 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_4(); |
934 | e545e620 | Thomas Schöpping | #endif
|
935 | #endif
|
||
936 | |||
937 | 1ef74af5 | Simon Welzel | #if (AMIROOS_CFG_SSSP_ENABLE == true) |
938 | 85e62aaf | Simon Welzel | /* event associations */
|
939 | 1e5f7648 | Thomas Schöpping | #if (AMIROOS_CFG_SSSP_STACK_START == true) && (AMIROOS_CFG_SSSP_STACK_END == true) |
940 | 85e62aaf | Simon Welzel | chEvtRegisterMaskWithFlags(&aos.events.io, &_eventListenerIO, IOEVENT_MASK, MODULE_SSSP_EVENTFLAGS_PD | MODULE_SSSP_EVENTFLAGS_SYNC); |
941 | 1e5f7648 | Thomas Schöpping | #elif (AMIROOS_CFG_SSSP_STACK_START == true) |
942 | 85e62aaf | Simon Welzel | chEvtRegisterMaskWithFlags(&aos.events.io, &_eventListenerIO, IOEVENT_MASK, MODULE_SSSP_EVENTFLAGS_PD | MODULE_SSSP_EVENTFLAGS_SYNC | MODULE_SSSP_EVENTFLAGS_UP); |
943 | 1e5f7648 | Thomas Schöpping | #elif (AMIROOS_CFG_SSSP_STACK_END == true) |
944 | 85e62aaf | Simon Welzel | chEvtRegisterMaskWithFlags(&aos.events.io, &_eventListenerIO, IOEVENT_MASK, MODULE_SSSP_EVENTFLAGS_PD | MODULE_SSSP_EVENTFLAGS_SYNC | MODULE_SSSP_EVENTFLAGS_DN); |
945 | 1e5f7648 | Thomas Schöpping | #else
|
946 | 85e62aaf | Simon Welzel | chEvtRegisterMaskWithFlags(&aos.events.io, &_eventListenerIO, IOEVENT_MASK, MODULE_SSSP_EVENTFLAGS_PD | MODULE_SSSP_EVENTFLAGS_SYNC | MODULE_SSSP_EVENTFLAGS_DN | MODULE_SSSP_EVENTFLAGS_UP); |
947 | 1e5f7648 | Thomas Schöpping | #endif
|
948 | 1ef74af5 | Simon Welzel | #endif // end of AMIROOS_CFG_SSSP_ENABLE condition |
949 | 85e62aaf | Simon Welzel | chEvtRegisterMask(&aos.events.os, &_eventListenerOS, OSEVENT_MASK); |
950 | e545e620 | Thomas Schöpping | |
951 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5)
|
||
952 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS)
|
||
953 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_5(AMIROOS_CFG_MAIN_INIT_HOOK_5_ARGS); |
954 | e545e620 | Thomas Schöpping | #else
|
955 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_5(); |
956 | e545e620 | Thomas Schöpping | #endif
|
957 | #endif
|
||
958 | |||
959 | 85e62aaf | Simon Welzel | /* periphery communication initialization */
|
960 | // CAN (mandatory)
|
||
961 | canStart(&MODULE_HAL_CAN, &moduleHalCanConfig); |
||
962 | 75ab9f8b | Simon Welzel | |
963 | 85e62aaf | Simon Welzel | // module specific initialization (if any)
|
964 | e545e620 | Thomas Schöpping | #ifdef MODULE_INIT_PERIPHERY_COMM
|
965 | 85e62aaf | Simon Welzel | MODULE_INIT_PERIPHERY_COMM(); |
966 | e545e620 | Thomas Schöpping | #endif
|
967 | 85e62aaf | Simon Welzel | // user interface (if any)
|
968 | e545e620 | Thomas Schöpping | #ifdef MODULE_HAL_PROGIF
|
969 | 85e62aaf | Simon Welzel | aosIOChannelInit(&_stdiochannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF); |
970 | aosIOChannelOutputEnable(&_stdiochannel); |
||
971 | aosIOStreamAddChannel(&aos.iostream, &_stdiochannel); |
||
972 | ba516b61 | Thomas Schöpping | #if (AMIROOS_CFG_SHELL_ENABLE == true) |
973 | 85e62aaf | Simon Welzel | aosShellChannelInit(&_stdshellchannel, (BaseAsynchronousChannel*)&MODULE_HAL_PROGIF); |
974 | aosShellChannelInputEnable(&_stdshellchannel); |
||
975 | aosShellChannelOutputEnable(&_stdshellchannel); |
||
976 | aosShellStreamAddChannel(&aos.shell.stream, &_stdshellchannel); |
||
977 | ba516b61 | Thomas Schöpping | #endif
|
978 | e545e620 | Thomas Schöpping | #endif
|
979 | |||
980 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_6)
|
||
981 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS)
|
||
982 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_6(AMIROOS_CFG_MAIN_INIT_HOOK_6_ARGS); |
983 | e545e620 | Thomas Schöpping | #else
|
984 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_6(); |
985 | #endif
|
||
986 | #endif
|
||
987 | // for (unsigned int hase = 0; hase < 10; ++hase) {
|
||
988 | // //toggle LED
|
||
989 | // palClearLine(LINE_LED_GREEN);
|
||
990 | // chThdSleepMilliseconds(1500);
|
||
991 | // palSetLine(LINE_LED_GREEN);
|
||
992 | // chThdSleepMilliseconds(500);
|
||
993 | // }
|
||
994 | // chprintf((BaseSequentialStream*)&SD2, "Hallo Welt!\n");
|
||
995 | |||
996 | /* module is ready -> print welcome prompt */
|
||
997 | aosprintf("\n");
|
||
998 | aosprintf("######################################################################\n");
|
||
999 | aosprintf("# AMiRo-OS is an operating system designed for the Autonomous Mini #\n");
|
||
1000 | aosprintf("# Robot (AMiRo) platform. #\n");
|
||
1001 | aosprintf("# Copyright (C) 2016..2018 Thomas Schöpping et al. #\n");
|
||
1002 | aosprintf("# #\n");
|
||
1003 | aosprintf("# This is free software; see the source for copying conditions. #\n");
|
||
1004 | aosprintf("# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR #\n");
|
||
1005 | aosprintf("# A PARTICULAR PURPOSE. #\n");
|
||
1006 | aosprintf("# The development of this software was supported by the Excellence #\n");
|
||
1007 | aosprintf("# Cluster EXC 227 Cognitive Interaction Technology. The Excellence #\n");
|
||
1008 | aosprintf("# Cluster EXC 227 is a grant of the Deutsche Forschungsgemeinschaft #\n");
|
||
1009 | aosprintf("# (DFG) in the context of the German Excellence Initiative. #\n");
|
||
1010 | aosprintf("######################################################################\n");
|
||
1011 | aosprintf("\n");
|
||
1012 | e545e620 | Thomas Schöpping | |
1013 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_7)
|
||
1014 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS)
|
||
1015 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_7(AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS); |
1016 | e545e620 | Thomas Schöpping | #else
|
1017 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_7(); |
1018 | #endif //AMIROOS_CFG_MAIN_INIT_HOOK_7 |
||
1019 | #endif //AMIROOS_CFG_MAIN_INIT_HOOK_7_ARGS |
||
1020 | e545e620 | Thomas Schöpping | |
1021 | 1ef74af5 | Simon Welzel | #if (AMIROOS_CFG_SSSP_ENABLE == true) |
1022 | 85e62aaf | Simon Welzel | /* SSSP startup OS synchronization phase (end of startup stage 2) */
|
1023 | while ((shutdown == AOS_SHUTDOWN_NONE) && (eventmask = aosSysSsspStartupOsInitSyncCheck(&_eventListenerIO)) != 0) { |
||
1024 | /*
|
||
1025 | e545e620 | Thomas Schöpping | * This code is executed if the received event was not about the SYS_SYNC control signal.
|
1026 | * The returned event could be caused by any listener (not only the argument).
|
||
1027 | */
|
||
1028 | 85e62aaf | Simon Welzel | // IO event
|
1029 | if (eventmask & _eventListenerIO.events) {
|
||
1030 | eventflags = chEvtGetAndClearFlags(&_eventListenerIO); |
||
1031 | // PD event
|
||
1032 | if (eventflags & MODULE_SSSP_EVENTFLAGS_PD) {
|
||
1033 | shutdown = AOS_SHUTDOWN_PASSIVE; |
||
1034 | } else {
|
||
1035 | 933df08e | Thomas Schöpping | #ifdef MODULE_SSSP_STARTUP_2_2_IOEVENT_HOOK
|
1036 | 85e62aaf | Simon Welzel | MODULE_SSSP_STARTUP_2_2_IOEVENT_HOOK(eventmask, eventflags); |
1037 | e545e620 | Thomas Schöpping | #else
|
1038 | 85e62aaf | Simon Welzel | // ignore any other IO events
|
1039 | e545e620 | Thomas Schöpping | #endif
|
1040 | |||
1041 | 85e62aaf | Simon Welzel | } |
1042 | |||
1043 | } |
||
1044 | // OS event
|
||
1045 | else if (eventmask & _eventListenerOS.events) { |
||
1046 | eventflags = chEvtGetAndClearFlags(&_eventListenerOS); |
||
1047 | _unexpectedEventError(eventmask, eventflags); |
||
1048 | } |
||
1049 | // unknown event
|
||
1050 | else {
|
||
1051 | _unexpectedEventError(eventmask, 0);
|
||
1052 | } |
||
1053 | } |
||
1054 | #endif //AMIROOS_CFG_SSSP_ENABLE |
||
1055 | /*
|
||
1056 | 933df08e | Thomas Schöpping | * There must be no delays at this point, thus no hook is allowed.
|
1057 | */
|
||
1058 | |||
1059 | 27d0378b | Simon Welzel | #if (AMIROOS_CFG_SSSP_ENABLE == true) |
1060 | 85e62aaf | Simon Welzel | /* SSSP startup stage 3 (module stack initialization) */
|
1061 | if (shutdown == AOS_SHUTDOWN_NONE) {
|
||
1062 | shutdown = _ssspModuleStackInitialization(); |
||
1063 | } |
||
1064 | #endif //AMIROOS_CFG_SSSP_ENABLE |
||
1065 | 9461fadc | Thomas Schöpping | |
1066 | 85e62aaf | Simon Welzel | /*
|
1067 | 9461fadc | Thomas Schöpping | * There must be no delays at this point, thus no hook is allowed.
|
1068 | */
|
||
1069 | 85e62aaf | Simon Welzel | #if (AMIROOS_CFG_SSSP_ENABLE == true) |
1070 | /* synchronize calendars */
|
||
1071 | if (shutdown == AOS_SHUTDOWN_NONE) {
|
||
1072 | 9461fadc | Thomas Schöpping | #if (AMIROOS_CFG_SSSP_MASTER == true) |
1073 | 85e62aaf | Simon Welzel | CANTxFrame frame; |
1074 | struct tm t;
|
||
1075 | uint64_t encoded; |
||
1076 | |||
1077 | frame.DLC = 8;
|
||
1078 | frame.RTR = CAN_RTR_DATA; |
||
1079 | frame.IDE = CAN_IDE_STD; |
||
1080 | frame.SID = CALENDERSYNC_CANMSGID; |
||
1081 | |||
1082 | aosDbgPrintf("transmitting current date/time...\t");
|
||
1083 | // get current date & time
|
||
1084 | aosSysGetDateTime(&t); |
||
1085 | // encode
|
||
1086 | encoded = _TM2U64(&t); |
||
1087 | // serialize
|
||
1088 | _serialize(frame.data8, encoded, 8);
|
||
1089 | // transmit
|
||
1090 | canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &frame, TIME_IMMEDIATE); |
||
1091 | |||
1092 | aosDbgPrintf("done\n");
|
||
1093 | 9461fadc | Thomas Schöpping | #else
|
1094 | 85e62aaf | Simon Welzel | CANRxFrame frame; |
1095 | uint64_t encoded; |
||
1096 | struct tm t;
|
||
1097 | |||
1098 | aosDbgPrintf("receiving current date/time...\t");
|
||
1099 | // receive message
|
||
1100 | if (canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &frame, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT)) == MSG_OK) {
|
||
1101 | // validate message
|
||
1102 | if (frame.DLC == 8 && |
||
1103 | frame.RTR == CAN_RTR_DATA && |
||
1104 | frame.IDE == CAN_IDE_STD && |
||
1105 | frame.SID == CALENDERSYNC_CANMSGID) { |
||
1106 | // deserialize
|
||
1107 | encoded = _deserialize(frame.data8, 8);
|
||
1108 | // decode
|
||
1109 | _U642TM(&t, encoded); |
||
1110 | // set current date & time
|
||
1111 | aosSysSetDateTime(&t); |
||
1112 | aosDbgPrintf("success\n");
|
||
1113 | } else {
|
||
1114 | aosDbgPrintf("fail (invalid message)\n");
|
||
1115 | } |
||
1116 | } else {
|
||
1117 | aosDbgPrintf("fail (timeout)\n");
|
||
1118 | } |
||
1119 | #endif //AMIROOS_CFG_SSSP_MASTER |
||
1120 | aosDbgPrintf("\n");
|
||
1121 | 9461fadc | Thomas Schöpping | } |
1122 | 85e62aaf | Simon Welzel | #endif //AMIROOS_CFG_SSSP_ENABLE |
1123 | aosprintf("Line 1123\n");
|
||
1124 | e545e620 | Thomas Schöpping | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_8)
|
1125 | 85e62aaf | Simon Welzel | |
1126 | e545e620 | Thomas Schöpping | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS)
|
1127 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_8(AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS); |
1128 | e545e620 | Thomas Schöpping | #else
|
1129 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_8(); |
1130 | #endif //AMIROOS_CFG_MAIN_INIT_HOOK_8_ARGS |
||
1131 | e545e620 | Thomas Schöpping | #endif
|
1132 | 75ab9f8b | Simon Welzel | |
1133 | e545e620 | Thomas Schöpping | |
1134 | 85e62aaf | Simon Welzel | |
1135 | aosprintf("aosSysStart \n");
|
||
1136 | /* completely start AMiRo-OS */
|
||
1137 | if (shutdown == AOS_SHUTDOWN_NONE) {
|
||
1138 | aosSysStart(); |
||
1139 | } |
||
1140 | aosprintf("aosSysStart done \n");
|
||
1141 | e545e620 | Thomas Schöpping | |
1142 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_9)
|
||
1143 | #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS)
|
||
1144 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_9(AMIROOS_CFG_MAIN_INIT_HOOK_9_ARGS); |
1145 | e545e620 | Thomas Schöpping | #else
|
1146 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_INIT_HOOK_9(); |
1147 | e545e620 | Thomas Schöpping | #endif
|
1148 | #endif
|
||
1149 | |||
1150 | 85e62aaf | Simon Welzel | /*
|
1151 | e545e620 | Thomas Schöpping | * ##########################################################################
|
1152 | * # infinite loop #
|
||
1153 | * ##########################################################################
|
||
1154 | */
|
||
1155 | |||
1156 | 85e62aaf | Simon Welzel | // sleep until a shutdown event is received
|
1157 | while (shutdown == AOS_SHUTDOWN_NONE) {
|
||
1158 | // wait for an event
|
||
1159 | e545e620 | Thomas Schöpping | #if (AMIROOS_CFG_MAIN_LOOP_TIMEOUT != 0) |
1160 | 85e62aaf | Simon Welzel | eventmask = chEvtWaitOneTimeout(ALL_EVENTS, chTimeUS2I(AMIROOS_CFG_MAIN_LOOP_TIMEOUT)); |
1161 | e545e620 | Thomas Schöpping | #else
|
1162 | 85e62aaf | Simon Welzel | eventmask = chEvtWaitOne(ALL_EVENTS); |
1163 | e545e620 | Thomas Schöpping | #endif
|
1164 | |||
1165 | #if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0)
|
||
1166 | #if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS)
|
||
1167 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_LOOP_HOOK_0(AMIROOS_CFG_MAIN_LOOP_HOOK_0_ARGS); |
1168 | e545e620 | Thomas Schöpping | #else
|
1169 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_LOOP_HOOK_0(); |
1170 | e545e620 | Thomas Schöpping | #endif
|
1171 | #endif
|
||
1172 | |||
1173 | 75ab9f8b | Simon Welzel | |
1174 | 85e62aaf | Simon Welzel | switch (eventmask) {
|
1175 | // if this was an I/O event
|
||
1176 | case IOEVENT_MASK:
|
||
1177 | // evaluate flags
|
||
1178 | eventflags = chEvtGetAndClearFlags(&_eventListenerIO); |
||
1179 | // PD event
|
||
1180 | #if (AMIROOS_CFG_SSSP_ENABLE == true) |
||
1181 | if (eventflags & MODULE_SSSP_EVENTFLAGS_PD) {
|
||
1182 | shutdown = AOS_SHUTDOWN_PASSIVE; |
||
1183 | } |
||
1184 | 75ab9f8b | Simon Welzel | #endif
|
1185 | 85e62aaf | Simon Welzel | // all other events
|
1186 | e545e620 | Thomas Schöpping | #ifdef MODULE_MAIN_LOOP_IO_EVENT
|
1187 | 85e62aaf | Simon Welzel | else {
|
1188 | MODULE_MAIN_LOOP_IO_EVENT(eventmask, eventflags); |
||
1189 | } |
||
1190 | e545e620 | Thomas Schöpping | #endif
|
1191 | break;
|
||
1192 | 85e62aaf | Simon Welzel | |
1193 | // if this was an OS event
|
||
1194 | case OSEVENT_MASK:
|
||
1195 | // evaluate flags
|
||
1196 | eventflags = chEvtGetAndClearFlags(&_eventListenerOS); |
||
1197 | switch (eventflags) {
|
||
1198 | case AOS_SYSTEM_EVENTFLAGS_HIBERNATE:
|
||
1199 | shutdown = AOS_SHUTDOWN_HIBERNATE; |
||
1200 | break;
|
||
1201 | case AOS_SYSTEM_EVENTFLAGS_DEEPSLEEP:
|
||
1202 | shutdown = AOS_SHUTDOWN_DEEPSLEEP; |
||
1203 | break;
|
||
1204 | case AOS_SYSTEM_EVENTFLAGS_TRANSPORTATION:
|
||
1205 | shutdown = AOS_SHUTDOWN_TRANSPORTATION; |
||
1206 | break;
|
||
1207 | case AOS_SYSTEM_EVENTFLAGS_RESTART:
|
||
1208 | shutdown = AOS_SHUTDOWN_RESTART; |
||
1209 | break;
|
||
1210 | default:
|
||
1211 | _unexpectedEventError(eventmask, eventflags); |
||
1212 | break;
|
||
1213 | } |
||
1214 | e545e620 | Thomas Schöpping | break;
|
1215 | 85e62aaf | Simon Welzel | |
1216 | // if this was any other event (should be impossible to occur)
|
||
1217 | default:
|
||
1218 | eventflags = 0;
|
||
1219 | e545e620 | Thomas Schöpping | _unexpectedEventError(eventmask, eventflags); |
1220 | break;
|
||
1221 | } |
||
1222 | |||
1223 | #if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1)
|
||
1224 | #if defined(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS)
|
||
1225 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_LOOP_HOOK_1(AMIROOS_CFG_MAIN_LOOP_HOOK_1_ARGS); |
1226 | e545e620 | Thomas Schöpping | #else
|
1227 | 85e62aaf | Simon Welzel | AMIROOS_CFG_MAIN_LOOP_HOOK_1(); |
1228 | e545e620 | Thomas Schöpping | #endif
|
1229 | #endif
|
||
1230 | 1ef74af5 | Simon Welzel | |
1231 | 85e62aaf | Simon Welzel | } |
1232 | //#endif //DEBUG
|
||
1233 | aosprintf("Line 1224\n");
|
||
1234 | /*
|
||
1235 | e545e620 | Thomas Schöpping | * ##########################################################################
|
1236 | * # system shutdown #
|
||
1237 | * ##########################################################################
|
||
1238 | */
|
||
1239 | |||
1240 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0)
|
||
1241 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS)
|
||
1242 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0_ARGS); |
||
1243 | #else
|
||
1244 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_0(); |
||
1245 | #endif
|
||
1246 | #endif
|
||
1247 | |||
1248 | 85e62aaf | Simon Welzel | // initialize/acknowledge shutdown
|
1249 | aosSysShutdownInit(shutdown); |
||
1250 | e545e620 | Thomas Schöpping | |
1251 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1)
|
||
1252 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS)
|
||
1253 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1_ARGS); |
||
1254 | #else
|
||
1255 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_1(); |
||
1256 | #endif
|
||
1257 | #endif
|
||
1258 | |||
1259 | 85e62aaf | Simon Welzel | // stop system threads
|
1260 | aosSysStop(); |
||
1261 | e545e620 | Thomas Schöpping | |
1262 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2)
|
||
1263 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS)
|
||
1264 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2_ARGS); |
||
1265 | #else
|
||
1266 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_2(); |
||
1267 | #endif
|
||
1268 | #endif
|
||
1269 | |||
1270 | 85e62aaf | Simon Welzel | // deinitialize system
|
1271 | aosSysDeinit(); |
||
1272 | e545e620 | Thomas Schöpping | |
1273 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3)
|
||
1274 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS)
|
||
1275 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3_ARGS); |
||
1276 | #else
|
||
1277 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_3(); |
||
1278 | #endif
|
||
1279 | #endif
|
||
1280 | |||
1281 | 85e62aaf | Simon Welzel | /* stop all periphery communication */
|
1282 | // CAN (mandatory)
|
||
1283 | canStop(&MODULE_HAL_CAN); |
||
1284 | e545e620 | Thomas Schöpping | #ifdef MODULE_SHUTDOWN_PERIPHERY_COMM
|
1285 | 85e62aaf | Simon Welzel | MODULE_SHUTDOWN_PERIPHERY_COMM(); |
1286 | e545e620 | Thomas Schöpping | #endif
|
1287 | |||
1288 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4)
|
||
1289 | #if defined(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS)
|
||
1290 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4(AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4_ARGS); |
||
1291 | #else
|
||
1292 | AMIROOS_CFG_MAIN_SHUTDOWN_HOOK_4(); |
||
1293 | #endif
|
||
1294 | #endif
|
||
1295 | |||
1296 | 85e62aaf | Simon Welzel | // finally hand over to bootloader
|
1297 | aosSysShutdownFinal(shutdown); |
||
1298 | e545e620 | Thomas Schöpping | |
1299 | 85e62aaf | Simon Welzel | /*
|
1300 | e545e620 | Thomas Schöpping | * ##########################################################################
|
1301 | * # after shutdown/restart #
|
||
1302 | * ##########################################################################
|
||
1303 | *
|
||
1304 | * NOTE: This code will not be executed, since the bootloader callbacks will stop/restart the MCU.
|
||
1305 | * It is included nevertheless for the sake of completeness and to explicitely indicate,
|
||
1306 | * which subsystems should NOT be shut down.
|
||
1307 | */
|
||
1308 | |||
1309 | 85e62aaf | Simon Welzel | // return an error, since this code should not be executed
|
1310 | return -1; |
||
1311 | e545e620 | Thomas Schöpping | } |
1312 | 53710ca3 | Marc Rothmann | |
1313 | /** @} */ |