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 6334873e0434b2855e48e96a3c74ba1cace9570e
Author: fangpeina <[email protected]>
AuthorDate: Fri Jul 3 17:08:11 2026 +0800

    drivers/ioexpander: add GPIOC_TOGGLE ioctl and go_toggle op
    
    Add atomic toggle support to the NuttX GPIO framework:
    - New go_toggle method in gpio_operations_s (optional)
    - New GPIOC_TOGGLE ioctl command
    - Software fallback (read+invert+write) when go_toggle is NULL
    
    Hardware drivers with native toggle registers can implement
    go_toggle for atomic, race-free pin inversion.
    
    Signed-off-by: fangpeina <[email protected]>
---
 drivers/ioexpander/gpio.c       | 41 +++++++++++++++++++++++++++++++++++++++++
 include/nuttx/ioexpander/gpio.h | 10 ++++++++++
 2 files changed, 51 insertions(+)

diff --git a/drivers/ioexpander/gpio.c b/drivers/ioexpander/gpio.c
index c4823759597..3c2a109658a 100644
--- a/drivers/ioexpander/gpio.c
+++ b/drivers/ioexpander/gpio.c
@@ -1059,6 +1059,47 @@ static int gpio_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
           break;
         }
 
+      /* Command:     GPIOC_TOGGLE
+       * Description: Toggle the value of an output GPIO.
+       * Argument:    A pointer to a bool value to receive the new level.
+       */
+
+      case GPIOC_TOGGLE:
+        if (dev->gp_pintype == GPIO_OUTPUT_PIN ||
+            dev->gp_pintype == GPIO_OUTPUT_PIN_OPENDRAIN)
+          {
+            FAR bool *ptr = (FAR bool *)((uintptr_t)arg);
+
+            DEBUGASSERT(ptr != NULL);
+
+            if (dev->gp_ops->go_toggle != NULL)
+              {
+                ret = dev->gp_ops->go_toggle(dev, ptr);
+              }
+            else
+              {
+                bool val;
+
+                DEBUGASSERT(dev->gp_ops->go_read != NULL);
+                DEBUGASSERT(dev->gp_ops->go_write != NULL);
+                ret = dev->gp_ops->go_read(dev, &val);
+                if (ret >= 0)
+                  {
+                    val = !val;
+                    ret = dev->gp_ops->go_write(dev, val);
+                    if (ret >= 0)
+                      {
+                        *ptr = val;
+                      }
+                  }
+              }
+          }
+        else
+          {
+            ret = -EACCES;
+          }
+        break;
+
       /* Unrecognized command */
 
       default:
diff --git a/include/nuttx/ioexpander/gpio.h b/include/nuttx/ioexpander/gpio.h
index 577a7b4b637..0cb7d7a4121 100644
--- a/include/nuttx/ioexpander/gpio.h
+++ b/include/nuttx/ioexpander/gpio.h
@@ -82,6 +82,11 @@
  *              interrupt source remains enabled.
  * Argument:    true to mask the interrupt;
  *              false to unmask the interrupt.
+ *
+ * Command:     GPIOC_TOGGLE
+ * Description: Toggle the value of an output GPIO and return the new level.
+ * Argument:    A pointer to a bool value to receive the result:
+ *              false=low value; true=high value.
  */
 
 #define GPIOC_WRITE       _GPIOC(1)
@@ -92,6 +97,7 @@
 #define GPIOC_SETPINTYPE  _GPIOC(6)
 #define GPIOC_SETDEBOUNCE _GPIOC(7)
 #define GPIOC_IRQ_SETMASK _GPIOC(8)
+#define GPIOC_TOGGLE      _GPIOC(9)
 
 /****************************************************************************
  * Public Types
@@ -141,6 +147,9 @@ typedef CODE int (*pin_interrupt_t)(FAR struct gpio_dev_s 
*dev, uint8_t pin);
  *   - go_setpintype.  Required for all pin types.
  *   - go_setdebounce.  Required for all pin types.
  *   - go_setmask.  Required only for the GPIO_INTERRUPT_PIN pin
+ *   - go_toggle.  Optional for GPIO_OUTPUT_PIN pin type.  When provided,
+ *     performs an atomic toggle and returns the new level.  When NULL,
+ *     GPIOC_TOGGLE falls back to go_read + go_write.
  */
 
 struct gpio_dev_s;
@@ -158,6 +167,7 @@ struct gpio_operations_s
   CODE int (*go_setdebounce)(FAR struct gpio_dev_s *gpio,
                              unsigned long duration);
   CODE int (*go_setmask)(FAR struct gpio_dev_s *dev, bool enable);
+  CODE int (*go_toggle)(FAR struct gpio_dev_s *dev, FAR bool *value);
 
   /* Describe whatever only this pin's controller can say, for
    * /proc/gpio: which pad carries the line, how its trigger is

Reply via email to