Skip to content

Commit d4e76b9

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 2b1968c commit d4e76b9

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
@@ -225,7 +225,7 @@ struct counter_config_info {
225225
/**
226226
* Frequency of the source clock if synchronous events are counted.
227227
*/
228-
uint32_t freq;
228+
uint64_t freq;
229229
/**
230230
* Flags (see @ref COUNTER_FLAGS).
231231
*/
@@ -264,6 +264,7 @@ typedef int (*counter_api_set_guard_period)(const struct device *dev,
264264
uint32_t ticks,
265265
uint32_t flags);
266266
typedef uint32_t (*counter_api_get_freq)(const struct device *dev);
267+
typedef uint64_t (*counter_api_get_freq_64)(const struct device *dev);
267268
typedef int (*counter_api_capture_callback_set)(const struct device *dev,
268269
uint8_t chan,
269270
uint32_t flags,
@@ -290,6 +291,7 @@ __subsystem struct counter_driver_api {
290291
counter_api_get_guard_period get_guard_period;
291292
counter_api_set_guard_period set_guard_period;
292293
counter_api_get_freq get_freq;
294+
counter_api_get_freq get_freq_64;
293295
#if defined(CONFIG_COUNTER_CAPTURE) || defined(__DOXYGEN__)
294296
counter_api_capture_callback_set capture_callback_set;
295297
counter_api_capture_enable capture_enable;
@@ -349,8 +351,37 @@ static inline uint32_t z_impl_counter_get_frequency(const struct device *dev)
349351
const struct counter_driver_api *api =
350352
(struct counter_driver_api *)dev->api;
351353

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

0 commit comments

Comments
 (0)