commit cda1472944648652764c6980f5ee3f3e91459ef3
Author: Thomas Schöpping <tschoepp@cit-ec.uni-bielefeld.de>
Date:   Mon Oct 28 11:15:03 2019 +0100

    Major SSSP overhaul:
    - SSSP no works even without Bootloader (AMiRo-OS implements startup and shutdown phases)
    - MSI and calendar synchronization are currently not available (WIP)
    - lots of minor changes

diff --git a/core/src/aos_system.c b/core/src/aos_system.c
index c9b3072..97270e6 100644
--- a/core/src/aos_system.c
+++ b/core/src/aos_system.c
@@ -84,14 +84,14 @@ aos_system_t aos;
 /*
  * forward declarations
  */
-#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
+#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
 static int _shellcmd_configcb(BaseSequentialStream* stream, int argc, char* argv[]);
 static int _shellcmd_infocb(BaseSequentialStream* stream, int argc, char* argv[]);
 static int _shellcmd_shutdowncb(BaseSequentialStream* stream, int argc, char* argv[]);
 #if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
 static int _shellcmd_kerneltestcb(BaseSequentialStream* stream, int argc, char* argv[]);
 #endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
-#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) */
+#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
 
 /**
  * @brief   Timer to accumulate system uptime.
@@ -108,7 +108,9 @@ static aos_timestamp_t _uptime;
  */
 static systime_t _synctime;
 
-#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER == true)) || defined(__DOXYGEN__)
+#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
+#if (AMIROOS_CFG_SSSP_MASTER == true) || defined(__DOXYGEN__)
+
 /**
  * @brief   Timer to drive the SYS_SYNC signal for system wide time synchronization according to SSSP.
  */
@@ -118,17 +120,21 @@ static virtual_timer_t _syssynctimer;
  * @brief   Last uptime of system wide time synchronization.
  */
 static aos_timestamp_t _syssynctime;
-#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER == true) */
 
-#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER != true) && (AMIROOS_CFG_PROFILE == true)) || defined(__DOXYGEN__)
+#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
+
+#if ((AMIROOS_CFG_SSSP_MASTER != true) && (AMIROOS_CFG_PROFILE == true)) || defined(__DOXYGEN__)
+
 /**
  * @brief   Offset between local clock and system wide synchronization signal.
  */
 static float _syssyncskew;
 
-#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER != true) && (AMIROOS_CFG_PROFILE == true) */
+#endif /* (AMIROOS_CFG_SSSP_MASTER != true) && (AMIROOS_CFG_PROFILE == true) */
+#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
+
+#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
 
-#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
 /**
  * @brief   Shell thread working area.
  */
@@ -157,19 +163,21 @@ static AOS_SHELL_COMMAND(_shellcmd_config, "module:config", _shellcmd_configcb);
 /**
  * @brief   Shell command to shutdown the system.
  */
-#if (AMIROOS_CFG_SSSP_ENABLE == true)
+#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
 static AOS_SHELL_COMMAND(_shellcmd_shutdown, "system:shutdown", _shellcmd_shutdowncb);
 #else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
 static AOS_SHELL_COMMAND(_shellcmd_shutdown, "module:shutdown", _shellcmd_shutdowncb);
 #endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
 
 #if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
+
 /**
  * @brief   Shell kommand to run a test of the ChibiOS/RT kernel.
  */
 static AOS_SHELL_COMMAND(_shellcmd_kerneltest, "kernel:test", _shellcmd_kerneltestcb);
+
 #endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
-#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) */
+#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
 
 /******************************************************************************/
 /* LOCAL FUNCTIONS                                                            */
@@ -261,7 +269,7 @@ static void _printSystemInfo(BaseSequentialStream* stream)
   _printSystemInfoLine(stream, "Architecture", SYSTEM_INFO_NAMEWIDTH, "%s", PORT_ARCHITECTURE_NAME);
   _printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
 #if (AMIROOS_CFG_SSSP_ENABLE == true)
