From 07e8ac9c198a140b76032815e6158a4fdb636e2a Mon Sep 17 00:00:00 2001
From: Martin Lenders <mail@martin-lenders.de>
Date: Tue, 2 Apr 2013 14:34:42 +0200
Subject: [PATCH] Remove swtimer

see #4
---
 core/include/hwtimer.h    |   2 +-
 sys/Makefile              |   3 -
 sys/auto_init/auto_init.c |   4 -
 sys/include/auto_init.h   |   4 -
 sys/include/swtimer.h     | 147 -------------------
 sys/ping/ping.c           |  12 +-
 sys/swtimer/Makefile      |   5 -
 sys/swtimer/swtimer.c     | 301 --------------------------------------
 8 files changed, 8 insertions(+), 470 deletions(-)
 delete mode 100644 sys/include/swtimer.h
 delete mode 100644 sys/swtimer/Makefile
 delete mode 100644 sys/swtimer/swtimer.c

diff --git a/core/include/hwtimer.h b/core/include/hwtimer.h
index 9dcd3fc5eb..1d53402556 100644
--- a/core/include/hwtimer.h
+++ b/core/include/hwtimer.h
@@ -7,7 +7,7 @@
  * interrupt context and must use the shortest possible execution time (e.g.
  * set a flag and trigger a worker thread).
  *
- * <b>hwtimer must not be used within applications</b>, use \ref swtimer
+ * <b>hwtimer must not be used within applications</b>, use \ref vtimer
  * instead.
  *
  * @defgroup hwtimer    Hardware timer
diff --git a/sys/Makefile b/sys/Makefile
index 7f5188b453..1332d17a16 100644
--- a/sys/Makefile
+++ b/sys/Makefile
@@ -49,9 +49,6 @@ endif
 ifneq (,$(findstring shell_commands,$(USEMODULE)))
     DIRS += shell/commands
 endif
-ifneq (,$(findstring swtimer,$(USEMODULE)))
-    DIRS += swtimer
-endif
 ifneq (,$(findstring timex,$(USEMODULE)))
     DIRS += timex
 endif
diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c
index 9921143b50..be6feabada 100644
--- a/sys/auto_init/auto_init.c
+++ b/sys/auto_init/auto_init.c
@@ -30,10 +30,6 @@ void auto_init(void) {
     DEBUG("Auto init vtimer module.\n");
     vtimer_init();
 #endif
-#ifdef MODULE_SWTIMER
-    DEBUG("Auto init swtimer module.\n");
-    swtimer_init();
-#endif
 #ifdef MODULE_UART0
     DEBUG("Auto init uart0 module.\n");
     board_uart0_init();
diff --git a/sys/include/auto_init.h b/sys/include/auto_init.h
index c3da973b34..a8d1b61d8d 100644
--- a/sys/include/auto_init.h
+++ b/sys/include/auto_init.h
@@ -5,10 +5,6 @@
 #include <hwtimer.h>
 #endif
 
-#ifdef MODULE_SWTIMER
-#include <swtimer.h>
-#endif
-
 #ifdef MODULE_SHT11
 #include <sht11.h>
 #endif
diff --git a/sys/include/swtimer.h b/sys/include/swtimer.h
deleted file mode 100644
index fc17204ba5..0000000000
--- a/sys/include/swtimer.h
+++ /dev/null
@@ -1,147 +0,0 @@
-/** \addtogroup system
- * @{ */
-
-/**
- * \defgroup swtimer Software Timer library
- *
- * The swtimer library provides functions for setting, resetting and restarting
- * software timers, and for checking if a swtimer has expired.
- *
- * @{
- */
-
-/**
- * \file
- * Timer library header file.
- */
-
-#ifndef __SWTIMER_H__
-#define __SWTIMER_H__
-#warning Swtimers are deprecated. use virtual timers (vtimer) instead.
-
-#include <stdint.h>
-
-#define MSG_TIMER 12345
-
-#define SWTIMER_WAKEUP 0
-#define SWTIMER_CALLBACK 1
-#define SWTIMER_MSG 2
-
-#undef wakeup
-
-typedef uint32_t swtime_t;
-
-/**
- * A swtimer.
- *
- * This structure is used for declaring a swtimer. The swtimer must be set
- * with swtimer_set() before it can be used.
- *
- * \hideinitializer
- */
-typedef struct swtimer_t {
-    swtime_t start;
-	swtime_t interval;
-
-    struct swtimer_t *next;
-
-    int action_type;
-    union {
-        struct { 
-            int pid;
-        } wakeup;
-        struct {
-            void (*f)(void*);
-            void *ptr;
-        } callback;
-        struct {
-            unsigned int value;
-            int target_pid;
-        } msg;
-    } action;
-} swtimer_t;
-
-/**
- * @brief   Current system time
- * @return  Time in ticks since system boot
- */
-swtime_t swtimer_now(void);
-
-/**
- * @brief   Initializes swtimer
- * @return  always 0
- */
-int swtimer_init(void);
-
-/**
- * @brief   set swtimer interval and activate 
- * @param[in]   t           pointer to preinitialised swtimer_t
- * @param[in]   interval    swtimer interval
- * @return  always 0
- */
-int swtimer_set(swtimer_t *t, swtime_t interval);
-
-/**
- * @brief   reset swtimer
- * @param[in]   t           pointer to preinitialised swtimer_t 
- */
-void swtimer_reset(swtimer_t *t);
-
-/**
- * @brief   restart swtimer
- * @param[in]   t           pointer to preinitialised swtimer_t 
- */
-void swtimer_restart(swtimer_t *t);
-
-/**
- * @brief   check if swtimer is expired
- * @param[in]   t           pointer to preinitialised swtimer_t
- * @return  
- */
-int swtimer_expired(swtimer_t *t);
-
-/**
- * @brief   will cause the calling thread to be suspended from excecution until the number of microseconds has elapsed
- * @param[in]   us          number of microseconds 
- * @return      0 on success, < 0 on error
- */
-int swtimer_usleep(swtime_t us);
-
-/**
- * @brief   set a swtimer with msg event handler
- * @param[in]   t           pointer to preinitialised swtimer_t
- * @param[in]   interval    swtimer interval
- * @param[in]   pid         process id
- * @param[in]   ptr         message value
- * @return      0 on success, < 0 on error
- */
-int swtimer_set_msg(swtimer_t *t, swtime_t interval, int pid, void *ptr);
-
-/**
- * @brief   set a swtimer with wakeup event
- * @param[in]   t           pointer to preinitialised swtimer_t
- * @param[in]   pid         process id
- * @return      0 on success, < 0 on error
- */
-int swtimer_set_wakeup(swtimer_t *t, swtime_t interval, int pid);
-
-/**
- * @brief   set a swtimer with callback function event handler 
- * @param[in]   t           pointer to preinitialised swtimer_t
- * @param[in]   interval    swtimer interval
- * @param[in]   f_ptr       pointer to callback function
- * @return      0 on success, < 0 on error
- */
-int swtimer_set_cb(swtimer_t *t, swtime_t interval, void (*f_ptr)(void *), void *ptr);
-
-/**
- * @brief   remove a swtimer
- * @param[in]   t           pointer to preinitialised swtimer_t
- * @return      0 on success, < 0 on error
- */
-int swtimer_remove(swtimer_t *t);
-
-#endif /* __SWTIMER_H__ */
-
-/** @} */
-/** @} */
diff --git a/sys/ping/ping.c b/sys/ping/ping.c
index c734ae5c90..fecc71f65f 100644
--- a/sys/ping/ping.c
+++ b/sys/ping/ping.c
@@ -6,14 +6,15 @@
 #include "cc110x/cc1100.h"
 #include "lpc2387.h"
 
-#include "swtimer.h"
+#include "vtimer.h"
+#include "timex.h"
 #include "gpioint.h"
 #include <ping.h>
 
 ping_payload *pipa;
 protocol_t protocol_id = 0;
 radio_address_t r_address = 0;
-uint64_t start = 0;
+timex_t start = 0;
 float rtt = 0;
 
 void ping_handler(void *payload, int payload_size, 
@@ -48,7 +49,7 @@ void ping(radio_address_t addr, uint8_t channr){
 	cc1100_set_channel(channr);
 	cc1100_set_address(r_address);
 	while(1){
-		start = swtimer_now();
+		start = vtimer_now();
 		int trans_ok = cc1100_send_csmaca(addr,
 			protocol_id,2,pipa->payload,sizeof(pipa->payload));
 		if(trans_ok < 0)
@@ -58,9 +59,10 @@ void ping(radio_address_t addr, uint8_t channr){
 }
 
 void calc_rtt(void){
-	uint64_t end = swtimer_now();
+	timex_t end = vtimer_now();
+	timex_t result = vtimer_sub(end, start);
 	
-	rtt = ((float)end - (float)start)/1000;	
+	rtt = result.seconds+(float)result.microseconds/100000;	
 }
 
 void print_success(void){
diff --git a/sys/swtimer/Makefile b/sys/swtimer/Makefile
deleted file mode 100644
index b586e5476e..0000000000
--- a/sys/swtimer/Makefile
+++ /dev/null
@@ -1,5 +0,0 @@
-INCLUDES = -I../include -I$(RIOTBASE)/core/include/ -I$(RIOTBASE)/drivers/include
-MODULE =swtimer
-
-include $(RIOTBASE)/Makefile.base
-
diff --git a/sys/swtimer/swtimer.c b/sys/swtimer/swtimer.c
deleted file mode 100644
index 9693e03a87..0000000000
--- a/sys/swtimer/swtimer.c
+++ /dev/null
@@ -1,301 +0,0 @@
-/**
- *
- * \ingroup system
- * @{
- */
-
-#include <stdio.h>
-#include <stdint.h>
-
-#include <msg.h>
-#include <thread.h>
-#include <hwtimer.h>
-#include <swtimer.h>
-#include <sched.h>
-#include <cpu.h>
-#include <irq.h>
-
-#define SWTIMER_OVERHEAD 80
-#define SWTIMER_SPIN_THRESHOLD 100
-
-//#define ENABLE_DEBUG
-
-#ifdef ENABLE_DEBUG
-#undef SWTIMER_OVERHEAD
-#define SWTIMER_OVERHEAD 7500
-#endif
-
-#include <debug.h>
-
-/* workaround for buggy mspgcc signal.h */
-#undef wakeup
-
-static void swtimer_update_alarm(void);
-static void swtimer_action(swtimer_t *swtimer);
-static void swtimer_trigger(void* ptr);
-static void swtimer_tick(void *ptr);
-static int swtimer_activate(swtimer_t *t);
-static void swtimer_priolist_insert(swtimer_t *t);
-static void swtimer_update_values(void);
-
-static swtimer_t *swtimer_list = NULL;
-static volatile swtime_t system_time = 0;
-volatile swtime_t swtimer_next_alarm_absolute = 0;
-static volatile unsigned long hwtimer_ticks_left = 0;
-static volatile int hwtimer_id = -1;
-
-extern unsigned long hwtimer_now(void);
-
-int swtimer_init(void) {
-    hwtimer_set_absolute(HWTIMER_MAXTICKS, swtimer_tick, NULL);
-    return 0;
-}
-
-int swtimer_set(swtimer_t *t, swtime_t interval) {
-    t->interval = interval;
-    t->next = NULL;
-    swtimer_activate(t);
-    return 0;
-}
-
-static int swtimer_activate(swtimer_t *t) {
-    DEBUG("swtimer_activate. now=%lu t->interval = %lu hwtimer_ticks=%lu\n", swtimer_now(), t->interval, HWTIMER_TICKS(t->interval));
-
-    if (!inISR()) dINT();
-    
-    if (t->interval <= SWTIMER_OVERHEAD) {
-        DEBUG("swtimer_activate: interval too short, triggering right away.\n");
-        swtimer_action(t);
-        if (!inISR()) eINT();
-        return 0;
-    }
-    
-    t->start = swtimer_now();
-
-    swtimer_priolist_insert(t);
-    
-    if (swtimer_list == t) {          
-        swtimer_update_values();
-        swtimer_update_alarm();
-    }
-    
-    if (!inISR())eINT();
-    
-    return 0;
-}
-
-static void swtimer_update_values(void) {
-            swtimer_next_alarm_absolute = swtimer_list->start + swtimer_list->interval;
-            swtime_t now = swtimer_now();
-            swtime_t offset = swtimer_next_alarm_absolute - now;
-            hwtimer_ticks_left = HWTIMER_TICKS(offset);
-            
-            DEBUG("swtimer_update_values abs: %lu offset: %lu hwtimer_ticks_left: %lu, now=%lu, hwtimer_now=%lu\n", swtimer_next_alarm_absolute, offset, hwtimer_ticks_left, swtimer_now(), hwtimer_now());
-}
-
-
-int swtimer_remove(swtimer_t *t) {
-    if ( (! swtimer_list) || (! t)) {
-        return -1;
-    }
-
-    if ( ! inISR() ) dINT();
-    if (t == swtimer_list) {
-        swtimer_list = t->next;
-        if (swtimer_list) {
-            swtimer_update_values();
-            swtimer_update_alarm();
-        } else {
-            swtimer_next_alarm_absolute = 0;
-            hwtimer_ticks_left = 0;
-            hwtimer_remove(hwtimer_id);
-            hwtimer_id = -1;
-        }
-    } else {
-        swtimer_t *cur = t;
-        while (cur) {
-            if (cur->next == t) {
-                cur->next = cur->next->next;
-                break;
-            }
-            cur = cur->next;
-        }
-    }
-    if (! inISR() ) eINT();
-    return 0;
-}
-
-swtime_t swtimer_now(void) {
-    swtime_t now = system_time;
-    now += HWTIMER_TICKS_TO_US(hwtimer_now());
-    return now;
-}
-
-int swtimer_set_msg(swtimer_t *t, swtime_t interval, int pid, void *ptr) {
-    t->action_type = SWTIMER_MSG;
-    t->action.msg.value = (unsigned int) ptr;
-    t->action.msg.target_pid = pid;
-    swtimer_set(t, interval);
-    return 0;
-}
-
-int swtimer_set_wakeup(swtimer_t *t, swtime_t interval, int pid) {
-    t->action_type = SWTIMER_WAKEUP;
-    t->action.wakeup.pid = pid;
-    swtimer_set(t, interval);
-    return 0;
-}
-
-int swtimer_set_cb(swtimer_t *t, swtime_t interval, void (*f_ptr)(void *), void *ptr) {
-    t->action_type = SWTIMER_CALLBACK;
-    t->action.callback.f = f_ptr;
-    t->action.callback.ptr = ptr;
-    swtimer_set(t, interval);
-    return 0;
-}
-
-static void swtimer_spin(swtime_t us) {
-    swtime_t target = swtimer_now() + us;
-    while (target > swtimer_now());
-}
-
-int swtimer_usleep(swtime_t us) {
-    if (inISR()) {
-        swtimer_spin(us);
-        return 0;
-    }
-    swtimer_t t;
-    t.interval = us;
-    t.action_type = SWTIMER_WAKEUP;
-    t.action.wakeup.pid = thread_getpid();
-    swtimer_activate(&t);
-    thread_sleep();
-    return 0;
-}
-
-static void swtimer_priolist_insert(swtimer_t *t) {
-    t->next = NULL;
-    if (swtimer_list == NULL) {
-//        DEBUG("swtimer: inserting first timer %x\n", (unsigned int)t);
-        swtimer_list = t;
-    } else {
-//        DEBUG("swtimer: inserting timer %x\n", (unsigned int)t);
-        swtime_t t_absolute = t->start + t->interval;
-        swtimer_t *last = NULL;
-        swtimer_t *cur = swtimer_list;
-        while (cur != NULL) {
-            if ( t_absolute < (cur->start + cur->interval) ) {
-//                DEBUG("swtimer: timer %x elapses before timer %x\n", (unsigned int) t, (unsigned int) cur);
-                t->next = cur;
-                if (last) {
-//                    DEBUG("swtimer: setting ->next of %x to %x\n", (unsigned int) last->next, (unsigned int) t);
-                    last->next = t;
-                } else {
-//                    DEBUG("swtimer: %x is first timer now.\n", (unsigned int)t);
-                    swtimer_list = t;
-                }
-                return;
-            } else {
-//                DEBUG("insertF\n");
-                if ( cur->next ) {
-//                    DEBUG("insertF1\n");
-                    last = cur;
-                    cur = cur->next;
-                } else {
-//                    DEBUG("insertF2\n");
-                    cur->next = t;
-                    return;
-                }
-            }
-        }
-    }
-}
-
-
-static void swtimer_set_hwtimer(unsigned int offset) { 
-    DEBUG("swtimer_set_hwtimer: hwtimer_now: %lu offset:%u\n", hwtimer_now(), offset);
-    if (hwtimer_id != -1) {
-        hwtimer_remove(hwtimer_id);
-    }
-
-    hwtimer_id = hwtimer_set (offset, swtimer_trigger, NULL);
-}
-
-static void swtimer_action(swtimer_t *swtimer) {
-    switch(swtimer->action_type) {
-        case SWTIMER_WAKEUP: 
-            {
-                thread_wakeup(swtimer->action.wakeup.pid);
-                break;
-            }
-        case SWTIMER_CALLBACK: 
-            {
-                swtimer->action.callback.f(swtimer->action.callback.ptr);
-                break;
-            }
-        case SWTIMER_MSG: 
-            {
-                msg_t m;
-                m.content.value = swtimer->action.msg.value;
-                int result = msg_send_int(&m, swtimer->action.msg.target_pid);
-                if (result < 0) {
-                    // error
-                }
-                break;
-            }
-    }
-}
-
-static void swtimer_trigger(void* ptr) {
-    swtimer_t *next = swtimer_list;
-    swtimer_list = swtimer_list->next;
-    swtimer_action(next);
-    if (! ptr) swtimer_update_alarm();
-}
-
-
-static void swtimer_update_alarm(void) {
-    DEBUG("swtimer_check_elapsed: Checking for elapsed timer...\n");
-    
-    while (swtimer_list) {
-        swtimer_update_values();
-        DEBUG("swtimer_check_elapsed: there are timers left to consider. hwtimer_ticks_left=%lu\n", hwtimer_ticks_left);
-
-        if (hwtimer_ticks_left > HWTIMER_MAXTICKS)  {
-            if ((long int) hwtimer_ticks_left < 0) {
-                printf("swtimer_update_alarm: We're late!\n");
-            }
-            return;
-            }
-
-        if (hwtimer_ticks_left < SWTIMER_SPIN_THRESHOLD) {
-            DEBUG("swtimer_check_elapsed: spinning..\n");
-            if (hwtimer_ticks_left != 0) hwtimer_spin(hwtimer_ticks_left);
-            DEBUG("swtimer_check_elapsed: spinning done. shooting timer.\n");
-            swtimer_trigger((void*)1); /* flag to prevent recursion */
-        } else {
-            DEBUG("swtimer_check_elapsed: Setting hwtimer.\n");
-            swtimer_set_hwtimer(hwtimer_ticks_left);
-            return;
-        }
-    }
-}
-
-
-static void swtimer_tick(void* offset_ptr) {
-    hwtimer_set_absolute(HWTIMER_MAXTICKS, swtimer_tick, NULL);
-    system_time += HWTIMER_TICKS_TO_US(HWTIMER_MAXTICKS);
-    
-//    DEBUG("swtimer_tick: system_time: %lu next timer: %lu ticks_left: %lu pid=%i\n", system_time, swtimer_next_alarm_absolute, hwtimer_ticks_left, thread_getpid());
-    DEBUG(".");
-
-    if (swtimer_next_alarm_absolute > 0) {
-        if (hwtimer_ticks_left > HWTIMER_MAXTICKS) {
-            hwtimer_ticks_left -= HWTIMER_MAXTICKS;
-            swtimer_update_alarm();
-        }
-    }
-}
-
-
-/** @} */
-- 
GitLab