Skip to content

Commit f7adb53

Browse files
author
Kent Hall
committed
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 <[email protected]>
1 parent fc2756c commit f7adb53

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

include/drivers/counter.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ typedef uint32_t (*counter_api_get_guard_period)(const struct device *dev,
188188
typedef int (*counter_api_set_guard_period)(const struct device *dev,
189189
uint32_t ticks,
190190
uint32_t flags);
191+
typedef uint32_t (*counter_api_get_freq)(const struct device *dev);
191192

192193
__subsystem struct counter_driver_api {
193194
counter_api_start start;
@@ -200,6 +201,7 @@ __subsystem struct counter_driver_api {
200201
counter_api_get_top_value get_top_value;
201202
counter_api_get_guard_period get_guard_period;
202203
counter_api_set_guard_period set_guard_period;
204+
counter_api_get_freq get_freq;
203205
};
204206

205207
/**
@@ -251,8 +253,10 @@ static inline uint32_t z_impl_counter_get_frequency(const struct device *dev)
251253
{
252254
const struct counter_config_info *config =
253255
(const struct counter_config_info *)dev->config;
256+
const struct counter_driver_api *api =
257+
(struct counter_driver_api *)dev->api;
254258

255-
return config->freq;
259+
return api->get_freq ? api->get_freq(dev) : config->freq;
256260
}
257261

258262
/**
@@ -268,9 +272,7 @@ __syscall uint32_t counter_us_to_ticks(const struct device *dev, uint64_t us);
268272
static inline uint32_t z_impl_counter_us_to_ticks(const struct device *dev,
269273
uint64_t us)
270274
{
271-
const struct counter_config_info *config =
272-
(const struct counter_config_info *)dev->config;
273-
uint64_t ticks = (us * config->freq) / USEC_PER_SEC;
275+
uint64_t ticks = (us * z_impl_counter_get_frequency(dev)) / USEC_PER_SEC;
274276

275277
return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks;
276278
}
@@ -288,10 +290,7 @@ __syscall uint64_t counter_ticks_to_us(const struct device *dev, uint32_t ticks)
288290
static inline uint64_t z_impl_counter_ticks_to_us(const struct device *dev,
289291
uint32_t ticks)
290292
{
291-
const struct counter_config_info *config =
292-
(const struct counter_config_info *)dev->config;
293-
294-
return ((uint64_t)ticks * USEC_PER_SEC) / config->freq;
293+
return ((uint64_t)ticks * USEC_PER_SEC) / z_impl_counter_get_frequency(dev);
295294
}
296295

297296
/**

0 commit comments

Comments
 (0)