commit 1d3e002f9ca63c96d8e15246e3796770f5b69290
Author: Thomas Schöpping <tschoepp@cit-ec.uni-bielefeld.de>
Date:   Fri Dec 21 15:03:38 2018 +0100

    - Several enhancements/fixes regarding the SSSP stack initialization.
    - When SSSP is disables the system:shutdown command is simplified and named "module:shutdown"

diff --git a/core/src/aos_main.cpp b/core/src/aos_main.cpp
index 31f7f85..cf0d117 100644
--- a/core/src/aos_main.cpp
+++ b/core/src/aos_main.cpp
@@ -78,7 +78,14 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 #define SSSP_STACKINIT_CANMSGID_ABORT           0x001
 
-#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
+#else /* AMIROOS_CFG_SSSP_ENABLE == false */
+
+/**
+ * @brief   Default shutdown mode if SSSP is unavailable.
+ */
+#define AOS_SHUTDOWN_DEFAULT                    AOS_SHUTDOWN_DEEPSLEEP
+
+#endif /* AMIROOS_CFG_SSSP_ENABLE */
 
 /**
  * @brief   CAN message identifier for calender synchronization message.
@@ -106,8 +113,8 @@ static AosIOChannel _stdiochannel;
  * @brief   I/O shell channel for the programmer interface.
  */
 static AosShellChannel _stdshellchannel;
-#endif
-#endif
+#endif /* AMIROOS_CFG_SHELL_ENABLE == true */
+#endif /* defined(MODULE_HAL_PROGIF) */
 
 /*
  * hook to add further static variables
@@ -283,7 +290,7 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
   aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE;
   sssp_modulestackinitstage_t stage = STAGE_3_1;
   eventmask_t eventmask = 0;
-  eventflags_t ioflags;
+  eventflags_t ioflags = 0;
   event_source_t eventSourceTimeout;
   event_source_t eventSourceDelay;
   event_listener_t eventListenerTimeout;
@@ -297,6 +304,7 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
   aos_ssspmoduleid_t lastid = 0;
 #endif
   flags_t flags;
+  aos_timestamp_t uptime;
 
   // initialize local varibles
   chEvtObjectInit(&eventSourceTimeout);
@@ -379,7 +387,7 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
     // reset wfe flag for the next iteration
     flags.wfe_next = true;
 
-    // waiting for events may be skipped
+    // waiting for events (may be skipped)
     if (flags.wfe) {
       // wait for any event to occur
       aosDbgPrintf("WFE...");
@@ -387,10 +395,10 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
       aosDbgPrintf("\t0x%08X", eventmask);
     } else {
       aosDbgPrintf("WFE skipped");
+      eventmask = 0;
     }
-    aos_timestamp_t uptime;
     aosSysGetUptime(&uptime);
-    aosDbgPrintf("\t%04ums\n", (uint32_t)(uptime / 1000));
+    aosDbgPrintf("\t%04ums\n", (uint32_t)(uptime / MICROSECONDS_PER_MILLISECOND));
 
     /*
      * execute some general tasks and high priority events
@@ -410,8 +418,10 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
       if (ioflags & MODULE_SSSP_EVENTFLAGS_PD) {
         aosDbgPrintf("PD evt\n");
         // deactivate S and UP
+        aosDbgPrintf("disabling S\n");
         apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
 #if (AMIROOS_CFG_SSSP_STACK_END != true)
+        aosDbgPrintf("disabling UP\n");
         apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_OFF);
 #endif
         // set shutdown flag and exit the loop
@@ -422,8 +432,10 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
       if (ioflags & MODULE_SSSP_EVENTFLAGS_SYNC) {
         apalControlGpioState_t sstate;
         apalControlGpioGet(&moduleSsspGpioSync, &sstate);
-        if (sstate == APAL_GPIO_OFF) {
-          aosDbgPrintf("-S evt\n");
+        if (sstate == APAL_GPIO_ON) {
+          aosDbgPrintf("S evt (enabled)\n");
+        } else {
+          aosDbgPrintf("S evt (disabled)\n");
           // either finish or abort
           if ((stage == STAGE_3_3_WAITFORID) && (aos.sssp.moduleId != 0)) {
             stage = STAGE_3_4_FINISH;
@@ -447,6 +459,7 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
     }
     // if a CAN event occurred
     if ((eventmask & eventListenerCan.events)) {
+      aosDbgPrintf("CAN evt\n");
       // fetch message
       if (flags.wfe) {
         canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canRxFrame, TIME_IMMEDIATE);
@@ -465,10 +478,12 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
     }
     // if a timeout event occurred
     if (eventmask & eventListenerTimeout.events) {
+      aosDbgPrintf("timeout evt\n");
       // is handled at the end of the loop (or must be cleared by FSM)
     }
     // if a delay event occurred
     if (eventmask & eventListenerDelay.events) {
+      aosDbgPrintf("delay evt\n");
       // is handled by FSM
     }
 
@@ -492,7 +507,7 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
             break;
           }
           // activate S
-          aosDbgPrintf("S+\n");
+          aosDbgPrintf("enabling S\n");
           apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_ON);
 #if (AMIROOS_CFG_SSSP_STACK_START == true)
           // proceed immediately
@@ -524,7 +539,7 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
             chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
             eventmask &= ~(eventListenerTimeout.events);
             // activate S
-            aosDbgPrintf("S+\n");
+            aosDbgPrintf("enabling S\n");
             apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_ON);
 #if (AMIROOS_CFG_SSSP_STACK_START == true)
             // proceed
@@ -567,7 +582,7 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
 #if (AMIROOS_CFG_SSSP_STACK_END == true)
           // sequence is already over
           // deactivate S
-          aosDbgPrintf("S-\n");
+          aosDbgPrintf("disabling S\n");
           apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
           // proceed
           stage = STAGE_3_3_WAITFORID;
@@ -580,10 +595,10 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
         // if a delay event occurred
         if (eventmask & eventListenerDelay.events) {
           // activate UP
-          aosDbgPrintf("UP+\n");
+          aosDbgPrintf("enabling UP\n");
           apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_ON);
           // deactivate S
-          aosDbgPrintf("S-\n");
+          aosDbgPrintf("disabling S\n");
           apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
           // explicitely clear timeout event flag
           chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
@@ -665,7 +680,7 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
 
         // if an IO event was received (DN signal)
         if ((eventmask & _eventListenerIO.events) && (ioflags & MODULE_SSSP_EVENTFLAGS_DN)) {
-          aosDbgPrintf("DN <-\n");
+          aosDbgPrintf("DN evt\n");
           // reset timeout timer
           chVTReset(&timerTimeout);
           chEvtWaitAnyTimeout(eventListenerTimeout.events, TIME_IMMEDIATE);
@@ -688,11 +703,11 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
         if (eventmask & eventListenerDelay.events) {
 #if (AMIROOS_CFG_SSSP_STACK_END != true)
           // activate UP
-          aosDbgPrintf("UP+\n");
+          aosDbgPrintf("enabling UP\n");
           apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_ON);
 #endif
           // deactivate S
-          aosDbgPrintf("S-\n");
+          aosDbgPrintf("disabling S\n");
           apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
           // reset the timeout timer
           chVTSet(&timerTimeout, chTimeUS2I(AOS_SYSTEM_SSSP_TIMEOUT), _ssspTimerCallback, &eventSourceTimeout);
@@ -794,7 +809,7 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
         aos.sssp.stage = AOS_SSSP_STARTUP_3_4;
 
         // deactivate S
-        aosDbgPrintf("S-\n");
+        aosDbgPrintf("disabling SYNC\n");
         apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
         // invalidate module ID
         aos.sssp.moduleId = 0;
@@ -808,7 +823,7 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
 
         break;
       } /* end of STAGE_3_4_ABORT */
