commit c53ef0b19cb670c47c2e3d9390f62e7661659e32
Author: Thomas Schöpping <tschoepp@cit-ec.uni-bielefeld.de>
Date:   Mon Nov 11 16:33:58 2019 +0100

    MSI implemented

diff --git a/core/src/aos_main.cpp b/core/src/aos_main.cpp
index 9d74144..322c566 100644
--- a/core/src/aos_main.cpp
+++ b/core/src/aos_main.cpp
@@ -49,10 +49,20 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 #if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
 
+
+#if (AMIROOS_CFG_SSSP_MSI == true) || defined(__DOXYGEN__)
+
+/**
+ * @brief   Event mask to identify SSSP timeout events (MSI only).
+ */
+#define EVENTMASK_SSSPTIMEOUT                   EVENT_MASK(2)
+
+#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
+
 /**
- * @brief   Event mask to identify SSSP timer events.
+ * @brief   Event mask to identify SSSP delay events.
  */
-#define EVENTMASK_SSSPTIMER                     EVENT_MASK(2)
+#define EVENTMASK_SSSPDELAY                     EVENT_MASK(3)
 
 #endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
 
@@ -273,13 +283,13 @@ int main(void)
   /* event associations */
 #if (AMIROOS_CFG_SSSP_ENABLE == true)
   {
-    eventflags_t flagsmask = AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK | MODULE_SSSP_EVENTFLAG_PD | MODULE_SSSP_EVENTFLAG_S;
+    eventflags_t flagsmask = AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK | moduleSsspEventflagPD() | moduleSsspEventflagS();
 #if (AMIROOS_CFG_SSSP_MSI == true)
 #if (AMIROOS_CFG_SSSP_STACK_START != true)
-    flagsmask |= MODULE_SSSP_EVENTFLAG_DN;
+    flagsmask |= moduleSsspEventflagDN();
 #endif /* (AMIROOS_CFG_SSSP_STACK_START != true) */
 #if (AMIROOS_CFG_SSSP_STACK_END != true)
-    flagsmask |= MODULE_SSSP_EVENTFLAG_UP;
+    flagsmask |= moduleSsspEventflagUP();
 #endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
 #endif /* (AMIROOS_CFG_SSSP_MSI == true) */
     chEvtRegisterMaskWithFlags(&aos.events.gpio, &_eventListenerGPIO, EVENTMASK_GPIO, flagsmask);
@@ -305,7 +315,7 @@ int main(void)
 
   // proceed to startup stage 1.2
 #if (AMIROOS_CFG_SSSP_MASTER == true)
-  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, 0, EVENTMASK_SSSPTIMER, &eventmask) != AOS_SUCCESS)) {
+  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, EVENTMASK_SSSPDELAY, &eventmask) != AOS_SUCCESS)) {
     /*
      * This code is executed if the received event was not about the delay.
      * The received event could be casued by any listener.
@@ -314,7 +324,7 @@ int main(void)
     if (eventmask & _eventListenerGPIO.events) {
       eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
       // PD event
-      if (eventflags & MODULE_SSSP_EVENTFLAG_PD) {
+      if (eventflags & moduleSsspEventflagPD()) {
         shutdown = AOS_SHUTDOWN_PASSIVE;
       } else {
 #if defined(MODULE_SSSP_STARTUP_1_1_GPIOEVENT_HOOK)
@@ -339,13 +349,13 @@ int main(void)
   }
 #else /* (AMIROOS_CFG_SSSP_MASTER == true) */
   if (shutdown == AOS_SHUTDOWN_NONE) {
-    aosSsspProceed(NULL, 0, 0, NULL);
+    aosSsspProceed(NULL, 0, NULL);
     aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_1_2);
   }
 #endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
 
   // proceed to startup stage 1.3
-  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, MODULE_SSSP_EVENTFLAG_S, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
+  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
     /*
      * This code is executed if the received event was not about a deactivation of the snychronization signal.
      * The received event could be caused by any listener.
@@ -354,7 +364,7 @@ int main(void)
     if (eventmask & _eventListenerGPIO.events) {
       eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
       // PD event
-      if (eventflags & MODULE_SSSP_EVENTFLAG_PD) {
+      if (eventflags & moduleSsspEventflagPD()) {
         shutdown = AOS_SHUTDOWN_PASSIVE;
       } else {
 #if defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK)
@@ -380,7 +390,7 @@ int main(void)
 
   // proceed to startup stage 2.1
 #if (AMIROOS_CFG_SSSP_MASTER == true)
-  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, 0, EVENTMASK_SSSPTIMER, &eventmask) != AOS_SUCCESS)) {
+  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, EVENTMASK_SSSPDELAY, &eventmask) != AOS_SUCCESS)) {
     /*
      * This code is executed if the received event was not about the delay.
      * The received event could be caused by any listener.
@@ -389,7 +399,7 @@ int main(void)
     if (eventmask & _eventListenerGPIO.events) {
       eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
       // PD event
-      if (eventflags & MODULE_SSSP_EVENTFLAG_PD) {
+      if (eventflags & moduleSsspEventflagPD()) {
         shutdown = AOS_SHUTDOWN_PASSIVE;
       } else {
 #if defined(MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK)
@@ -410,7 +420,7 @@ int main(void)
     }
   }
 #else /* (AMIROOS_CFG_SSSP_MASTER == true) */
