diff options
author | Lorenz Meier <lm@inf.ethz.ch> | 2013-01-26 12:07:01 +0100 |
---|---|---|
committer | Lorenz Meier <lm@inf.ethz.ch> | 2013-01-26 12:07:01 +0100 |
commit | beb463874808e94d8ca99ab14402895c10cfb63e (patch) | |
tree | 33efd417f138b964660d21deaf74bfb733b03c24 /nuttx/sched/clock_time2ticks.c | |
parent | 7459169230c4fbb738aaa79f61592048e537cf02 (diff) | |
parent | beb45222985f1eb9fbe21b22b95c30ab8ca5bbac (diff) | |
download | px4-firmware-beb463874808e94d8ca99ab14402895c10cfb63e.tar.gz px4-firmware-beb463874808e94d8ca99ab14402895c10cfb63e.tar.bz2 px4-firmware-beb463874808e94d8ca99ab14402895c10cfb63e.zip |
Merge branch 'master' of github.com:PX4/Firmware into statemachine_docs
Diffstat (limited to 'nuttx/sched/clock_time2ticks.c')
-rw-r--r-- | nuttx/sched/clock_time2ticks.c | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/nuttx/sched/clock_time2ticks.c b/nuttx/sched/clock_time2ticks.c index 9265872bb..012f4e4f1 100644 --- a/nuttx/sched/clock_time2ticks.c +++ b/nuttx/sched/clock_time2ticks.c @@ -89,14 +89,48 @@ int clock_time2ticks(FAR const struct timespec *reltime, FAR int *ticks) { +#ifdef CONFIG_HAVE_LONG_LONG + int64_t relnsec; + + /* Convert the relative time into nanoseconds. The range of the int64_t is + * sufficiently large that there is no real need for range checking. + */ + + relnsec = (int64_t)reltime->tv_sec * NSEC_PER_SEC + + (int64_t)reltime->tv_nsec; + + /* Convert nanoseconds to clock ticks, rounding up to the smallest integer + * that is greater than or equal to the exact number of tick. + */ + + *ticks = (int)((relnsec + NSEC_PER_TICK - 1) / NSEC_PER_TICK); + return OK; +#else int32_t relusec; - /* Convert the relative time into microseconds.*/ + /* This function uses an int32_t to only the relative time in microseconds. + * that means that the maximum supported relative time is 2,147,487.647 + * seconds + */ + +#if 0 // overkill + DEBUGASSERT(reltime->tv_sec < 2147487 || + reltime->tv_sec == 2147487 && + reltime->tv_nsec <= 647 * NSEC_PER_MSEC); +#endif + + /* Convert the relative time into microseconds, rounding up to the smallest + * value that is greater than or equal to the exact number of microseconds. + */ - relusec = reltime->tv_sec * USEC_PER_SEC + reltime->tv_nsec / NSEC_PER_USEC; + relusec = reltime->tv_sec * USEC_PER_SEC + + (reltime->tv_nsec + NSEC_PER_USEC - 1) / NSEC_PER_USEC; - /* Convert microseconds to clock ticks */ + /* Convert microseconds to clock ticks, rounding up to the smallest integer + * that is greater than or equal to the exact number of tick. + */ - *ticks = (relusec + USEC_PER_TICK - 1) / USEC_PER_TICK; + *ticks = (int)((relusec + USEC_PER_TICK - 1) / USEC_PER_TICK); return OK; +#endif } |