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 a7a3a3aaf0429b5ba883bbbf8eb813ba4e633165
Author: Alan Carvalho de Assis <[email protected]>
AuthorDate: Sat Nov 14 18:34:21 2026 -0300

    arch/arm/stm32: Time stamp the edges in the capture lower half.
    
    The capture lower half of the STM32 F1/F2/F3/F4/L1 timers ran the
    timer in reset slave mode: the counter restarted on every rising
    edge, so it could not time stamp the edges, and edge notification
    (bind/unbind) was not implemented.
    
    * Let the counter run freely and compute the period and the high time
      from the differences between the captured values (modulo the timer
      resolution).  The frequency and duty cycle reported to the
      applications are unchanged.
    * Implement bind() and unbind() (CONFIG_CAPTURE_NOTIFY).  The rising
      edge is captured by the configured channel and the falling edge by
      its paired channel, whose interrupt is only enabled when the
      falling edge is notified.
    * Implement getcount(), which reads the counter, and getcapture(),
      which returns the counter latched on the notified edge.
    
    With a 32-bit timer (TIM2 or TIM5) clocked at the full timer clock,
    the lower half can be the counter and the PPS input of the PPS
    disciplined PTP clock (CONFIG_PTP_PPS), with the resolution of one
    timer clock (11.9 ns at 84 MHz).
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Alan Carvalho de Assis <[email protected]>
---
 .../common/stm32/stm32_capture_m3m4_v1_lowerhalf.c | 231 ++++++++++++++++++---
 1 file changed, 207 insertions(+), 24 deletions(-)

diff --git a/arch/arm/src/common/stm32/stm32_capture_m3m4_v1_lowerhalf.c 
b/arch/arm/src/common/stm32/stm32_capture_m3m4_v1_lowerhalf.c
index a289887d67f..91c317adc35 100644
--- a/arch/arm/src/common/stm32/stm32_capture_m3m4_v1_lowerhalf.c
+++ b/arch/arm/src/common/stm32/stm32_capture_m3m4_v1_lowerhalf.c
@@ -86,6 +86,14 @@ struct stm32_lowerhalf_s
   uint8_t                duty;       /* Result pwm frequency */
   uint32_t               freq;       /* Result pwm frequency */
   uint32_t               edges;      /* Result pwm edges */
+  uint32_t               rise;       /* Counter at the last rising edge */
+  uint32_t               capture;    /* Counter at the last notified edge */
+  bool                   valid;      /* rise holds a previous edge */
+#ifdef CONFIG_CAPTURE_NOTIFY
+  capture_notify_t       cb;         /* Edge notification callback */
+  void                  *priv;       /* Argument of the callback */
+  enum cap_type_e        type;       /* Edges that are notified */
+#endif
 };
 
 /****************************************************************************
@@ -101,6 +109,14 @@ static int stm32_stop(struct cap_lowerhalf_s *lower);
 static int stm32_getduty(struct cap_lowerhalf_s *lower, uint8_t *duty);
 static int stm32_getfreq(struct cap_lowerhalf_s *lower, uint32_t *freq);
 static int stm32_getedges(struct cap_lowerhalf_s *lower, uint32_t *edges);
+#ifdef CONFIG_CAPTURE_NOTIFY
+static int stm32_bind(struct cap_lowerhalf_s *lower, enum cap_type_e type,
+                      capture_notify_t cb, void *priv);
+static int stm32_unbind(struct cap_lowerhalf_s *lower);
+#endif
+static int stm32_getcount(struct cap_lowerhalf_s *lower, uint32_t *count);
+static int stm32_getcapture(struct cap_lowerhalf_s *lower,
+                            uint32_t *count);
 
 /****************************************************************************
  * Private Data
@@ -115,6 +131,12 @@ static const struct cap_ops_s g_cap_ops =
   .getduty     = stm32_getduty,
   .getfreq     = stm32_getfreq,
   .getedges    = stm32_getedges,
+#ifdef CONFIG_CAPTURE_NOTIFY
+  .bind        = stm32_bind,
+  .unbind      = stm32_unbind,
+#endif
+  .getcount    = stm32_getcount,
+  .getcapture  = stm32_getcapture,
 };
 
 #ifdef CONFIG_STM32_TIM1_CAP
@@ -256,35 +278,69 @@ static struct stm32_lowerhalf_s g_cap14_lowerhalf =
 static int stm32_cap_handler(int irq, void * context, void * arg)
 {
   struct stm32_lowerhalf_s *lower = (struct stm32_lowerhalf_s *) arg;
+  uint32_t mask = UINT32_MAX >> (32 - lower->resolution);
   uint8_t ch = 0x3 & lower->channel;
-  int period = 0;
+  uint8_t pair = 0x3 & (~ch);
+  uint64_t high;
+  uint32_t period;
+  uint32_t rise;
   int flags = 0;
 
   flags = (int)STM32_CAP_GETFLAGS(lower->cap) ;
 
   STM32_CAP_ACKFLAGS(lower->cap, flags);
 
-  /* The counter is reset on the capture edge, so each captured value is
-   * one less than the number of timer ticks it represents.
+  /* The counter runs freely, channel 'ch' latches it on the rising edge
+   * and channel 'pair' on the falling edge of the same input.  The period
+   * is the difference between two rising edges and the high time the
+   * difference between a rising edge and the next falling edge.
    */
 
