This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new f989860c4a0 timer: add timer operation support
f989860c4a0 is described below

commit f989860c4a095409e8662d5e19aa77aae9e2f724
Author: xucheng5 <[email protected]>
AuthorDate: Tue Nov 26 15:58:27 2024 +0800

    timer: add timer operation support
    
    Add basic timer operations to the kernel, including registration,
    start/stop, and callback handling. This provides a consistent timer
    interface for use by kernel components and drivers.
    
    Signed-off-by: zhaohaiyang1 <[email protected]>
---
 .../components/drivers/character/timers/timer.rst  | 102 ++++++++++++++++++++-
 drivers/timers/timer.c                             |  47 ++++++++++
 include/nuttx/timers/timer.h                       |  22 +++--
 3 files changed, 163 insertions(+), 8 deletions(-)

diff --git a/Documentation/components/drivers/character/timers/timer.rst 
b/Documentation/components/drivers/character/timers/timer.rst
index 659316255a0..441f9975bef 100644
--- a/Documentation/components/drivers/character/timers/timer.rst
+++ b/Documentation/components/drivers/character/timers/timer.rst
@@ -110,6 +110,9 @@ systems calls. The available ``ioctl`` commands are:
  * :c:macro:`TCIOC_SETTIMEOUT`
  * :c:macro:`TCIOC_NOTIFICATION`
  * :c:macro:`TCIOC_MAXTIMEOUT`
+ * :c:macro:`TCIOC_TICK_GETSTATUS`
+ * :c:macro:`TCIOC_TICK_SETTIMEOUT`
+ * :c:macro:`TCIOC_TICK_MAXTIMEOUT`
 
 These ``ioctl`` commands internally call lower-half layer operations and the
 parameters are forwarded to these ops through the ``ioctl`` system call. The 
return
@@ -280,7 +283,7 @@ The ``TCIOC_SETTIMEOUT`` command calls the ``settimeout`` 
operation, which is de
 
 .. c:function:: int settimeout(uint32_t timeout)
 
-  The getstatus operation sets a timeout interval to trigger the alarm and then
+  The settimeout operation sets a timeout interval to trigger the alarm and 
then
   trigger an interrupt. It defines the timer interval in which the handler will
   be called.
 
