Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_main.cpp @ aed3754b

History | View | Annotate | Download (43.012 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
/**
65
 * @brief   CAN message identifier for initialization of the SSSP stack initialization sequence.
66
 */
67
#define SSSP_STACKINIT_CANMSGID_INIT            0x003
68

    
69
/**
70
 * @brief   CAN message identifier for transmitting module IDs during the SSSP stack initialization sequence.
71
 */
72
#define SSSP_STACKINIT_CANMSGID_MODULEID        0x002
73

    
74
/**
75
 * @brief   CAN message identifier for abortion of the SSSP stack initialization sequence.
76
 */
77
#define SSSP_STACKINIT_CANMSGID_ABORT           0x001
78

    
79
/**
80
 * @brief   CAN message identifier for calender synchronization message.
81
 */
82
#define CALENDERSYNC_CANMSGID                   0x004
83

    
84
/**
85
 * @brief   Listener object for I/O events.
86
 */
87
static event_listener_t _eventListenerIO;
88

    
89
/**
90
 * @brief   Listener object for OS events.
91
 */
92
static event_listener_t _eventListenerOS;
93

    
94
#if defined(MODULE_HAL_PROGIF) || defined(__DOXYGEN__)
95
/**
96
 * @brief   I/O channel for the programmer interface.
97
 */
98
static AosIOChannel _stdiochannel;
99

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

    
108
/*
109
 * hook to add further static variables
110
 */
111
#if defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES)
112
AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES
113
#endif
114

    
115
/**
116
 * @brief   Prints an error message about an unexpected event.
117
 *
118
 * @param[in] mask    The event mask.
119
 * @param[in] flags   The event flags.
120
 */
121
static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags)
122
{
123
#if (AMIROOS_CFG_DBG == true)
124
  aosprintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
125
#else
126
  (void)(mask);
127
  (void)(flags);
128
#endif
129
  return;
130
}
131

    
132
/**
133
 * @brief   Callback function to be used during SSSP stack initialization sequence.
134
 *
135
 * @param[in] par   A pointer to an @p event_source_t to be fired.
136
 */
137
static void _ssspTimerCallback(void* par)
138
{
139
  aosDbgCheck(par != NULL);
140

    
141
  chSysLockFromISR();
142
  chEvtBroadcastI((event_source_t*)par);
143
  chSysUnlockFromISR();
144

    
145
  return;
146
}
147

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

    
160
  for (uint8_t byte = 0; byte < n; ++byte) {
161
    dst[byte] = (uint8_t)((src >> (byte * 8)) & 0xFF);
162
  }
163

    
164
  return;
165
}
166

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

    
180
  uint64_t result = 0;
181
  for (uint8_t byte = 0; byte < n; ++byte) {
182
    result |= ((uint64_t)src[byte]) << (byte * 8);
183
  }
184

    
185
  return result;
186
}
187

    
188
/**
189
 * @brief   Converter function to encode a TM value to a single unsigned 64 bit integer.
190
 *
191
 * @details Contents of the TM struct are mapped as follows:
192
 *            bits  |63     62|61      53|52    50|49         26|25     22|21     17|16     12|11      6|5       0|
193
 *            #bits |       2 |        9 |      3 |          24 |       4 |       5 |       5 |       6 |       6 |
194
 *            value |   isdst |     yday |   wday |        year |     mon |    mday |    hour |     min |     sec |
195
 *            range | special | [0, 365] | [0, 6] | [1900, ...] | [0, 11] | [1, 31] | [0, 23] | [0, 59] | [0, 61] |
196
 *          The Daylight Saving Time Flag (isdsst) is encoded as follows:
197
 *            DST not in effect         -> 0
198
 *            DST in effect             -> 1
199
 *            no information available  -> 2
200
 *
201
 * @param[in] src   Pointer to the TM struct to encode.
202
 *
203
 * @return  An unsigned 64 bit integer, which holds the encoded time value.
204
 */
205
inline uint64_t _TM2U64(struct tm* src)
206
{
207
  aosDbgCheck(src != NULL);
208

    
209
  return (((uint64_t)(src->tm_sec  & 0x0000003F) << (0))               |
210
          ((uint64_t)(src->tm_min  & 0x0000003F) << (6))               |
211
          ((uint64_t)(src->tm_hour & 0x0000001F) << (12))              |
212
          ((uint64_t)(src->tm_mday & 0x0000001F) << (17))              |
213
          ((uint64_t)(src->tm_mon  & 0x0000000F) << (22))              |
214
          ((uint64_t)(src->tm_year & 0x00FFFFFF) << (26))              |
215
          ((uint64_t)(src->tm_wday & 0x00000007) << (50))              |
216
          ((uint64_t)(src->tm_yday & 0x000001FF) << (53))              |
217
          ((uint64_t)((src->tm_isdst == 0) ? 0 : (src->tm_isdst > 0) ? 1 : 2) << (62)));
218
}
219

    
220
/**
221
 * @brief   Converter functiomn to retrieve the encoded TM value from an unsigned 64 bit integer.
222
 *
223
 * @details For information on the encoding, please refer to @p _TM2U64 function.
224
 *
225
 * @param[out] dst  The TM struct to fill with the decoded values.
226
 * @param[in]  src  Unsigned 64 bit integer holding the encoded TM value.
227
 */
228
inline void _U642TM(struct tm* dst, const uint64_t src)
229
{
230
  aosDbgCheck(dst != NULL);
231

    
232
  dst->tm_sec  = (src >> 0)  & 0x0000003F;
233
  dst->tm_min  = (src >> 6)  & 0x0000003F;
234
  dst->tm_hour = (src >> 12) & 0x0000001F;
235
  dst->tm_mday = (src >> 17) & 0x0000001F;
236
  dst->tm_mon  = (src >> 22) & 0x0000000F;
237
  dst->tm_year = (src >> 26) & 0x00FFFFFF;
238
  dst->tm_wday = (src >> 50) & 0x00000007;
239
  dst->tm_yday = (src >> 53) & 0x000001FF;
240
  dst->tm_isdst = (((src >> 62) & 0x03) == 0) ? 0 : (((src >> 62) & 0x03) > 0) ? 1 : -1;
241

    
242
  return;
243
}
244

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

    
269
  typedef struct {
270
    bool loop     : 1;
271
    bool wfe      : 1;
272
    bool wfe_next : 1;
273
  } flags_t;
274

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

    
294
  // initialize local varibles
295
  chEvtObjectInit(&eventSourceTimeout);
296
  chEvtObjectInit(&eventSourceDelay);
297
  chVTObjectInit(&timerTimeout);
298
  chVTObjectInit(&timerDelay);
299
  canTxFrame.RTR = CAN_RTR_DATA;
300
  canTxFrame.IDE = CAN_IDE_STD;
301
  flags.loop = true;
302
  flags.wfe = false; // do not wait for events in the initial iteration of the FSM loop
303
  flags.wfe_next = true;
304

    
305
  // initialize system variables
306
  aos.sssp.stage = AOS_SSSP_STARTUP_3_1;
307
  aos.sssp.moduleId = 0;
308

    
309
  // listen to events (timout, delay, CAN receive)
310
  chEvtRegisterMask(&eventSourceTimeout, &eventListenerTimeout, TIMEOUTEVENT_MASK);
311
  chEvtRegisterMask(&eventSourceDelay, &eventListenerDelay, DELAYEVENT_MASK);
312
  chEvtRegisterMask(&MODULE_HAL_CAN.rxfull_event, &eventListenerCan, CANEVENT_MASK);
313

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