This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit d419fd3af508bc1b03254abf4709647caceb1d02 Author: yangsong8 <[email protected]> AuthorDate: Fri Sep 4 16:55:45 2026 +0800 drivers/timers: add i.MX GPT ICU lower-half driver Extend the i.MX GPT driver with the Input Capture Unit lower-half. The shared core ISR now also dispatches edge-capture events, and the ICU lower-half registers each channel as /dev/capN, deriving frequency, duty cycle and edge count from the captured counter values and plugging into the generic capture upper-half. The channel count is configurable via TIMER_IMX_GPT_NUM_ICU (range 1-2, default 1). Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: yangsong8 <[email protected]> --- drivers/timers/CMakeLists.txt | 3 + drivers/timers/Kconfig | 18 ++ drivers/timers/Make.defs | 3 + drivers/timers/capture_imx_gpt.c | 380 +++++++++++++++++++++++++++++++++++++++ drivers/timers/imx_gpt.c | 40 ++++- include/nuttx/timers/imx_gpt.h | 51 ++++++ 6 files changed, 492 insertions(+), 3 deletions(-) diff --git a/drivers/timers/CMakeLists.txt b/drivers/timers/CMakeLists.txt index ad4284aeb64..c9096ca79d1 100644 --- a/drivers/timers/CMakeLists.txt +++ b/drivers/timers/CMakeLists.txt @@ -133,6 +133,9 @@ if(CONFIG_TIMER_IMX_GPT) if(CONFIG_TIMER_IMX_GPT_OCU) list(APPEND SRCS ocu_imx_gpt.c) endif() + if(CONFIG_TIMER_IMX_GPT_ICU) + list(APPEND SRCS capture_imx_gpt.c) + endif() endif() if(CONFIG_GOLDFISH_TIMER) diff --git a/drivers/timers/Kconfig b/drivers/timers/Kconfig index 5526054ee41..9f9c5476a99 100644 --- a/drivers/timers/Kconfig +++ b/drivers/timers/Kconfig @@ -194,6 +194,24 @@ config TIMER_IMX_GPT_NUM_OCU Number of Output Compare Unit channels to register as /dev/ocuN. The i.MX GPT hardware provides up to 3 output compare channels. +config TIMER_IMX_GPT_ICU + bool "Input Capture Unit (ICU) support" + default y + select CAPTURE + select CAPTURE_NOTIFY + ---help--- + Register the GPT input-capture channels as /dev/capN and plug + them into the generic capture upper-half driver. + +config TIMER_IMX_GPT_NUM_ICU + int "Number of ICU (input capture) channels" + depends on TIMER_IMX_GPT_ICU + default 1 + range 1 2 + ---help--- + Number of Input Capture Unit channels to register as /dev/capN. + The i.MX GPT hardware provides up to 2 input capture channels. + endif # TIMER_IMX_GPT config TIMER diff --git a/drivers/timers/Make.defs b/drivers/timers/Make.defs index e4734a5e55c..14d098bc4e6 100644 --- a/drivers/timers/Make.defs +++ b/drivers/timers/Make.defs @@ -181,6 +181,9 @@ ifeq ($(CONFIG_TIMER_IMX_GPT),y) CSRCS += imx_gpt.c ifeq ($(CONFIG_TIMER_IMX_GPT_OCU),y) CSRCS += ocu_imx_gpt.c +endif +ifeq ($(CONFIG_TIMER_IMX_GPT_ICU),y) + CSRCS += capture_imx_gpt.c endif TMRDEPPATH = --dep-path timers TMRVPATH = :timers diff --git a/drivers/timers/capture_imx_gpt.c b/drivers/timers/capture_imx_gpt.c new file mode 100644 index 00000000000..76c51518417 --- /dev/null +++ b/drivers/timers/capture_imx_gpt.c @@ -0,0 +1,380 @@ +/**************************************************************************** + * drivers/timers/capture_imx_gpt.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <debug.h> +#include <errno.h> + +#include <nuttx/irq.h> +#include <nuttx/kmalloc.h> +#include <nuttx/spinlock.h> +#include <nuttx/timers/capture.h> +#include <nuttx/timers/imx_gpt.h> + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int imx_gpt_cap_start(FAR struct cap_lowerhalf_s *lower); +static int imx_gpt_cap_stop(FAR struct cap_lowerhalf_s *lower); +static int imx_gpt_cap_getduty(FAR struct cap_lowerhalf_s *lower, + FAR uint8_t *duty); +static int imx_gpt_cap_getfreq(FAR struct cap_lowerhalf_s *lower, + FAR uint32_t *freq); +static int imx_gpt_cap_getedges(FAR struct cap_lowerhalf_s *lower, + FAR uint32_t *edges); +#ifdef CONFIG_CAPTURE_NOTIFY +static int imx_gpt_cap_bind(FAR struct cap_lowerhalf_s *lower, + enum cap_type_e type, capture_notify_t cb, FAR void *priv); +static int imx_gpt_cap_unbind(FAR struct cap_lowerhalf_s *lower); +#endif +static int imx_gpt_cap_start_capture(FAR struct cap_lowerhalf_s *lower); +static int imx_gpt_cap_stop_capture(FAR struct cap_lowerhalf_s *lower); +static int imx_gpt_cap_get_input_state(FAR struct cap_lowerhalf_s *lower, + FAR uint8_t *state); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct cap_ops_s g_imx_gpt_cap_ops = +{ + .start = imx_gpt_cap_start, + .stop = imx_gpt_cap_stop, + .getduty = imx_gpt_cap_getduty, + .getfreq = imx_gpt_cap_getfreq, + .getedges = imx_gpt_cap_getedges, +#ifdef CONFIG_CAPTURE_NOTIFY + .bind = imx_gpt_cap_bind, + .unbind = imx_gpt_cap_unbind, +#endif + .start_capture = imx_gpt_cap_start_capture, + .stop_capture = imx_gpt_cap_stop_capture, + .get_input_state = imx_gpt_cap_get_input_state, + .ioctl = NULL, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: imx_gpt_cap_enable + * + * Description: + * Reset the per-channel measurement state, program the input-capture + * mode to both edges and enable this channel's capture interrupt. + * Must be called with dev->lock held. + * + ****************************************************************************/ + +static void imx_gpt_cap_enable(FAR struct imx_gpt_icu_s *priv) +{ + FAR struct imx_gpt_dev_s *dev = priv->dev; + + priv->running = true; + priv->have_rise = false; + priv->level = 0; + priv->period = 0; + priv->high_time = 0; + priv->edges = 0; + + /* Capture on both edges so frequency and duty can be derived. */ + + imx_gpt_modreg(dev, IMX_GPT_CR, GPT_CR_IM_MASK(priv->ch), + GPT_IM_BOTH << GPT_CR_IM_SHIFT(priv->ch)); + imx_gpt_modreg(dev, IMX_GPT_IR, 0, GPT_IR_IF(priv->ch)); +} + +/**************************************************************************** + * Name: imx_gpt_cap_disable + * + * Description: + * Disable this channel's capture interrupt and input-capture mode. + * Must be called with dev->lock held. + * + ****************************************************************************/ + +static void imx_gpt_cap_disable(FAR struct imx_gpt_icu_s *priv) +{ + FAR struct imx_gpt_dev_s *dev = priv->dev; + + priv->running = false; + imx_gpt_modreg(dev, IMX_GPT_IR, GPT_IR_IF(priv->ch), 0); + imx_gpt_modreg(dev, IMX_GPT_CR, GPT_CR_IM_MASK(priv->ch), + GPT_IM_DISABLE << GPT_CR_IM_SHIFT(priv->ch)); +} + +/**************************************************************************** + * Name: imx_gpt_cap_start / imx_gpt_cap_stop + ****************************************************************************/ + +static int imx_gpt_cap_start(FAR struct cap_lowerhalf_s *lower) +{ + FAR struct imx_gpt_icu_s *priv = (FAR struct imx_gpt_icu_s *)lower; + irqstate_t flags; + + flags = spin_lock_irqsave(&priv->dev->lock); + imx_gpt_cap_enable(priv); + spin_unlock_irqrestore(&priv->dev->lock, flags); + return OK; +} + +static int imx_gpt_cap_stop(FAR struct cap_lowerhalf_s *lower) +{ + FAR struct imx_gpt_icu_s *priv = (FAR struct imx_gpt_icu_s *)lower; + irqstate_t flags; + + flags = spin_lock_irqsave(&priv->dev->lock); + imx_gpt_cap_disable(priv); + spin_unlock_irqrestore(&priv->dev->lock, flags); + return OK; +} + +/**************************************************************************** + * Name: imx_gpt_cap_getduty / getfreq / getedges + ****************************************************************************/ + +static int imx_gpt_cap_getduty(FAR struct cap_lowerhalf_s *lower, + FAR uint8_t *duty) +{ + FAR struct imx_gpt_icu_s *priv = (FAR struct imx_gpt_icu_s *)lower; + irqstate_t flags; + uint32_t period; + uint32_t high; + + flags = spin_lock_irqsave(&priv->dev->lock); + period = priv->period; + high = priv->high_time; + spin_unlock_irqrestore(&priv->dev->lock, flags); + + *duty = (period != 0) ? (uint8_t)(((uint64_t)high * 100) / period) : 0; + return OK; +} + +static int imx_gpt_cap_getfreq(FAR struct cap_lowerhalf_s *lower, + FAR uint32_t *freq) +{ + FAR struct imx_gpt_icu_s *priv = (FAR struct imx_gpt_icu_s *)lower; + irqstate_t flags; + uint32_t period; + + flags = spin_lock_irqsave(&priv->dev->lock); + period = priv->period; + spin_unlock_irqrestore(&priv->dev->lock, flags); + + /* Signal frequency = tick clock / ticks-per-period. */ + + *freq = (period != 0) ? (priv->dev->freq / period) : 0; + return OK; +} + +static int imx_gpt_cap_getedges(FAR struct cap_lowerhalf_s *lower, + FAR uint32_t *edges) +{ + FAR struct imx_gpt_icu_s *priv = (FAR struct imx_gpt_icu_s *)lower; + irqstate_t flags; + + flags = spin_lock_irqsave(&priv->dev->lock); + *edges = priv->edges; + spin_unlock_irqrestore(&priv->dev->lock, flags); + return OK; +} + +/**************************************************************************** + * Name: imx_gpt_cap_bind / imx_gpt_cap_unbind + ****************************************************************************/ + +#ifdef CONFIG_CAPTURE_NOTIFY +static int imx_gpt_cap_bind(FAR struct cap_lowerhalf_s *lower, + enum cap_type_e type, capture_notify_t cb, + FAR void *priv_arg) +{ + FAR struct imx_gpt_icu_s *priv = (FAR struct imx_gpt_icu_s *)lower; + irqstate_t flags; + + flags = spin_lock_irqsave(&priv->dev->lock); + + priv->cb = cb; + priv->priv = priv_arg; + priv->type = type; + + /* Ensure edge capture is active so the callback can fire even if start() + * has not been issued separately. + */ + + if (!priv->running) + { + imx_gpt_cap_enable(priv); + } + + spin_unlock_irqrestore(&priv->dev->lock, flags); + return OK; +} + +static int imx_gpt_cap_unbind(FAR struct cap_lowerhalf_s *lower) +{ + FAR struct imx_gpt_icu_s *priv = (FAR struct imx_gpt_icu_s *)lower; + irqstate_t flags; + + flags = spin_lock_irqsave(&priv->dev->lock); + priv->cb = NULL; + priv->priv = NULL; + spin_unlock_irqrestore(&priv->dev->lock, flags); + return OK; +} +#endif + +/**************************************************************************** + * Name: imx_gpt_cap_start_capture / stop_capture / get_input_state + ****************************************************************************/ + +static int imx_gpt_cap_start_capture(FAR struct cap_lowerhalf_s *lower) +{ + return imx_gpt_cap_start(lower); +} + +static int imx_gpt_cap_stop_capture(FAR struct cap_lowerhalf_s *lower) +{ + return imx_gpt_cap_stop(lower); +} + +static int imx_gpt_cap_get_input_state(FAR struct cap_lowerhalf_s *lower, + FAR uint8_t *state) +{ + FAR struct imx_gpt_icu_s *priv = (FAR struct imx_gpt_icu_s *)lower; + irqstate_t flags; + + flags = spin_lock_irqsave(&priv->dev->lock); + *state = (priv->level != 0) ? CAP_STATE_ACTIVE : CAP_STATE_IDLE; + spin_unlock_irqrestore(&priv->dev->lock, flags); + return OK; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: imx_gpt_icu_on_edge + * + * Description: + * Edge-capture interrupt handler for one ICU channel. Invoked from the + * shared core ISR with dev->lock already held. Reads the latched capture + * register, updates the period / high-time / edge-count derivations and + * invokes the notify callback subject to the configured edge filter. + * + ****************************************************************************/ + +void imx_gpt_icu_on_edge(FAR struct imx_gpt_icu_s *icu) +{ + FAR struct imx_gpt_dev_s *dev = icu->dev; + uint32_t t; + uint8_t newlevel; + + /* The capture register holds the counter value latched at the edge. */ + + t = imx_gpt_getreg(dev, IMX_GPT_ICR(icu->ch)); + + /* Both-edge capture: infer direction by toggling the tracked level, + * starting from the idle (low) assumption made at enable time. + */ + + newlevel = icu->level ? 0 : 1; + + if (newlevel != 0) + { + /* Rising edge: close the previous period (32-bit wrap-safe delta). */ + + if (icu->have_rise) + { + icu->period = t - icu->last_rise; + } + + icu->last_rise = t; + icu->have_rise = true; + } + else + { + /* Falling edge: high-level duration within the current period. */ + + if (icu->have_rise) + { + icu->high_time = t - icu->last_rise; + } + } + + icu->level = newlevel; + icu->edges++; + +#ifdef CONFIG_CAPTURE_NOTIFY + if (icu->cb != NULL && + (icu->type == CAP_TYPE_BOTH || + (icu->type == CAP_TYPE_RISING && newlevel != 0) || + (icu->type == CAP_TYPE_FALLING && newlevel == 0))) + { + icu->cb(&icu->lower, icu->priv); + } +#endif +} + +/**************************************************************************** + * Name: imx_gpt_icu_register + * + * Description: + * Allocate, initialise and register one ICU channel as an /dev/capN + * character device. Called by the shared core during initialisation. + * + ****************************************************************************/ + +FAR struct imx_gpt_icu_s * +imx_gpt_icu_register(FAR struct imx_gpt_dev_s *dev, int ch, + FAR const char *devpath) +{ + FAR struct imx_gpt_icu_s *priv; + int ret; + + priv = kmm_zalloc(sizeof(*priv)); + if (priv == NULL) + { + return NULL; + } + + priv->lower.ops = &g_imx_gpt_cap_ops; + priv->dev = dev; + priv->ch = (uint8_t)ch; + + ret = cap_register(devpath, &priv->lower); + if (ret < 0) + { + tmrerr("ERROR: cap_register(%s) failed: %d\n", devpath, ret); + kmm_free(priv); + return NULL; + } + + return priv; +} diff --git a/drivers/timers/imx_gpt.c b/drivers/timers/imx_gpt.c index ad2de9a2105..c9a8dcfdb33 100644 --- a/drivers/timers/imx_gpt.c +++ b/drivers/timers/imx_gpt.c @@ -57,7 +57,7 @@ static int imx_gpt_interrupt(int irq, FAR void *context, FAR void *arg) irqstate_t flags; uint32_t status; uint32_t handled = 0; -#ifdef CONFIG_TIMER_IMX_GPT_OCU +#if defined(CONFIG_TIMER_IMX_GPT_OCU) || defined(CONFIG_TIMER_IMX_GPT_ICU) int ch; #endif @@ -83,6 +83,22 @@ static int imx_gpt_interrupt(int irq, FAR void *context, FAR void *arg) } #endif +#ifdef CONFIG_TIMER_IMX_GPT_ICU + /* ICU edge-capture channels. */ + + for (ch = 0; ch < IMX_GPT_NUM_ICU; ch++) + { + if ((status & GPT_SR_IF(ch)) != 0) + { + handled |= GPT_SR_IF(ch); + if (dev->icu[ch] != NULL) + { + imx_gpt_icu_on_edge(dev->icu[ch]); + } + } + } +#endif + /* Counter roll-over accounting. */ if ((status & GPT_SR_ROV) != 0) @@ -151,17 +167,19 @@ int imx_gpt_initialize(uintptr_t base, int irq, uint32_t freq, int ocu_bus, int cap_bus) { FAR struct imx_gpt_dev_s *dev; -#ifdef CONFIG_TIMER_IMX_GPT_OCU +#if defined(CONFIG_TIMER_IMX_GPT_OCU) || defined(CONFIG_TIMER_IMX_GPT_ICU) char devpath[20]; int ch; #endif int ret; DEBUGASSERT(base != 0); - UNUSED(cap_bus); #ifndef CONFIG_TIMER_IMX_GPT_OCU UNUSED(ocu_bus); #endif +#ifndef CONFIG_TIMER_IMX_GPT_ICU + UNUSED(cap_bus); +#endif dev = kmm_zalloc(sizeof(*dev)); if (dev == NULL) @@ -194,6 +212,22 @@ int imx_gpt_initialize(uintptr_t base, int irq, uint32_t freq, } #endif +#ifdef CONFIG_TIMER_IMX_GPT_ICU + /* Register the ICU channels as /dev/cap<cap_bus + ch>. */ + + for (ch = 0; ch < IMX_GPT_NUM_ICU; ch++) + { + snprintf(devpath, sizeof(devpath), "/dev/cap%d", cap_bus + ch); + dev->icu[ch] = imx_gpt_icu_register(dev, ch, devpath); + if (dev->icu[ch] == NULL) + { + tmrerr("ERROR: failed to register %s\n", devpath); + ret = -ENODEV; + goto err; + } + } +#endif + /* Attach and enable the shared interrupt now that all channels exist. */ ret = irq_attach(irq, imx_gpt_interrupt, dev); diff --git a/include/nuttx/timers/imx_gpt.h b/include/nuttx/timers/imx_gpt.h index dd47589d908..f6a713af5cc 100644 --- a/include/nuttx/timers/imx_gpt.h +++ b/include/nuttx/timers/imx_gpt.h @@ -38,6 +38,7 @@ #include <nuttx/config.h> #include <nuttx/spinlock.h> +#include <nuttx/timers/capture.h> #include <nuttx/timers/ocu.h> #include <stdint.h> @@ -52,6 +53,9 @@ #ifdef CONFIG_TIMER_IMX_GPT_OCU #define IMX_GPT_NUM_OCU CONFIG_TIMER_IMX_GPT_NUM_OCU #endif +#ifdef CONFIG_TIMER_IMX_GPT_ICU +#define IMX_GPT_NUM_ICU CONFIG_TIMER_IMX_GPT_NUM_ICU +#endif /* Register offsets (32-bit, little-endian, 4-byte access). */ @@ -69,6 +73,7 @@ /* Per-channel register helpers. */ #define IMX_GPT_OCR(ch) (IMX_GPT_OCR1 + ((ch) * 4)) /* ch = 0..2 */ +#define IMX_GPT_ICR(ch) (IMX_GPT_ICR1 + ((ch) * 4)) /* ch = 0..1 */ /* GPT_CR bit definitions. */ @@ -79,6 +84,15 @@ #define GPT_CR_FRR (1 << 9) /* Free-run (1) / restart (0) */ #define GPT_CR_SWR (1 << 15) /* Software reset */ +/* Input capture mode (2 bits/channel): 0=off 1=rising 2=falling 3=both. */ + +#define GPT_CR_IM_SHIFT(ch) (16 + ((ch) * 2)) +#define GPT_CR_IM_MASK(ch) (0x3 << GPT_CR_IM_SHIFT(ch)) +#define GPT_IM_DISABLE 0 +#define GPT_IM_RISING 1 +#define GPT_IM_FALLING 2 +#define GPT_IM_BOTH 3 + /* Output compare mode (3 bits/channel): * 0=disconnect 1=toggle 2=clear(low) 3=set(high) 4=low-pulse. */ @@ -97,9 +111,11 @@ /* GPT_SR / GPT_IR share the same bit layout. */ #define GPT_SR_OF(ch) (1 << (ch)) /* OCU compare match, ch 0..2 */ +#define GPT_SR_IF(ch) (1 << (3 + (ch))) /* ICU edge capture, ch 0..1 */ #define GPT_SR_ROV (1 << 5) /* Counter roll-over */ #define GPT_IR_OF(ch) GPT_SR_OF(ch) +#define GPT_IR_IF(ch) GPT_SR_IF(ch) #define GPT_IR_ROV GPT_SR_ROV #define GPT_TIMER_MAX 0xffffffffu @@ -129,6 +145,29 @@ struct imx_gpt_ocu_s }; #endif +/* ICU (input capture) per-channel lower-half. */ + +#ifdef CONFIG_TIMER_IMX_GPT_ICU +struct imx_gpt_icu_s +{ + struct cap_lowerhalf_s lower; /* Must be first (cast-compat) */ + FAR struct imx_gpt_dev_s *dev; /* Back-pointer to shared core */ + uint8_t ch; /* ICU channel index 0..1 */ + bool running; /* Capture enabled */ + bool have_rise; /* last_rise is valid */ + uint8_t level; /* Current inferred input level */ +#ifdef CONFIG_CAPTURE_NOTIFY + enum cap_type_e type; /* Notify edge filter */ + capture_notify_t cb; /* Upper-half notify callback */ + FAR void *priv; /* Callback private pointer */ +#endif + uint32_t last_rise; /* Timestamp of last rising edge */ + uint32_t period; /* Ticks between rising edges */ + uint32_t high_time; /* High-level ticks in a period */ + uint32_t edges; /* Accumulated edge count */ +}; +#endif + /* Shared "core" device: one MMIO block, one IRQ, one counter. */ struct imx_gpt_dev_s @@ -141,6 +180,9 @@ struct imx_gpt_dev_s #ifdef CONFIG_TIMER_IMX_GPT_OCU FAR struct imx_gpt_ocu_s *ocu[IMX_GPT_NUM_OCU]; #endif +#ifdef CONFIG_TIMER_IMX_GPT_ICU + FAR struct imx_gpt_icu_s *icu[IMX_GPT_NUM_ICU]; +#endif }; /**************************************************************************** @@ -221,6 +263,12 @@ imx_gpt_ocu_register(FAR struct imx_gpt_dev_s *dev, int ch, FAR const char *devpath); #endif +#ifdef CONFIG_TIMER_IMX_GPT_ICU +FAR struct imx_gpt_icu_s * +imx_gpt_icu_register(FAR struct imx_gpt_dev_s *dev, int ch, + FAR const char *devpath); +#endif + /**************************************************************************** * Name: imx_gpt_ocu_on_match * @@ -234,6 +282,9 @@ imx_gpt_ocu_register(FAR struct imx_gpt_dev_s *dev, int ch, #ifdef CONFIG_TIMER_IMX_GPT_OCU void imx_gpt_ocu_on_match(FAR struct imx_gpt_ocu_s *ocu); #endif +#ifdef CONFIG_TIMER_IMX_GPT_ICU +void imx_gpt_icu_on_edge(FAR struct imx_gpt_icu_s *icu); +#endif #undef EXTERN #ifdef __cplusplus
