Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_main.cpp @ 41fc7088

History | View | Annotate | Download (44.007 KB)

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

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

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

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

    
19
/**
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
#include <module.h>
31

    
32
/*
33
 * hook to add further includes
34
 */
35
#if defined(AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER)
36
#include AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER
37
#endif
38

    
39
/**
40
 * @brief   Event mask to identify I/O events.
41
 */
42
#define IOEVENT_MASK                            EVENT_MASK(0)
43

    
44
/**
45
 * @brief   Event mask to identify OS events.
46
 */
47
#define OSEVENT_MASK                            EVENT_MASK(1)
48

    
49
/**
50
 * @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
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
65

    
66
/**
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
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
82

    
83
/**
84
 * @brief   CAN message identifier for calender synchronization message.
85
 */
86
#define CALENDERSYNC_CANMSGID                   0x004
87

    
88
/**
89
 * @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
 * @brief   I/O channel for the programmer interface.
101
 */
102
static AosIOChannel _stdiochannel;
103

    
104
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
105
/**
106
 * @brief   I/O shell channel for the programmer interface.
107
 */
108
static AosShellChannel _stdshellchannel;
109
#endif
110
#endif
111

    
112
/*
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
/**
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
static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags)
126
{
127
#if (AMIROOS_CFG_DBG == true)
128
  aosprintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
129
#else
130
  (void)(mask);
131
  (void)(flags);
132
#endif
133
  return;
134
}
135

    
136
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
137
/**
138
 * @brief   Callback function to be used during SSSP stack initialization sequence.
139
 *
140
 * @param[in] par   A pointer to an @p event_source_t to be fired.
141
 */
142
static void _ssspTimerCallback(void* par)
143
{
144
  aosDbgCheck(par != NULL);
145

    
146
  chSysLockFromISR();
147
  chEvtBroadcastI((event_source_t*)par);
148
  chSysUnlockFromISR();
149

    
150
  return;
151
}
152
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
153

    
154
/**
155
 * @brief   Helper function to serialize data.
156
 *
157
 * @param[out]  dst   Pointer to the output buffer.
158
 * @param[in]   src   Data to be serialized.
159
 * @param[in]   n     Number of bytes to serialize.
160
 */
161
inline void _serialize(uint8_t* dst, const uint64_t src, const uint8_t n)
162
{
163
  aosDbgCheck(dst != NULL);
164
  aosDbgCheck(n > 0 && n <= 8);
165

    
166
  for (uint8_t byte = 0; byte < n; ++byte) {
167
    dst[byte] = (uint8_t)((src >> (byte * 8)) & 0xFF);
168
  }
169

    
170
  return;
171
}
172

    
173
/**
174
 * @brief   Helper function to deserialize data.
175
 *
176
 * @param[in] src   Pointer to the buffer of data to be deserialzed.
177
 * @param[in] n     Number of bytes to deserialize.
178
 *
179
 * @return    The deserialized 32 bit data.
180
 */
181
inline uint64_t _deserialize(uint8_t* src, const uint8_t n)
182
{
183
  aosDbgCheck(src != NULL);
184
  aosDbgCheck(n > 0 && n <= 8);
185

    
186
  uint64_t result = 0;
187
  for (uint8_t byte = 0; byte < n; ++byte) {
188
    result |= ((uint64_t)src[byte]) << (byte * 8);
189
  }
190

    
191
  return result;
192
}
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
  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
}
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
  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
}
250

    
251
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
252
/**
253
 * @brief   Implementation of the SSSP module stack initialization sequence (startup phase 3).
254
 *
255
 * @return Shutdown value.
256
 * @retval AOS_SHUTDOWN_NONE      No shutdown signal received
257
 * @retval AOS_SHUTDOWN_PASSIVE   Shutdown signal received.
258
 */
