Merged from external project:
[bertos.git] / bertos / drv / timer.c
index 4ace035cdac9875ba5df10119a7892e34c70fac9..4d1309466da6b37fb870ad244aa4f802df57f74a 100644 (file)
@@ -52,7 +52,7 @@
 #include <cpu/irq.h>
 #include <cpu/power.h> // cpu_relax()
 
-#include <kern/preempt.h> // proc_decQuantun()
+#include <kern/proc_p.h> // proc_decQuantun()
 
 /*
  * Include platform-specific binding code if we're hosted.
@@ -286,15 +286,10 @@ void timer_busyWait(hptime_t delay)
        hptime_t now, prev = timer_hw_hpread();
        hptime_t delta;
 
-       for(;;)
+       for (;;)
        {
                now = timer_hw_hpread();
-               /*
-                * We rely on hptime_t being unsigned here to
-                * reduce the modulo to an AND in the common
-                * case of TIMER_HW_CNT.
-                */
-               delta = (now - prev) % TIMER_HW_CNT;
+               delta = (now < prev) ? (TIMER_HW_CNT - prev + now) : (now - prev);
                if (delta >= delay)
                        break;
                delay -= delta;
@@ -345,9 +340,6 @@ DEFINE_TIMER_ISR
 
        TIMER_STROBE_ON;
 
-       /* Perform hw IRQ handling */
-       timer_hw_irq();
-
        /* Update the master ms counter */
        ++_clock;
 
@@ -358,6 +350,9 @@ DEFINE_TIMER_ISR
                timer_poll(&timers_queue);
        #endif
 
+       /* Perform hw IRQ handling */
+       timer_hw_irq();
+
        TIMER_STROBE_OFF;
 }