amiro-os / core / src / aos_main.cpp @ 78b92b33
History | View | Annotate | Download (51.813 KB)
1 |
/*
|
---|---|
2 |
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
3 |
Copyright (C) 2016..2019 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 |
/**
|
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 |
#include <amiroos.h> |
30 |
|
31 |
/*
|
32 |
* hook to add further includes
|
33 |
*/
|
34 |
#if defined(AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER)
|
35 |
#include AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER
|
36 |
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER) */ |
37 |
|
38 |
/******************************************************************************/
|
39 |
/* LOCAL DEFINITIONS */
|
40 |
/******************************************************************************/
|
41 |
|
42 |
/**
|
43 |
* @brief Event mask to identify GPIO events.
|
44 |
*/
|
45 |
#define GPIOEVENT_MASK EVENT_MASK(0) |
46 |
|
47 |
/**
|
48 |
* @brief Event mask to identify OS events.
|
49 |
*/
|
50 |
#define OSEVENT_MASK EVENT_MASK(1) |
51 |
|
52 |
/**
|
53 |
* @brief Event mask to idetify CAN events.
|
54 |
*/
|
55 |
#define CANEVENT_MASK EVENT_MASK(2) |
56 |
|
57 |
/**
|
58 |
* @brief Event mask to idetify timeout events.
|
59 |
*/
|
60 |
#define TIMEOUTEVENT_MASK EVENT_MASK(3) |
61 |
|
62 |
/**
|
63 |
* @brief Event mask to idetify signal delay events.
|
64 |
*/
|
65 |
#define DELAYEVENT_MASK EVENT_MASK(4) |
66 |
|
67 |
#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_CAN == TRUE)) || defined(__DOXYGEN__) |
68 |
#define SSSP_STAGE3_ENABLE true |
69 |
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_CAN == TRUE) */ |
70 |
#define SSSP_STAGE3_ENABLE false |
71 |
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_CAN == TRUE) */ |
72 |
|
73 |
#if (SSSP_STAGE3_ENABLE == true) || defined(__DOXYGEN__) |
74 |
|
75 |
/**
|
76 |
* @brief CAN message identifier for initialization of the SSSP stack initialization sequence.
|
77 |
*/
|
78 |
#define SSSP_STACKINIT_CANMSGID_INIT 0x003 |
79 |
|
80 |
/**
|
81 |
* @brief CAN message identifier for transmitting module IDs during the SSSP stack initialization sequence.
|
82 |
*/
|
83 |
#define SSSP_STACKINIT_CANMSGID_MODULEID 0x002 |
84 |
|
85 |
/**
|
86 |
* @brief CAN message identifier for abortion of the SSSP stack initialization sequence.
|
87 |
*/
|
88 |
#define SSSP_STACKINIT_CANMSGID_ABORT 0x001 |
89 |
|
90 |
#endif /* (SSSP_STAGE3_ENABLE == true) */ |
91 |
|
92 |
#if (AMIROOS_CFG_SSSP_ENABLE != true) || defined(__DOXYGEN__) |
93 |
/**
|
94 |
* @brief Default shutdown mode if SSSP is unavailable.
|
95 |
*/
|
96 |
#define AOS_SHUTDOWN_DEFAULT AOS_SHUTDOWN_DEEPSLEEP
|
97 |
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */ |
98 |
|
99 |
#if (HAL_USE_CAN == TRUE) || defined(__DOXYGEN__)
|
100 |
/**
|
101 |
* @brief CAN message identifier for calender synchronization message.
|
102 |
*/
|
103 |
#define CALENDERSYNC_CANMSGID 0x004 |
104 |
#endif /* (HAL_USE_CAN == TRUE) */ |
105 |
|
106 |
/******************************************************************************/
|
107 |
/* EXPORTED VARIABLES */
|
108 |
/******************************************************************************/
|
109 |
|
110 |
/******************************************************************************/
|
111 |
/* LOCAL TYPES */
|
112 |
/******************************************************************************/
|
113 |
|
114 |
/******************************************************************************/
|
115 |
/* LOCAL VARIABLES */
|
116 |
/******************************************************************************/
|
117 |
|
118 |
/**
|
119 |
* @brief Listener object for GPIO events.
|
120 |
*/
|
121 |
static event_listener_t _eventListenerGPIO;
|
122 |
|
123 |
/**
|
124 |
* @brief Listener object for OS events.
|
125 |
*/
|
126 |
static event_listener_t _eventListenerOS;
|
127 |
|
128 |
#if defined(MODULE_HAL_PROGIF) || defined(__DOXYGEN__)
|
129 |
|
130 |
/**
|
131 |
* @brief I/O channel for the programmer interface.
|
132 |
*/
|
133 |
static AosIOChannel _stdiochannel;
|
134 |
|
135 |
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__) |
136 |
|
137 |
/**
|
138 |
* @brief I/O shell channel for the programmer interface.
|
139 |
*/
|
140 |
static AosShellChannel _stdshellchannel;
|
141 |
|
142 |
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */ |
143 |
#endif /* defined(MODULE_HAL_PROGIF) */ |
144 |
|
145 |
/*
|
146 |
* hook to add further static variables
|
147 |
*/
|
148 |
#if defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES)
|
149 |
AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES |
150 |
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES) */ |
151 |
|
152 |
/******************************************************************************/
|
153 |
/* LOCAL FUNCTIONS */
|
154 |
/******************************************************************************/
|
155 |
|
156 |
/**
|
157 |
* @brief Prints an error message about an unexpected event.
|
158 |
*
|
159 |
* @param[in] mask The event mask.
|
160 |
* @param[in] flags The event flags.
|
161 |
*/
|
162 |
static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags) |
163 |
{ |
164 |
#if (AMIROOS_CFG_DBG == true) |
165 |
aosprintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
|
166 |
#else /* (AMIROOS_CFG_DBG == true) */ |
167 |
(void)(mask);
|
168 |
(void)(flags);
|
169 |
#endif /* (AMIROOS_CFG_DBG == true) */ |
170 |
return;
|
171 |
} |
172 |
|
173 |
#if (SSSP_STAGE3_ENABLE == true) || defined(__DOXYGEN__) |
174 |
/**
|
175 |
* @brief Callback function to be used during SSSP stack initialization sequence.
|
176 |
*
|
177 |
* @param[in] par A pointer to an @p event_source_t to be fired.
|
178 |
*/
|
179 |
static void _ssspTimerCallback(void* par) |
180 |
{ |
181 |
aosDbgCheck(par != NULL);
|
182 |
|
183 |
chSysLockFromISR(); |
184 |
chEvtBroadcastI((event_source_t*)par); |
185 |
chSysUnlockFromISR(); |
186 |
|
187 |
return;
|
188 |
} |
189 |
#endif /* (SSSP_STAGE3_ENABLE == true) */ |
190 |
|
191 |
/**
|
192 |
* @brief Helper function to serialize data.
|
193 |
*
|
194 |
* @param[out] dst Pointer to the output buffer.
|
195 |
* @param[in] src Data to be serialized.
|
196 |
* @param[in] n Number of bytes to serialize.
|
197 |
*/
|
198 |
inline void _serialize(uint8_t* dst, const uint64_t src, const uint8_t n) |
199 |
{ |
200 |
aosDbgCheck(dst != NULL);
|
201 |
aosDbgCheck(n > 0 && n <= 8); |
202 |
|
203 |
for (uint8_t byte = 0; byte < n; ++byte) { |
204 |
dst[byte] = (uint8_t)((src >> (byte * 8)) & 0xFF); |
205 |
} |
206 |
|
207 |
return;
|
208 |
} |
209 |
|
210 |
/**
|
211 |
* @brief Helper function to deserialize data.
|
212 |
*
|
213 |
* @param[in] src Pointer to the buffer of data to be deserialzed.
|
214 |
* @param[in] n Number of bytes to deserialize.
|
215 |
*
|
216 |
* @return The deserialized 32 bit data.
|
217 |
*/
|
218 |
inline uint64_t _deserialize(uint8_t* src, const uint8_t n) |
219 |
{ |
220 |
aosDbgCheck(src != NULL);
|
221 |
aosDbgCheck(n > 0 && n <= 8); |
222 |
|
223 |
uint64_t result = 0;
|
224 |
for (uint8_t byte = 0; byte < n; ++byte) { |
225 |
result |= ((uint64_t)src[byte]) << (byte * 8);
|
226 |
} |
227 |
|
228 |
return result;
|
229 |
} |
230 |
|
231 |
#if (HAL_USE_RTC == TRUE) || defined(__DOXYGEN__)
|
232 |
|
233 |
/**
|
234 |
* @brief Converter function to encode a TM value to a single unsigned 64 bit integer.
|
235 |
*
|
236 |
* @details Contents of the TM struct are mapped as follows:
|
237 |
* bits |63 62|61 53|52 50|49 26|25 22|21 17|16 12|11 6|5 0|
|
238 |
* #bits | 2 | 9 | 3 | 24 | 4 | 5 | 5 | 6 | 6 |
|
239 |
* value | isdst | yday | wday | year | mon | mday | hour | min | sec |
|
240 |
* range | special | [0, 365] | [0, 6] | [1900, ...] | [0, 11] | [1, 31] | [0, 23] | [0, 59] | [0, 61] |
|
241 |
* The Daylight Saving Time Flag (isdsst) is encoded as follows:
|
242 |
* DST not in effect -> 0
|
243 |
* DST in effect -> 1
|
244 |
* no information available -> 2
|
245 |
*
|
246 |
* @param[in] src Pointer to the TM struct to encode.
|
247 |
*
|
248 |
* @return An unsigned 64 bit integer, which holds the encoded time value.
|
249 |
*/
|
250 |
inline uint64_t _TM2U64(struct tm* src) |
251 |
{ |
252 |
aosDbgCheck(src != NULL);
|
253 |
|
254 |
return (((uint64_t)(src->tm_sec & 0x0000003F) << (0)) | |
255 |
((uint64_t)(src->tm_min & 0x0000003F) << (6)) | |
256 |
((uint64_t)(src->tm_hour & 0x0000001F) << (12)) | |
257 |
((uint64_t)(src->tm_mday & 0x0000001F) << (17)) | |
258 |
((uint64_t)(src->tm_mon & 0x0000000F) << (22)) | |
259 |
((uint64_t)(src->tm_year & 0x00FFFFFF) << (26)) | |
260 |
((uint64_t)(src->tm_wday & 0x00000007) << (50)) | |
261 |
((uint64_t)(src->tm_yday & 0x000001FF) << (53)) | |
262 |
((uint64_t)((src->tm_isdst == 0) ? 0 : (src->tm_isdst > 0) ? 1 : 2) << (62))); |
263 |
} |
264 |
|
265 |
/**
|
266 |
* @brief Converter functiomn to retrieve the encoded TM value from an unsigned 64 bit integer.
|
267 |
*
|
268 |
* @details For information on the encoding, please refer to @p _TM2U64 function.
|
269 |
*
|
270 |
* @param[out] dst The TM struct to fill with the decoded values.
|
271 |
* @param[in] src Unsigned 64 bit integer holding the encoded TM value.
|
272 |
*/
|
273 |
inline void _U642TM(struct tm* dst, const uint64_t src) |
274 |
{ |
275 |
aosDbgCheck(dst != NULL);
|
276 |
|
277 |
dst->tm_sec = (src >> 0) & 0x0000003F; |
278 |
dst->tm_min = (src >> 6) & 0x0000003F; |
279 |
dst->tm_hour = (src >> 12) & 0x0000001F; |
280 |
dst->tm_mday = (src >> 17) & 0x0000001F; |
281 |
dst->tm_mon = (src >> 22) & 0x0000000F; |
282 |
dst->tm_year = (src >> 26) & 0x00FFFFFF; |
283 |
dst->tm_wday = (src >> 50) & 0x00000007; |
284 |
dst->tm_yday = (src >> 53) & 0x000001FF; |
285 |
dst->tm_isdst = (((src >> 62) & 0x03) == 0) ? 0 : (((src >> 62) & 0x03) > 0) ? 1 : -1; |
286 |
|
287 |
return;
|
288 |
} |
289 |
|
290 |
#endif /* (HAL_USE_RTC == TRUE) */ |
291 |
|
292 |
#if (SSSP_STAGE3_ENABLE) || defined(__DOXYGEN__)
|
293 |
/**
|
294 |
* @brief Implementation of the SSSP module stack initialization sequence (startup phase 3).
|
295 |
*
|
296 |
* @return Shutdown value.
|
297 |
* @retval AOS_SHUTDOWN_NONE No shutdown signal received
|
298 |
* @retval AOS_SHUTDOWN_PASSIVE Shutdown signal received.
|
299 |
*/
|
300 |
aos_shutdown_t _ssspModuleStackInitialization(void)
|
301 |
{ |
302 |
// local types
|
303 |
/**
|
304 |
* @brief States for the internal state machine to implement SSSP startup stage 3.
|
305 |
*/
|
306 |
typedef enum { |
307 |
STAGE_3_1, /**< Initiation of SSSP startup stage 3. */
|
308 |
STAGE_3_2, /**< Starting the sequence and broadcasting the first ID. */
|
309 |
STAGE_3_3_WAITFORFIRSTID, /**< Waiting for first ID after initiation. */
|
310 |
STAGE_3_3_WAITFORIDORSIG, /**< Waiting for next ID or activation of neighbor signal. */
|
311 |
STAGE_3_3_WAITFORID, /**< Waiting for next ID (after the module has set its own ID). */
|
312 |
STAGE_3_4_FINISH, /**< Successful finish of stage 3. */
|
313 |
STAGE_3_4_ABORT_ACTIVE, /**< Aborting stage 3 (active). */
|
314 |
STAGE_3_4_ABORT, /**< Aborting stage 3 (passive). */
|
315 |
} sssp_modulestackinitstage_t; |
316 |
|
317 |
typedef struct { |
318 |
bool loop : 1; |
319 |
bool wfe : 1; |
320 |
bool wfe_next : 1; |
321 |
} flags_t; |
322 |
|
323 |
// local variables
|
324 |
aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE; |
325 |
sssp_modulestackinitstage_t stage = STAGE_3_1; |
326 |
eventmask_t eventmask = 0;
|
327 |
eventflags_t ioflags = 0;
|
328 |
event_source_t eventSourceTimeout; |
329 |
event_source_t eventSourceDelay; |
330 |
event_listener_t eventListenerTimeout; |
331 |
event_listener_t eventListenerDelay; |
332 |
event_listener_t eventListenerCan; |
333 |
virtual_timer_t timerTimeout; |
334 |
virtual_timer_t timerDelay; |
335 |
CANTxFrame canTxFrame; |
336 |
CANRxFrame canRxFrame; |
337 |
#if (AMIROOS_CFG_SSSP_STACK_START != true) || (AMIROOS_CFG_DBG == true) |
338 |
aos_ssspmoduleid_t lastid = 0;
|
339 |
#endif /* (AMIROOS_CFG_SSSP_STACK_START != true) || (AMIROOS_CFG_DBG == true) */ |
340 |
flags_t flags; |
341 |
aos_timestamp_t uptime; |
342 |
|
343 |
// initialize local varibles
|
344 |
chEvtObjectInit(&eventSourceTimeout); |
345 |
chEvtObjectInit(&eventSourceDelay); |
346 |
chVTObjectInit(&timerTimeout); |
347 |
chVTObjectInit(&timerDelay); |
348 |
canTxFrame.RTR = CAN_RTR_DATA; |
349 |
canTxFrame.IDE = CAN_IDE_STD; |
350 |
flags.loop = true;
|
351 |
flags.wfe = false; // do not wait for events in the initial iteration of the FSM loop |
352 |
flags.wfe_next = true;
|
353 |
|
354 |
// initialize system variables
|
355 |
aos.sssp.stage = AOS_SSSP_STARTUP_3_1; |
356 |
aos.sssp.moduleId = 0;
|
357 |
|
358 |
// listen to events (timout, delay, CAN receive)
|
359 |
chEvtRegisterMask(&eventSourceTimeout, &eventListenerTimeout, TIMEOUTEVENT_MASK); |
360 |
chEvtRegisterMask(&eventSourceDelay, &eventListenerDelay, DELAYEVENT_MASK); |
361 |
chEvtRegisterMask(&MODULE_HAL_CAN.rxfull_event, &eventListenerCan, CANEVENT_MASK); |
362 |
|
363 |
/*
|
364 |
* FSM in a loop.
|
365 |
*
|
366 |
* This is a fully event-based FSM for the module stack initialization
|
367 |
* sequence, defined by SSSP as startup stage 3. There are five different
|
368 |
* events that can occur at this point:
|
369 |
* I/O events: The input level of an input pin has changed. Such events must
|
370 |
* be handled differently depending on the current state. Most
|
371 |
* of the time, however, such events can be ignored.
|
372 |
* OS events: Such events are only available after this stage completed and
|
373 |
* thus should never occur. However, there is an optional hook
|
374 |
* to handle such events, nevertheless.
|
375 |
* CAN events: At least one CAN message was received. Note that this event
|
376 |
* will only fire again if all input buffers have been cleared.
|
377 |
* timeouts: If some module does not support the sequence of there is any
|
378 |
* issue, such a case is detected via timeouts and must be
|
379 |
* handled accordingly (see abort state). In some cases, it is
|
380 |
* possible that a timeout event occurres 'simultaneously' with
|
381 |
* some other event. This can be caused by several timing issues
|
382 |
* and is a valid situation. As a result, any other events
|
383 |
* should be handled before the timeout event. If the other
|
384 |
* events are expected and valid, this implementation requires
|
385 |
* the timeout event flag to be cleared explicitely. Otherwise
|
386 |
* it is evaluated at the end of each iteration of the loop.
|
387 |
* delays: Depending on the current state, delays are required by SSSP
|
388 |
* for timing of the sequential activation of signals.
|
389 |
*/
|
390 |
aosDbgPrintf("SSSP stack initialization sequence:\n");
|
391 |
while (flags.loop) {
|
392 |
#if (AMIROOS_CFG_DBG == true) |
393 |
switch (stage) {
|
394 |
case STAGE_3_1:
|
395 |
aosDbgPrintf(">>> 3-1\n");
|
396 |
break;
|
397 |
case STAGE_3_2:
|
398 |
aosDbgPrintf(">>> 3-2\n");
|
399 |
break;
|
400 |
case STAGE_3_3_WAITFORFIRSTID:
|
401 |
aosDbgPrintf(">>> 3-3 (1st ID)\n");
|
402 |
break;
|
403 |
case STAGE_3_3_WAITFORIDORSIG:
|
404 |
aosDbgPrintf(">>> 3-3 (ID/sig)\n");
|
405 |
break;
|
406 |
case STAGE_3_3_WAITFORID:
|
407 |
aosDbgPrintf(">>> 3-3 (ID)\n");
|
408 |
break;
|
409 |
case STAGE_3_4_FINISH:
|
410 |
aosDbgPrintf(">>> 3-4 (finish)\n");
|
411 |
break;
|
412 |
case STAGE_3_4_ABORT_ACTIVE:
|
413 |
aosDbgPrintf(">>> 3-4 (active abort)\n");
|
414 |
break;
|
415 |
case STAGE_3_4_ABORT:
|
416 |
aosDbgPrintf(">>> 3-4 (abort)\n");
|
417 |
break;
|
418 |
} |
419 |
#endif /* (AMIROOS_CFG_DBG == true) */ |
420 |
|
421 |
// reset wfe flag for the next iteration
|
422 |
flags.wfe_next = true;
|
423 |
|
424 |
// waiting for events (may be skipped)
|
425 |
if (flags.wfe) {
|
426 |
// wait for any event to occur
|
427 |
aosDbgPrintf("WFE...");
|
428 |
eventmask = chEvtWaitAnyTimeout(ALL_EVENTS, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT)); |
429 |
aosDbgPrintf("\t0x%08X", eventmask);
|
430 |
} else {
|
431 |
aosDbgPrintf("WFE skipped");
|
432 |
eventmask = 0;
|
433 |
} |
434 |
aosSysGetUptime(&uptime); |
435 |
aosDbgPrintf("\t%04ums\n", (uint32_t)(uptime / MICROSECONDS_PER_MILLISECOND));
|
436 |
|
437 |
/*
|
438 |
* execute some general tasks and high priority events
|
439 |
*/
|
440 |
// no event occurred at all
|
441 |
if ((flags.wfe) && (eventmask == 0)) { |
442 |
aosDbgPrintf("ERR: no evt\n");
|
443 |
// enforce timeout event
|
444 |
chEvtBroadcast(&eventSourceTimeout); |
445 |
continue;
|
446 |
} |
447 |
// if an IO event occurred
|
448 |
if (eventmask & _eventListenerGPIO.events) {
|
449 |
ioflags = chEvtGetAndClearFlags(&_eventListenerGPIO); |
450 |
aosDbgPrintf("INFO: IO evt (0x%08X)\n", ioflags);
|
451 |
// a power-down event occurred
|
452 |
if (ioflags & MODULE_SSSP_EVENTFLAG_PD) {
|
453 |
aosDbgPrintf("PD evt\n");
|
454 |
// deactivate S and UP
|
455 |
aosDbgPrintf("disabling S\n");
|
456 |
apalControlGpioSet(&moduleSsspGpioS, APAL_GPIO_OFF); |
457 |
#if (AMIROOS_CFG_SSSP_STACK_END != true) |
458 |
aosDbgPrintf("disabling UP\n");
|
459 |
apalControlGpioSet(&moduleSsspGpioUP, APAL_GPIO_OFF); |
460 |
#endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */ |
461 |
// set shutdown flag and exit the loop
|
462 |
shutdown = AOS_SHUTDOWN_PASSIVE; |
463 |
break;
|
464 |
} |
465 |
// the S signal was deactivated
|
466 |
if (ioflags & MODULE_SSSP_EVENTFLAG_S) {
|
467 |
apalControlGpioState_t sstate; |
468 |
apalControlGpioGet(&moduleSsspGpioS, &sstate); |
469 |
if (sstate == APAL_GPIO_ON) {
|
470 |
aosDbgPrintf("S evt (enabled)\n");
|
471 |
} else {
|
472 |
aosDbgPrintf("S evt (disabled)\n");
|
473 |
// either finish or abort
|
474 |
if ((stage == STAGE_3_3_WAITFORID) && (aos.sssp.moduleId != 0)) { |
475 |
stage = STAGE_3_4_FINISH; |
476 |
} else if (stage != STAGE_3_4_ABORT) { |
477 |
stage = STAGE_3_4_ABORT_ACTIVE; |
478 |
} |
479 |
} |
480 |
} |
481 |
} |
482 |
// an OS event occurred
|
483 |
if (eventmask & _eventListenerOS.events) {
|
484 |
aosDbgPrintf("WARN: OS evt\n");
|
485 |
// get the flags
|
486 |
eventflags_t oseventflags = chEvtGetAndClearFlags(&_eventListenerOS); |
487 |
// there should be no OS events at this point
|
488 |
#if defined(MODULE_SSSP_STARTUP_3_OSEVENT_HOOK)
|
489 |
MODULE_SSSP_STARTUP_3_OSEVENT_HOOK(eventmask, eventflags); |
490 |
#else /* defined(MODULE_SSSP_STARTUP_3_OSEVENT_HOOK) */ |
491 |
_unexpectedEventError(eventmask, oseventflags); |
492 |
#endif /* defined(MODULE_SSSP_STARTUP_3_OSEVENT_HOOK) */ |
493 |
} |
494 |
// if a CAN event occurred
|
495 |
if ((eventmask & eventListenerCan.events)) {
|
496 |
aosDbgPrintf("CAN evt\n");
|
497 |
// fetch message
|
498 |
if (flags.wfe) {
|
499 |
canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canRxFrame, TIME_IMMEDIATE); |
500 |
aosDbgPrintf("CAN <- 0x%03X\n", canRxFrame.SID);
|
501 |
} |
502 |
// identify and handle abort messgaes
|
503 |
if (canRxFrame.SID == SSSP_STACKINIT_CANMSGID_ABORT) {
|
504 |
stage = STAGE_3_4_ABORT; |
505 |
} |
506 |
// warn if a unexpected message was received
|
507 |
else if ((canRxFrame.SID != SSSP_STACKINIT_CANMSGID_INIT) && |
508 |
(canRxFrame.SID != SSSP_STACKINIT_CANMSGID_MODULEID)) { |
509 |
aosDbgPrintf("WARN: unknown msg\n");
|
510 |
} |
511 |
// any further pending messages are fetched at the end of the loop
|
512 |
} |
513 |
// if a timeout event occurred
|
514 |
if (eventmask & eventListenerTimeout.events) {
|
515 |
aosDbgPrintf("timeout evt\n");
|
516 |
// is handled at the end of the loop (or must be cleared by FSM)
|
517 |
} |
518 |
// if a delay event occurred
|
519 |
if (eventmask & eventListenerDelay.events) {
|
520 |
aosDbgPrintf("delay evt\n");
|
521 |
// is handled by FSM
|
522 |
} |
523 |
|
524 |
/*
|
525 |
* this is the actual FSM
|
526 |
*/
|
527 |
switch (stage) {
|
528 |
case STAGE_3_1:
|
529 |
{ |
530 |
aos.sssp.stage = AOS_SSSP_STARTUP_3_1; |
531 |
|
532 |
// there was no event at all (skipped wfe)
|
533 |
if (eventmask == 0 && flags.wfe == false) { |
534 |
#if (AMIROOS_CFG_SSSP_MASTER == true) |
535 |
// initialize the stage by transmitting an according CAN message
|
536 |
aosDbgPrintf("CAN -> init\n");
|
537 |
canTxFrame.DLC = 0;
|
538 |
canTxFrame.SID = SSSP_STACKINIT_CANMSGID_INIT; |
539 |
if (canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_IMMEDIATE) != MSG_OK) {
|
540 |
chEvtBroadcast(&eventSourceTimeout); |
541 |
break;
|
542 |
} |
543 |
// activate S
|
544 |
aosDbgPrintf("enabling S\n");
|
545 |
apalControlGpioSet(&moduleSsspGpioS, APAL_GPIO_ON); |
546 |
#if (AMIROOS_CFG_SSSP_STACK_START == true) |
547 |
// proceed immediately
|
548 |
stage = STAGE_3_2; |
549 |
flags.wfe_next = false;
|
550 |
#else /* (AMIROOS_CFG_SSSP_STACK_START == true) */ |
551 |
// set the timeout timer
|
552 |
chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout); |
553 |
// proceed
|
554 |
stage = STAGE_3_3_WAITFORFIRSTID; |
555 |
#endif /* (AMIROOS_CFG_SSSP_STACK_START == true) */ |
556 |
#else /* (AMIROOS_CFG_SSSP_MASTER == true) */ |
557 |
// set the timeout timer
|
558 |
chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout); |
559 |
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */ |
560 |
} |
561 |
|
562 |
#if (AMIROOS_CFG_SSSP_MASTER != true) |
563 |
// a CAN message was received
|
564 |
else if (eventmask & eventListenerCan.events) { |
565 |
// if an initiation message was received
|
566 |
if (canRxFrame.DLC == 0 && |
567 |
canRxFrame.RTR == CAN_RTR_DATA && |
568 |
canRxFrame.IDE == CAN_IDE_STD && |
569 |
canRxFrame.SID == SSSP_STACKINIT_CANMSGID_INIT) { |
570 |
aosDbgPrintf("init msg\n");
|
571 |
// reset the timeout timer and clear pending flags
|
572 |
chVTReset(&timerTimeout); |
573 |
chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE); |
574 |
eventmask &= ~(eventListenerTimeout.events); |
575 |
// activate S
|
576 |
aosDbgPrintf("enabling S\n");
|
577 |
apalControlGpioSet(&moduleSsspGpioS, APAL_GPIO_ON); |
578 |
#if (AMIROOS_CFG_SSSP_STACK_START == true) |
579 |
// proceed
|
580 |
stage = STAGE_3_2; |
581 |
flags.wfe_next = false;
|
582 |
#else /* (AMIROOS_CFG_SSSP_STACK_START == true) */ |
583 |
// set the timeout timer
|
584 |
chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout); |
585 |
// proceed
|
586 |
stage = STAGE_3_3_WAITFORFIRSTID; |
587 |
#endif /* (AMIROOS_CFG_SSSP_STACK_START == true) */ |
588 |
} |
589 |
} |
590 |
#endif /* (AMIROOS_CFG_SSSP_MASTER != true) */ |
591 |
|
592 |
break;
|
593 |
} /* end of STAGE_3_1 */
|
594 |
|
595 |
case STAGE_3_2:
|
596 |
{ |
597 |
#if (AMIROOS_CFG_SSSP_STACK_START == true) |
598 |
aos.sssp.stage = AOS_SSSP_STARTUP_3_2; |
599 |
|
600 |
// if this stage was just entered
|
601 |
if (flags.wfe == false) { |
602 |
// set the module ID
|
603 |
aos.sssp.moduleId = 1;
|
604 |
// broadcast module ID
|
605 |
aosDbgPrintf("CAN -> ID (%u)\n", aos.sssp.moduleId);
|
606 |
canTxFrame.DLC = 4;
|
607 |
canTxFrame.SID = SSSP_STACKINIT_CANMSGID_MODULEID; |
608 |
_serialize(canTxFrame.data8, aos.sssp.moduleId, 4);
|
609 |
if (canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canTxFrame, TIME_IMMEDIATE) != MSG_OK) {
|
610 |
chEvtBroadcast(&eventSourceTimeout); |
611 |
break;
|
612 |
} |
613 |
#if (AMIROOS_CFG_DBG == true) |
614 |
lastid = aos.sssp.moduleId; |
615 |
#endif /* (AMIROOS_CFG_DBG == true) */ |
616 |
#if (AMIROOS_CFG_SSSP_STACK_END == true) |
617 |
// sequence is already over
|
618 |
// deactivate S
|
619 |
aosDbgPrintf("disabling S\n");
|
620 |
apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF); |
621 |
// proceed
|
622 |
stage = STAGE_3_3_WAITFORID; |
623 |
#else /* (AMIROOS_CFG_SSSP_STACK_END == true) */ |
624 |
// set the delay timer so the UP signal is activated later
|
625 |
chVTSet(&timerDelay, chTimeUS2I(AMIROOS_CFG_SSSP_SIGNALDELAY), _ssspTimerCallback, &eventSourceDelay); |
626 |
#endif /* (AMIROOS_CFG_SSSP_STACK_END == true) */ |
627 |
} |
628 |
|
629 |
// if a delay event occurred
|
630 |
if (eventmask & eventListenerDelay.events) {
|
631 |
// activate UP
|
632 |
aosDbgPrintf("enabling UP\n");
|
633 |
apalControlGpioSet(&moduleSsspGpioUP, APAL_GPIO_ON); |
634 |
// deactivate S
|
635 |
aosDbgPrintf("disabling S\n");
|
636 |
apalControlGpioSet(&moduleSsspGpioS, APAL_GPIO_OFF); |
637 |
// explicitely clear timeout event flag
|
638 |
chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE); |
639 |
eventmask &= ~(eventListenerTimeout.events); |
640 |
// proceed
|
641 |
stage = STAGE_3_3_WAITFORID; |
642 |
} |
643 |
#endif /* (AMIROOS_CFG_SSSP_STACK_START == true) */ |
644 |
|
645 |
break;
|
646 |
} /* end of STAGE_3_2 */
|
647 |
|
648 |
case STAGE_3_3_WAITFORFIRSTID:
|
649 |
{ |
650 |
#if (AMIROOS_CFG_SSSP_STACK_START != true) |
651 |
aos.sssp.stage = AOS_SSSP_STARTUP_3_3; |
652 |
|
653 |
// a CAN message was received
|
654 |
if (eventmask & eventListenerCan.events) {
|
655 |
// if an ID message was received
|
656 |
if (canRxFrame.DLC == 4 && |
657 |
canRxFrame.RTR == CAN_RTR_DATA && |
658 |
canRxFrame.IDE == CAN_IDE_STD && |
659 |
canRxFrame.SID == SSSP_STACKINIT_CANMSGID_MODULEID) { |
660 |
aosDbgPrintf("ID (%u)\n", (uint32_t)_deserialize(canRxFrame.data8, 4)); |
661 |
// validate received ID
|
662 |
if (lastid < _deserialize(canRxFrame.data8, 4)) { |
663 |
// store received ID
|
664 |
lastid = _deserialize(canRxFrame.data8, 4);
|
665 |
// restart timeout timer
|
666 |
chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout); |
667 |
// proceed
|
668 |
stage = STAGE_3_3_WAITFORIDORSIG; |
669 |
} else {
|
670 |
aosDbgPrintf("ERR: invalid ID\n");
|
671 |
// abort
|
672 |
stage = STAGE_3_4_ABORT_ACTIVE; |
673 |
flags.wfe_next = false;
|
674 |
} |
675 |
// explicitely clear timeout event flag
|
676 |