-  _printSystemInfoLine(stream, "AMiRo-OS" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (SSSP %u.%u)", AMIROOS_VERSION_MAJOR, AMIROOS_VERSION_MINOR, AMIROOS_VERSION_PATCH, AMIROOS_RELEASE_TYPE, AOS_SYSTEM_SSSP_VERSION_MAJOR, AOS_SYSTEM_SSSP_VERSION_MINOR);
+  _printSystemInfoLine(stream, "AMiRo-OS" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (SSSP %u.%u)", AMIROOS_VERSION_MAJOR, AMIROOS_VERSION_MINOR, AMIROOS_VERSION_PATCH, AMIROOS_RELEASE_TYPE, AOS_SSSP_VERSION_MAJOR, AOS_SSSP_VERSION_MINOR);
 #else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
   _printSystemInfoLine(stream, "AMiRo-OS" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s", AMIROOS_VERSION_MAJOR, AMIROOS_VERSION_MINOR, AMIROOS_VERSION_PATCH, AMIROOS_RELEASE_TYPE);
 #endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
@@ -273,7 +281,10 @@ static void _printSystemInfo(BaseSequentialStream* stream)
   _printSystemInfoLine(stream, "Compiled" , SYSTEM_INFO_NAMEWIDTH, "%s - %s", __DATE__, __TIME__);
 
   // print static information about the bootloader
+#if (AMIROOS_CFG_BOOTLOADER != AOS_BOOTLOADER_NONE)
   _printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
+#endif /* (AMIROOS_CFG_BOOTLOADER != AOS_BOOTLOADER_NONE) */
+#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
   if (BL_CALLBACK_TABLE_ADDRESS->magicNumber == BL_MAGIC_NUMBER) {
     _printSystemInfoLine(stream, "AMiRo-BLT", SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (SSSP %u.%u)", BL_CALLBACK_TABLE_ADDRESS->vBootloader.major, BL_CALLBACK_TABLE_ADDRESS->vBootloader.minor, BL_CALLBACK_TABLE_ADDRESS->vBootloader.patch,
                          (BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_Release) ? "stable" :
@@ -284,31 +295,30 @@ static void _printSystemInfo(BaseSequentialStream* stream)
                          "<release type unknown>",
                          BL_CALLBACK_TABLE_ADDRESS->vSSSP.major, BL_CALLBACK_TABLE_ADDRESS->vSSSP.minor);
 #if (AMIROOS_CFG_SSSP_ENABLE == true)
-    if (BL_CALLBACK_TABLE_ADDRESS->vSSSP.major != AOS_SYSTEM_SSSP_VERSION_MAJOR) {
-      if (stream) {
-        chprintf(stream, "WARNING: Bootloader and AMiRo-OS implement incompatible SSSP versions!\n");
-      } else {
-        aosprintf("WARNING: Bootloader and AMiRo-OS implement incompatible SSSP versions!\n");
-      }
+    if (BL_CALLBACK_TABLE_ADDRESS->vSSSP.major != AOS_SSSP_VERSION_MAJOR) {
+      const char* msg = "WARNING: AMiRo-BLT and AMiRo-OS implement incompatible SSSP versions!\n";
+      stream ? chprintf(stream, msg) : aosprintf(msg);
     }
 #endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
     _printSystemInfoLine(stream, "Compiler", SYSTEM_INFO_NAMEWIDTH, "%s %u.%u.%u", (BL_CALLBACK_TABLE_ADDRESS->vCompiler.identifier == BL_VERSION_ID_GCC) ? "GCC" : "<compiler unknown>", BL_CALLBACK_TABLE_ADDRESS->vCompiler.major, BL_CALLBACK_TABLE_ADDRESS->vCompiler.minor, BL_CALLBACK_TABLE_ADDRESS->vCompiler.patch); // TODO: support other compilers than GCC
   } else {
-    if (stream) {
-      chprintf(stream, "Bootloader incompatible or not available.\n");
-    } else {
-      aosprintf("Bootloader incompatible or not available.\n");
-    }
+    const char* msg = "WARNING: AMiRo-BLT incompatible or not available.\n";
+    stream ? chprintf(stream, "%s", msg) : aosprintf("%s", msg);
   }
+#endif /* (AMIROOS_CFG_BOOTLOADER == X) */
 
   // print dynamic information about the module
   _printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
 #if (AMIROOS_CFG_SSSP_ENABLE == true)
+#if (AMIROOS_CFG_SSSP_MSI == true)
   if (aos.sssp.moduleId != 0) {
     _printSystemInfoLine(stream, "Module ID", SYSTEM_INFO_NAMEWIDTH, "%u", aos.sssp.moduleId);
   } else {
     _printSystemInfoLine(stream, "Module ID", SYSTEM_INFO_NAMEWIDTH, "not available");
   }
+#else /* (AMIROOS_CFG_SSSP_MSI == true) */
+  _printSystemInfoLine(stream, "Module ID", SYSTEM_INFO_NAMEWIDTH, "not available");
+#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
 #endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
 #if (HAL_USE_RTC == TRUE)
   _printSystemInfoLine(stream, "Date", SYSTEM_INFO_NAMEWIDTH, "%04u-%02u-%02u (%s)",
@@ -317,13 +327,6 @@ static void _printSystemInfo(BaseSequentialStream* stream)
                        dt.tm_mday,
                        (dt.tm_wday == 0) ? "Sunday" : (dt.tm_wday == 1) ? "Monday" : (dt.tm_wday == 2) ? "Tuesday" : (dt.tm_wday == 3) ? "Wednesday" : (dt.tm_wday == 4) ? "Thursday" : (dt.tm_wday == 5) ? "Friday" : "Saturday");
   _printSystemInfoLine(stream, "Time", SYSTEM_INFO_NAMEWIDTH, "%02u:%02u:%02u", dt.tm_hour, dt.tm_min, dt.tm_sec);
-#if (AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_CAN != TRUE)
-#if (AMIROOS_CFG_SSSP_MASTER == true)
-  _printSystemInfoLine(stream, "", SYSTEM_INFO_NAMEWIDTH, "Date & Time were not synchronized to slave modules.");
-#else /* (AMIROOS_CFG_SSSP_MASTER == true) */
-  _printSystemInfoLine(stream, "", SYSTEM_INFO_NAMEWIDTH, "Date & Time were not synchronized from master module.");
-#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
-#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_CAN != TRUE) */
 #endif /* (HAL_USE_RTC == TRUE) */
 
   _printSystemInfoSeparator(stream, '=', SYSTEM_INFO_WIDTH);
@@ -331,7 +334,7 @@ static void _printSystemInfo(BaseSequentialStream* stream)
   return;
 }
 
-#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
+#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
 /**
  * @brief   Callback function for the system:config shell command.
  *
@@ -456,7 +459,7 @@ static int _shellcmd_configcb(BaseSequentialStream* stream, int argc, char* argv
         chprintf(stream, "unknown option '%s' or invalid value '%s'\n", argv[2], argv[3]);
         return AOS_INVALIDARGUMENTS;
       }
-      dt.tm_wday = aosTimeDayOfWeekFromDate(dt.tm_mday, dt.tm_mon+1, dt.tm_year+1900) % 7;
+      dt.tm_wday = aosTimeDayOfWeekFromDate((uint16_t)dt.tm_mday, (uint8_t)dt.tm_mon+1, (uint16_t)dt.tm_year+1900) % DAYS_PER_WEEK;
       aosSysSetDateTime(&dt);
 
       // read and print new date and time
@@ -554,7 +557,7 @@ static int _shellcmd_infocb(BaseSequentialStream* stream, int argc, char* argv[]
 }
 
 /**
- * @brief   Callback function for the sytem:shutdown shell command.
+ * @brief   Callback function for the sytem:shutdown or module:shutdown shell command.
  *
  * @param[in] stream    The I/O stream to use.
  * @param[in] argc      Number of arguments.
@@ -568,7 +571,20 @@ static int _shellcmd_shutdowncb(BaseSequentialStream* stream, int argc, char* ar
 {
   aosDbgCheck(stream != NULL);
 
-#if (AMIROOS_CFG_SSSP_ENABLE == true)
+#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_NONE)
+
+  (void)argv;
+  (void)argc;
+
+  // broadcast shutdown event
+  chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_MASK);
+  // set terminate flag so no further prompt will be printed
+  chThdTerminate(chThdGetSelfX());
+
+  return AOS_OK;
+
+#elif (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
+
   // print help text
   if (argc != 2 || strcmp(argv[1], "--help") == 0) {
     chprintf(stream, "Usage: %s OPTION\n", argv[0]);
@@ -592,22 +608,30 @@ static int _shellcmd_shutdowncb(BaseSequentialStream* stream, int argc, char* ar
   // handle argument
   else {
     if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--hibernate") == 0) {
-      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_HIBERNATE);
+      // broadcast shutdown event
+      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_HIBERNATE);
+      // set terminate flag so no further prompt will be printed
       chThdTerminate(chThdGetSelfX());
       return AOS_OK;
     }
     else if (strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "--deepsleep") == 0) {
-      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_DEEPSLEEP);
+      // broadcast shutdown event
+      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_DEEPSLEEP);
+      // set terminate flag so no further prompt will be printed
       chThdTerminate(chThdGetSelfX());
       return AOS_OK;
     }
     else if (strcmp(argv[1], "-t") == 0 || strcmp(argv[1], "--transportation") == 0) {
-      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_TRANSPORTATION);
+      // broadcast shutdown event
+      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_TRANSPORTATION);
+      // set terminate flag so no further prompt will be printed
       chThdTerminate(chThdGetSelfX());
       return AOS_OK;
     }
     else if (strcmp(argv[1], "-r") == 0 || strcmp(argv[1], "--restart") == 0) {
-      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_RESTART);
+      // broadcast shutdown event
+      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_RESTART);
+      // set terminate flag so no further prompt will be printed
       chThdTerminate(chThdGetSelfX());
       return AOS_OK;
     }
@@ -616,17 +640,12 @@ static int _shellcmd_shutdowncb(BaseSequentialStream* stream, int argc, char* ar
       return AOS_INVALIDARGUMENTS;
     }
   }
-#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
-  (void)argv;
-  (void)argc;
 
-  chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN);
-  chThdTerminate(chThdGetSelfX());
-  return AOS_OK;
-#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
+#endif /* (AMIROOS_CFG_BOOTLOADER == X) */
 }
 
 #if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
+
 /**
  * @brief   Callback function for the kernel:test shell command.
  *
@@ -647,8 +666,9 @@ static int _shellcmd_kerneltestcb(BaseSequentialStream* stream, int argc, char*
 
   return retval;
 }
+
 #endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
-#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) */
+#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
 
 // suppress warning in case no interrupt GPIOs are defined
 #pragma GCC diagnostic push
@@ -658,12 +678,12 @@ static int _shellcmd_kerneltestcb(BaseSequentialStream* stream, int argc, char*
  *
  * @param[in] args   Pointer to the GPIO line identifier.
  */
-static void _extiCallback(void* args)
+static void _gpioCallback(void* args)
 {
   aosDbgCheck((args != NULL) && (*((ioline_t*)args) != PAL_NOLINE) && (PAL_PAD(*((ioline_t*)args)) < sizeof(eventflags_t) * 8));
 
   chSysLockFromISR();
-  chEvtBroadcastFlagsI(&aos.events.io, AOS_IOEVENT_FLAG(PAL_PAD(*((ioline_t*)args))));
+  chEvtBroadcastFlagsI(&aos.events.gpio, AOS_GPIOEVENT_FLAG(PAL_PAD(*((ioline_t*)args))));
   chSysUnlockFromISR();
 
   return;
@@ -671,6 +691,7 @@ static void _extiCallback(void* args)
 #pragma GCC diagnostic pop
 
 #if ((AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER != true)) || defined(__DOXYGEN__)
+
 /**
  * @brief   Callback function for the Sync signal interrupt.
  *
@@ -680,16 +701,17 @@ static void _signalSyncCallback(void *args)
 {
   aosDbgCheck((args != NULL) && (*((ioline_t*)args) != PAL_NOLINE) && (PAL_PAD(*((ioline_t*)args)) < sizeof(eventflags_t) * 8));
 
-  apalControlGpioState_t s_state;
+  apalControlGpioState_t state;
   aos_timestamp_t uptime;
 
   chSysLockFromISR();
+
   // if the system is in operation phase
-  if (aos.sssp.stage == AOS_SSSP_OPERATION) {
+  if (aos.sssp.stage == AOS_SSSP_STAGE_OPERATION) {
     // read signal S
-    apalControlGpioGet(&moduleSsspGpioSync, &s_state);
+    apalControlGpioGet(&moduleSsspGpioS, &state);
     // if S was toggled from on to off
-    if (s_state == APAL_GPIO_OFF) {
+    if (state == APAL_GPIO_OFF) {
       // get current uptime
       aosSysGetUptimeX(&uptime);
       // align the uptime with the synchronization period
@@ -706,12 +728,14 @@ static void _signalSyncCallback(void *args)
       }
     }
   }
+
   // broadcast event
-  chEvtBroadcastFlagsI(&aos.events.io, AOS_IOEVENT_FLAG(PAL_PAD(*((ioline_t*)args))));
+  chEvtBroadcastFlagsI(&aos.events.gpio, AOS_GPIOEVENT_FLAG(PAL_PAD(*((ioline_t*)args))));
   chSysUnlockFromISR();
 
   return;
 }
+
 #endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER != true) */
 
 /**
@@ -737,32 +761,33 @@ static void _uptimeCallback(void* par)
 }
 
 #if ((AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER == true)) || defined (__DOXYGEN__)
+
 /**
  * @brief   Periodic system synchronization callback function.
  * @details Toggles the SYS_SYNC signal and reconfigures the system synchronization timer.
  *
- * @param[in] par   Unuesed parameters.
+ * @param[in] par   Unused parameters.
  */
 static void _sysSyncTimerCallback(void* par)
 {
   (void)par;
 
-  apalControlGpioState_t s_state;
+  apalControlGpioState_t state;
   aos_timestamp_t uptime;
 
   chSysLockFromISR();
   // toggle and read signal S
-  apalGpioToggle(moduleSsspGpioSync.gpio);
-  apalControlGpioGet(&moduleSsspGpioSync, &s_state);
+  apalGpioToggle(moduleSsspGpioS.gpio);
+  apalControlGpioGet(&moduleSsspGpioS, &state);
   // if S was toggled from off to on
-  if (s_state == APAL_GPIO_ON) {
+  if (state == APAL_GPIO_ON) {
     // reconfigure the timer precisely, because the logically falling edge (next interrupt) snychronizes the system time
     _syssynctime += AMIROOS_CFG_SSSP_SYSSYNCPERIOD;
     aosSysGetUptimeX(&uptime);
     chVTSetI(&_syssynctimer, chTimeUS2I(_syssynctime - uptime), _sysSyncTimerCallback, NULL);
   }
   // if S was toggled from on to off
-  else /* if (s_state == APAL_GPIO_OFF) */ {
+  else /* if (state == APAL_GPIO_OFF) */ {
     // reconfigure the timer (lazy)
     chVTSetI(&_syssynctimer, chTimeUS2I(AMIROOS_CFG_SSSP_SYSSYNCPERIOD / 2), _sysSyncTimerCallback, NULL);
   }
@@ -770,8 +795,26 @@ static void _sysSyncTimerCallback(void* par)
 
   return;
 }
+
 #endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER == true) */
 
+/**
+ * @brief   Retreive the number of active threads.
+ *
+ * @return  Number of active threads.
+ */
+size_t _numActiveThreads(void)
+{
+  size_t threads = 0;
+  thread_t* tp = chRegFirstThread();
+  while (tp) {
+    threads += (tp->state == CH_STATE_FINAL) ? 1 : 0;
+    tp = chRegNextThread(tp);
+  }
+
+  return threads;
+}
+
 /******************************************************************************/
 /* EXPORTED FUNCTIONS                                                         */
 /******************************************************************************/
@@ -782,15 +825,19 @@ static void _sysSyncTimerCallback(void* par)
  *
  * @param[in] shellPrompt   String to be printed as prompt of the system shell.
  */
-#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
+#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
 void aosSysInit(const char* shellPrompt)
-#else /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) */
+#else /* (AMIROOS_CFG_SHELL_ENABLE == true) */
 void aosSysInit(void)
-#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) */
+#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
 {
   /* set control thread to maximum priority */
   chThdSetPriority(AOS_THD_CTRLPRIO);
 
+#if (AMIROOS_CFG_SSSP_ENABLE == true)
+  aosSsspInit();
+#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
+
   /* set local variables */
   chVTObjectInit(&_systimer);
 #if (AMIROOS_CFG_SSSP_ENABLE == true)
@@ -813,39 +860,38 @@ void aosSysInit(void)
 #endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
 
   /* initialize aos configuration */
-#if (AMIROOS_CFG_SSSP_ENABLE == true)
-  aos.sssp.stage = AOS_SSSP_STARTUP_2_1;
-  aos.sssp.moduleId = 0;
-#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
   aosIOStreamInit(&aos.iostream);
-  chEvtObjectInit(&aos.events.io);
+  chEvtObjectInit(&aos.events.gpio);
   chEvtObjectInit(&aos.events.os);
 
-  /* interrupt setup */
 #if (AMIROOS_CFG_SSSP_ENABLE == true)
+
+  /* SSSP signal interrupt setup */
   // PD signal
-  palSetLineCallback(moduleSsspGpioPd.gpio->line, _extiCallback, &moduleSsspGpioPd.gpio->line);
-  palEnableLineEvent(moduleSsspGpioPd.gpio->line, APAL2CH_EDGE(moduleSsspGpioPd.meta.edge));
-  // SYNC signal
+  palSetLineCallback(moduleSsspGpioPD.gpio->line, _gpioCallback, &moduleSsspGpioPD.gpio->line);
+  palEnableLineEvent(moduleSsspGpioPD.gpio->line, APAL2CH_EDGE(moduleSsspGpioPD.meta.edge));
+  // S signal
 #if (AMIROOS_CFG_SSSP_MASTER == true)
-  palSetLineCallback(moduleSsspGpioSync.gpio->line, _extiCallback, &moduleSsspGpioSync.gpio->line);
+  palSetLineCallback(moduleSsspGpioS.gpio->line, _gpioCallback, &moduleSsspGpioS.gpio->line);
 #else /* (AMIROOS_CFG_SSSP_MASTER == true) */
-  palSetLineCallback(moduleSsspGpioSync.gpio->line, _signalSyncCallback, &moduleSsspGpioSync.gpio->line);
+  palSetLineCallback(moduleSsspGpioS.gpio->line, _signalSyncCallback, &moduleSsspGpioS.gpio->line);
 #endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
-  palEnableLineEvent(moduleSsspGpioSync.gpio->line, APAL2CH_EDGE(moduleSsspGpioSync.meta.edge));
+  palEnableLineEvent(moduleSsspGpioS.gpio->line, APAL2CH_EDGE(moduleSsspGpioS.meta.edge));
 #if (AMIROOS_CFG_SSSP_STACK_START != true)
   // DN signal
-  palSetLineCallback(moduleSsspGpioDn.gpio->line, _extiCallback, &moduleSsspGpioDn.gpio->line);
-  palEnableLineEvent(moduleSsspGpioDn.gpio->line, APAL2CH_EDGE(moduleSsspGpioDn.meta.edge));
+  palSetLineCallback(moduleSsspGpioDN.gpio->line, _gpioCallback, &moduleSsspGpioDN.gpio->line);
+  palEnableLineEvent(moduleSsspGpioDN.gpio->line, APAL2CH_EDGE(moduleSsspGpioDN.meta.edge));
 #endif /* (AMIROOS_CFG_SSSP_STACK_START != true) */
 #if (AMIROOS_CFG_SSSP_STACK_END != true)
   // UP signal
-  palSetLineCallback(moduleSsspGpioUp.gpio->line, _extiCallback, &moduleSsspGpioUp.gpio->line);
-  palEnableLineEvent(moduleSsspGpioUp.gpio->line, APAL2CH_EDGE(moduleSsspGpioUp.meta.edge));
+  palSetLineCallback(moduleSsspGpioUP.gpio->line, _gpioCallback, &moduleSsspGpioUP.gpio->line);
+  palEnableLineEvent(moduleSsspGpioUP.gpio->line, APAL2CH_EDGE(moduleSsspGpioUP.meta.edge));
 #endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
+
 #endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
 
-#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true)
+#if (AMIROOS_CFG_SHELL_ENABLE == true)
+
   /* init shell */
   aosShellInit(&aos.shell,
                &aos.events.os,
@@ -861,7 +907,8 @@ void aosSysInit(void)
 #if (AMIROOS_CFG_TESTS_ENABLE == true)
   aosShellAddCommand(&aos.shell, &_shellcmd_kerneltest);
 #endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
-#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) */
+
+#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
 
   return;
 }
@@ -869,13 +916,9 @@ void aosSysInit(void)
 /**
  * @brief   Starts the system and all system threads.
  */
-inline void aosSysStart(void)
+void aosSysStart(void)
 {
-#if (AMIROOS_CFG_SSSP_ENABLE == true)
-  // update the system SSSP stage
-  aos.sssp.stage = AOS_SSSP_OPERATION;
-
-#if (AMIROOS_CFG_SSSP_MASTER == true)
+#if (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER == true)
   {
     chSysLock();
     // start the system synchronization counter
@@ -886,76 +929,43 @@ inline void aosSysStart(void)
     chVTSetI(&_syssynctimer, chTimeUS2I((t > (AMIROOS_CFG_SSSP_SYSSYNCPERIOD / 2)) ? (t - (AMIROOS_CFG_SSSP_SYSSYNCPERIOD / 2)) : (t + (AMIROOS_CFG_SSSP_SYSSYNCPERIOD / 2))), _sysSyncTimerCallback, NULL);
     chSysUnlock();
   }
-#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
-#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
+#endif /* (AMIROOS_CFG_SSSP_MASTER == true) && (AMIROOS_CFG_SSSP_ENABLE == true) */
 
   // print system information;
   _printSystemInfo((BaseSequentialStream*)&aos.iostream);
   aosprintf("\n");
 
-#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true)
+#if (AMIROOS_CFG_SHELL_ENABLE == true)
   // start system shell thread
 #if (CH_CFG_USE_THREADHIERARCHY == TRUE)
   aos.shell.thread = chThdCreateStatic(_shell_wa, sizeof(_shell_wa), AMIROOS_CFG_SHELL_THREADPRIO, aosShellThread, &aos.shell, &ch.mainthread);
 #else /* (CH_CFG_USE_THREADHIERARCHY == TRUE) */
   aos.shell.thread = chThdCreateStatic(_shell_wa, sizeof(_shell_wa), AMIROOS_CFG_SHELL_THREADPRIO, aosShellThread, &aos.shell);
 #endif /* (CH_CFG_USE_THREADHIERARCHY == TRUE) */
-#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) */
+#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
 
   return;
 }
 
 #if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
+
 /**
- * @brief   Implements the SSSP startup synchronization step.
- *
- * @param[in] syncEvtListener   Event listener that receives the Sync event.
- *
- * @return    If another event that the listener is interested in was received, its mask is returned.
- *            Otherwise an empty mask (0) is returned.
+ * @brief   Start the system uptime measurement.
  */
-eventmask_t aosSysSsspStartupOsInitSyncCheck(event_listener_t* syncEvtListener)
+void aosSysStartUptime(void)
 {
-  aosDbgCheck(syncEvtListener != NULL);
+  chSysLock();
 
-  // local variables
-  eventmask_t m;
-  eventflags_t f;
-  apalControlGpioState_t s;
-
-  // update the system SSSP stage
-  aos.sssp.stage = AOS_SSSP_STARTUP_2_2;
-
-  // deactivate the sync signal to indicate that the module is ready (SSSPv1 stage 2.1 of startup phase)
-  apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF);
-
-  // wait for any event to occur (do not apply any filter in order not to miss any event)
-  m = chEvtWaitOne(ALL_EVENTS);
-  f = chEvtGetAndClearFlags(syncEvtListener);
-  apalControlGpioGet(&moduleSsspGpioSync, &s);
-
-  // if the event was a system event,
-  //   and it was fired because of the SysSync control signal,
-  //   and the SysSync control signal has been deactivated
-  if (m & syncEvtListener->events &&
-      f == MODULE_SSSP_EVENTFLAGS_SYNC &&
-      s == APAL_GPIO_OFF) {
-    chSysLock();
-    // start the uptime counter
-    _synctime = chVTGetSystemTimeX();
-    _uptime = 0;
-    chVTSetI(&_systimer, SYSTIMER_PERIOD, &_uptimeCallback, NULL);
-    chSysUnlock();
+  // start the uptime counter
+  _synctime = chVTGetSystemTimeX();
+  _uptime = 0;
+  chVTSetI(&_systimer, SYSTIMER_PERIOD, &_uptimeCallback, NULL);
 
-    return 0;
-  }
-  // an unexpected event occurred
-  else {
-    // reassign the flags to the event and return the event mask
-    syncEvtListener->flags |= f;
-    return m;
-  }
+  chSysUnlock();
+
+  return;
 }
+
 #endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
 
 /**
@@ -963,7 +973,7 @@ eventmask_t aosSysSsspStartupOsInitSyncCheck(event_listener_t* syncEvtListener)
  *
  * @param[out] ut   The system uptime.
  */
-inline void aosSysGetUptimeX(aos_timestamp_t* ut)
+void aosSysGetUptimeX(aos_timestamp_t* ut)
 {
   aosDbgCheck(ut != NULL);
 
@@ -1020,54 +1030,53 @@ void aosSysShutdownInit(aos_shutdown_t shutdown)
   aosDbgCheck(shutdown != AOS_SHUTDOWN_NONE);
 
 #if (AMIROOS_CFG_SSSP_ENABLE == true)
+
 #if (AMIROOS_CFG_SSSP_MASTER == true)
   // deactivate the system synchronization timer
   chVTReset(&_syssynctimer);
+  // activate SSSP S signal in case the synchronization timer had it deactivated.
+  apalControlGpioSet(&moduleSsspGpioS, APAL_GPIO_ON);
 #endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
 
-  // update the system SSSP stage
-  aos.sssp.stage = AOS_SSSP_SHUTDOWN_1_1;
-
-  chSysLock();
-  // activate the SYS_PD control signal only, if this module initiated the shutdown
+  // activate the PD signal only if this module initiated the shutdown
   if (shutdown != AOS_SHUTDOWN_PASSIVE) {
-    apalControlGpioSet(&moduleSsspGpioPd, APAL_GPIO_ON);
+    apalControlGpioSet(&moduleSsspGpioPD, APAL_GPIO_ON);
+    aosSsspShutdownInit();
   }
-  // activate the SYS_SYNC signal
-  apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_ON);
-  chSysUnlock();
-#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
 
   switch (shutdown) {
     case AOS_SHUTDOWN_PASSIVE:
-      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN);
+      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_PASSIVE);
       aosprintf("shutdown request received...\n");
       break;
+#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_NONE)
+    case AOS_SHUTDOWN_ACTIVE:
+      aosprintf("shutdown initiated...\n");
+      break;
+#elif (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
     case AOS_SHUTDOWN_HIBERNATE:
-      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_HIBERNATE);
       aosprintf("shutdown to hibernate mode...\n");
       break;
     case AOS_SHUTDOWN_DEEPSLEEP:
-      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_DEEPSLEEP);
       aosprintf("shutdown to deepsleep mode...\n");
       break;
     case AOS_SHUTDOWN_TRANSPORTATION:
-      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_TRANSPORTATION);
       aosprintf("shutdown to transportation mode...\n");
       break;
     case AOS_SHUTDOWN_RESTART:
-      chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_RESTART);
       aosprintf("restarting system...\n");
       break;
-    // must never occur
+#endif /* (AMIROOS_CFG_BOOTLOADER == X) */
     case AOS_SHUTDOWN_NONE:
-    default:
+      // must never occur
+      aosDbgAssert(false);
       break;
   }
 
