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 c3895568ddea25c1ef4e66753649ee0fbf319db9
Author: qinwei1 <[email protected]>
AuthorDate: Wed Aug 12 09:00:38 2026 +0800

    drivers/timers: expose SP804 Timer as a periodic /dev/timerN
    
    - Add a timer lowerhalf that drives SP804 Timer 1 in 32-bit periodic mode
      and plugs into the timer upper-half (timer_register()), so it can be
      exposed as /dev/timerN for apps/examples/timer.
    - the existing Timer 2 oneshot path is untouched. Purely additive.
    
    Signed-off-by: qinwei1 <[email protected]>
---
 drivers/timers/CMakeLists.txt               |   5 +-
 drivers/timers/Make.defs                    |   5 +-
 {include/nuttx => drivers}/timers/sp804.h   |  62 +++---
 drivers/timers/{sp804.c => sp804_oneshot.c} |  32 +--
 drivers/timers/sp804_timer.c                | 291 ++++++++++++++++++++++++++++
 include/nuttx/timers/sp804.h                |  28 +++
 6 files changed, 359 insertions(+), 64 deletions(-)

diff --git a/drivers/timers/CMakeLists.txt b/drivers/timers/CMakeLists.txt
index 3cfe6e363fb..2d619f2cb3f 100644
--- a/drivers/timers/CMakeLists.txt
+++ b/drivers/timers/CMakeLists.txt
@@ -62,7 +62,10 @@ if(CONFIG_RTC_PCF8563)
 endif()
 
 if(CONFIG_TIMER_SP804)
-  list(APPEND SRCS sp804.c)
+  list(APPEND SRCS sp804_oneshot.c)
+  if(CONFIG_TIMER)
+    list(APPEND SRCS sp804_timer.c)
+  endif()
 endif()
 
 if(CONFIG_RTC_PL031)
diff --git a/drivers/timers/Make.defs b/drivers/timers/Make.defs
index bf3f1a239b7..2062a483c49 100644
--- a/drivers/timers/Make.defs
+++ b/drivers/timers/Make.defs
@@ -80,7 +80,10 @@ ifeq ($(CONFIG_RTC_PCF8563),y)
 endif
 
 ifeq ($(CONFIG_TIMER_SP804),y)
-  CSRCS += sp804.c
+  CSRCS += sp804_oneshot.c
+ifeq ($(CONFIG_TIMER),y)
+  CSRCS += sp804_timer.c
+endif
   TMRDEPPATH = --dep-path timers
   TMRVPATH = :timers
 endif
diff --git a/include/nuttx/timers/sp804.h b/drivers/timers/sp804.h
similarity index 56%
copy from include/nuttx/timers/sp804.h
copy to drivers/timers/sp804.h
index 4a096d5225f..f7db97e1bc1 100644
--- a/include/nuttx/timers/sp804.h
+++ b/drivers/timers/sp804.h
@@ -1,5 +1,5 @@
 /****************************************************************************
- * include/nuttx/timers/sp804.h
+ * drivers/timers/sp804.h
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -20,49 +20,45 @@
  *
  ****************************************************************************/
 
-#ifndef __INCLUDE_NUTTX_TIMERS_SP804_H
-#define __INCLUDE_NUTTX_TIMERS_SP804_H
+#ifndef __DRIVERS_TIMERS_SP804_H
+#define __DRIVERS_TIMERS_SP804_H
 
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
 #include <nuttx/config.h>
-#include <nuttx/timers/oneshot.h>
+
+#include <stdint.h>
 
 /****************************************************************************
- * Public Function Prototypes
+ * Pre-processor Definitions
  ****************************************************************************/
 
