diff --git a/src/drivers/drv_hrt.h b/src/drivers/drv_hrt.h index 19e2456b1258240ab8942d3da9006cff8ae62255..17fe4ba107f0f7c77281d273b997c976d4c79c37 100644 --- a/src/drivers/drv_hrt.h +++ b/src/drivers/drv_hrt.h @@ -92,6 +92,14 @@ __EXPORT extern hrt_abstime ts_to_abstime(const struct timespec *ts); */ __EXPORT extern void abstime_to_ts(struct timespec *ts, hrt_abstime abstime); +/** + * Compute the delta between a timestamp taken in the past + * and now. + * + * This function is not interrupt save. + */ +__EXPORT extern hrt_abstime hrt_elapsed_time(const volatile hrt_abstime *then); + /** * Compute the delta between a timestamp taken in the past * and now. @@ -99,7 +107,7 @@ __EXPORT extern void abstime_to_ts(struct timespec *ts, hrt_abstime abstime); * This function is safe to use even if the timestamp is updated * by an interrupt during execution. */ -__EXPORT extern hrt_abstime hrt_elapsed_time(const volatile hrt_abstime *then); +__EXPORT extern hrt_abstime hrt_elapsed_time_atomic(const volatile hrt_abstime *then); /** * Store the absolute time in an interrupt-safe fashion. diff --git a/src/drivers/stm32/drv_hrt.c b/src/drivers/stm32/drv_hrt.c index 6fcbbf8e36a97b269a7a99af711961571996e368..f44ad152452c82802587392c5554b6e9da1235a4 100644 --- a/src/drivers/stm32/drv_hrt.c +++ b/src/drivers/stm32/drv_hrt.c @@ -720,6 +720,17 @@ abstime_to_ts(struct timespec *ts, hrt_abstime abstime) */ hrt_abstime hrt_elapsed_time(const volatile hrt_abstime *then) +{ + hrt_abstime delta = hrt_absolute_time() - *then; + + return delta; +} + +/** + * Compare a time value with the current time as atomic operation + */ +hrt_abstime +hrt_elapsed_time_atomic(const volatile hrt_abstime *then) { irqstate_t flags = px4_enter_critical_section();