Revision 1678f270 core/src/aos_main.cpp

View differences:

core/src/aos_main.cpp
1 1
/*
2 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.
3
Copyright (C) 2016..2019  Thomas Schöpping et al.
4 4

  
5 5
This program is free software: you can redistribute it and/or modify
6 6
it under the terms of the GNU General Public License as published by
......
61 61
 */
62 62
#define DELAYEVENT_MASK                         EVENT_MASK(4)
63 63

  
64
#if (AMIROOS_CFG_SSSP_ENABLE == true)
64
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
65 65

  
66 66
/**
67 67
 * @brief   CAN message identifier for initialization of the SSSP stack initialization sequence.
......
78 78
 */
79 79
#define SSSP_STACKINIT_CANMSGID_ABORT           0x001
80 80

  
81
#endif
81
#else /* AMIROOS_CFG_SSSP_ENABLE == false */
82

  
83
/**
84
 * @brief   Default shutdown mode if SSSP is unavailable.
85
 */
86
#define AOS_SHUTDOWN_DEFAULT                    AOS_SHUTDOWN_DEEPSLEEP
87

  
88
#endif /* AMIROOS_CFG_SSSP_ENABLE */
82 89

  
83 90
/**
84 91
 * @brief   CAN message identifier for calender synchronization message.
......
106 113
 * @brief   I/O shell channel for the programmer interface.
107 114
 */
108 115
static AosShellChannel _stdshellchannel;
109
#endif
110
#endif
116
#endif /* AMIROOS_CFG_SHELL_ENABLE == true */
117
#endif /* defined(MODULE_HAL_PROGIF) */
111 118

  
112 119
/*
113 120
 * hook to add further static variables
......
125 132
static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags)
126 133
{
127 134
#if (AMIROOS_CFG_DBG == true)
128
    aosprintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
135
  aosprintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
129 136
#else
130
    (void)(mask);
131
    (void)(flags);
137
  (void)(mask);
138
  (void)(flags);
132 139
#endif
133
    return;
140
  return;
134 141
}
135 142

  
136

  
137
#if (AMIROOS_CFG_SSSP_ENABLE == true)
143
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
138 144
/**
139 145
 * @brief   Callback function to be used during SSSP stack initialization sequence.
140 146
 *
......
142 148
 */
143 149
static void _ssspTimerCallback(void* par)
144 150
{
145
    aosDbgCheck(par != NULL);
151
  aosDbgCheck(par != NULL);
146 152

  
147
    chSysLockFromISR();
148
    chEvtBroadcastI((event_source_t*)par);
149
    chSysUnlockFromISR();
153
  chSysLockFromISR();
154
  chEvtBroadcastI((event_source_t*)par);
155
  chSysUnlockFromISR();
150 156

  
151
    return;
157
  return;
152 158
}
153
#endif
159
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
160

  
154 161
/**
155 162
 * @brief   Helper function to serialize data.
156 163
 *
......
160 167
 */
161 168
inline void _serialize(uint8_t* dst, const uint64_t src, const uint8_t n)
162 169
{
163
    aosDbgCheck(dst != NULL);
164
    aosDbgCheck(n > 0 && n <= 8);
170
  aosDbgCheck(dst != NULL);
171
  aosDbgCheck(n > 0 && n <= 8);
165 172

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

  
170
    return;
177
  return;
171 178
}
172 179

  
173 180
/**
......
180 187
 */
181 188
inline uint64_t _deserialize(uint8_t* src, const uint8_t n)
182 189
{
183
    aosDbgCheck(src != NULL);
184
    aosDbgCheck(n > 0 && n <= 8);
190
  aosDbgCheck(src != NULL);
191
  aosDbgCheck(n > 0 && n <= 8);
185 192

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

  
191
    return result;
198
  return result;
192 199
}
193 200

  
194 201
/**
......
210 217
 */
211 218
inline uint64_t _TM2U64(struct tm* src)
212 219
{
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)));
220
  aosDbgCheck(src != NULL);
221

  
222
  return (((uint64_t)(src->tm_sec  & 0x0000003F) << (0))               |
223
          ((uint64_t)(src->tm_min  & 0x0000003F) << (6))               |
224
          ((uint64_t)(src->tm_hour & 0x0000001F) << (12))              |
225
          ((uint64_t)(src->tm_mday & 0x0000001F) << (17))              |
226
          ((uint64_t)(src->tm_mon  & 0x0000000F) << (22))              |
227
          ((uint64_t)(src->tm_year & 0x00FFFFFF) << (26))              |
228
          ((uint64_t)(src->tm_wday & 0x00000007) << (50))              |
229
          ((uint64_t)(src->tm_yday & 0x000001FF) << (53))              |
230
          ((uint64_t)((src->tm_isdst == 0) ? 0 : (src->tm_isdst > 0) ? 1 : 2) << (62)));