@@ -362,7 +365,7 @@ This command may be used like so:
   ret = ioctl(fd, TCIOC_MAXTIMEOUT, (uint32_t*)(&max_timeout));
   if (ret < 0)
     {
-      fprintf(stderr, "ERROR: Failed to reat the timer's maximum timeout: 
%d\n", errno);
+      fprintf(stderr, "ERROR: Failed to read the timer's maximum timeout: 
%d\n", errno);
       close(fd);
       return EXIT_FAILURE;
     }
@@ -371,5 +374,100 @@ This command may be used like so:
 
   printf("Maximum supported timeout: %" PRIu32 "\n", max_timeout);
 
+The ``TCIOC_TICK_GETSTATUS`` command invokes the ``getstatus`` lower-half
+operation and returns the current timer status expressed in timer ticks.
+The conversion from microseconds to ticks is performed by the timer
+upper-half driver.
+
+  The ``getstatus`` operation gathers the timer's current information.
+  When invoked via ``TCIOC_TICK_GETSTATUS``, the ``timeout`` and
+  ``timeleft`` fields are converted from microseconds to timer ticks
+  before being returned to the caller.
+
+  :param status: A writable pointer to a struct timer_status_s.
+                 This structure contains the same fields as used by
+                 `TCIOC_GETSTATUS`, but the timeout and timeleft
+                 values are expressed in timer ticks instead of
+                 microseconds.
+  :return: A Linux System Error Code for failing or 0 for success.
+
+This command may be used like so:
+
+.. code-block:: c
+
+  /* Get timer status in ticks */
+
+  ret = ioctl(fd, TCIOC_TICK_GETSTATUS, (unsigned long)((uintptr_t)&status));
+  if (ret < 0)
+    {
+      fprintf(stderr, "ERROR: Failed to get timer tick status: %d\n", errno);
+      close(fd);
+      return EXIT_FAILURE;
+    }
+
+  printf("flags: %08lx timeout(ticks): %lu timeleft(ticks): %lu\n",
+          (unsigned long)status.flags, (unsigned long)status.timeout,
+          (unsigned long)status.timeleft);
+
+The ``TCIOC_TICK_SETTIMEOUT`` command calls the ``settimeout`` operation and
+sets a new timeout value expressed in timer ticks.
+
+  The settimeout operation configures the timer to expire after the specified
+  number of timer ticks and resets the timer. The timeout value is converted
+  from ticks to microseconds by the timer upper-half driver before invoking
+  the lower-half settimeout operation.
+
+  :param timeout: An argument of type uint32_t that specifies the timeout
+                  interval in timer ticks.
+  :return: A Linux System Error Code for failing or 0 for success.
+
+This command may be used like so:
+
+.. code-block:: c
+
+  /* Set timer timeout in ticks */
+
+  printf("Set timer timeout to %lu ticks\n",
+  (unsigned long)timeout_ticks);
+
+  ret = ioctl(fd, TCIOC_TICK_SETTIMEOUT, timeout_ticks);
+  if (ret < 0)
+    {
+      fprintf(stderr, "ERROR: Failed to set timer tick timeout: %d\n", errno);
+      close(fd);
+      return EXIT_FAILURE;
+    }
+
+The ``TCIOC_TICK_MAXTIMEOUT`` command calls the ``maxtimeout`` operation and
+returns the maximum supported timeout value expressed in timer ticks.
+
+  The maxtimeout operation gets the maximum timeout value that can be
+  configured for the timer when using tick-based time units.
+
+  :param maxtimeout: A writable pointer to a variable of uint32_t type in
+                     which the maximum supported timeout (in ticks) will be
+                     stored.
+  :return: A Linux System Error Code for failing or 0 for success.
+
+This command may be used like so:
+
+.. code-block:: c
+
+  /* Get the maximum timer timeout in ticks */
+
+  printf("Get the maximum timer timeout in ticks\n");
+
+  ret = ioctl(fd, TCIOC_TICK_MAXTIMEOUT, (uint32_t *)(&max_timeout));
+  if (ret < 0)
+    {
+      fprintf(stderr, "ERROR: Failed to read timer tick maximum timeout: 
%d\n", errno);
+      close(fd);
+      return EXIT_FAILURE;
+    }
+
+  /* Print the maximum supported timeout (ticks) */
+
+  printf("Maximum supported timeout (ticks): %" PRIu32 "\n", max_timeout);
+
 Those snippets were taken from the Example which provides a great resource to
 demonstrate how to use those ``ioctl`` commands.
diff --git a/drivers/timers/timer.c b/drivers/timers/timer.c
index 1318fc79714..311cf6e0d9f 100644
--- a/drivers/timers/timer.c
+++ b/drivers/timers/timer.c
@@ -325,6 +325,24 @@ static int timer_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
       }
       break;
 
+    case TCIOC_TICK_GETSTATUS:
+      {
+        FAR struct timer_status_s *status;
+
+        /* Get the current timer status */
+
+        status = (FAR struct timer_status_s *)((uintptr_t)arg);
+        if (status)
+          {
+            ret = TIMER_TICK_GETSTATUS(lower, status);
+          }
+        else
+          {
+            ret = -EINVAL;
+          }
+      }
+      break;
+
     /* cmd:         TCIOC_SETTIMEOUT
      * Description: Reset the timeout to this value
      * Argument:    A 32-bit timeout value in microseconds.
@@ -341,6 +359,22 @@ static int timer_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
       }
       break;
 
+    /* cmd:         TCIOC_TICK_SETTIMEOUT
+     * Description: Reset the timeout to this value
+     * Argument:    A 32-bit timeout value in ticks.
+     *
+     * TODO: pass pointer to uint64 ns? Need to determine if these timers
+     * are 16 or 32 bit...
+     */
+
+    case TCIOC_TICK_SETTIMEOUT:
+      {
+        /* Set a new timeout value (and reset the timer) */
+
+        ret = TIMER_TICK_SETTIMEOUT(lower, (uint32_t)arg);
+      }
+      break;
+
     /* cmd:         TCIOC_NOTIFICATION
      * Description: Notify application via a signal when the timer expires.
      * Argument:    signal information
@@ -377,6 +411,19 @@ static int timer_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
       }
       break;
 
+    /* cmd:         TCIOC_TICK_MAXTIMEOUT
+     * Description: Get the maximum supported timeout value
+     * Argument:    A 32-bit timeout value in ticks.
+     */
+
+    case TCIOC_TICK_MAXTIMEOUT:
+      {
+        /*  Get the maximum supported timeout value */
+
+        ret = TIMER_TICK_MAXTIMEOUT(lower, (FAR uint32_t *)arg);
+      }
+      break;
+
     /* Any unrecognized IOCTL commands might be platform-specific ioctl
      * commands
      */
diff --git a/include/nuttx/timers/timer.h b/include/nuttx/timers/timer.h
index 87128c89153..43f7f3ee5dc 100644
--- a/include/nuttx/timers/timer.h
+++ b/include/nuttx/timers/timer.h
@@ -68,6 +68,13 @@
  *                      struct timer_notify_s.
  * TCIOC_MAXTIMEOUT   - Get the maximum supported timeout value
  *                      Argument: A 32-bit timeout value in microseconds.
+ * TCIOC_TICK_GETSTATUS  - Get the status of the timer.
+ *                         Argument:  A writeable pointer to struct
+ *                         timer_status_s.
+ * TCIOC_TICK_SETTIMEOUT - Reset the timer timeout to this value
+ *                         Argument: A 32-bit timeout value in ticks.
+ * TCIOC_TICK_MAXTIMEOUT - Get the maximum supported timeout value
+ *                         Argument: A 32-bit timeout value in ticks.
  *
  * WARNING: May change TCIOC_SETTIMEOUT to pass pointer to 64bit nanoseconds
  * or timespec structure.
@@ -78,12 +85,15 @@
  * range.
  */
 
-#define TCIOC_START        _TCIOC(0x0001)
-#define TCIOC_STOP         _TCIOC(0x0002)
-#define TCIOC_GETSTATUS    _TCIOC(0x0003)
-#define TCIOC_SETTIMEOUT   _TCIOC(0x0004)
-#define TCIOC_NOTIFICATION _TCIOC(0x0005)
-#define TCIOC_MAXTIMEOUT   _TCIOC(0x0006)
+#define TCIOC_START           _TCIOC(0x0001)
+#define TCIOC_STOP            _TCIOC(0x0002)
+#define TCIOC_GETSTATUS       _TCIOC(0x0003)
+#define TCIOC_SETTIMEOUT      _TCIOC(0x0004)
+#define TCIOC_NOTIFICATION    _TCIOC(0x0005)
+#define TCIOC_MAXTIMEOUT      _TCIOC(0x0006)
+#define TCIOC_TICK_GETSTATUS  _TCIOC(0x0007)
+#define TCIOC_TICK_SETTIMEOUT _TCIOC(0x0008)
+#define TCIOC_TICK_MAXTIMEOUT _TCIOC(0x0009)
 
 /* Bit Settings *************************************************************/
 

Reply via email to