-#if (AMIROOS_CFG_SSSP_ENABLE == true)
-  // update the system SSSP stage
-  aos.sssp.stage = AOS_SSSP_SHUTDOWN_1_2;
+#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
+
+  aosprintf("shutdown initiated...\n");
+
 #endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
 
   return;
@@ -1078,9 +1087,15 @@ void aosSysShutdownInit(aos_shutdown_t shutdown)
  */
 void aosSysStop(void)
 {
-#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true)
-  chThdWait(aos.shell.thread);
-#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) */
+  // wait until the calling thread is the only remaining active thread
+#if (CH_CFG_NO_IDLE_THREAD == TRUE)
+  while (_numActiveThreads() > 1) {
+#else /* (CH_CFG_NO_IDLE_THREAD == TRUE) */
+  while (_numActiveThreads() > 2) {
+#endif /* (CH_CFG_NO_IDLE_THREAD == TRUE) */
+    aosDbgPrintf("waiting for all threads to terminate...\n");
+    chThdYield();
+  }
 
   return;
 }
@@ -1093,13 +1108,17 @@ void aosSysDeinit(void)
   return;
 }
 
+#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_SHUTDOWN != true)) ||                 \
+    ((AMIROOS_CFG_SSSP_ENABLE != true) && (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)) || \
+    defined(__DOXYGEN__)
+
 /**
  * @brief   Finally shuts down the system and calls the bootloader callback function.
  * @note    This function should be called from the thtead with highest priority.
  *
  * @param[in] shutdown    Type of shutdown.
  */