-#ifdef __cplusplus
-extern "C"
-{
-#endif
+/* SP804 per-timer register offsets */
 
-/****************************************************************************
- * Name: sp804_initialize
- *
- * Description:
- *   Initialize the SP804 dual timer and return a oneshot lowerhalf
- *   driver instance that can be bound to the OS alarm infrastructure
- *   via up_alarm_set_lowerhalf().
- *
- * Input Parameters:
- *   base - Physical base address of the SP804 timer block.
- *   irq  - IRQ number associated with the timer interrupt.
- *   freq - Input clock frequency in Hz.
- *
- * Returned Value:
- *   On success, a non-NULL oneshot lowerhalf pointer is returned.
- *   NULL is returned on any failure.
- *
- ****************************************************************************/
+#define SP804_LOAD          0x00  /* Load register */
+#define SP804_VALUE         0x04  /* Current value (RO) */
+#define SP804_CONTROL       0x08  /* Control register */
+#define SP804_INTCLR        0x0c  /* Interrupt clear (WO) */
+
+/* Timer 2 sits at base + 0x20 with the same layout */
+
+#define SP804_TIMER2_OFF    0x20
+
+/* Control register bits */
+
+#define SP804_CTRL_ONESHOT  (1 << 0)  /* One-shot mode */
+#define SP804_CTRL_32BIT    (1 << 1)  /* 32-bit counter */
+#define SP804_CTRL_IE       (1 << 5)  /* Interrupt enable */
+#define SP804_CTRL_PERIODIC (1 << 6)  /* Periodic (auto-reload) mode */
+#define SP804_CTRL_ENABLE   (1 << 7)  /* Timer enable */
 
-FAR struct oneshot_lowerhalf_s *
-sp804_initialize(uintptr_t base, int irq, uint32_t freq);
+/* Helper macros */
 
-#ifdef __cplusplus
-}
-#endif
+#define sp804_getreg(b, o) \
+  (*(FAR volatile uint32_t *)((b) + (o)))
+#define sp804_putreg(v, b, o) \
+  (*((FAR volatile uint32_t *)((b) + (o))) = (v))
 
-#endif /* __INCLUDE_NUTTX_TIMERS_SP804_H */
+#endif /* __DRIVERS_TIMERS_SP804_H */
diff --git a/drivers/timers/sp804.c b/drivers/timers/sp804_oneshot.c
similarity index 83%
rename from drivers/timers/sp804.c
rename to drivers/timers/sp804_oneshot.c
index f65348dd541..81f75213bd5 100644
--- a/drivers/timers/sp804.c
+++ b/drivers/timers/sp804_oneshot.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * drivers/timers/sp804.c
+ * drivers/timers/sp804_oneshot.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -25,37 +25,11 @@
  ****************************************************************************/
 
 #include <nuttx/arch.h>
+#include <nuttx/clock.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/timers/sp804.h>
 
