Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_main.cpp @ 165bcce7

History | View | Annotate | Download (46.453 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
37

    
38
/******************************************************************************/
39
/* LOCAL DEFINITIONS                                                          */
40
/******************************************************************************/
41

    
42
/**
43
 * @brief   Event mask to identify I/O events.
44
 */
45
#define IOEVENT_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) || defined(__DOXYGEN__)
68

    
69
/**
70
 * @brief   CAN message identifier for initialization of the SSSP stack initialization sequence.
71
 */
72
#define SSSP_STACKINIT_CANMSGID_INIT            0x003
73

    
74
/**
75
 * @brief   CAN message identifier for transmitting module IDs during the SSSP stack initialization sequence.
76
 */
77
#define SSSP_STACKINIT_CANMSGID_MODULEID        0x002
78

    
79
/**
80
 * @brief   CAN message identifier for abortion of the SSSP stack initialization sequence.
81
 */
82
#define SSSP_STACKINIT_CANMSGID_ABORT           0x001
83

    
84
#else /* AMIROOS_CFG_SSSP_ENABLE == false */
85

    
86
/**
87
 * @brief   Default shutdown mode if SSSP is unavailable.
88
 */
89
#define AOS_SHUTDOWN_DEFAULT                    AOS_SHUTDOWN_DEEPSLEEP
90

    
91
#endif /* AMIROOS_CFG_SSSP_ENABLE */
92

    
93
/**
94
 * @brief   CAN message identifier for calender synchronization message.
95
 */
96
#define CALENDERSYNC_CANMSGID                   0x004
97

    
98
/******************************************************************************/
99
/* EXPORTED VARIABLES                                                         */
100
/******************************************************************************/
101

    
102
/******************************************************************************/
103
/* LOCAL TYPES                                                                */
104
/******************************************************************************/
105

    
106
/******************************************************************************/
107
/* LOCAL VARIABLES                                                            */
108
/******************************************************************************/
109

    
110
/**
111
 * @brief   Listener object for I/O events.
112
 */
113
static event_listener_t _eventListenerIO;
114

    
115
/**
116
 * @brief   Listener object for OS events.
117
 */
118
static event_listener_t _eventListenerOS;
119

    
120
#if defined(MODULE_HAL_PROGIF) || defined(__DOXYGEN__)
121
/**
122
 * @brief   I/O channel for the programmer interface.
123
 */
124
static AosIOChannel _stdiochannel;
125

    
126
#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
127
/**
128
 * @brief   I/O shell channel for the programmer interface.
129
 */
130
static AosShellChannel _stdshellchannel;
131
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true)*/
132
#endif /* defined(MODULE_HAL_PROGIF) */
133

    
134
/*
135
 * hook to add further static variables
136
 */
137
#if defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES)
138
AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES
139
#endif
140

    
141
/******************************************************************************/
142
/* LOCAL FUNCTIONS                                                            */
143
/******************************************************************************/
144

    
145
/**
146
 * @brief   Prints an error message about an unexpected event.
147
 *
148
 * @param[in] mask    The event mask.
149
 * @param[in] flags   The event flags.
150
 */
151
static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags)
152
{
153
#if (AMIROOS_CFG_DBG == true)
154
  aosprintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
155
#else
156
  (void)(mask);
157
  (void)(flags);
158
#endif
159
  return;
160
}
161

    
162
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
163
/**
164
 * @brief   Callback function to be used during SSSP stack initialization sequence.
165
 *
166
 * @param[in] par   A pointer to an @p event_source_t to be fired.
167
 */
168
static void _ssspTimerCallback(void* par)
169
{
170
  aosDbgCheck(par != NULL);
171

    
172
  chSysLockFromISR();
173
  chEvtBroadcastI((event_source_t*)par);
174
  chSysUnlockFromISR();
175

    
176
  return;
177
}
178
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
179

    
180
/**
181
 * @brief   Helper function to serialize data.
182
 *
183
 * @param[out]  dst   Pointer to the output buffer.
184
 * @param[in]   src   Data to be serialized.
185
 * @param[in]   n     Number of bytes to serialize.
186
 */
187
inline void _serialize(uint8_t* dst, const uint64_t src, const uint8_t n)
188
{
189
  aosDbgCheck(dst != NULL);
190
  aosDbgCheck(n > 0 && n <= 8);
191

    
192
  for (uint8_t byte = 0; byte < n; ++byte) {
193
    dst[byte] = (uint8_t)((src >> (byte * 8)) & 0xFF);
194
  }
195

    
196
  return;
197
}
198

    
199
/**
200
 * @brief   Helper function to deserialize data.
201
 *
202
 * @param[in] src   Pointer to the buffer of data to be deserialzed.
203
 * @param[in] n     Number of bytes to deserialize.
204
 *
205
 * @return    The deserialized 32 bit data.
206
 */
207
inline uint64_t _deserialize(uint8_t* src, const uint8_t n)
208
{
209
  aosDbgCheck(src != NULL);
210
  aosDbgCheck(n > 0 && n <= 8);
211

    
212
  uint64_t result = 0;
213
  for (uint8_t byte = 0; byte < n; ++byte) {
214
    result |= ((uint64_t)src[byte]) << (byte * 8);
215
  }
216

    
217
  return result;
218
}
219

    
220
/**
221
 * @brief   Converter function to encode a TM value to a single unsigned 64 bit integer.
222
 *
223
 * @details Contents of the TM struct are mapped as follows:
224
 *            bits  |63     62|61      53|52    50|49         26|25     22|21     17|16     12|11      6|5       0|
225
 *            #bits |       2 |        9 |      3 |          24 |       4 |       5 |       5 |       6 |       6 |
226
 *            value |   isdst |     yday |   wday |        year |     mon |    mday |    hour |     min |     sec |
227
 *            range | special | [0, 365] | [0, 6] | [1900, ...] | [0, 11] | [1, 31] | [0, 23] | [0, 59] | [0, 61] |
228
 *          The Daylight Saving Time Flag (isdsst) is encoded as follows:
229
 *            DST not in effect         -> 0
230
 *            DST in effect             -> 1
231
 *            no information available  -> 2
232
 *
233
 * @param[in] src   Pointer to the TM struct to encode.
234
 *
235
 * @return  An unsigned 64 bit integer, which holds the encoded time value.
236
 */
237
inline uint64_t _TM2U64(struct tm* src)
238
{
239
  aosDbgCheck(src != NULL);
240

    
241
  return (((uint64_t)(src->tm_sec  & 0x0000003F) << (0))               |
242
          ((uint64_t)(src->tm_min  & 0x0000003F) << (6))               |
243
          ((uint64_t)(src->tm_hour & 0x0000001F) << (12))              |
244
          ((uint64_t)(src->tm_mday & 0x0000001F) << (17))              |
245
          ((uint64_t)(src->tm_mon  & 0x0000000F) << (22))              |
246
          ((uint64_t)(src->tm_year & 0x00FFFFFF) << (26))              |
247
          ((uint64_t)(src->tm_wday & 0x00000007) << (50))              |
248
          ((uint64_t)(src->tm_yday & 0x000001FF) << (53))              |
249
          ((uint64_t)((src->tm_isdst == 0) ? 0 : (src->tm_isdst > 0) ? 1 : 2) << (62)));
250
}
251

    
252
/**
253
 * @brief   Converter functiomn to retrieve the encoded TM value from an unsigned 64 bit integer.
254
 *
255
 * @details For information on the encoding, please refer to @p _TM2U64 function.
256
 *
257
 * @param[out] dst  The TM struct to fill with the decoded values.
258
 * @param[in]  src  Unsigned 64 bit integer holding the encoded TM value.
259
 */
260
inline void _U642TM(struct tm* dst, const uint64_t src)
261
{
262
  aosDbgCheck(dst != NULL);
263

    
264
  dst->tm_sec  = (src >> 0)  & 0x0000003F;
265
  dst->tm_min  = (src >> 6)  & 0x0000003F;
266
  dst->tm_hour = (src >> 12) & 0x0000001F;
267
  dst->tm_mday = (src >> 17) & 0x0000001F;
268
  dst->tm_mon  = (src >> 22) & 0x0000000F;
269
  dst->tm_year = (src >> 26) & 0x00FFFFFF;
270
  dst->tm_wday = (src >> 50) & 0x00000007;
271
  dst->tm_yday = (src >> 53) & 0x000001FF;
272
  dst->tm_isdst = (((src >> 62) & 0x03) == 0) ? 0 : (((src >> 62) & 0x03) > 0) ? 1 : -1;
273

    
274
  return;
275
}
276

    
277
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
278
/**
279
 * @brief   Implementation of the SSSP module stack initialization sequence (startup phase 3).
280
 *
281
 * @return Shutdown value.
282
 * @retval AOS_SHUTDOWN_NONE      No shutdown signal received
283
 * @retval AOS_SHUTDOWN_PASSIVE   Shutdown signal received.
284
 */
