Skip to content

Commit

Permalink
prevent rounding 0 if HWTIMER_SPEED > 1000000L
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benpicco committed Oct 6, 2014
1 parent 7b3f0c5 commit 033d17f
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion core/include/hwtimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,22 @@
* @param[in] us number of microseconds
* @return kernel timer ticks
*/
#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)
Expand All @@ -69,7 +77,11 @@
/**
* @brief microseconds before hwtimer overflow
*/
#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 */

Expand Down

0 comments on commit 033d17f

Please sign in to comment.