-/****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
-
-/* SP804 per-timer register offsets */
-
-#define SP804_LOAD          0x00  /* Load register */
-#define SP804_VALUE         0x04  /* Current value (RO) */
-#define SP804_CONTROL       0x08  /* Control register */
-#define SP804_INTCLR        0x0c  /* Interrupt clear (WO) */
-
-/* Timer 2 sits at base + 0x20 with the same layout */
-
-#define SP804_TIMER2_OFF    0x20
-
-/* Control register bits */
-
-#define SP804_CTRL_ONESHOT  (1 << 0)  /* One-shot mode */
-#define SP804_CTRL_32BIT    (1 << 1)  /* 32-bit counter */
-#define SP804_CTRL_IE       (1 << 5)  /* Interrupt enable */
-#define SP804_CTRL_ENABLE   (1 << 7)  /* Timer enable */
-
-/* Helper macros */
-
-#define sp804_getreg(b, o) \
-  (*(FAR volatile uint32_t *)((b) + (o)))
-#define sp804_putreg(v, b, o) \
-  (*((FAR volatile uint32_t *)((b) + (o))) = (v))
+#include "sp804.h"
 
 /****************************************************************************
  * Private Types
diff --git a/drivers/timers/sp804_timer.c b/drivers/timers/sp804_timer.c
new file mode 100644
index 00000000000..0927e01cc8e
--- /dev/null
+++ b/drivers/timers/sp804_timer.c
@@ -0,0 +1,291 @@
+/****************************************************************************
+ * drivers/timers/sp804_timer.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * SP804 "timer" lower-half (periodic /dev/timerN)
+ *
+ * sp804_oneshot.c binds SP804 Timer 2 as a oneshot alarm for the OS.  This
+ * file exposes SP804 Timer 1 as a standard NuttX timer lower-half so it can
+ * be registered as /dev/timerN and exercised by apps/examples/timer.  The
+ * two are independent register windows (Timer 1 at base+0x00) and only one
+ * of them is used on a given board.
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/clock.h>
+#include <nuttx/irq.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/timers/sp804.h>
+
+#include "sp804.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sp804_timer_lowerhalf_s
+{
+  struct timer_lowerhalf_s lower;  /* Publicly visible portion */
+  uintptr_t base;                  /* Timer 1 register base */
+  uint32_t  freq;                  /* Input clock frequency (Hz) */
+  uint32_t  load;                  /* Reload value for the configured period */
+  tccb_t    callback;              /* Upper-half timeout callback */
+  FAR void *arg;                   /* Argument for the callback */
+  bool      started;               /* True if the timer is running */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sp804_timer_us_to_ticks / sp804_timer_ticks_to_us
+ ****************************************************************************/
+
+static uint32_t
+sp804_timer_us_to_ticks(FAR struct sp804_timer_lowerhalf_s *p, uint32_t usec)
+{
+  uint64_t ticks = ((uint64_t)usec * p->freq) / USEC_PER_SEC;
+
+  if (ticks < 1)
+    {
+      ticks = 1;
+    }
+  else if (ticks > UINT32_MAX)
+    {
+      ticks = UINT32_MAX;
+    }
+
+  return (uint32_t)ticks;
+}
+
+static uint32_t
+sp804_timer_ticks_to_us(FAR struct sp804_timer_lowerhalf_s *p,
+                        uint32_t ticks)
+{
+  return (uint32_t)(((uint64_t)ticks * USEC_PER_SEC) / p->freq);
+}
+
+/****************************************************************************
+ * Name: sp804_timer_interrupt
+ ****************************************************************************/
+
+static int sp804_timer_interrupt(int irq, FAR void *context, FAR void *arg)
+{
+  FAR struct sp804_timer_lowerhalf_s *priv = arg;
+  uint32_t next = 0;
+
+  /* Acknowledge the interrupt (write to INTCLR). */
+
+  sp804_putreg(1, priv->base, SP804_INTCLR);
+
+  if (priv->callback != NULL &&
+      priv->callback(&next, priv->arg))
+    {
+      /* The upper half wants to keep running.  The hardware reloads
+       * automatically in periodic mode, but honour a requested change of
+       * period if one was supplied.
+       */
+
+      if (next > 0)
+        {
+          uint32_t load = sp804_timer_us_to_ticks(priv, next);
+
+          priv->load = load;
+          sp804_putreg(load, priv->base, SP804_LOAD);
+        }
+    }
+  else
+    {
+      /* No callback or callback asked to stop: halt the timer. */
+
+      sp804_putreg(0, priv->base, SP804_CONTROL);
+      priv->started = false;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: sp804_timer_start
+ ****************************************************************************/
+
+static int sp804_timer_start(FAR struct timer_lowerhalf_s *lower)
+{
+  FAR struct sp804_timer_lowerhalf_s *priv =
+    (FAR struct sp804_timer_lowerhalf_s *)lower;
+
+  sp804_putreg(0, priv->base, SP804_CONTROL);
+  sp804_putreg(priv->load, priv->base, SP804_LOAD);
+  sp804_putreg(1, priv->base, SP804_INTCLR);
+  sp804_putreg(SP804_CTRL_ENABLE | SP804_CTRL_32BIT |
+               SP804_CTRL_PERIODIC | SP804_CTRL_IE,
+               priv->base, SP804_CONTROL);
+  priv->started = true;
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: sp804_timer_stop
+ ****************************************************************************/
+
+static int sp804_timer_stop(FAR struct timer_lowerhalf_s *lower)
+{
+  FAR struct sp804_timer_lowerhalf_s *priv =
+    (FAR struct sp804_timer_lowerhalf_s *)lower;
+
+  sp804_putreg(0, priv->base, SP804_CONTROL);
+  sp804_putreg(1, priv->base, SP804_INTCLR);
+  priv->started = false;
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: sp804_timer_getstatus
+ ****************************************************************************/
+
+static int sp804_timer_getstatus(FAR struct timer_lowerhalf_s *lower,
+                                 FAR struct timer_status_s *status)
+{
+  FAR struct sp804_timer_lowerhalf_s *priv =
+    (FAR struct sp804_timer_lowerhalf_s *)lower;
+
+  status->flags = 0;
+  if (priv->started)
+    {
+      status->flags |= TCFLAGS_ACTIVE;
+    }
+
+  if (priv->callback != NULL)
+    {
+      status->flags |= TCFLAGS_HANDLER;
+    }
+
+  status->timeout  = sp804_timer_ticks_to_us(priv, priv->load);
+  status->timeleft = sp804_timer_ticks_to_us(priv,
+                       sp804_getreg(priv->base, SP804_VALUE));
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: sp804_timer_settimeout
+ ****************************************************************************/
+
+static int sp804_timer_settimeout(FAR struct timer_lowerhalf_s *lower,
+                                  uint32_t timeout)
+{
+  FAR struct sp804_timer_lowerhalf_s *priv =
+    (FAR struct sp804_timer_lowerhalf_s *)lower;
+
+  priv->load = sp804_timer_us_to_ticks(priv, timeout);
+  sp804_putreg(priv->load, priv->base, SP804_LOAD);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: sp804_timer_setcallback
+ ****************************************************************************/
+
+static void sp804_timer_setcallback(FAR struct timer_lowerhalf_s *lower,
+                                    tccb_t callback, FAR void *arg)
+{
+  FAR struct sp804_timer_lowerhalf_s *priv =
+    (FAR struct sp804_timer_lowerhalf_s *)lower;
+  irqstate_t flags;
+
+  flags = up_irq_save();
+  priv->callback = callback;
+  priv->arg      = arg;
+  up_irq_restore(flags);
+}
+
+/****************************************************************************
+ * Name: sp804_timer_maxtimeout
+ ****************************************************************************/
+
+static int sp804_timer_maxtimeout(FAR struct timer_lowerhalf_s *lower,
+                                  FAR uint32_t *maxtimeout)
+{
+  FAR struct sp804_timer_lowerhalf_s *priv =
+    (FAR struct sp804_timer_lowerhalf_s *)lower;
+
+  *maxtimeout = sp804_timer_ticks_to_us(priv, UINT32_MAX);
+
+  return OK;
+}
+
+static const struct timer_ops_s g_sp804_timer_ops =
+{
+  .start      = sp804_timer_start,
+  .stop       = sp804_timer_stop,
+  .getstatus  = sp804_timer_getstatus,
+  .settimeout = sp804_timer_settimeout,
+  .setcallback = sp804_timer_setcallback,
+  .ioctl      = NULL,
+  .maxtimeout = sp804_timer_maxtimeout,
+};
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sp804_timer_initialize
+ ****************************************************************************/
+
+FAR struct timer_lowerhalf_s *
+sp804_timer_initialize(uintptr_t base, int irq, uint32_t freq)
+{
+  FAR struct sp804_timer_lowerhalf_s *priv;
+
+  priv = kmm_zalloc(sizeof(*priv));
+  if (priv == NULL)
+    {
+      return NULL;
+    }
+
+  priv->lower.ops = &g_sp804_timer_ops;
+  priv->base      = base;
+  priv->freq      = freq;
+  priv->load      = freq;   /* Default 1s period until settimeout() */
+  priv->started   = false;
+
+  /* Leave the timer disabled until started. */
+
+  sp804_putreg(0, priv->base, SP804_CONTROL);
+  sp804_putreg(1, priv->base, SP804_INTCLR);
+
+  irq_attach(irq, sp804_timer_interrupt, priv);
+  up_enable_irq(irq);
+
+  return &priv->lower;
+}
diff --git a/include/nuttx/timers/sp804.h b/include/nuttx/timers/sp804.h
index 4a096d5225f..fcf637fbc6e 100644
--- a/include/nuttx/timers/sp804.h
+++ b/include/nuttx/timers/sp804.h
@@ -29,6 +29,7 @@
 
 #include <nuttx/config.h>
 #include <nuttx/timers/oneshot.h>
+#include <nuttx/timers/timer.h>
 
 /****************************************************************************
  * Public Function Prototypes
@@ -61,6 +62,33 @@ extern "C"
 FAR struct oneshot_lowerhalf_s *
 sp804_initialize(uintptr_t base, int irq, uint32_t freq);
 
+/****************************************************************************
+ * Name: sp804_timer_initialize
+ *
+ * Description:
+ *   Initialize the SP804 dual timer as a free-running periodic timer and
+ *   return a "timer" lowerhalf instance that can be bound to the timer
+ *   upper-half driver (timer_register()) and exposed as /dev/timerN.
+ *
+ *   This uses SP804 Timer 1 in 32-bit periodic mode: the counter reloads
+ *   from the programmed value on each expiry and raises the shared SP804
+ *   interrupt, giving the periodic-notification semantics expected by
+ *   apps/examples/timer.
+ *
+ * Input Parameters:
+ *   base - Physical base address of the SP804 timer block.
+ *   irq  - IRQ number associated with the timer interrupt.
+ *   freq - Input clock frequency in Hz.
+ *
+ * Returned Value:
+ *   On success, a non-NULL timer lowerhalf pointer is returned.
+ *   NULL is returned on any failure.
+ *
+ ****************************************************************************/
+
+FAR struct timer_lowerhalf_s *
+sp804_timer_initialize(uintptr_t base, int irq, uint32_t freq);
+
 #ifdef __cplusplus
 }
 #endif

Reply via email to