X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=drv%2Ftimer.h;h=cd7aa1b48f6344f8bda8d235a4557b1b901b3f4b;hb=2535cb94ec2183791128f8bbd109ca69a960cf78;hp=7e2a628871f7a069f85d9f29a8095dc4244d6781;hpb=c338086be997ecb73aacaea64b5a8eb1c11a614c;p=bertos.git diff --git a/drv/timer.h b/drv/timer.h index 7e2a6288..cd7aa1b4 100644 --- a/drv/timer.h +++ b/drv/timer.h @@ -42,7 +42,10 @@ #define DRV_TIMER_H #include -#include +#include +#include +#include + /* * Include platform-specific binding header if we're hosted. @@ -112,7 +115,7 @@ INLINE ticks_t ms_to_ticks(mtime_t ms) return (ms * TIMER_TICKS_PER_SEC) / 1000; #else /* Fast timer: don't overflow ticks_t. */ - return ms * ((TIMER_TICKS_PER_SEC + 500) / 1000); + return ms * DIV_ROUND(TIMER_TICKS_PER_SEC, 1000); #endif } @@ -124,7 +127,7 @@ INLINE ticks_t us_to_ticks(utime_t us) return ((us / 1000) * TIMER_TICKS_PER_SEC) / 1000; #else /* Fast timer: don't overflow ticks_t. */ - return (us * ((TIMER_TICKS_PER_SEC + 500) / 1000)) / 1000; + return (us * DIV_ROUND(TIMER_TICKS_PER_SEC, 1000)) / 1000; #endif } @@ -156,9 +159,9 @@ INLINE utime_t ticks_to_us(ticks_t ticks) INLINE hptime_t us_to_hptime(utime_t us) { #if TIMER_HW_HPTICKS_PER_SEC > 10000000UL - return us * ((TIMER_HW_HPTICKS_PER_SEC + 500000UL) / 1000000UL); + return us * DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, 1000000UL); #else - return (us * TIMER_HW_HPTICKS_PER_SEC + 500000UL) / 1000000UL; + return (us * ((TIMER_HW_HPTICKS_PER_SEC + 500) / 1000UL) + 500) / 1000UL; #endif } @@ -166,9 +169,9 @@ INLINE hptime_t us_to_hptime(utime_t us) INLINE utime_t hptime_to_us(hptime_t hpticks) { #if TIMER_HW_HPTICKS_PER_SEC < 100000UL - return hpticks * (1000000UL / TIMER_HW_HPTICKS_PER_SEC); + return hpticks * DIV_ROUND(1000000UL, TIMER_HW_HPTICKS_PER_SEC); #else - return (hpticks * 1000000UL) / TIMER_HW_HPTICKS_PER_SEC; + return (hpticks * 1000UL) / DIV_ROUND(TIMER_HW_HPTICKS_PER_SEC, 1000UL); #endif /* TIMER_HW_HPTICKS_PER_SEC < 100000UL */ }