224 231
}
225 232

  
226 233
/**
......
233 240
 */
234 241
inline void _U642TM(struct tm* dst, const uint64_t src)
235 242
{
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;
243
  aosDbgCheck(dst != NULL);
244

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

  
255
  return;
249 256
}
250
#if (AMIROOS_CFG_SSSP_ENABLE == true)
257

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

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

  
281
    // local variables
282
    aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE;
283
    sssp_modulestackinitstage_t stage = STAGE_3_1;
284
    eventmask_t eventmask = 0;
285
    eventflags_t ioflags;
286
    event_source_t eventSourceTimeout;
287
    event_source_t eventSourceDelay;
288
    event_listener_t eventListenerTimeout;
289
    event_listener_t eventListenerDelay;
290
    event_listener_t eventListenerCan;
291
    virtual_timer_t timerTimeout;
292
    virtual_timer_t timerDelay;
293
    CANTxFrame canTxFrame;
294
    CANRxFrame canRxFrame;
272
  typedef enum {
273
    STAGE_3_1,                  /**< Initiation of SSSP startup stage 3. */
274
    STAGE_3_2,                  /**< Starting the sequence and broadcasting the first ID. */
275
    STAGE_3_3_WAITFORFIRSTID,   /**< Waiting for first ID after initiation. */
276
    STAGE_3_3_WAITFORIDORSIG,   /**< Waiting for next ID or activation of neighbor signal. */
277
    STAGE_3_3_WAITFORID,        /**< Waiting for next ID (after the module has set its own ID). */
278
    STAGE_3_4_FINISH,           /**< Successful finish of stage 3. */
279
    STAGE_3_4_ABORT_ACTIVE,     /**< Aborting stage 3 (active). */
280
    STAGE_3_4_ABORT,            /**< Aborting stage 3 (passive). */
281
  } sssp_modulestackinitstage_t;
282

  
283
  typedef struct {
284
    bool loop     : 1;
285
    bool wfe      : 1;
286
    bool wfe_next : 1;
287
  } flags_t;
288

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

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

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

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

  
320
    /*
304
  aos_ssspmoduleid_t lastid = 0;
305
#endif
306
  flags_t flags;
307
  aos_timestamp_t uptime;
308

  
309
  // initialize local varibles
310
  chEvtObjectInit(&eventSourceTimeout);
311
  chEvtObjectInit(&eventSourceDelay);
312
  chVTObjectInit(&timerTimeout);
313
  chVTObjectInit(&timerDelay);
314
  canTxFrame.RTR = CAN_RTR_DATA;
315
  canTxFrame.IDE = CAN_IDE_STD;
316
  flags.loop = true;
317
  flags.wfe = false; // do not wait for events in the initial iteration of the FSM loop
318
  flags.wfe_next = true;
319

  
320
  // initialize system variables
321
  aos.sssp.stage = AOS_SSSP_STARTUP_3_1;
322
  aos.sssp.moduleId = 0;
323

  
324
  // listen to events (timout, delay, CAN receive)
325
  chEvtRegisterMask(&eventSourceTimeout, &eventListenerTimeout, TIMEOUTEVENT_MASK);
326
  chEvtRegisterMask(&eventSourceDelay, &eventListenerDelay, DELAYEVENT_MASK);
327
  chEvtRegisterMask(&MODULE_HAL_CAN.rxfull_event, &eventListenerCan, CANEVENT_MASK);
328

  
329
  /*
321 330
   * FSM in a loop.
322 331
   *
323 332
   * This is a fully event-based FSM for the module stack initialization
......
344 353
   *  delays:     Depending on the current state, delays are required by SSSP
345 354
   *              for timing of the sequential activation of signals.
346 355
   */
347
    aosDbgPrintf("SSSP stack initialization sequence:\n");