-  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, MODULE_SSSP_EVENTFLAG_S, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
+  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
     /*
      * This code is executed if the received event was not about a deactivation of the snychronization signal.
      * The received event could be caused by any listener.
@@ -419,7 +429,7 @@ int main(void)
     if (eventmask & _eventListenerGPIO.events) {
       eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
       // PD event
-      if (eventflags & MODULE_SSSP_EVENTFLAG_PD) {
+      if (eventflags & moduleSsspEventflagPD()) {
         shutdown = AOS_SHUTDOWN_PASSIVE;
       } else {
 #if defined(MODULE_SSSP_STARTUP_1_2_GPIOEVENT_HOOK)
@@ -525,7 +535,7 @@ int main(void)
 
   // proceed to startup stage 2.2
 #if (AMIROOS_CFG_SSSP_MASTER == true)
-  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, 0, EVENTMASK_SSSPTIMER, &eventmask) != AOS_SUCCESS)) {
+  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, EVENTMASK_SSSPDELAY, &eventmask) != AOS_SUCCESS)) {
     /*
      * This code is executed if the received event was not about the delay.
      * The received event could be caused by any listener.
@@ -534,7 +544,7 @@ int main(void)
     if (eventmask & _eventListenerGPIO.events) {
       eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
       // PD event
-      if (eventflags & MODULE_SSSP_EVENTFLAG_PD) {
+      if (eventflags & moduleSsspEventflagPD()) {
         shutdown = AOS_SHUTDOWN_PASSIVE;
       } else {
 #if defined(MODULE_SSSP_STARTUP_1_3_GPIOEVENT_HOOK)
@@ -559,13 +569,13 @@ int main(void)
   }
 #else /* (AMIROOS_CFG_SSSP_MASTER == true) */
   if (shutdown == AOS_SHUTDOWN_NONE) {
-    aosSsspProceed(NULL, 0, 0, NULL);
+    aosSsspProceed(NULL, 0, NULL);
     aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_2_2);
   }
 #endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
 
   // proceed to startup stage 3 (MSI is enabled), or to operation phase (no MSI)
-  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, MODULE_SSSP_EVENTFLAG_S, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
+  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(&_eventListenerGPIO, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS)) {
     /*
      * This code is executed if the received event was not about a deactivation of the snychronization signal.
      * The received event could be caused by any listener.
@@ -574,7 +584,7 @@ int main(void)
     if (eventmask & _eventListenerGPIO.events) {
       eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
       // PD event
-      if (eventflags & MODULE_SSSP_EVENTFLAG_PD) {
+      if (eventflags & moduleSsspEventflagPD()) {
         shutdown = AOS_SHUTDOWN_PASSIVE;
       } else {
 #if defined(MODULE_SSSP_STARTUP_2_2_GPIOEVENT_HOOK)
@@ -596,7 +606,7 @@ int main(void)
   }
   if (shutdown == AOS_SHUTDOWN_NONE) {
 #if (AMIROOS_CFG_SSSP_MSI == true)
-    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_3_1);
+    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_3);
 #else /* (AMIROOS_CFG_SSSP_MSI == true) */
     aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_OPERATION);
 #endif /* (AMIROOS_CFG_SSSP_MSI == true) */
@@ -607,8 +617,43 @@ int main(void)
    */
 
 #if (AMIROOS_CFG_SSSP_MSI == true)
-
-  //TODO: MSI
+  {
+    // initialize temporary MSI data
+    aos_ssspmsidata_t msidata;
+    aosSsspMsiInit(&msidata, EVENTMASK_SSSPDELAY, EVENTMASK_SSSPTIMEOUT, &_eventListenerGPIO);
+
+    // execute module stack initialization (MSI) but react to unrelated events as well
+    while ((shutdown == AOS_SHUTDOWN_NONE) && (aos.sssp.stage != AOS_SSSP_STAGE_OPERATION)) {
+      // execute MSI routine
+      aosSsspMsi(&msidata, &eventmask);
+      // handly event (if any)
+      if (eventmask) {
+        // GPIO event
+        if (eventmask & _eventListenerGPIO.events) {
+          eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
+          // PD event
+          if (eventflags & moduleSsspEventflagPD()) {
+            shutdown = AOS_SHUTDOWN_PASSIVE;
+          } else {
+  #if defined(MODULE_SSSP_STARTUP_3_GPIOEVENT_HOOK)
+            MODULE_SSSP_STARTUP_3_GPIOEVENT_HOOK(eventmask, eventflags);
+  #else /* defined(MODULE_SSSP_STARTUP_3_GPIOEVENT_HOOK) */
+            /* silently ignore any other GPIO events */
+  #endif /* defined(MODULE_SSSP_STARTUP_3_GPIOEVENT_HOOK) */
+          }
+        }
+        // OS event
+        else if (eventmask & _eventListenerOS.events) {
+          eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
+          _unexpectedEventError(eventmask, eventflags);
+        }
+        // unknown event
+        else {
+          _unexpectedEventError(eventmask, 0);
+        }
+      }
+    }
+  }
 
 #if (HAL_USE_RTC == TRUE)
 
