diff --git a/doc/releases/migration-guide-4.3.rst b/doc/releases/migration-guide-4.3.rst index 135f02d3a033f..78200cbae3a1b 100644 --- a/doc/releases/migration-guide-4.3.rst +++ b/doc/releases/migration-guide-4.3.rst @@ -78,6 +78,24 @@ Comparator and :c:macro:`NRF_COMP_AIN_VDDH_DIV5` represents VDDH/5. The old ``string`` properties type is deprecated. +Counter +======= + +* Drivers that support 64-bit ticks now can use the new KConfig + :kconfig:option:`CONFIG_COUNTER_64BITS` to enable this feature. The drivers that support + this feature are :dtcompatible:`intel,ace-art-counter`, + :dtcompatible:`intel,ace-rtc-counter`, :dtcompatible:`ti,cc23x0-rtc`, + :dtcompatible:`espressif,esp32-counter`, and the :dtcompatible:`st,stm32-rtc` if + :kconfig:option:`CONFIG_COUNTER_RTC_STM32_SUBSECONDS` is enabled. This moves the ``uint32_t`` + previously used for ticks to a typedef :c:type:`counter_ticks_t` that is ``uint64_t`` when 64-bit + ticks are enabled and ``uint32_t`` otherwise. The API functions that use ticks have been + updated to use this typedef. This change is backward compatible as long as the application + does not enable 64-bit ticks. Enabling 64-bit ticks may require changes in the application + code to use the new typedef and to handle the larger tick values. The + :c:func:`counter_get_value_64` has been removed in favor of just using + :c:func:`counter_get_value` along with the :c:type:`counter_ticks_t` as it's parameter + defined as ``uint64_t`` with :kconfig:option:`CONFIG_COUNTER_64BITS` enabled. (:github:`94189`) + DMA === diff --git a/drivers/counter/Kconfig b/drivers/counter/Kconfig index d7827341d0780..4b38eb90470c9 100644 --- a/drivers/counter/Kconfig +++ b/drivers/counter/Kconfig @@ -22,6 +22,14 @@ config COUNTER_SHELL help Enable Shell Commands for Counter and Timer +config COUNTER_64BITS + bool + help + Select this option if the counter driver should use 64-bit + tick values for counter_ticks_t instead of 32-bit. + This is typically set by drivers that require extended + tick ranges beyond the 32-bit limit. + module = COUNTER module-str = counter source "subsys/logging/Kconfig.template.log_config" diff --git a/drivers/counter/Kconfig.ace b/drivers/counter/Kconfig.ace index a7c5d611a92ce..62268c731af73 100644 --- a/drivers/counter/Kconfig.ace +++ b/drivers/counter/Kconfig.ace @@ -5,6 +5,7 @@ config ACE_V1X_ART_COUNTER bool "DSP ART Wall Clock for ACE V1X" depends on DT_HAS_INTEL_ACE_ART_COUNTER_ENABLED + select COUNTER_64BITS default y help DSP ART Wall Clock used by ACE V1X. @@ -12,6 +13,7 @@ config ACE_V1X_ART_COUNTER config ACE_V1X_RTC_COUNTER bool "DSP RTC Wall Clock for ACE V1X" depends on DT_HAS_INTEL_ACE_RTC_COUNTER_ENABLED + select COUNTER_64BITS default y help DSP RTC Wall Clock used by ACE V1X. diff --git a/drivers/counter/Kconfig.cc23x0_rtc b/drivers/counter/Kconfig.cc23x0_rtc index ce4e9ad6b2c19..2b227fb2efe12 100644 --- a/drivers/counter/Kconfig.cc23x0_rtc +++ b/drivers/counter/Kconfig.cc23x0_rtc @@ -6,5 +6,6 @@ config COUNTER_CC23X0_RTC default y depends on DT_HAS_TI_CC23X0_RTC_ENABLED depends on CC23X0_SYSTIM_TIMER + select COUNTER_64BITS help Enable counter driver based on RTC timer for cc23x0 diff --git a/drivers/counter/Kconfig.esp32_tmr b/drivers/counter/Kconfig.esp32_tmr index 099c1d8206409..366e7d25e0a4b 100644 --- a/drivers/counter/Kconfig.esp32_tmr +++ b/drivers/counter/Kconfig.esp32_tmr @@ -7,6 +7,7 @@ config COUNTER_TMR_ESP32 bool "ESP32 Counter Driver based on GP-Timers" default y depends on DT_HAS_ESPRESSIF_ESP32_COUNTER_ENABLED + select COUNTER_64BITS help Enables the Counter driver API based on Espressif's General Purpose Timers for ESP32 series devices. diff --git a/drivers/counter/Kconfig.stm32_rtc b/drivers/counter/Kconfig.stm32_rtc index 4273d1c493139..bfd7243783e0d 100644 --- a/drivers/counter/Kconfig.stm32_rtc +++ b/drivers/counter/Kconfig.stm32_rtc @@ -23,6 +23,7 @@ config COUNTER_RTC_STM32_SAVE_VALUE_BETWEEN_RESETS config COUNTER_RTC_STM32_SUBSECONDS bool "Use the subseconds as a basic tick." depends on !SOC_SERIES_STM32F1X + select COUNTER_64BITS help Use the subseconds as the basic time tick. It increases resolution of the counter. The frequency of the time is RTC Source Clock divided diff --git a/drivers/counter/counter_ace_v1x_art.c b/drivers/counter/counter_ace_v1x_art.c index 380b0cab4a025..a5534cece03a6 100644 --- a/drivers/counter/counter_ace_v1x_art.c +++ b/drivers/counter/counter_ace_v1x_art.c @@ -103,7 +103,7 @@ int counter_ace_v1x_art_get_value(const struct device *dev, uint64_t *value) } static DEVICE_API(counter, ace_v1x_art_counter_apis) = { - .get_value_64 = counter_ace_v1x_art_get_value + .get_value = counter_ace_v1x_art_get_value }; DEVICE_DT_DEFINE(DT_NODELABEL(ace_art_counter), NULL, NULL, NULL, NULL, diff --git a/drivers/counter/counter_ace_v1x_rtc.c b/drivers/counter/counter_ace_v1x_rtc.c index ed0e53b6ebe26..ae6ba363f4f70 100644 --- a/drivers/counter/counter_ace_v1x_rtc.c +++ b/drivers/counter/counter_ace_v1x_rtc.c @@ -36,7 +36,7 @@ int counter_ace_v1x_rtc_init(const struct device *dev) } static DEVICE_API(counter, ace_v1x_rtc_counter_apis) = { - .get_value_64 = counter_ace_v1x_rtc_get_value + .get_value = counter_ace_v1x_rtc_get_value }; DEVICE_DT_DEFINE(DT_NODELABEL(ace_rtc_counter), counter_ace_v1x_rtc_init, NULL, NULL, NULL, diff --git a/drivers/counter/counter_cc23x0_rtc.c b/drivers/counter/counter_cc23x0_rtc.c index 97085359d26bf..251440ac11993 100644 --- a/drivers/counter/counter_cc23x0_rtc.c +++ b/drivers/counter/counter_cc23x0_rtc.c @@ -31,16 +31,6 @@ struct counter_cc23x0_data { struct counter_alarm_cfg alarm_cfg0; }; -static int counter_cc23x0_get_value(const struct device *dev, uint32_t *ticks) -{ - /* Resolution is 8us and max timeout ~9.5h */ - const struct counter_cc23x0_config *config = dev->config; - - *ticks = HWREG(config->base + RTC_O_TIME8U); - - return 0; -} - static int counter_cc23x0_get_value_64(const struct device *dev, uint64_t *ticks) { const struct counter_cc23x0_config *config = dev->config; @@ -211,8 +201,7 @@ static int counter_cc23x0_init(const struct device *dev) static DEVICE_API(counter, rtc_cc23x0_api) = { .start = counter_cc23x0_start, .stop = counter_cc23x0_stop, - .get_value = counter_cc23x0_get_value, - .get_value_64 = counter_cc23x0_get_value_64, + .get_value = counter_cc23x0_get_value_64, .set_alarm = counter_cc23x0_set_alarm, .cancel_alarm = counter_cc23x0_cancel_alarm, .get_top_value = counter_cc23x0_get_top_value, diff --git a/drivers/counter/counter_esp32_tmr.c b/drivers/counter/counter_esp32_tmr.c index 995b5101c2b13..ed7b9032fcd77 100644 --- a/drivers/counter/counter_esp32_tmr.c +++ b/drivers/counter/counter_esp32_tmr.c @@ -26,10 +26,10 @@ typedef bool (*timer_isr_t)(void *); struct counter_esp32_top_data { counter_top_callback_t callback; - uint32_t ticks; + counter_ticks_t ticks; void *user_data; bool auto_reload; - uint32_t guard_period; + counter_ticks_t guard_period; }; struct counter_esp32_config { @@ -47,7 +47,7 @@ struct counter_esp32_config { struct counter_esp32_data { struct counter_alarm_cfg alarm_cfg; struct counter_esp32_top_data top_data; - uint32_t ticks; + counter_ticks_t ticks; uint32_t clock_src_hz; timer_hal_context_t hal_ctx; }; @@ -120,17 +120,7 @@ static int counter_esp32_stop(const struct device *dev) return 0; } -static int counter_esp32_get_value(const struct device *dev, uint32_t *ticks) -{ - struct counter_esp32_data *data = dev->data; - - timer_ll_trigger_soft_capture(data->hal_ctx.dev, data->hal_ctx.timer_id); - *ticks = (uint32_t)timer_ll_get_counter_value(data->hal_ctx.dev, data->hal_ctx.timer_id); - - return 0; -} - -static int counter_esp32_get_value_64(const struct device *dev, uint64_t *ticks) +static int counter_esp32_get_value_64(const struct device *dev, counter_ticks_t *ticks) { struct counter_esp32_data *data = dev->data; @@ -146,12 +136,12 @@ static int counter_esp32_set_alarm(const struct device *dev, uint8_t chan_id, ARG_UNUSED(chan_id); struct counter_esp32_data *data = dev->data; bool absolute = alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE; - uint32_t ticks = alarm_cfg->ticks; - uint32_t top = data->top_data.ticks; - uint32_t max_rel_val = data->top_data.ticks; - uint64_t now; + counter_ticks_t ticks = alarm_cfg->ticks; + counter_ticks_t top = data->top_data.ticks; + counter_ticks_t max_rel_val = data->top_data.ticks; + counter_ticks_t now; uint64_t target; - uint32_t diff; + uint64_t diff; int err = 0; bool irq_on_late = 0; @@ -177,7 +167,7 @@ static int counter_esp32_set_alarm(const struct device *dev, uint8_t chan_id, timer_ll_set_alarm_value(data->hal_ctx.dev, data->hal_ctx.timer_id, target); - diff = (alarm_cfg->ticks - (uint32_t)now); + diff = (alarm_cfg->ticks - now); if (diff > max_rel_val) { if (absolute) { err = -ETIME; @@ -220,7 +210,7 @@ static int counter_esp32_set_top_value(const struct device *dev, const struct co { const struct counter_esp32_config *config = dev->config; struct counter_esp32_data *data = dev->data; - uint32_t now; + counter_ticks_t now; if (data->alarm_cfg.callback) { return -EBUSY; @@ -230,7 +220,7 @@ static int counter_esp32_set_top_value(const struct device *dev, const struct co return -ENOTSUP; } - counter_esp32_get_value(dev, &now); + counter_esp32_get_value_64(dev, &now); if (!(cfg->flags & COUNTER_TOP_CFG_DONT_RESET)) { timer_hal_set_counter_value(&data->hal_ctx, 0); @@ -267,7 +257,7 @@ static uint32_t counter_esp32_get_pending_int(const struct device *dev) return timer_ll_get_intr_status(data->hal_ctx.dev); } -static uint32_t counter_esp32_get_top_value(const struct device *dev) +static counter_ticks_t counter_esp32_get_top_value(const struct device *dev) { struct counter_esp32_data *data = dev->data; @@ -291,7 +281,7 @@ static int counter_esp32_reset(const struct device *dev) return 0; } -static uint32_t counter_esp32_get_guard_period(const struct device *dev, uint32_t flags) +static counter_ticks_t counter_esp32_get_guard_period(const struct device *dev, uint32_t flags) { struct counter_esp32_data *data = dev->data; @@ -300,7 +290,8 @@ static uint32_t counter_esp32_get_guard_period(const struct device *dev, uint32_ return data->top_data.guard_period; } -static int counter_esp32_set_guard_period(const struct device *dev, uint32_t ticks, uint32_t flags) +static int counter_esp32_set_guard_period(const struct device *dev, counter_ticks_t ticks, + uint32_t flags) { struct counter_esp32_data *data = dev->data; @@ -317,9 +308,8 @@ static int counter_esp32_set_guard_period(const struct device *dev, uint32_t tic static DEVICE_API(counter, counter_api) = { .start = counter_esp32_start, .stop = counter_esp32_stop, - .get_value = counter_esp32_get_value, .reset = counter_esp32_reset, - .get_value_64 = counter_esp32_get_value_64, + .get_value = counter_esp32_get_value_64, .set_alarm = counter_esp32_set_alarm, .cancel_alarm = counter_esp32_cancel_alarm, .set_top_value = counter_esp32_set_top_value, @@ -336,9 +326,9 @@ static void counter_esp32_isr(void *arg) struct counter_esp32_data *data = dev->data; counter_alarm_callback_t cb = data->alarm_cfg.callback; void *cb_data = data->alarm_cfg.user_data; - uint32_t now; + counter_ticks_t now; - counter_esp32_get_value(dev, &now); + counter_esp32_get_value_64(dev, &now); if (cb) { timer_ll_enable_intr(data->hal_ctx.dev, @@ -374,7 +364,7 @@ static void counter_esp32_isr(void *arg) static struct counter_esp32_data counter_data_##idx; \ \ static const struct counter_esp32_config counter_config_##idx = { \ - .counter_info = {.max_top_value = UINT32_MAX, \ + .counter_info = {.max_top_value = UINT64_MAX, \ .flags = COUNTER_CONFIG_INFO_COUNT_UP, \ .channels = 1}, \ .config = \ diff --git a/drivers/counter/counter_handlers.c b/drivers/counter/counter_handlers.c index 697d8d41f9631..4c252e416dcbb 100644 --- a/drivers/counter/counter_handlers.c +++ b/drivers/counter/counter_handlers.c @@ -46,7 +46,7 @@ static inline uint32_t z_vrfy_counter_get_frequency(const struct device *dev) } #include -static inline uint32_t z_vrfy_counter_us_to_ticks(const struct device *dev, +static inline counter_ticks_t z_vrfy_counter_us_to_ticks(const struct device *dev, uint64_t us) { K_OOPS(K_SYSCALL_OBJ(dev, K_OBJ_DRIVER_COUNTER)); @@ -56,16 +56,16 @@ static inline uint32_t z_vrfy_counter_us_to_ticks(const struct device *dev, #include static inline uint64_t z_vrfy_counter_ticks_to_us(const struct device *dev, - uint32_t ticks) + counter_ticks_t ticks) { K_OOPS(K_SYSCALL_OBJ(dev, K_OBJ_DRIVER_COUNTER)); return z_impl_counter_ticks_to_us((const struct device *)dev, - (uint32_t)ticks); + (counter_ticks_t)ticks); } #include static inline int z_vrfy_counter_get_value(const struct device *dev, - uint32_t *ticks) + counter_ticks_t *ticks) { K_OOPS(K_SYSCALL_DRIVER_COUNTER(dev, get_value)); K_OOPS(K_SYSCALL_MEMORY_WRITE(ticks, sizeof(*ticks))); @@ -73,15 +73,6 @@ static inline int z_vrfy_counter_get_value(const struct device *dev, } #include -static inline int z_vrfy_counter_get_value_64(const struct device *dev, - uint64_t *ticks) -{ - K_OOPS(K_SYSCALL_DRIVER_COUNTER(dev, get_value_64)); - K_OOPS(K_SYSCALL_MEMORY_WRITE(ticks, sizeof(*ticks))); - return z_impl_counter_get_value_64((const struct device *)dev, ticks); -} -#include - static inline int z_vrfy_counter_set_channel_alarm(const struct device *dev, uint8_t chan_id, const struct counter_alarm_cfg *alarm_cfg) @@ -124,21 +115,21 @@ static inline int z_vrfy_counter_set_top_value(const struct device *dev, } #include -static inline uint32_t z_vrfy_counter_get_top_value(const struct device *dev) +static inline counter_ticks_t z_vrfy_counter_get_top_value(const struct device *dev) { K_OOPS(K_SYSCALL_DRIVER_COUNTER(dev, get_top_value)); return z_impl_counter_get_top_value((const struct device *)dev); } #include -static inline uint32_t z_vrfy_counter_get_max_top_value(const struct device *dev) +static inline counter_ticks_t z_vrfy_counter_get_max_top_value(const struct device *dev) { K_OOPS(K_SYSCALL_OBJ(dev, K_OBJ_DRIVER_COUNTER)); return z_impl_counter_get_max_top_value((const struct device *)dev); } #include -static inline uint32_t z_vrfy_counter_get_guard_period(const struct device *dev, +static inline counter_ticks_t z_vrfy_counter_get_guard_period(const struct device *dev, uint32_t flags) { K_OOPS(K_SYSCALL_OBJ(dev, K_OBJ_DRIVER_COUNTER)); @@ -148,7 +139,7 @@ static inline uint32_t z_vrfy_counter_get_guard_period(const struct device *dev, #include static inline int z_vrfy_counter_set_guard_period(const struct device *dev, - uint32_t ticks, uint32_t flags) + counter_ticks_t ticks, uint32_t flags) { K_OOPS(K_SYSCALL_OBJ(dev, K_OBJ_DRIVER_COUNTER)); return z_impl_counter_set_guard_period((const struct device *)dev, diff --git a/drivers/counter/counter_ite_it8xxx2.c b/drivers/counter/counter_ite_it8xxx2.c index aaa871f9a8cf7..c22850cbb3147 100644 --- a/drivers/counter/counter_ite_it8xxx2.c +++ b/drivers/counter/counter_ite_it8xxx2.c @@ -282,8 +282,8 @@ static int counter_it8xxx2_init(const struct device *dev) uint8_t et7_ctrl = counter_it8xxx2_read8(dev, ET7CTRL); uint8_t et8_ctrl = counter_it8xxx2_read8(dev, ET8CTRL); - LOG_DBG("max top value = 0x%08x", config->info.max_top_value); - LOG_DBG("frequency = %d", config->info.freq); + LOG_DBG("max top value = 0x%08x", (uint32_t)config->info.max_top_value); + LOG_DBG("frequency = %llu", config->info.freq); LOG_DBG("channels = %d", config->info.channels); /* First time enable: enable and re-start timer -> disable timer */ diff --git a/drivers/counter/counter_ll_stm32_rtc.c b/drivers/counter/counter_ll_stm32_rtc.c index fe97936915efa..c0d4fe3677f41 100644 --- a/drivers/counter/counter_ll_stm32_rtc.c +++ b/drivers/counter/counter_ll_stm32_rtc.c @@ -97,12 +97,6 @@ LOG_MODULE_REGISTER(counter_rtc_stm32, CONFIG_COUNTER_LOG_LEVEL); /* Adjust the second sync prescaler to get 1Hz on ck_spre */ #define RTC_SYNCPRE ((RTCCLK_FREQ / (1 + RTC_ASYNCPRE)) - 1) -#ifndef CONFIG_COUNTER_RTC_STM32_SUBSECONDS -typedef uint32_t tick_t; -#else -typedef uint64_t tick_t; -#endif - struct rtc_stm32_config { struct counter_config_info counter_info; uint32_t async_prescaler; @@ -405,12 +399,12 @@ static int rtc_stm32_stop(const struct device *dev) } #if !defined(COUNTER_NO_DATE) -tick_t rtc_stm32_read(const struct device *dev) +counter_ticks_t rtc_stm32_read(const struct device *dev) { struct tm now = { 0 }; time_t ts; uint32_t rtc_date, rtc_time; - tick_t ticks; + counter_ticks_t ticks; #ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS uint32_t rtc_subseconds; #endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */ @@ -465,7 +459,7 @@ tick_t rtc_stm32_read(const struct device *dev) return ticks; } #else /* defined(COUNTER_NO_DATE) */ -tick_t rtc_stm32_read(const struct device *dev) +counter_ticks_t rtc_stm32_read(const struct device *dev) { uint32_t ticks; @@ -477,19 +471,11 @@ tick_t rtc_stm32_read(const struct device *dev) } #endif /* !defined(COUNTER_NO_DATE) */ -static int rtc_stm32_get_value(const struct device *dev, uint32_t *ticks) -{ - *ticks = (uint32_t)rtc_stm32_read(dev); - return 0; -} - -#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS -static int rtc_stm32_get_value_64(const struct device *dev, uint64_t *ticks) +static int rtc_stm32_get_value(const struct device *dev, counter_ticks_t *ticks) { *ticks = rtc_stm32_read(dev); return 0; } -#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */ #ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS static void rtc_stm32_set_int_pending(void) @@ -513,8 +499,8 @@ static int rtc_stm32_set_alarm(const struct device *dev, uint8_t chan_id, struct rtc_stm32_data *data = dev->data; int ret = 0; - tick_t now = rtc_stm32_read(dev); - tick_t ticks = alarm_cfg->ticks; + counter_ticks_t now = rtc_stm32_read(dev); + counter_ticks_t ticks = alarm_cfg->ticks; if (data->callback != NULL) { LOG_DBG("Alarm busy"); @@ -659,7 +645,7 @@ static uint32_t rtc_stm32_get_pending_int(const struct device *dev) } -static uint32_t rtc_stm32_get_top_value(const struct device *dev) +static counter_ticks_t rtc_stm32_get_top_value(const struct device *dev) { const struct counter_config_info *info = dev->config; @@ -836,7 +822,11 @@ static const struct stm32_pclken rtc_clk[] = STM32_DT_INST_CLOCKS(0); static const struct rtc_stm32_config rtc_config = { .counter_info = { +#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS + .max_top_value = UINT64_MAX, +#else .max_top_value = UINT32_MAX, +#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */ #ifndef CONFIG_COUNTER_RTC_STM32_SUBSECONDS /* freq = 1Hz for not subsec based driver */ .freq = RTCCLK_FREQ / ((RTC_ASYNCPRE + 1) * (RTC_SYNCPRE + 1)), @@ -895,9 +885,6 @@ static DEVICE_API(counter, rtc_stm32_driver_api) = { .start = rtc_stm32_start, .stop = rtc_stm32_stop, .get_value = rtc_stm32_get_value, -#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS - .get_value_64 = rtc_stm32_get_value_64, -#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */ .set_alarm = rtc_stm32_set_alarm, .cancel_alarm = rtc_stm32_cancel_alarm, .set_top_value = rtc_stm32_set_top_value, diff --git a/drivers/counter/counter_ll_stm32_timer.c b/drivers/counter/counter_ll_stm32_timer.c index 72f7f26322da4..22824bafe1d6a 100644 --- a/drivers/counter/counter_ll_stm32_timer.c +++ b/drivers/counter/counter_ll_stm32_timer.c @@ -89,7 +89,7 @@ static void(*const clear_it_flag[TIMER_MAX_CH])(TIM_TypeDef *) = { struct counter_stm32_data { counter_top_callback_t top_cb; void *top_user_data; - uint32_t guard_period; + counter_ticks_t guard_period; atomic_t cc_int_pending; uint32_t freq; }; @@ -136,11 +136,11 @@ static int counter_stm32_stop(const struct device *dev) return 0; } -static uint32_t counter_stm32_get_top_value(const struct device *dev) +static counter_ticks_t counter_stm32_get_top_value(const struct device *dev) { const struct counter_stm32_config *config = dev->config; - return LL_TIM_GetAutoReload(config->timer); + return (counter_ticks_t)LL_TIM_GetAutoReload(config->timer); } static uint32_t counter_stm32_read(const struct device *dev) @@ -150,9 +150,9 @@ static uint32_t counter_stm32_read(const struct device *dev) return LL_TIM_GetCounter(config->timer); } -static int counter_stm32_get_value(const struct device *dev, uint32_t *ticks) +static int counter_stm32_get_value(const struct device *dev, counter_ticks_t *ticks) { - *ticks = counter_stm32_read(dev); + *ticks = (counter_ticks_t)counter_stm32_read(dev); return 0; } @@ -437,7 +437,7 @@ static int counter_stm32_init_timer(const struct device *dev) return 0; } -static uint32_t counter_stm32_get_guard_period(const struct device *dev, uint32_t flags) +static counter_ticks_t counter_stm32_get_guard_period(const struct device *dev, uint32_t flags) { struct counter_stm32_data *data = dev->data; @@ -445,7 +445,7 @@ static uint32_t counter_stm32_get_guard_period(const struct device *dev, uint32_ return data->guard_period; } -static int counter_stm32_set_guard_period(const struct device *dev, uint32_t guard, +static int counter_stm32_set_guard_period(const struct device *dev, counter_ticks_t guard, uint32_t flags) { struct counter_stm32_data *data = dev->data; @@ -464,6 +464,26 @@ static uint32_t counter_stm32_get_freq(const struct device *dev) return data->freq; } +static int counter_stm32_reset_timer(const struct device *dev) +{ + const struct counter_stm32_config *config = dev->config; + TIM_TypeDef *timer = config->timer; + + LL_TIM_SetCounter(timer, 0); + + return 0; +} + +static int counter_stm32_set_value(const struct device *dev, counter_ticks_t ticks) +{ + const struct counter_stm32_config *config = dev->config; + TIM_TypeDef *timer = config->timer; + + LL_TIM_SetCounter(timer, (uint32_t)ticks); + + return 0; +} + static void counter_stm32_top_irq_handle(const struct device *dev) { struct counter_stm32_data *data = dev->data; @@ -510,6 +530,8 @@ static DEVICE_API(counter, counter_stm32_driver_api) = { .get_guard_period = counter_stm32_get_guard_period, .set_guard_period = counter_stm32_set_guard_period, .get_freq = counter_stm32_get_freq, + .reset = counter_stm32_reset_timer, + .set_value = counter_stm32_set_value, }; #define TIM_IRQ_HANDLE_CC(timx, cc) \ diff --git a/drivers/counter/counter_mcux_gpt.c b/drivers/counter/counter_mcux_gpt.c index c0e714ea41dc1..81ec115fd51cd 100644 --- a/drivers/counter/counter_mcux_gpt.c +++ b/drivers/counter/counter_mcux_gpt.c @@ -199,7 +199,7 @@ static int mcux_gpt_init(const struct device *dev) /* Adjust divider to match expected freq */ if (clock_freq % config->info.freq) { - LOG_ERR("Cannot Adjust GPT freq to %u\n", config->info.freq); + LOG_ERR("Cannot Adjust GPT freq to %llu\n", config->info.freq); LOG_ERR("clock src is %u\n", clock_freq); return -EINVAL; } diff --git a/drivers/counter/counter_xlnx_axi_timer.c b/drivers/counter/counter_xlnx_axi_timer.c index 9cb837ec73498..1a8c1e3f9db39 100644 --- a/drivers/counter/counter_xlnx_axi_timer.c +++ b/drivers/counter/counter_xlnx_axi_timer.c @@ -294,7 +294,7 @@ static int xlnx_axi_timer_init(const struct device *dev) const struct xlnx_axi_timer_config *config = dev->config; LOG_DBG("max top value = 0x%08x", config->info.max_top_value); - LOG_DBG("frequency = %d", config->info.freq); + LOG_DBG("frequency = %llu", config->info.freq); LOG_DBG("channels = %d", config->info.channels); xlnx_axi_timer_write32(dev, config->info.max_top_value, TLR0_OFFSET); diff --git a/drivers/sensor/sensor_clock_external.c b/drivers/sensor/sensor_clock_external.c index 5baf1ac156398..2c7b6a59f66fc 100644 --- a/drivers/sensor/sensor_clock_external.c +++ b/drivers/sensor/sensor_clock_external.c @@ -40,17 +40,10 @@ int sensor_clock_get_cycles(uint64_t *cycles) __ASSERT_NO_MSG(counter_is_counting_up(external_sensor_clock)); int rc; - const struct counter_driver_api *api = - (const struct counter_driver_api *)external_sensor_clock->api; + counter_ticks_t result; - if (api->get_value_64) { - rc = counter_get_value_64(external_sensor_clock, cycles); - } else { - uint32_t result_32; - - rc = counter_get_value(external_sensor_clock, &result_32); - *cycles = (uint64_t)result_32; - } + rc = counter_get_value(external_sensor_clock, &result); + *cycles = (uint64_t)result; return rc; } diff --git a/drivers/timer/cortex_m_systick.c b/drivers/timer/cortex_m_systick.c index e2563ddd9d761..77bead5fc8812 100644 --- a/drivers/timer/cortex_m_systick.c +++ b/drivers/timer/cortex_m_systick.c @@ -107,7 +107,7 @@ static cycle_t cycle_pre_idle; #if defined(CONFIG_CORTEX_M_SYSTICK_LPM_TIMER_COUNTER) /* Idle timer value before entering the idle state. */ -static uint32_t idle_timer_pre_idle; +static counter_ticks_t idle_timer_pre_idle; /* Idle timer used for timer while entering the idle state */ static const struct device *idle_timer = DEVICE_DT_GET(DT_CHOSEN(zephyr_cortex_m_idle_timer)); @@ -151,7 +151,7 @@ uint64_t z_cms_lptim_hook_on_lpm_exit(void) /** * Calculate how much time elapsed according to counter. */ - uint32_t idle_timer_post, idle_timer_diff; + counter_ticks_t idle_timer_post, idle_timer_diff; counter_get_value(idle_timer, &idle_timer_post); diff --git a/drivers/timer/stm32_lptim_timer.c b/drivers/timer/stm32_lptim_timer.c index 23fbfcdab7b59..80c7d4e3997b8 100644 --- a/drivers/timer/stm32_lptim_timer.c +++ b/drivers/timer/stm32_lptim_timer.c @@ -91,7 +91,7 @@ static bool timeout_stdby; static cycle_t lptim_cnt_pre_stdby; /* Standby timer value before entering the standby state. */ -static uint32_t stdby_timer_pre_stdby; +static counter_ticks_t stdby_timer_pre_stdby; /* Standby timer used for timer while entering the standby state */ static const struct device *stdby_timer = DEVICE_DT_GET(DT_CHOSEN(st_lptim_stdby_timer)); @@ -592,7 +592,7 @@ void sys_clock_idle_exit(void) #ifdef CONFIG_STM32_LPTIM_STDBY_TIMER if (timeout_stdby) { cycle_t missed_lptim_cnt; - uint32_t stdby_timer_diff, stdby_timer_post, dticks; + counter_ticks_t stdby_timer_diff, stdby_timer_post, dticks; uint64_t stdby_timer_us; /* Get current value for standby timer and reset LPTIM counter value diff --git a/include/zephyr/drivers/counter.h b/include/zephyr/drivers/counter.h index f8d19bc69dafa..1fcab1fc34c0a 100644 --- a/include/zephyr/drivers/counter.h +++ b/include/zephyr/drivers/counter.h @@ -35,6 +35,18 @@ extern "C" { #endif +/** + * @brief Define the typedef to be 32b or 64b for ticks + * + * It is still okay for a driver or application to still use `uint32_t` or + * `uint64_t` if it is to always expected to be that. + */ +#ifdef CONFIG_COUNTER_64BITS +typedef uint64_t counter_ticks_t; +#else +typedef uint32_t counter_ticks_t; +#endif + /** * @anchor COUNTER_FLAGS * @name Counter device capabilities @@ -93,7 +105,7 @@ extern "C" { * * Alarm callback must be called from the same context as if it was set on time. */ -#define COUNTER_ALARM_CFG_EXPIRE_WHEN_LATE BIT(1) +#define COUNTER_ALARM_CFG_EXPIRE_WHEN_LATE BIT(1) /**@} */ @@ -120,17 +132,12 @@ extern "C" { * @param ticks Counter value that triggered the alarm. * @param user_data User data. */ -typedef void (*counter_alarm_callback_t)(const struct device *dev, - uint8_t chan_id, uint32_t ticks, - void *user_data); +typedef void (*counter_alarm_callback_t)(const struct device *dev, uint8_t chan_id, + counter_ticks_t ticks, void *user_data); /** @brief Alarm callback structure. */ struct counter_alarm_cfg { - /** - * Callback called on alarm (cannot be NULL). - */ - counter_alarm_callback_t callback; /** * Number of ticks that triggers the alarm. * @@ -147,7 +154,11 @@ struct counter_alarm_cfg { * counter_ticks_to_us). Alternatively, the counter implementation may * count asynchronous events. */ - uint32_t ticks; + counter_ticks_t ticks; + /** + * Callback called on alarm (cannot be NULL). + */ + counter_alarm_callback_t callback; /** * User data returned in callback. */ @@ -163,8 +174,7 @@ struct counter_alarm_cfg { * @param dev Pointer to the device structure for the driver instance. * @param user_data User data provided in @ref counter_set_top_value. */ -typedef void (*counter_top_callback_t)(const struct device *dev, - void *user_data); +typedef void (*counter_top_callback_t)(const struct device *dev, void *user_data); /** @brief Top value configuration structure. */ @@ -172,7 +182,7 @@ struct counter_top_cfg { /** * Top value. */ - uint32_t ticks; + counter_ticks_t ticks; /** * Callback function (can be NULL). */ @@ -191,13 +201,13 @@ struct counter_top_cfg { */ struct counter_config_info { /** - * Maximal (default) top value on which counter is reset (cleared or reloaded). + * Frequency of the source clock if synchronous events are counted. */ - uint32_t max_top_value; + uint64_t freq; /** - * Frequency of the source clock if synchronous events are counted. + * Maximal (default) top value on which counter is reset (cleared or reloaded). */ - uint32_t freq; + counter_ticks_t max_top_value; /** * Flags (see @ref COUNTER_FLAGS). */ @@ -212,33 +222,28 @@ struct counter_config_info { typedef int (*counter_api_start)(const struct device *dev); typedef int (*counter_api_stop)(const struct device *dev); -typedef int (*counter_api_get_value)(const struct device *dev, - uint32_t *ticks); -typedef int (*counter_api_get_value_64)(const struct device *dev, - uint64_t *ticks); +typedef int (*counter_api_get_value)(const struct device *dev, counter_ticks_t *ticks); typedef int (*counter_api_reset)(const struct device *dev); -typedef int (*counter_api_set_alarm)(const struct device *dev, - uint8_t chan_id, +typedef int (*counter_api_set_value)(const struct device *dev, counter_ticks_t ticks); +typedef int (*counter_api_set_alarm)(const struct device *dev, uint8_t chan_id, const struct counter_alarm_cfg *alarm_cfg); -typedef int (*counter_api_cancel_alarm)(const struct device *dev, - uint8_t chan_id); +typedef int (*counter_api_cancel_alarm)(const struct device *dev, uint8_t chan_id); typedef int (*counter_api_set_top_value)(const struct device *dev, const struct counter_top_cfg *cfg); typedef uint32_t (*counter_api_get_pending_int)(const struct device *dev); -typedef uint32_t (*counter_api_get_top_value)(const struct device *dev); -typedef uint32_t (*counter_api_get_guard_period)(const struct device *dev, - uint32_t flags); -typedef int (*counter_api_set_guard_period)(const struct device *dev, - uint32_t ticks, - uint32_t flags); +typedef counter_ticks_t (*counter_api_get_top_value)(const struct device *dev); +typedef counter_ticks_t (*counter_api_get_guard_period)(const struct device *dev, uint32_t flags); +typedef int (*counter_api_set_guard_period)(const struct device *dev, counter_ticks_t ticks, + uint32_t flags); typedef uint32_t (*counter_api_get_freq)(const struct device *dev); +typedef uint64_t (*counter_api_get_freq_64)(const struct device *dev); __subsystem struct counter_driver_api { counter_api_start start; counter_api_stop stop; counter_api_get_value get_value; - counter_api_get_value_64 get_value_64; counter_api_reset reset; + counter_api_set_value set_value; counter_api_set_alarm set_alarm; counter_api_cancel_alarm cancel_alarm; counter_api_set_top_value set_top_value; @@ -247,6 +252,7 @@ __subsystem struct counter_driver_api { counter_api_get_guard_period get_guard_period; counter_api_set_guard_period set_guard_period; counter_api_get_freq get_freq; + counter_api_get_freq_64 get_freq_64; }; /** @@ -261,8 +267,7 @@ __syscall bool counter_is_counting_up(const struct device *dev); static inline bool z_impl_counter_is_counting_up(const struct device *dev) { - const struct counter_config_info *config = - (const struct counter_config_info *)dev->config; + const struct counter_config_info *config = (const struct counter_config_info *)dev->config; return config->flags & COUNTER_CONFIG_INFO_COUNT_UP; } @@ -278,8 +283,7 @@ __syscall uint8_t counter_get_num_of_channels(const struct device *dev); static inline uint8_t z_impl_counter_get_num_of_channels(const struct device *dev) { - const struct counter_config_info *config = - (const struct counter_config_info *)dev->config; + const struct counter_config_info *config = (const struct counter_config_info *)dev->config; return config->channels; } @@ -290,36 +294,46 @@ static inline uint8_t z_impl_counter_get_num_of_channels(const struct device *de * @param[in] dev Pointer to the device structure for the driver instance. * * @return Frequency of the counter in Hz, or zero if the counter does - * not have a fixed frequency. + * not have a fixed frequency, or UINT32_MAX if the counter frequency + * is higher or equal to UINT32_MAX, in which case it is recommended to + * use counter_get_frequency_64(). */ __syscall uint32_t counter_get_frequency(const struct device *dev); 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; + 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 api->get_freq ? api->get_freq(dev) : config->freq; + if (api->get_freq) { + return api->get_freq(dev); + } else { + return config->freq > UINT32_MAX ? UINT32_MAX : (uint32_t)config->freq; + } } /** - * @brief Function to convert microseconds to ticks. + * @brief Function to get counter frequency in 64bits. * * @param[in] dev Pointer to the device structure for the driver instance. - * @param[in] us Microseconds. * - * @return Converted ticks. Ticks will be saturated if exceed 32 bits. + * @return Frequency of the counter in Hz, or zero if the counter does + * not have a fixed frequency. */ -__syscall uint32_t counter_us_to_ticks(const struct device *dev, uint64_t us); +__syscall uint64_t counter_get_frequency_64(const struct device *dev); -static inline uint32_t z_impl_counter_us_to_ticks(const struct device *dev, - uint64_t us) +static inline uint64_t z_impl_counter_get_frequency_64(const struct device *dev) { - uint64_t ticks = (us * z_impl_counter_get_frequency(dev)) / USEC_PER_SEC; - - return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks; + 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; + + if (api->get_freq_64) { + return api->get_freq_64(dev); + } else if (api->get_freq) { + return (uint64_t)api->get_freq(dev); + } else { + return config->freq; + } } /** @@ -330,14 +344,73 @@ static inline uint32_t z_impl_counter_us_to_ticks(const struct device *dev, * * @return Converted microseconds. */ -__syscall uint64_t counter_ticks_to_us(const struct device *dev, uint32_t ticks); +__syscall uint64_t counter_ticks_to_us(const struct device *dev, counter_ticks_t ticks); + +static inline uint64_t z_impl_counter_ticks_to_us(const struct device *dev, counter_ticks_t ticks) +{ + return ((uint64_t)ticks * USEC_PER_SEC) / z_impl_counter_get_frequency_64(dev); +} + +/** + * @brief Function to convert ticks to nanoseconds. + * + * @param[in] dev Pointer to the device structure for the driver instance. + * @param[in] ticks Ticks. + * + * @return Converted nanoseconds. + */ +__syscall uint64_t counter_ticks_to_ns(const struct device *dev, counter_ticks_t ticks); + +static inline uint64_t z_impl_counter_ticks_to_ns(const struct device *dev, counter_ticks_t ticks) +{ + return ((uint64_t)ticks * NSEC_PER_SEC) / z_impl_counter_get_frequency_64(dev); +} + +/** + * @brief Function to convert microseconds to ticks. + * + * @param[in] dev Pointer to the device structure for the driver instance. + * @param[in] us Microseconds. + * + * @return Converted ticks. Ticks will be saturated if exceed 32 bits. + */ +__syscall counter_ticks_t counter_us_to_ticks(const struct device *dev, uint64_t us); + +/** + * @brief Function to convert nanoseconds to ticks. + * + * @param[in] dev Pointer to the device structure for the driver instance. + * @param[in] ns Nanoseconds. + * + * @return Converted ticks. Ticks will be saturated if exceed 32 bits. + */ +__syscall counter_ticks_t counter_ns_to_ticks(const struct device *dev, uint64_t ns); + +#ifndef CONFIG_COUNTER_64BITS +static inline counter_ticks_t z_impl_counter_us_to_ticks(const struct device *dev, uint64_t us) +{ + uint64_t ticks = (us * z_impl_counter_get_frequency_64(dev)) / USEC_PER_SEC; + + return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks; +} + +static inline counter_ticks_t z_impl_counter_ns_to_ticks(const struct device *dev, uint64_t ns) +{ + uint64_t ticks = (ns * z_impl_counter_get_frequency_64(dev)) / NSEC_PER_SEC; -static inline uint64_t z_impl_counter_ticks_to_us(const struct device *dev, - uint32_t ticks) + return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks; +} +#else +static inline counter_ticks_t z_impl_counter_us_to_ticks(const struct device *dev, uint64_t us) { - return ((uint64_t)ticks * USEC_PER_SEC) / z_impl_counter_get_frequency(dev); + return (us * z_impl_counter_get_frequency_64(dev)) / USEC_PER_SEC; } +static inline counter_ticks_t z_impl_counter_ns_to_ticks(const struct device *dev, uint64_t ns) +{ + return (ns * z_impl_counter_get_frequency_64(dev)) / NSEC_PER_SEC; +} +#endif /** * @brief Function to retrieve maximum top value that can be set. * @@ -345,12 +418,11 @@ static inline uint64_t z_impl_counter_ticks_to_us(const struct device *dev, * * @return Max top value. */ -__syscall uint32_t counter_get_max_top_value(const struct device *dev); +__syscall counter_ticks_t counter_get_max_top_value(const struct device *dev); -static inline uint32_t z_impl_counter_get_max_top_value(const struct device *dev) +static inline counter_ticks_t z_impl_counter_get_max_top_value(const struct device *dev) { - const struct counter_config_info *config = - (const struct counter_config_info *)dev->config; + const struct counter_config_info *config = (const struct counter_config_info *)dev->config; return config->max_top_value; } @@ -367,8 +439,7 @@ __syscall int counter_start(const struct device *dev); static inline int z_impl_counter_start(const struct device *dev) { - const struct counter_driver_api *api = - (struct counter_driver_api *)dev->api; + const struct counter_driver_api *api = (struct counter_driver_api *)dev->api; return api->start(dev); } @@ -386,8 +457,7 @@ __syscall int counter_stop(const struct device *dev); static inline int z_impl_counter_stop(const struct device *dev) { - const struct counter_driver_api *api = - (struct counter_driver_api *)dev->api; + const struct counter_driver_api *api = (struct counter_driver_api *)dev->api; return api->stop(dev); } @@ -400,59 +470,54 @@ static inline int z_impl_counter_stop(const struct device *dev) * @retval 0 If successful. * @retval Negative error code on failure getting the counter value */ -__syscall int counter_get_value(const struct device *dev, uint32_t *ticks); +__syscall int counter_get_value(const struct device *dev, counter_ticks_t *ticks); -static inline int z_impl_counter_get_value(const struct device *dev, - uint32_t *ticks) +static inline int z_impl_counter_get_value(const struct device *dev, counter_ticks_t *ticks) { - const struct counter_driver_api *api = - (struct counter_driver_api *)dev->api; + const struct counter_driver_api *api = (struct counter_driver_api *)dev->api; return api->get_value(dev, ticks); } /** - * @brief Get current counter 64-bit value. + * @brief Reset the counter to the initial value. * @param dev Pointer to the device structure for the driver instance. - * @param ticks Pointer to where to store the current counter value * * @retval 0 If successful. - * @retval Negative error code on failure getting the counter value + * @retval -errno Negative error code on failure resetting the counter value. */ -__syscall int counter_get_value_64(const struct device *dev, uint64_t *ticks); +__syscall int counter_reset(const struct device *dev); -static inline int z_impl_counter_get_value_64(const struct device *dev, - uint64_t *ticks) +static inline int z_impl_counter_reset(const struct device *dev) { - const struct counter_driver_api *api = - (struct counter_driver_api *)dev->api; + const struct counter_driver_api *api = (struct counter_driver_api *)dev->api; - if (!api->get_value_64) { + if (!api->reset) { return -ENOSYS; } - return api->get_value_64(dev, ticks); + return api->reset(dev); } /** - * @brief Reset the counter to the initial value. + * @brief Set current counter value. * @param dev Pointer to the device structure for the driver instance. + * @param ticks Tick value to set * * @retval 0 If successful. - * @retval -errno Negative error code on failure resetting the counter value. + * @retval Negative error code on failure setting the counter value */ -__syscall int counter_reset(const struct device *dev); +__syscall int counter_set_value(const struct device *dev, counter_ticks_t ticks); -static inline int z_impl_counter_reset(const struct device *dev) +static inline int z_impl_counter_set_value(const struct device *dev, counter_ticks_t ticks) { - const struct counter_driver_api *api = - (struct counter_driver_api *)dev->api; + const struct counter_driver_api *api = (struct counter_driver_api *)dev->api; - if (!api->reset) { + if (!api->set_value) { return -ENOSYS; } - return api->reset(dev); + return api->set_value(dev, ticks); } /** @@ -475,16 +540,13 @@ static inline int z_impl_counter_reset(const struct device *dev) * @retval -ETIME if absolute alarm was set too late. * @retval -EBUSY if alarm is already active. */ -__syscall int counter_set_channel_alarm(const struct device *dev, - uint8_t chan_id, +__syscall int counter_set_channel_alarm(const struct device *dev, uint8_t chan_id, const struct counter_alarm_cfg *alarm_cfg); -static inline int z_impl_counter_set_channel_alarm(const struct device *dev, - uint8_t chan_id, +static inline int z_impl_counter_set_channel_alarm(const struct device *dev, uint8_t chan_id, const struct counter_alarm_cfg *alarm_cfg) { - const struct counter_driver_api *api = - (struct counter_driver_api *)dev->api; + const struct counter_driver_api *api = (struct counter_driver_api *)dev->api; if (chan_id >= counter_get_num_of_channels(dev)) { return -ENOTSUP; @@ -505,14 +567,11 @@ static inline int z_impl_counter_set_channel_alarm(const struct device *dev, * @retval -ENOTSUP if request is not supported or the counter was not started * yet. */ -__syscall int counter_cancel_channel_alarm(const struct device *dev, - uint8_t chan_id); +__syscall int counter_cancel_channel_alarm(const struct device *dev, uint8_t chan_id); -static inline int z_impl_counter_cancel_channel_alarm(const struct device *dev, - uint8_t chan_id) +static inline int z_impl_counter_cancel_channel_alarm(const struct device *dev, uint8_t chan_id) { - const struct counter_driver_api *api = - (struct counter_driver_api *)dev->api; + const struct counter_driver_api *api = (struct counter_driver_api *)dev->api; if (chan_id >= counter_get_num_of_channels(dev)) { return -ENOTSUP; @@ -545,15 +604,12 @@ static inline int z_impl_counter_cancel_channel_alarm(const struct device *dev, * @retval -ETIME if @ref COUNTER_TOP_CFG_DONT_RESET was set and new top value * is smaller than current counter value (counter counting up). */ -__syscall int counter_set_top_value(const struct device *dev, - const struct counter_top_cfg *cfg); +__syscall int counter_set_top_value(const struct device *dev, const struct counter_top_cfg *cfg); static inline int z_impl_counter_set_top_value(const struct device *dev, - const struct counter_top_cfg - *cfg) + const struct counter_top_cfg *cfg) { - const struct counter_driver_api *api = - (struct counter_driver_api *)dev->api; + const struct counter_driver_api *api = (struct counter_driver_api *)dev->api; if (cfg->ticks > counter_get_max_top_value(dev)) { return -EINVAL; @@ -579,8 +635,7 @@ __syscall int counter_get_pending_int(const struct device *dev); static inline int z_impl_counter_get_pending_int(const struct device *dev) { - const struct counter_driver_api *api = - (struct counter_driver_api *)dev->api; + const struct counter_driver_api *api = (struct counter_driver_api *)dev->api; return api->get_pending_int(dev); } @@ -592,12 +647,11 @@ static inline int z_impl_counter_get_pending_int(const struct device *dev) * * @return Top value. */ -__syscall uint32_t counter_get_top_value(const struct device *dev); +__syscall counter_ticks_t counter_get_top_value(const struct device *dev); -static inline uint32_t z_impl_counter_get_top_value(const struct device *dev) +static inline counter_ticks_t z_impl_counter_get_top_value(const struct device *dev) { - const struct counter_driver_api *api = - (struct counter_driver_api *)dev->api; + const struct counter_driver_api *api = (struct counter_driver_api *)dev->api; return api->get_top_value(dev); } @@ -654,15 +708,13 @@ static inline uint32_t z_impl_counter_get_top_value(const struct device *dev) * @retval -ENOSYS if function or flags are not supported. * @retval -EINVAL if ticks value is invalid. */ -__syscall int counter_set_guard_period(const struct device *dev, - uint32_t ticks, - uint32_t flags); +__syscall int counter_set_guard_period(const struct device *dev, counter_ticks_t ticks, + uint32_t flags); -static inline int z_impl_counter_set_guard_period(const struct device *dev, - uint32_t ticks, uint32_t flags) +static inline int z_impl_counter_set_guard_period(const struct device *dev, counter_ticks_t ticks, + uint32_t flags) { - const struct counter_driver_api *api = - (struct counter_driver_api *)dev->api; + const struct counter_driver_api *api = (struct counter_driver_api *)dev->api; if (!api->set_guard_period) { return -ENOSYS; @@ -682,14 +734,12 @@ static inline int z_impl_counter_set_guard_period(const struct device *dev, * @return Guard period given in counter ticks or 0 if function or flags are * not supported. */ -__syscall uint32_t counter_get_guard_period(const struct device *dev, - uint32_t flags); +__syscall counter_ticks_t counter_get_guard_period(const struct device *dev, uint32_t flags); -static inline uint32_t z_impl_counter_get_guard_period(const struct device *dev, - uint32_t flags) +static inline counter_ticks_t z_impl_counter_get_guard_period(const struct device *dev, + uint32_t flags) { - const struct counter_driver_api *api = - (struct counter_driver_api *)dev->api; + const struct counter_driver_api *api = (struct counter_driver_api *)dev->api; return (api->get_guard_period) ? api->get_guard_period(dev, flags) : 0; } diff --git a/samples/drivers/counter/alarm/src/main.c b/samples/drivers/counter/alarm/src/main.c index bffa3fdd9a4f2..25d94eea09280 100644 --- a/samples/drivers/counter/alarm/src/main.c +++ b/samples/drivers/counter/alarm/src/main.c @@ -78,12 +78,18 @@ struct counter_alarm_cfg alarm_cfg; #error Unable to find a counter device node in devicetree #endif +#ifdef CONFIG_COUNTER_64BITS +#define COUNTER_TICKS_FMT "%llu" +#else +#define COUNTER_TICKS_FMT "%u" +#endif + static void test_counter_interrupt_fn(const struct device *counter_dev, - uint8_t chan_id, uint32_t ticks, + uint8_t chan_id, counter_ticks_t ticks, void *user_data) { struct counter_alarm_cfg *config = user_data; - uint32_t now_ticks; + counter_ticks_t now_ticks; uint64_t now_usec; int now_sec; int err; @@ -107,7 +113,7 @@ static void test_counter_interrupt_fn(const struct device *counter_dev, /* Set a new alarm with a double length duration */ config->ticks = config->ticks * 2U; - printk("Set alarm in %u sec (%u ticks)\n", + printk("Set alarm in %u sec (" COUNTER_TICKS_FMT " ticks)\n", (uint32_t)(counter_ticks_to_us(counter_dev, config->ticks) / USEC_PER_SEC), config->ticks); @@ -140,7 +146,7 @@ int main(void) err = counter_set_channel_alarm(counter_dev, ALARM_CHANNEL_ID, &alarm_cfg); - printk("Set alarm in %u sec (%u ticks)\n", + printk("Set alarm in %u sec (" COUNTER_TICKS_FMT " ticks)\n", (uint32_t)(counter_ticks_to_us(counter_dev, alarm_cfg.ticks) / USEC_PER_SEC), alarm_cfg.ticks); diff --git a/submanifests/optional.yaml b/submanifests/optional.yaml index 04accf4152c01..7e618855ecf93 100644 --- a/submanifests/optional.yaml +++ b/submanifests/optional.yaml @@ -29,7 +29,7 @@ manifest: groups: - optional - name: sof - revision: ba8de7551f88a4f8d4533791274fa85b37ec332e + revision: 56c9cb8e936302955ad05412b7bc266cf4b74078 path: modules/audio/sof remote: upstream groups: diff --git a/tests/boards/espressif/interrupt/src/main.c b/tests/boards/espressif/interrupt/src/main.c index 5876397f3d83c..5cdc188360fff 100644 --- a/tests/boards/espressif/interrupt/src/main.c +++ b/tests/boards/espressif/interrupt/src/main.c @@ -44,7 +44,7 @@ static int token[3]; static int success_cnt; static int error_cnt; -static void alarm_handler_c(const struct device *dev, uint8_t chan_id, uint32_t counter, +static void alarm_handler_c(const struct device *dev, uint8_t chan_id, counter_ticks_t counter, void *user_data) { if (dev == timer2 && token[0] == TOKEN_A && token[1] == TOKEN_B && token[2] == 0) { @@ -54,7 +54,7 @@ static void alarm_handler_c(const struct device *dev, uint8_t chan_id, uint32_t k_busy_wait(ISR_DELAY_US); } -static void alarm_handler_b(const struct device *dev, uint8_t chan_id, uint32_t counter, +static void alarm_handler_b(const struct device *dev, uint8_t chan_id, counter_ticks_t counter, void *user_data) { if (dev == timer1 && token[0] == TOKEN_A && token[1] == 0 && token[2] == 0) { @@ -64,7 +64,7 @@ static void alarm_handler_b(const struct device *dev, uint8_t chan_id, uint32_t k_busy_wait(ISR_DELAY_US); } -static void alarm_handler_a(const struct device *dev, uint8_t chan_id, uint32_t counter, +static void alarm_handler_a(const struct device *dev, uint8_t chan_id, counter_ticks_t counter, void *user_data) { if (dev == timer0 && token[0] == 0 && token[1] == 0 && token[2] == 0) { diff --git a/tests/drivers/counter/counter_basic_api/src/test_counter.c b/tests/drivers/counter/counter_basic_api/src/test_counter.c index 841853d3b7f0f..1bab0dd7629d0 100644 --- a/tests/drivers/counter/counter_basic_api/src/test_counter.c +++ b/tests/drivers/counter/counter_basic_api/src/test_counter.c @@ -11,6 +11,12 @@ #include LOG_MODULE_REGISTER(test); +#ifdef CONFIG_COUNTER_64BITS +#define COUNTER_TICKS_FMT "%llu" +#else +#define COUNTER_TICKS_FMT "%u" +#endif + static struct k_sem top_cnt_sem; static volatile uint32_t top_cnt; static struct k_sem alarm_cnt_sem; @@ -282,8 +288,8 @@ static void top_handler(const struct device *dev, void *user_data) static void test_set_top_value_with_alarm_instance(const struct device *dev) { int err; - uint32_t cnt; - uint32_t top_value; + counter_ticks_t cnt; + counter_ticks_t top_value; uint32_t counter_period_us; uint32_t top_handler_cnt; struct counter_top_cfg top_cfg = { @@ -335,8 +341,8 @@ ZTEST(counter_basic, test_set_top_value_with_alarm) static void test_set_top_value_without_alarm_instance(const struct device *dev) { int err; - uint32_t cnt; - uint32_t top_value; + counter_ticks_t cnt; + counter_ticks_t top_value; uint32_t counter_period_us; struct counter_top_cfg top_cfg = { .callback = NULL, @@ -378,17 +384,17 @@ ZTEST_USER(counter_no_callback, test_set_top_value_without_alarm) } static void alarm_handler(const struct device *dev, uint8_t chan_id, - uint32_t counter, + counter_ticks_t counter, void *user_data) { /* Arbitrary limit for alarm processing - time between hw expiration * and read-out from counter in the handler. */ static const uint64_t processing_limit_us = 1000; - uint32_t now; + counter_ticks_t now; int err; - uint32_t top; - uint32_t diff; + counter_ticks_t top; + counter_ticks_t diff; err = counter_get_value(dev, &now); zassert_true(err == 0, "%s: Counter read failed (err: %d)", @@ -404,10 +410,10 @@ static void alarm_handler(const struct device *dev, uint8_t chan_id, } zassert_true(diff <= counter_us_to_ticks(dev, processing_limit_us), - "Unexpected distance between reported alarm value(%u) " - "and actual counter value (%u), top:%d (processing " - "time limit (%d us) might be exceeded?", - counter, now, top, (int)processing_limit_us); + "Unexpected distance between reported alarm value(" COUNTER_TICKS_FMT ") " + "and actual counter value (" COUNTER_TICKS_FMT "), top:" COUNTER_TICKS_FMT + " (processing time limit (%d us) might be exceeded?", + counter, now, top, (int)processing_limit_us); if (user_data) { zassert_true(&cntr_alarm_cfg == user_data, @@ -539,7 +545,7 @@ ZTEST(counter_basic, test_single_shot_alarm_top) static void *clbk_data[10]; static void alarm_handler2(const struct device *dev, uint8_t chan_id, - uint32_t counter, + counter_ticks_t counter, void *user_data) { if (IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS)) { @@ -714,10 +720,10 @@ ZTEST(counter_basic, test_all_channels) static void test_valid_function_without_alarm(const struct device *dev) { int err; - uint32_t ticks; - uint32_t ticks_expected; - uint32_t tick_current; - uint32_t ticks_tol; + counter_ticks_t ticks; + counter_ticks_t ticks_expected; + counter_ticks_t tick_current; + counter_ticks_t ticks_tol; uint32_t wait_for_us; uint32_t freq = counter_get_frequency(dev);