348
    while (flags.loop) {
356
  aosDbgPrintf("SSSP stack initialization sequence:\n");
357
  while (flags.loop) {
349 358
#if (AMIROOS_CFG_DBG == true)
350
        switch (stage) {
351
        case STAGE_3_1:
352
            aosDbgPrintf(">>> 3-1\n");
353
            break;
354
        case STAGE_3_2:
355
            aosDbgPrintf(">>> 3-2\n");
356
            break;
357
        case STAGE_3_3_WAITFORFIRSTID:
358
            aosDbgPrintf(">>> 3-3 (1st ID)\n");
359
            break;
360
        case STAGE_3_3_WAITFORIDORSIG:
361
            aosDbgPrintf(">>> 3-3 (ID/sig)\n");
362
            break;
363
        case STAGE_3_3_WAITFORID:
364
            aosDbgPrintf(">>> 3-3 (ID)\n");
365
            break;
366
        case STAGE_3_4_FINISH:
367
            aosDbgPrintf(">>> 3-4 (finish)\n");
368
            break;
369
        case STAGE_3_4_ABORT_ACTIVE:
370
            aosDbgPrintf(">>> 3-4 (active abort)\n");
371
            break;
372
        case STAGE_3_4_ABORT:
373
            aosDbgPrintf(">>> 3-4 (abort)\n");
374
            break;
375
        }
359
    switch (stage) {
360
      case STAGE_3_1:
361
        aosDbgPrintf(">>> 3-1\n");
362
        break;
363
      case STAGE_3_2:
364
        aosDbgPrintf(">>> 3-2\n");
365
        break;
366
      case STAGE_3_3_WAITFORFIRSTID:
367
        aosDbgPrintf(">>> 3-3 (1st ID)\n");
368
        break;
369
      case STAGE_3_3_WAITFORIDORSIG:
370
        aosDbgPrintf(">>> 3-3 (ID/sig)\n");
371
        break;
372
      case STAGE_3_3_WAITFORID:
373
        aosDbgPrintf(">>> 3-3 (ID)\n");
374
        break;
375
      case STAGE_3_4_FINISH:
376
        aosDbgPrintf(">>> 3-4 (finish)\n");
377
        break;
378
      case STAGE_3_4_ABORT_ACTIVE:
379
        aosDbgPrintf(">>> 3-4 (active abort)\n");
380
        break;
381
      case STAGE_3_4_ABORT:
382
        aosDbgPrintf(">>> 3-4 (abort)\n");
383
        break;
384
    }
376 385
#endif
377 386

  
378
        // reset wfe flag for the next iteration
379
        flags.wfe_next = true;
387
    // reset wfe flag for the next iteration
388
    flags.wfe_next = true;
380 389

  
381
        // waiting for events may be skipped
382
        if (flags.wfe) {
383
            // wait for any event to occur
384
            aosDbgPrintf("WFE...");
385
            eventmask = chEvtWaitAnyTimeout(ALL_EVENTS, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT));
386
            aosDbgPrintf("\t0x%08X", eventmask);
387
        } else {
388
            aosDbgPrintf("WFE skipped");
389
        }
390
        aos_timestamp_t uptime;
391
        aosSysGetUptime(&uptime);
392
        aosDbgPrintf("\t%04ums\n", (uint32_t)(uptime / 1000));
390
    // waiting for events (may be skipped)
391
    if (flags.wfe) {
392
      // wait for any event to occur
393
      aosDbgPrintf("WFE...");
394
      eventmask = chEvtWaitAnyTimeout(ALL_EVENTS, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT));
395
      aosDbgPrintf("\t0x%08X", eventmask);
396
    } else {
397
      aosDbgPrintf("WFE skipped");
398
      eventmask = 0;
399
    }
400
    aosSysGetUptime(&uptime);
401
    aosDbgPrintf("\t%04ums\n", (uint32_t)(uptime / MICROSECONDS_PER_MILLISECOND));
393 402

  
394
        /*
403
    /*
395 404
     * execute some general tasks and high priority events
396 405
     */
397
        // no event occurred at all
398
        if ((flags.wfe) && (eventmask == 0)) {
399
            aosDbgPrintf("ERR: no evt\n");
400
            // enforce timeout event
401
            chEvtBroadcast(&eventSourceTimeout);
402
            continue;
403
        }
404
        // if an IO event occurred
405
        if (eventmask & _eventListenerIO.events) {
406
            ioflags = chEvtGetAndClearFlags(&_eventListenerIO);
407
            aosDbgPrintf("INFO: IO evt (0x%08X)\n", ioflags);
408
            // a power-down event occurred
409
            if (ioflags & MODULE_SSSP_EVENTFLAGS_PD) {
410
                aosDbgPrintf("PD evt\n");
411
                // deactivate S and UP
412
                apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
406
    // no event occurred at all
407
    if ((flags.wfe) && (eventmask == 0)) {
408
      aosDbgPrintf("ERR: no evt\n");
409
      // enforce timeout event
410
      chEvtBroadcast(&eventSourceTimeout);
411
      continue;
412
    }
413
    // if an IO event occurred
414
    if (eventmask & _eventListenerIO.events) {
415
      ioflags = chEvtGetAndClearFlags(&_eventListenerIO);
416
      aosDbgPrintf("INFO: IO evt (0x%08X)\n", ioflags);
417