-  period = STM32_CAP_GETCAPTURE(lower->cap, ch);
-
-  if (period != 0)
+  if (flags & STM32_CAP_FLAG_IRQ_CH(ch))
     {
-      period++;
-      lower->duty = (100 * (STM32_CAP_GETCAPTURE(lower->cap,
-                                                 0x3 & (~ch)) + 1)) /
-                    period;
-      lower->freq = lower->clock / period;
+      rise = STM32_CAP_GETCAPTURE(lower->cap, ch);
+
+      if (lower->valid)
+        {
+          period = (rise - lower->rise) & mask;
+          high   = (STM32_CAP_GETCAPTURE(lower->cap, pair) - lower->rise) &
+                   mask;
+
+          if (period != 0)
+            {
+              lower->duty = 100 * high / period;
+              lower->freq = lower->clock / period;
+            }
+          else
+            {
+              lower->duty = 0;
+              lower->freq = 0;
+            }
+        }
+
+      lower->rise  = rise;
+      lower->valid = true;
+      lower->edges++;
+
+#ifdef CONFIG_CAPTURE_NOTIFY
+      if (lower->cb != NULL && lower->type != CAP_TYPE_FALLING)
+        {
+          lower->capture = rise;
+          lower->cb((struct cap_lowerhalf_s *)lower, lower->priv);
+        }
+#endif
     }
-  else
+
+#ifdef CONFIG_CAPTURE_NOTIFY
+  /* The interrupt of the falling edge is only enabled when it is notified */
+
+  if ((flags & STM32_CAP_FLAG_IRQ_CH(pair)) && lower->cb != NULL &&
+      lower->type != CAP_TYPE_RISING)
     {
-      lower->duty = 0;
-      lower->freq = 0;
+      lower->capture = STM32_CAP_GETCAPTURE(lower->cap, pair);
+      lower->cb((struct cap_lowerhalf_s *)lower, lower->priv);
     }
-
-  lower->edges++;
+#endif
 
   return OK;
 }
@@ -318,13 +374,12 @@ static int stm32_start(struct cap_lowerhalf_s *lower)
     }
 
   priv->edges = 0;
