From c67b44c96a8c26d7fbcd32172e640bbbdced72a8 Mon Sep 17 00:00:00 2001 From: Kent Hall Date: Tue, 7 Dec 2021 21:37:03 -0500 Subject: [PATCH] drivers: counter: Added get_freq to API Optional counter API function which allows for a driver to determine counter frequency at runtime; if set, this supersedes whatever is set statically in the counter_config_info struct. Signed-off-by: Kent Hall --- include/drivers/counter.h | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/include/drivers/counter.h b/include/drivers/counter.h index b649b303b0c..f0e0ce52aa3 100644 --- a/include/drivers/counter.h +++ b/include/drivers/counter.h @@ -188,6 +188,7 @@ typedef uint32_t (*counter_api_get_guard_period)(const struct device *dev, typedef int (*counter_api_set_guard_period)(const struct device *dev, uint32_t ticks, uint32_t flags); +typedef uint32_t (*counter_api_get_freq)(const struct device *dev); __subsystem struct counter_driver_api { counter_api_start start; @@ -200,6 +201,7 @@ __subsystem struct counter_driver_api { counter_api_get_top_value get_top_value; counter_api_get_guard_period get_guard_period; counter_api_set_guard_period set_guard_period; + counter_api_get_freq get_freq; }; /** @@ -251,8 +253,10 @@ static inline uint32_t z_impl_counter_get_frequency(const struct device *dev) { const struct counter_config_info *config = (const struct counter_config_info *)dev->config; + const struct counter_driver_api *api = + (struct counter_driver_api *)dev->api; - return config->freq; + return api->get_freq ? api->get_freq(dev) : config->freq; } /** @@ -268,9 +272,7 @@ __syscall uint32_t counter_us_to_ticks(const struct device *dev, uint64_t us); static inline uint32_t z_impl_counter_us_to_ticks(const struct device *dev, uint64_t us) { - const struct counter_config_info *config = - (const struct counter_config_info *)dev->config; - uint64_t ticks = (us * config->freq) / USEC_PER_SEC; + uint64_t ticks = (us * z_impl_counter_get_frequency(dev)) / USEC_PER_SEC; return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks; } @@ -288,10 +290,7 @@ __syscall uint64_t counter_ticks_to_us(const struct device *dev, uint32_t ticks) static inline uint64_t z_impl_counter_ticks_to_us(const struct device *dev, uint32_t ticks) { - const struct counter_config_info *config = - (const struct counter_config_info *)dev->config; - - return ((uint64_t)ticks * USEC_PER_SEC) / config->freq; + return ((uint64_t)ticks * USEC_PER_SEC) / z_impl_counter_get_frequency(dev); } /**