Skip to content

Commit f3dec28

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 6f323c8 commit f3dec28

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
@@ -468,13 +468,63 @@ static inline int z_impl_counter_capture_disable(const struct device *dev,
468468
__syscall uint32_t counter_us_to_ticks(const struct device *dev, uint64_t us);
469469

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

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

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

488538
static inline uint64_t z_impl_counter_ticks_to_us(const struct device *dev,
489-
uint32_t ticks)
539+
uint32_t ticks)
490540
{
491541
return ((uint64_t)ticks * USEC_PER_SEC) / z_impl_counter_get_frequency(dev);
492542
}
493543

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

0 commit comments

Comments
 (0)