+  priv->valid = false;
 
   switch (priv->channel)
     {
       case 1:
-        STM32_CAP_SETSMC(priv->cap, STM32_CAP_SMS_RST |
-                         STM32_CAP_TS_TI1FP1 |
-                         STM32_CAP_MSM_MASK);
+        STM32_CAP_SETSMC(priv->cap, STM32_CAP_SMS_INT);
 
         STM32_CAP_SETCLOCK(priv->cap, priv->clock, maxtimeout);
 
@@ -345,9 +400,7 @@ static int stm32_start(struct cap_lowerhalf_s *lower)
         break;
 
       case 2:
-        STM32_CAP_SETSMC(priv->cap, STM32_CAP_SMS_RST |
-                         STM32_CAP_TS_TI2FP2 |
-                         STM32_CAP_MSM_MASK);
+        STM32_CAP_SETSMC(priv->cap, STM32_CAP_SMS_INT);
 
         STM32_CAP_SETCLOCK(priv->cap, priv->clock, maxtimeout);
 
@@ -398,7 +451,8 @@ static int stm32_stop(struct cap_lowerhalf_s *lower)
       switch (priv->channel)
         {
           case 1:
-            STM32_CAP_ENABLEINT(priv->cap, STM32_CAP_FLAG_IRQ_CH_1, false);
+            STM32_CAP_ENABLEINT(priv->cap, STM32_CAP_FLAG_IRQ_CH_1 |
+                                STM32_CAP_FLAG_IRQ_CH_2, false);
             STM32_CAP_SETCHANNEL(priv->cap, 1,
                                  STM32_CAP_EDGE_DISABLED |
                                  STM32_CAP_MAPPED_TI1);
@@ -408,7 +462,8 @@ static int stm32_stop(struct cap_lowerhalf_s *lower)
             break;
 
           case 2:
-            STM32_CAP_ENABLEINT(priv->cap, STM32_CAP_FLAG_IRQ_CH_2, false);
+            STM32_CAP_ENABLEINT(priv->cap, STM32_CAP_FLAG_IRQ_CH_1 |
+                                STM32_CAP_FLAG_IRQ_CH_2, false);
             STM32_CAP_SETCHANNEL(priv->cap, 2,
                                  STM32_CAP_EDGE_DISABLED |
                                  STM32_CAP_MAPPED_TI1);
@@ -519,6 +574,134 @@ static int stm32_getedges(struct cap_lowerhalf_s *lower, 
uint32_t *edges)
   return OK;
 }
 
+#ifdef CONFIG_CAPTURE_NOTIFY
+/****************************************************************************
+ * Name: stm32_bind
+ *
+ * Description:
+ *   Bind the edge notification callback.  The rising edge is captured by
+ *   the configured channel and the falling edge by its paired channel,
+ *   whose interrupt is only enabled when the falling edge is notified.
+ *
+ * Input Parameters:
+ *   lower - A pointer the publicly visible representation of the
+ *           "lower-half" driver state structure.
+ *   type  - The edges to notify.
+ *   cb    - The callback, called from the interrupt handler.
+ *   priv  - The argument of the callback.
+ *
+ * Returned Value:
+ *   Zero on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+static int stm32_bind(struct cap_lowerhalf_s *lower, enum cap_type_e type,
+                      capture_notify_t cb, void *priv)
+{
+  struct stm32_lowerhalf_s *dev = (struct stm32_lowerhalf_s *)lower;
+  uint8_t pair = 0x3 & (~dev->channel);
+  irqstate_t flags;
+
+  if (dev->channel != 1 && dev->channel != 2)
+    {
+      return -EINVAL;
+    }
+
+  flags = enter_critical_section();
+
+  dev->cb   = cb;
+  dev->priv = priv;
+  dev->type = type;
+
+  STM32_CAP_ACKFLAGS(dev->cap, STM32_CAP_FLAG_IRQ_CH(pair));
+  STM32_CAP_ENABLEINT(dev->cap, STM32_CAP_FLAG_IRQ_CH(pair),
+                      type != CAP_TYPE_RISING);
+
+  leave_critical_section(flags);
+  return OK;
+}
+
+/****************************************************************************
+ * Name: stm32_unbind
+ *
+ * Description:
+ *   Unbind the edge notification callback.
+ *
+ * Input Parameters:
+ *   lower - A pointer the publicly visible representation of the
+ *           "lower-half" driver state structure.
+ *
+ * Returned Value:
+ *   Zero on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+static int stm32_unbind(struct cap_lowerhalf_s *lower)
+{
+  struct stm32_lowerhalf_s *dev = (struct stm32_lowerhalf_s *)lower;
+  uint8_t pair = 0x3 & (~dev->channel);
+  irqstate_t flags;
+
+  flags = enter_critical_section();
+
+  STM32_CAP_ENABLEINT(dev->cap, STM32_CAP_FLAG_IRQ_CH(pair), false);
+  dev->cb   = NULL;
+  dev->priv = NULL;
+
+  leave_critical_section(flags);
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: stm32_getcount
+ *
+ * Description:
+ *   Get the current value of the free running counter.
+ *
+ * Input Parameters:
+ *   lower - A pointer the publicly visible representation of the
+ *           "lower-half" driver state structure.
+ *   count - The counter value.
+ *
+ * Returned Value:
+ *   Zero on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+static int stm32_getcount(struct cap_lowerhalf_s *lower, uint32_t *count)
+{
+  struct stm32_lowerhalf_s *priv = (struct stm32_lowerhalf_s *)lower;
+
+  *count = STM32_CAP_GETCAPTURE(priv->cap, STM32_CAP_CHANNEL_COUNTER);
+  return OK;
+}
+
+/****************************************************************************
+ * Name: stm32_getcapture
+ *
+ * Description:
+ *   Get the counter value latched by the hardware on the last notified
+ *   edge.
+ *
+ * Input Parameters:
+ *   lower - A pointer the publicly visible representation of the
+ *           "lower-half" driver state structure.
+ *   count - The latched counter value.
+ *
+ * Returned Value:
+ *   Zero on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+static int stm32_getcapture(struct cap_lowerhalf_s *lower, uint32_t *count)
+{
+  struct stm32_lowerhalf_s *priv = (struct stm32_lowerhalf_s *)lower;
+
+  *count = priv->capture;
+  return OK;
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/

Reply via email to