@@ -640,13 +685,13 @@ int main(void)
 
 #if (AMIROOS_CFG_SSSP_ENABLE == true)
   /* ignore all SSSP signal events, except for PD and configured events */
-  eventflags = MODULE_SSSP_EVENTFLAG_S;
+  eventflags = moduleSsspEventflagS();
 #if (AMIROOS_CFG_SSSP_MSI == true)
 #if (AMIROOS_CFG_SSSP_STACK_START != true)
-  eventflags |= MODULE_SSSP_EVENTFLAG_DN;
+  eventflags |= moduleSsspEventflagDN();
 #endif /* (AMIROOS_CFG_SSSP_STACK_START != true) */
 #if (AMIROOS_CFG_SSSP_STACK_END != true)
-  eventflags |= MODULE_SSSP_EVENTFLAG_UP;
+  eventflags |= moduleSsspEventflagUP();
 #endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
 #endif /* (AMIROOS_CFG_SSSP_MSI == true) */
   eventflags &= ~((eventflags_t)AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK);
@@ -677,7 +722,7 @@ int main(void)
         eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
 #if (AMIROOS_CFG_SSSP_ENABLE == true)
         // PD event
-        if (eventflags & MODULE_SSSP_EVENTFLAG_PD) {
+        if (eventflags & moduleSsspEventflagPD()) {
           aosSsspShutdownInit(false);
           shutdown = AOS_SHUTDOWN_PASSIVE;
         }
@@ -742,7 +787,7 @@ int main(void)
      * Note that events for the optional signals UP and DN are not enabled as they are not utilized during SSSP shutdown phase.
      */
     chSysLock();
-    _eventListenerGPIO.wflags |= MODULE_SSSP_EVENTFLAG_S;
+    _eventListenerGPIO.wflags |= moduleSsspEventflagS();
     chSysUnlock();
 
 #endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
@@ -816,7 +861,7 @@ int main(void)
 #if (AMIROOS_CFG_SSSP_ENABLE == true)
 
   // proceed to SSSP shutdown stage 1.3
-  aosSsspProceed(NULL, 0, 0, NULL);
+  aosSsspProceed(NULL, 0, NULL);
   aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_1_3);
 
 #if (AMIROOS_CFG_SSSP_SHUTDOWN == true)
@@ -828,7 +873,7 @@ int main(void)
    */
 
   // proceed to SSSP shutdown stage 2.1
-  while (aosSsspProceed(&_eventListenerGPIO, MODULE_SSSP_EVENTFLAG_S, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS) {
+  while (aosSsspProceed(&_eventListenerGPIO, EVENTMASK_GPIO, &eventmask) != AOS_SUCCESS) {
     /*
      * This code is executed if the received event was not about a deactivation of the synchronization signal.
      * The received event could be caused by any listener.
@@ -855,7 +900,7 @@ int main(void)
   aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_2_1);
 
   // proceed to SSSP shutdown stage 2.2
-  aosSsspProceed(NULL, 0, 0, &eventmask);
+  aosSsspProceed(NULL, 0, &eventmask);
   aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_2_2);
   if (shutdown == AOS_SHUTDOWN_PASSIVE) {
     aosDbgPrintf("%s request received\n", eventmask ? "shutdown" : "restart");
@@ -875,7 +920,7 @@ int main(void)
     unsigned int identifier = 0;
 
     // receive the identifier, which specifies the type of shutdown/restart
-    while ((eventmask = aosSsspShutdownWaitForIdentifierPulse(&_eventListenerGPIO, MODULE_SSSP_EVENTFLAG_S, EVENTMASK_SSSPTIMER, &identifier)) != EVENTMASK_SSSPTIMER) {
+    while ((eventmask = aosSsspShutdownWaitForIdentifierPulse(&_eventListenerGPIO, moduleSsspEventflagS(), EVENTMASK_SSSPDELAY, &identifier)) != EVENTMASK_SSSPDELAY) {
       // GPIO event
       if (eventmask & _eventListenerGPIO.events) {
         eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
@@ -901,7 +946,7 @@ int main(void)
   }
 
   // proceed to SSSP shutdown stage 2.3
-  aosSsspProceed(NULL, 0, 0, NULL);
+  aosSsspProceed(NULL, 0, NULL);
   aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_SHUTDOWN_2_3);
 
   /*