259
aos_shutdown_t _ssspModuleStackInitialization(void)
260
{
261
  // local types
262
  /**
263
   * @brief   States for the internal state machine to implement SSSP startup stage 3.
264
   */
265
  typedef enum {
266
    STAGE_3_1,                  /**< Initiation of SSSP startup stage 3. */
267
    STAGE_3_2,                  /**< Starting the sequence and broadcasting the first ID. */
268
    STAGE_3_3_WAITFORFIRSTID,   /**< Waiting for first ID after initiation. */
269
    STAGE_3_3_WAITFORIDORSIG,   /**< Waiting for next ID or activation of neighbor signal. */
270
    STAGE_3_3_WAITFORID,        /**< Waiting for next ID (after the module has set its own ID). */
271
    STAGE_3_4_FINISH,           /**< Successful finish of stage 3. */
272
    STAGE_3_4_ABORT_ACTIVE,     /**< Aborting stage 3 (active). */
273
    STAGE_3_4_ABORT,            /**< Aborting stage 3 (passive). */
274
  } sssp_modulestackinitstage_t;
275

    
276
  typedef struct {
277
    bool loop     : 1;
278
    bool wfe      : 1;
279
    bool wfe_next : 1;
280
  } flags_t;
281

    
282
  // local variables
283
  aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE;
284
  sssp_modulestackinitstage_t stage = STAGE_3_1;
285
  eventmask_t eventmask = 0;
286
  eventflags_t ioflags;
287
  event_source_t eventSourceTimeout;
288
  event_source_t eventSourceDelay;
289
  event_listener_t eventListenerTimeout;
290
  event_listener_t eventListenerDelay;
291
  event_listener_t eventListenerCan;
292
  virtual_timer_t timerTimeout;
293
  virtual_timer_t timerDelay;
294
  CANTxFrame canTxFrame;
295
  CANRxFrame canRxFrame;
296
#if (AMIROOS_CFG_SSSP_STACK_START != true) || (AMIROOS_CFG_DBG == true)
297
  aos_ssspmoduleid_t lastid = 0;
298
#endif
299
  flags_t flags;
300

    
301
  // initialize local varibles
302
  chEvtObjectInit(&eventSourceTimeout);
303
  chEvtObjectInit(&eventSourceDelay);
304
  chVTObjectInit(&timerTimeout);
305
  chVTObjectInit(&timerDelay);
306
  canTxFrame.RTR = CAN_RTR_DATA;
307
  canTxFrame.IDE = CAN_IDE_STD;
308
  flags.loop = true;
309
  flags.wfe = false; // do not wait for events in the initial iteration of the FSM loop
310
  flags.wfe_next = true;
311

    
312
  // initialize system variables
313
  aos.sssp.stage = AOS_SSSP_STARTUP_3_1;
314
  aos.sssp.moduleId = 0;
315

    
316
  // listen to events (timout, delay, CAN receive)
317
  chEvtRegisterMask(&eventSourceTimeout, &eventListenerTimeout, TIMEOUTEVENT_MASK);
318
  chEvtRegisterMask(&eventSourceDelay, &eventListenerDelay, DELAYEVENT_MASK);
319
  chEvtRegisterMask(&MODULE_HAL_CAN.rxfull_event, &eventListenerCan, CANEVENT_MASK);
320

    
321
  /*
322
   * FSM in a loop.
323
   *
324
   * This is a fully event-based FSM for the module stack initialization
325
   * sequence, defined by SSSP as startup stage 3. There are five different
326
   * events that can occur at this point:
327
   *  I/O events: The input level of an input pin has changed. Such events must
328
   *              be handled differently depending on the current state. Most
329
   *              of the time, however, such events can be ignored.
330
   *  OS events:  Such events are only available after this stage completed and
331
   *              thus should never occur. However, there is an optional hook
332
   *              to handle such events, nevertheless.
333
   *  CAN events: At least one CAN message was received. Note that this event
334
   *              will only fire again if all input buffers have been cleared.
335
   *  timeouts:   If some module does not support the sequence of there is any
336
   *              issue, such a case is detected via timeouts and must be
337
   *              handled accordingly (see abort state). In some cases, it is
338
   *              possible that a timeout event occurres 'simultaneously' with
339
   *              some other event. This can be caused by several timing issues
340
   *              and is a valid situation. As a result, any other events
341
   *              should be handled before the timeout event. If the other
342
   *              events are expected and valid, this implementation requires
343
   *              the timeout event flag to be cleared explicitely. Otherwise
344
   *              it is evaluated at the end of each iteration of the loop.
345
   *  delays:     Depending on the current state, delays are required by SSSP
346
   *              for timing of the sequential activation of signals.
347
   */
348
  aosDbgPrintf("SSSP stack initialization sequence:\n");
349
  while (flags.loop) {
350
#if (AMIROOS_CFG_DBG == true)
351
    switch (stage) {
352
      case STAGE_3_1:
353
        aosDbgPrintf(">>> 3-1\n");
354
        break;
355
      case STAGE_3_2: