Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_main.cpp @ 23437e98

History | View | Annotate | Download (46.587 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
#if (HAL_USE_RTC == TRUE) || defined(__DOXYGEN__)
221

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

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

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

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

    
276
  return;
277
}
278

    
279
#endif /* HAL_USE_RTC == TRUE */
280

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

    
306
  typedef struct {
307
    bool loop     : 1;
308
    bool wfe      : 1;
309
    bool wfe_next : 1;
310
  } flags_t;
311

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

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

    
343
  // initialize system variables
344
  aos.sssp.stage = AOS_SSSP_STARTUP_3_1;
345
  aos.sssp.moduleId = 0;
346

    
347
  // listen to events (timout, delay, CAN receive)
348
  chEvtRegisterMask(&eventSourceTimeout, &eventListenerTimeout, TIMEOUTEVENT_MASK);
349
  chEvtRegisterMask(&eventSourceDelay, &eventListenerDelay, DELAYEVENT_MASK);
350
  chEvtRegisterMask(&MODULE_HAL_CAN.rxfull_event, &eventListenerCan, CANEVENT_MASK);
351

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