Skip to content

Commit 6385efe

Browse files
drivers: counter: change freq to 64 bits
Some counters, such as those that use fractional adders, tick at resolutions greater than 4294967295Hz. This changes the freq in the common info to a uint64_t. This retains the existing `counter_get_freq` function which will still return a 32bit value. This also adds a `counter_get_freq_64` function to get a 64bit value for the frequency. Signed-off-by: Ryan McClelland <ryanmcclelland@meta.com>
1 parent e49e554 commit 6385efe

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

drivers/counter/counter_ite_it8xxx2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static int counter_it8xxx2_init(const struct device *dev)
275275
const struct counter_it8xxx2_config *config = dev->config;
276276

277277
LOG_DBG("max top value = 0x%08x", config->info.max_top_value);
278-
LOG_DBG("frequency = %d", config->info.freq);
278+
LOG_DBG("frequency = %llu", config->info.freq);
279279
LOG_DBG("channels = %d", config->info.channels);
280280

281281
/* set the top value of top timer */

drivers/counter/counter_mcux_gpt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static int mcux_gpt_init(const struct device *dev)
199199

200200
/* Adjust divider to match expected freq */
201201
if (clock_freq % config->info.freq) {
202-
LOG_ERR("Cannot Adjust GPT freq to %u\n", config->info.freq);
202+
LOG_ERR("Cannot Adjust GPT freq to %llu\n", config->info.freq);
203203
LOG_ERR("clock src is %u\n", clock_freq);
204204
return -EINVAL;
205205
}

drivers/counter/counter_xlnx_axi_timer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ static int xlnx_axi_timer_init(const struct device *dev)
294294
const struct xlnx_axi_timer_config *config = dev->config;
295295

296296
LOG_DBG("max top value = 0x%08x", config->info.max_top_value);
297-
LOG_DBG("frequency = %d", config->info.freq);
297+
LOG_DBG("frequency = %llu", config->info.freq);
298298
LOG_DBG("channels = %d", config->info.channels);
299299

300300
xlnx_axi_timer_write32(dev, config->info.max_top_value, TLR0_OFFSET);

include/zephyr/drivers/counter.h

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ struct counter_config_info {
224224
/**
225225
* Frequency of the source clock if synchronous events are counted.
226226
*/
227-
uint32_t freq;
227+
uint64_t freq;
228228
/**
229229
* Flags (see @ref COUNTER_FLAGS).
230230
*/
@@ -263,6 +263,7 @@ typedef int (*counter_api_set_guard_period)(const struct device *dev,
263263
uint32_t ticks,
264264
uint32_t flags);
265265
typedef uint32_t (*counter_api_get_freq)(const struct device *dev);
266+
typedef uint64_t (*counter_api_get_freq_64)(const struct device *dev);
266267
typedef int (*counter_api_capture_callback_set)(const struct device *dev,
267268
uint8_t chan,
268269
uint32_t flags,
@@ -289,6 +290,7 @@ __subsystem struct counter_driver_api {
289290
counter_api_get_guard_period get_guard_period;
290291
counter_api_set_guard_period set_guard_period;
291292
counter_api_get_freq get_freq;
293+
counter_api_get_freq get_freq_64;
292294
#ifdef CONFIG_COUNTER_CAPTURE
293295
counter_api_capture_callback_set capture_callback_set;
294296
counter_api_capture_enable capture_enable;
@@ -348,8 +350,37 @@ static inline uint32_t z_impl_counter_get_frequency(const struct device *dev)
348350
const struct counter_driver_api *api =
349351
(struct counter_driver_api *)dev->api;
350352

351-
return api->get_freq ? api->get_freq(dev) : config->freq;
353+
if (config->freq) {
354+
return config->freq > UINT32_MAX ? UINT32_MAX : (uint32_t)config->freq;
355+
} else {
356+
return api->get_freq(dev);
357+
}
352358
}
359+
360+
/**
361+
* @brief Function to get counter frequency in 64bits.
362+
*
363+
* @param[in] dev Pointer to the device structure for the driver instance.
364+
*
365+
* @return Frequency of the counter in Hz, or zero if the counter does
366+
* not have a fixed frequency.
367+
*/
368+
__syscall uint64_t counter_get_frequency_64(const struct device *dev);
369+
370+
static inline uint64_t z_impl_counter_get_frequency_64(const struct device *dev)
371+
{
372+
const struct counter_config_info *config =
373+
(const struct counter_config_info *)dev->config;
374+
const struct counter_driver_api *api =
375+
(struct counter_driver_api *)dev->api;
376+
377+
if (config->freq) {
378+
return config->freq;
379+
} else {
380+
return api->get_freq_64 ? api->get_freq_64(dev) : -ENOTSUP;
381+
}
382+
}
383+
353384
#ifdef CONFIG_COUNTER_CAPTURE
354385
__syscall int counter_capture_callback_set(const struct device *dev,
355386
uint8_t chan,

0 commit comments

Comments
 (0)