prevent rounding 0 if HWTIMER_SPEED > 1000000L

The current macros in hwtimer.h expect HWTIMER_SPEED to be < 1000000L, otherwise integer arithmetic will round the result down to 0.
Add a case to prevent that.
dev/timer
Benjamin Valentin 9 years ago
parent 3d1ff65307
commit d6ca7c44c2

@ -82,14 +82,22 @@ per second for the current architecture."
* @param[in] us number of microseconds
* @return kernel timer ticks
*/
#define HWTIMER_TICKS(us) ((us) / (1000000L / HWTIMER_SPEED))
#if HWTIMER_SPEED > 1000000L
#define HWTIMER_TICKS(us) ((us) * (HWTIMER_SPEED / 1000000L))
#else
#define HWTIMER_TICKS(us) ((us) / (1000000L / HWTIMER_SPEED))
#endif
/**
* @brief Convert ticks to microseconds
* @param[in] ticks number of ticks
* @return microseconds
*/
#define HWTIMER_TICKS_TO_US(ticks) ((ticks) * (1000000L/HWTIMER_SPEED))
#if HWTIMER_SPEED > 1000000L
#define HWTIMER_TICKS_TO_US(ticks) ((ticks) / (HWTIMER_SPEED / 1000000L))
#else
#define HWTIMER_TICKS_TO_US(ticks) ((ticks) * (1000000L / HWTIMER_SPEED))
#endif
/**
* @brief Maximum hwtimer tick count (before overflow)
@ -103,7 +111,11 @@ number of ticks countable on the current architecture."
/**
* @brief microseconds before hwtimer overflow
*/
#define HWTIMER_OVERFLOW_MICROS() (1000000L / HWTIMER_SPEED * HWTIMER_MAXTICKS)
#if HWTIMER_SPEED > 1000000L
#define HWTIMER_OVERFLOW_MICROS() (HWTIMER_MAXTICKS / HWTIMER_SPEED * 1000000L)
#else
#define HWTIMER_OVERFLOW_MICROS() (1000000L / HWTIMER_SPEED * HWTIMER_MAXTICKS)
#endif
typedef uint32_t timer_tick_t; /**< data type for hwtimer ticks */

Loading…
Cancel
Save