-    }
+    } /* end of switch(stage) */
 
     // fetch pending CAN message (if any)
     if ((eventmask & eventListenerCan.events) && (canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &canRxFrame, TIME_IMMEDIATE) == MSG_OK)) {
@@ -827,7 +842,6 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
     // apply wfe value for next iteration
     flags.wfe = flags.wfe_next;
   } /* end of FSM loop */
-  aosDbgPrintf("\n");
 
   // unregister all events (timeout, delay, CAN receive)
   chEvtUnregister(&eventSourceTimeout, &eventListenerTimeout);
@@ -838,9 +852,13 @@ aos_shutdown_t _ssspModuleStackInitialization(void)
 
   // reset all control signals
 #if (AMIROOS_CFG_SSSP_STACK_END != true)
+  aosDbgPrintf("disabling UP\n");
   apalControlGpioSet(&moduleSsspGpioUp, APAL_GPIO_OFF);
 #endif
+  aosDbgPrintf("disabling S\n");
   apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
+  aosSysGetUptime(&uptime);
+  aosDbgPrintf("done\t%04ums\n", (uint32_t)(uptime / MICROSECONDS_PER_MILLISECOND));
 
   return shutdown;
 }
@@ -947,9 +965,7 @@ int main(void)
 #else
   chEvtRegisterMaskWithFlags(&aos.events.io, &_eventListenerIO, IOEVENT_MASK, MODULE_SSSP_EVENTFLAGS_PD | MODULE_SSSP_EVENTFLAGS_SYNC | MODULE_SSSP_EVENTFLAGS_DN | MODULE_SSSP_EVENTFLAGS_UP);
 #endif
-#else /* AMIROOS_CFG_SSSP_ENABLE == false */
-  chEvtRegisterMask(&aos.events.io, &_eventListenerIO, IOEVENT_MASK);
-#endif /* AMIROOS_CFG_SSSP_ENABLE */
+#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
   chEvtRegisterMask(&aos.events.os, &_eventListenerOS, OSEVENT_MASK);
 
 #if defined(AMIROOS_CFG_MAIN_INIT_HOOK_5)
@@ -1186,6 +1202,7 @@ int main(void)
         // evaluate flags
         eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
         switch (eventflags) {
+#if (AMIROOS_CFG_SSSP_ENABLE == true)
           case AOS_SYSTEM_EVENTFLAGS_HIBERNATE:
             shutdown = AOS_SHUTDOWN_HIBERNATE;
             break;
@@ -1198,6 +1215,11 @@ int main(void)
           case AOS_SYSTEM_EVENTFLAGS_RESTART:
             shutdown = AOS_SHUTDOWN_RESTART;
             break;
+#else /* AMIROOS_CFG_SSSP_ENABLE == false */
+          case AOS_SYSTEM_EVENTFLAGS_SHUTDOWN:
+            shutdown = AOS_SHUTDOWN_DEFAULT;
+            break;
+#endif /* AMIROOS_CFG_SSSP_ENABLE */
           default:
             _unexpectedEventError(eventmask, eventflags);
             break;
