Skip to content

Commit bed095d

Browse files
drivers: counter: add inline function for ns/us to ticks
Add inline helper functions for converting ticks to nanoseconds. Also implement an inline helper for converting microseconds or nanoseconds with a 64bit tick value. Signed-off-by: Ryan McClelland <ryanmcclelland@meta.com>
1 parent 6385efe commit bed095d

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

include/zephyr/drivers/counter.h

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,63 @@ static inline int z_impl_counter_capture_disable(const struct device *dev,
467467
__syscall uint32_t counter_us_to_ticks(const struct device *dev, uint64_t us);
468468

469469
static inline uint32_t z_impl_counter_us_to_ticks(const struct device *dev,
470-
uint64_t us)
470+
uint64_t us)
471471
{
472472
uint64_t ticks = (us * z_impl_counter_get_frequency(dev)) / USEC_PER_SEC;
473473

474474
return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks;
475475
}
476476

477+
/**
478+
* @brief Function to convert microseconds to ticks with 64 bits.
479+
*
480+
* @param[in] dev Pointer to the device structure for the driver instance.
481+
* @param[in] us Microseconds.
482+
*
483+
* @return Converted ticks.
484+
*/
485+
__syscall uint64_t counter_us_to_ticks_64(const struct device *dev, uint64_t us);
486+
487+
static inline uint64_t z_impl_counter_us_to_ticks_64(const struct device *dev,
488+
uint64_t us)
489+
{
490+
return (us * z_impl_counter_get_frequency(dev)) / USEC_PER_SEC;
491+
}
492+
493+
/**
494+
* @brief Function to convert nanoseconds to ticks.
495+
*
496+
* @param[in] dev Pointer to the device structure for the driver instance.
497+
* @param[in] ns Nanoseconds.
498+
*
499+
* @return Converted ticks. Ticks will be saturated if exceed 32 bits.
500+
*/
501+
__syscall uint32_t counter_ns_to_ticks(const struct device *dev, uint64_t ns);
502+
503+
static inline uint32_t z_impl_counter_ns_to_ticks(const struct device *dev,
504+
uint64_t ns)
505+
{
506+
uint64_t ticks = (ns * z_impl_counter_get_frequency(dev)) / NSEC_PER_SEC;
507+
508+
return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks;
509+
}
510+
511+
/**
512+
* @brief Function to convert nanoseconds to ticks with 64 bits.
513+
*
514+
* @param[in] dev Pointer to the device structure for the driver instance.
515+
* @param[in] ns Nanoseconds.
516+
*
517+
* @return Converted ticks.
518+
*/
519+
__syscall uint64_t counter_ns_to_ticks_64(const struct device *dev, uint64_t ns);
520+
521+
static inline uint64_t z_impl_counter_ns_to_ticks_64(const struct device *dev,
522+
uint64_t ns)
523+
{
524+
return (ns * z_impl_counter_get_frequency(dev)) / NSEC_PER_SEC;
525+
}
526+
477527
/**
478528
* @brief Function to convert ticks to microseconds.
479529
*
@@ -485,11 +535,43 @@ static inline uint32_t z_impl_counter_us_to_ticks(const struct device *dev,
485535
__syscall uint64_t counter_ticks_to_us(const struct device *dev, uint32_t ticks);
486536

487537
static inline uint64_t z_impl_counter_ticks_to_us(const struct device *dev,
488-
uint32_t ticks)
538+
uint32_t ticks)
489539
{
490540
return ((uint64_t)ticks * USEC_PER_SEC) / z_impl_counter_get_frequency(dev);
491541
}
492542

543+
/**
544+
* @brief Function to convert ticks to nanoseconds.
545+
*
546+
* @param[in] dev Pointer to the device structure for the driver instance.
547+
* @param[in] ticks Ticks.
548+
*
549+
* @return Converted nanoseconds.
550+
*/
551+
__syscall uint64_t counter_ticks_to_ns(const struct device *dev, uint32_t ticks);
552+
553+
static inline uint64_t z_impl_counter_ticks_to_ns(const struct device *dev,
554+
uint32_t ticks)
555+
{
556+
return ((uint64_t)ticks * NSEC_PER_SEC) / z_impl_counter_get_frequency(dev);
557+
}
558+
559+
/**
560+
* @brief Function to convert ticks to nanoseconds.
561+
*
562+
* @param[in] dev Pointer to the device structure for the driver instance.
563+
* @param[in] ticks Ticks.
564+
*
565+
* @return Converted nanoseconds.
566+
*/
567+
__syscall uint64_t counter_ticks_to_ns_64(const struct device *dev, uint64_t ticks);
568+
569+
static inline uint64_t z_impl_counter_ticks_to_ns_64(const struct device *dev,
570+
uint64_t ticks)
571+
{
572+
return (ticks * NSEC_PER_SEC) / z_impl_counter_get_frequency(dev);
573+
}
574+
493575
/**
494576
* @brief Function to retrieve maximum top value that can be set.
495577
*

0 commit comments

Comments
 (0)