285
aos_shutdown_t _ssspModuleStackInitialization(void)
286
{
287
  // local types
288
  /**
289
   * @brief   States for the internal state machine to implement SSSP startup stage 3.
290
   */
291
  typedef enum {
292
    STAGE_3_1,                  /**< Initiation of SSSP startup stage 3. */
293
    STAGE_3_2,                  /**< Starting the sequence and broadcasting the first ID. */
294
    STAGE_3_3_WAITFORFIRSTID,   /**< Waiting for first ID after initiation. */
295
    STAGE_3_3_WAITFORIDORSIG,   /**< Waiting for next ID or activation of neighbor signal. */
296
    STAGE_3_3_WAITFORID,        /**< Waiting for next ID (after the module has set its own ID). */
297
    STAGE_3_4_FINISH,           /**< Successful finish of stage 3. */
298
    STAGE_3_4_ABORT_ACTIVE,     /**< Aborting stage 3 (active). */
299
    STAGE_3_4_ABORT,            /**< Aborting stage 3 (passive). */
300
  } sssp_modulestackinitstage_t;
301

    
302
  typedef struct {
303
    bool loop     : 1;
304
    bool wfe      : 1;
305
    bool wfe_next : 1;
306
  } flags_t;
307

    
308
  // local variables
309
  aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE;
310
  sssp_modulestackinitstage_t stage = STAGE_3_1;
311
  eventmask_t eventmask = 0;
312
  eventflags_t ioflags = 0;
313
  event_source_t eventSourceTimeout;
314
  event_source_t eventSourceDelay;
315
  event_listener_t eventListenerTimeout;
316
  event_listener_t eventListenerDelay;
317
  event_listener_t eventListenerCan;
318
  virtual_timer_t timerTimeout;
319
  virtual_timer_t timerDelay;
320
  CANTxFrame canTxFrame;
321
  CANRxFrame canRxFrame;
322
#if (AMIROOS_CFG_SSSP_STACK_START != true) || (AMIROOS_CFG_DBG == true)
323
  aos_ssspmoduleid_t lastid = 0;
324
#endif
325
  flags_t flags;
326
  aos_timestamp_t uptime;
327

    
328
  // initialize local varibles
329
  chEvtObjectInit(&eventSourceTimeout);
330
  chEvtObjectInit(&eventSourceDelay);
331
  chVTObjectInit(&timerTimeout);
332
  chVTObjectInit(&timerDelay);
333
  canTxFrame.RTR = CAN_RTR_DATA;
334
  canTxFrame.IDE = CAN_IDE_STD;
335
  flags.loop = true;
336
  flags.wfe = false; // do not wait for events in the initial iteration of the FSM loop
337
  flags.wfe_next = true;
338

    
339
  // initialize system variables
340
  aos.sssp.stage = AOS_SSSP_STARTUP_3_1;
341
  aos.sssp.moduleId = 0;
342

    
343
  // listen to events (timout, delay, CAN receive)
344
  chEvtRegisterMask(&eventSourceTimeout, &eventListenerTimeout, TIMEOUTEVENT_MASK);
345
  chEvtRegisterMask(&eventSourceDelay, &eventListenerDelay, DELAYEVENT_MASK);
346
  chEvtRegisterMask(&MODULE_HAL_CAN.rxfull_event, &eventListenerCan, CANEVENT_MASK);
347

    
348
  /*
349
   * FSM in a loop.
350
   *
351
   * This is a fully event-based FSM for the module stack initialization
352
   * sequence, defined by SSSP as startup stage 3. There are five different
353
   * events that can occur at this point:
354
   *  I/O events: The input level of an input pin has changed. Such events must
355
   *              be handled differently depending on the current state. Most
356
   *              of the time, however, such events can be ignored.
357
   *  OS events:  Such events are only available after this stage completed and