commit 23ec8223744a9d361188b06131f00fcad1f90e9b
Author: Thomas Schöpping <tschoepp@cit-ec.uni-bielefeld.de>
Date:   Tue Feb 4 18:09:22 2020 +0100

    timer and periodic timer have been unified in a single functional unit: timer.
    
    The resulting timer object is slightly larger than one-shot timers have been, but much smaller than periodic timers.
    Size of resulting binary has been slightly decreased as well.

diff --git a/core/src/aos_timer.c b/core/src/aos_timer.c
index 224eb2f..89abce4 100644
--- a/core/src/aos_timer.c
+++ b/core/src/aos_timer.c
@@ -51,8 +51,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
  * forward declarations
  */
 static void _intermediateCb(void* timer);
-static void _fireCb(void *timer);
-static void _periodicCb(void* ptimer);
+static void _triggerCb(void *timer);
 
 /**
  * @brief   Setup a timer according to its configuration.
@@ -61,61 +60,47 @@ static void _periodicCb(void* ptimer);
  */
 static void _setupTimer(aos_timer_t* timer)
 {
+  // local variables
   aos_timestamp_t uptime;
+  aos_timestamp_t timedelta;
 
   // get current system uptime
   aosSysGetUptimeX(&uptime);
 
-  // if the wakeup time is more than TIME_IMMEDIATE in the future
-  if ( (timer->wkuptime > uptime) && ((timer->wkuptime - uptime) > TIME_IMMEDIATE) ) {
-    // split the time delta if necessary
-    if ((timer->wkuptime - uptime) > AOS_TIMER_MAX_INTERVAL_US) {
-      chVTSetI(&(timer->vt), chTimeUS2I(AOS_TIMER_MAX_INTERVAL_US), _intermediateCb, timer);
-    } else {
-      chVTSetI(&(timer->vt), chTimeUS2I(timer->wkuptime - uptime), _fireCb, timer);
+  // if this is a periodic timer
+  if (timer->interval > 0) {
+    // if the periodic timer is already initialized
+    if (timer->triggertime > 0) {
+      timer->triggertime += timer->interval;
+    }
+    // if the periodic timer is not initialized yet
+    else {
+      timer->triggertime = uptime + timer->interval;
     }
-  } else {
-    vtfunc_t fn = timer->callback;
-    timer->callback = NULL;
-    fn(timer->cbparam);
   }
 
-  return;
-}
-
-/**
- * @brief   Setup a periodic timer according to its configuration.
- *
- * @param[in] ptimer  Pointer to the periodic timer to setup.
- */
-static void _setupPeriodicTimer(aos_periodictimer_t *ptimer)
-{
-  // if the periodic timer is being initialized
-  if (ptimer->timer.wkuptime == 0) {
-    aos_timestamp_t uptime;
+  // calculate the time delta (may be 'negative' at this point)
+  timedelta = timer->triggertime - uptime;
 
-    // get current system uptime
-    aosSysGetUptimeX(&uptime);
-    ptimer->timer.wkuptime = uptime + ptimer->interval;
-  }
-  // if the peridic timer is reactivated after it has fired
-  else {
-    ptimer->timer.wkuptime += ptimer->interval;
+  // if the trigger time is more than TIME_IMMEDIATE in the future
+  if ((timer->triggertime > uptime) && (timedelta > chTimeI2US(TIME_IMMEDIATE))) {
+    // split the time delta if necessary
+    if (timedelta > AOS_TIMER_MAX_INTERVAL_US) {
+      chVTSetI(&timer->vt, chTimeUS2I(AOS_TIMER_MAX_INTERVAL_US), _intermediateCb, timer);
+    } else {
+      chVTSetI(&timer->vt, chTimeUS2I(timedelta), _triggerCb, timer);
+    }
+  } else {
+    // trigger immediately
+    timer->callback(timer->cbparam);
   }
 
-  // set the callback and parameters
-  ptimer->timer.callback = _periodicCb;
-  ptimer->timer.cbparam = ptimer;
-
-  // setup the timer
-  _setupTimer(&(ptimer->timer));
-
   return;
 }
 
 /**
  * @brief   Callback function for intermediate interrupts.
- * @details This is required if the desired time to fire is too far in the future so that the interval must be split.
+ * @details This is required if the desired time to trigger is too far in the future so that the interval must be split.
  *
  * @param[in] timer   Pointer to a aos_timer_t to reactivate.
  */
@@ -127,29 +112,21 @@ static void _intermediateCb(void* timer)
 }
 
 /**
- * @brief   Callback function for the final fire event of the timer.
+ * @brief   Callback function for the trigger event of the timer.
  *
  * @param[in] timer   Pointer to a aos_timer_t to call its callback.
  */
-static void _fireCb(void *timer)
+static void _triggerCb(void *timer)
 {
   chSysLockFromISR();
   ((aos_timer_t*)timer)->callback(((aos_timer_t*)timer)->cbparam);
+  // reenable periodic timers
+  if (((aos_timer_t*)timer)->interval > 0) {
+    _setupTimer((aos_timer_t*)timer);
+  }
   chSysUnlockFromISR();
 }
 
-/**
- * @brief   Callback function for periodic timer interrupts.
- * @details This function calls the original callback and reenables the timer afterwards.
- *
- * @param[in] ptimer  Pointer to a aos_periodictimer_t to reactivate.
- */
-static void _periodicCb(void* ptimer)
-{
-  ((aos_periodictimer_t*)ptimer)->callback(((aos_periodictimer_t*)ptimer)->cbparam);
-  _setupPeriodicTimer((aos_periodictimer_t*)ptimer);
-}
-
 /******************************************************************************/
 /* EXPORTED FUNCTIONS                                                         */
 /******************************************************************************/
@@ -164,7 +141,8 @@ void aosTimerInit(aos_timer_t* timer)
   aosDbgAssert(timer != NULL);
 
   chVTObjectInit(&(timer->vt));
-  timer->wkuptime = 0;
+  timer->triggertime = 0;
+  timer->interval = 0;
   timer->callback = NULL;
   timer->cbparam = NULL;
 
@@ -172,22 +150,22 @@ void aosTimerInit(aos_timer_t* timer)
 }
 
 /**
- * @brief   Set timer to fire at an absolute system time.
+ * @brief   Set timer to trigger at an absolute system time.
  *
  * @param[in] timer   Pointer to the timer to set.
- * @param[in] uptime  Pointer to the absolute system time for the timer to fire.
+ * @param[in] uptime  Absolute system time for the timer to trigger.
  * @param[in] cb      Pointer to a callback function to be called.
  *                    In contrast to ChibiOS callback functions, this gets called from an already ISR locked context.
  *                    Thus it may not contain any chSysLockX() or chSysUnlockX() calls and so forth.
- * @param[in] par     Pointer to a parameter fpr the callback function (may be NULL).
+ * @param[in] par     Pointer to a parameter for the callback function (may be NULL).
  */
-void aosTimerSetAbsoluteI(aos_timer_t *timer, aos_timestamp_t *uptime, vtfunc_t cb, void *par)
+void aosTimerSetAbsoluteI(aos_timer_t *timer, const aos_timestamp_t uptime, vtfunc_t cb, void *par)
 {
   aosDbgCheck(timer != NULL);
-  aosDbgCheck(uptime != NULL);
   aosDbgCheck(cb != NULL);
 
-  timer->wkuptime = *uptime;
+  timer->triggertime = uptime;
+  timer->interval = 0;
   timer->callback = cb;
   timer->cbparam = par;
   _setupTimer(timer);
@@ -196,16 +174,16 @@ void aosTimerSetAbsoluteI(aos_timer_t *timer, aos_timestamp_t *uptime, vtfunc_t
 }
 
 /**
- * @brief   Set timer to fire after a relative interval.
+ * @brief   Set timer to trigger after a relative interval.
  *
  * @param[in] timer   Pointer to the timer to set.
- * @param[in] offset  Relative interval to set for the timer to fire.
+ * @param[in] offset  Relative interval to set for the timer to trigger.
  * @param[in] cb      Pointer to a callback function to be called.
  *                    In contrast to ChibiOS callback functions, this gets called from an already ISR locked context.
  *                    Thus it may not contain any chSysLockX() or chSysUnlockX() calls and so forth.
  * @param[in] par     Pointer to a parameter for the callback function (may be NULL).
  */
-void aosTimerSetIntervalI(aos_timer_t *timer, aos_interval_t offset, vtfunc_t cb, void *par)
+void aosTimerSetIntervalI(aos_timer_t *timer, const aos_interval_t offset, vtfunc_t cb, void *par)
 {
   aosDbgCheck(timer != NULL);
   aosDbgCheck(cb != NULL);
@@ -213,7 +191,8 @@ void aosTimerSetIntervalI(aos_timer_t *timer, aos_interval_t offset, vtfunc_t cb
   aos_timestamp_t uptime;
 
   aosSysGetUptimeX(&uptime);
-  timer->wkuptime = uptime + offset;
+  timer->triggertime = uptime + offset;
+  timer->interval = 0;
   timer->callback = cb;
   timer->cbparam = par;
   _setupTimer(timer);
@@ -222,25 +201,25 @@ void aosTimerSetIntervalI(aos_timer_t *timer, aos_interval_t offset, vtfunc_t cb
 }
 
 /**
- * @brief   Set timer to fire after a long relative interval.
+ * @brief   Set timer to trigger after a long relative interval.
  *
  * @param[in] timer   Pointer to the timer to set.
- * @param[in] offset  Pointer to a long interval value to set for the timer to fire.
+ * @param[in] offset  Long interval value to set for the timer to trigger.
  * @param[in] cb      Pointer to a callback function to be called.
  *                    In contrast to ChibiOS callback functions, this gets called from an already ISR locked context.
  *                    Thus it may not contain any chSysLockX() or chSysUnlockX() calls and so forth.
  * @param[in] par     Pointer to a parameter for the callback function (may be NULL).
  */
-void aosTimerSetLongIntervalI(aos_timer_t *timer, aos_longinterval_t *offset, vtfunc_t cb, void *par)
+void aosTimerSetLongIntervalI(aos_timer_t *timer, const aos_longinterval_t offset, vtfunc_t cb, void *par)
 {
   aosDbgCheck(timer != NULL);
-  aosDbgCheck(offset != NULL);
   aosDbgCheck(cb != NULL);
 
   aos_timestamp_t uptime;
 
   aosSysGetUptimeX(&uptime);
-  timer->wkuptime = uptime + *offset;
+  timer->triggertime = uptime + offset;
+  timer->interval = 0;
   timer->callback = cb;
   timer->cbparam = par;
   _setupTimer(timer);
@@ -249,66 +228,51 @@ void aosTimerSetLongIntervalI(aos_timer_t *timer, aos_longinterval_t *offset, vt
 }
 
 /**
- * @brief   Initialize a aos_periodictimer_t object.
- *
- * @param[in] timer   The periodic timer to initialize.
- */
-void aosPeriodicTimerInit(aos_periodictimer_t *ptimer)
-{
-  aosDbgAssert(ptimer != NULL);
-
-  aosTimerInit(&(ptimer->timer));
-  ptimer->interval = 0;
-
-  return;
-}
-
-/**
- * @brief   Set a periodic timer to fire in the specified interval.
+ * @brief   Set timer to trigger periodically in the specified interval.
  *
- * @param[in] ptimer    Pointer to the periodic timer to set.
- * @param[in] interval  Interval for the periodic timer to fire,
+ * @param[in] timer     Pointer to the periodic timer to set.
+ * @param[in] interval  Interval for the periodic timer to trigger periodically.
  * @param[in] cb        Pointer to a callback function to be called.
  *                      In contrast to ChibiOS callback functions, this gets called from an already ISR locked context.
  *                      Thus it may not contain any chSysLockX() or chSysUnlockX() calls and so forth.
  * @param[in] par       Pointer to a parameter for the callback function (may be NULL).
  */
-void aosPeriodicTimerSetI(aos_periodictimer_t* ptimer, aos_interval_t interval, vtfunc_t cb, void* par)
+void aosTimerPeriodicIntervalI(aos_timer_t* timer, const aos_interval_t interval, vtfunc_t cb, void* par)
 {
-  aosDbgCheck(ptimer != NULL);
+  aosDbgCheck(timer != NULL);
   aosDbgCheck(interval > TIME_IMMEDIATE);
   aosDbgCheck(cb != NULL);
 
-  ptimer->timer.wkuptime = 0;
-  ptimer->interval = interval;
-  ptimer->callback = cb;
-  ptimer->cbparam = par;
-  _setupPeriodicTimer(ptimer);
+  timer->triggertime = 0;
+  timer->interval = interval;
+  timer->callback = cb;
+  timer->cbparam = par;
+  _setupTimer(timer);
 
   return;
 }
 
 /**
- * @brief   Set a periodic timer to fire in the specified interval.
+ * @brief   Set timer to trigger periodically in the specified interval.
  *
- * @param[in] ptimer    Pointer to the periodic timer to set.
- * @param[in] interval  Pointer to a long interval value for the periodic timer to fire,
+ * @param[in] timer     Pointer to the periodic timer to set.
+ * @param[in] interval  Long interval value for the periodic timer to trigger periodically.
  * @param[in] cb        Pointer to a callback function to be called.
  *                      In contrast to other callback functions, this get called from an already ISR locked context.
  *                      This it may not contain any chSysLockX() or chSysUnlockX() calls.
  * @param[in] par       Pointer to a parameter for the callback function (may be NULL).
  */
-void aosPeriodicTimerSetLongI(aos_periodictimer_t* ptimer, aos_longinterval_t* interval, vtfunc_t cb, void* par)
+void aosTimerPeriodicLongIntervalI(aos_timer_t* timer, const aos_longinterval_t interval, vtfunc_t cb, void* par)
 {
-  aosDbgCheck(ptimer != NULL);
-  aosDbgCheck(*interval > TIME_IMMEDIATE);
+  aosDbgCheck(timer != NULL);
+  aosDbgCheck(interval > TIME_IMMEDIATE);
   aosDbgCheck(cb != NULL);
 
-  ptimer->timer.wkuptime = 0;
-  ptimer->interval = *interval;
-  ptimer->callback = cb;
-  ptimer->cbparam = par;
-  _setupPeriodicTimer(ptimer);
+  timer->triggertime = 0;
+  timer->interval = interval;
+  timer->callback = cb;
+  timer->cbparam = par;
+  _setupTimer(timer);
 
   return;
 }