-void aosSysShutdownFinal(aos_shutdown_t shutdown)
+void aosSysShutdownToBootloader(aos_shutdown_t shutdown)
 {
   // check arguments
   aosDbgCheck(shutdown != AOS_SHUTDOWN_NONE);
@@ -1107,20 +1126,18 @@ void aosSysShutdownFinal(aos_shutdown_t shutdown)
   // disable all interrupts
   irqDeinit();
 
-#if (AMIROOS_CFG_SSSP_ENABLE == true)
-  // update the system SSSP stage
-  aos.sssp.stage = AOS_SSSP_SHUTDOWN_1_3;
-#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
-
-  // validate bootloader
+#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
+  // validate AMiRo-BLT
   if ((BL_CALLBACK_TABLE_ADDRESS->magicNumber == BL_MAGIC_NUMBER) &&
       (BL_CALLBACK_TABLE_ADDRESS->vBootloader.major == BL_VERSION_MAJOR) &&
       (BL_CALLBACK_TABLE_ADDRESS->vBootloader.minor >= BL_VERSION_MINOR)) {
     // call bootloader callback depending on arguments
     switch (shutdown) {
+#if (AMIROOS_CFG_SSSP_ENABLE == true)
       case AOS_SHUTDOWN_PASSIVE:
         BL_CALLBACK_TABLE_ADDRESS->cbHandleShutdownRequest();
         break;
+#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
       case AOS_SHUTDOWN_HIBERNATE:
         BL_CALLBACK_TABLE_ADDRESS->cbShutdownHibernate();
         break;
@@ -1139,23 +1156,26 @@ void aosSysShutdownFinal(aos_shutdown_t shutdown)
         break;
     }
   } else {
-    // fallback if bootloader was found to be invalid
-    aosprintf("Bootloader incompatible or not available!\n");
+    // fallback if AMiRo-BLT was found to be invalid
+    aosprintf("ERROR: AMiRo-BLT incompatible or not available!\n");
     aosThdMSleep(10);
     chSysDisable();
   }
+#endif /* (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT) */
 
   return;
 }
 
+#endif /* ((AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_SHUTDOWN != true)) || ((AMIROOS_CFG_SSSP_ENABLE != true) && (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)) */
+
 /**
  * @brief   Generic callback function for GPIO interrupts.
  *
  * @return  Pointer to the callback function.
  */
-palcallback_t aosSysGetStdExtiCallback(void)
+palcallback_t aosSysGetStdGpioCallback(void)
 {
-  return _extiCallback;
+  return _gpioCallback;
 }
 
 /** @} */
