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 bc750536e7e501afb6bbe921e0bd99e2b4f537f7 Author: fangpeina <[email protected]> AuthorDate: Thu Jul 16 19:24:03 2026 +0800 drivers/ioexpander: add toggle op to IOExpander interface Add an optional ioe_toggle callback to struct ioexpander_ops_s and the IOEXP_TOGGLE access macro. When the lower half provides this op, gpio_lower_half forwards it as go_toggle so that GPIOC_TOGGLE reaches the hardware atomically instead of falling back to a non-atomic read-invert-write sequence in gpio.c. Signed-off-by: fangpeina <[email protected]> --- drivers/ioexpander/gpio_lower_half.c | 21 +++++++++++++++++++++ include/nuttx/ioexpander/ioexpander.h | 22 ++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/drivers/ioexpander/gpio_lower_half.c b/drivers/ioexpander/gpio_lower_half.c index facdad59cfe..4d28b50cb70 100644 --- a/drivers/ioexpander/gpio_lower_half.c +++ b/drivers/ioexpander/gpio_lower_half.c @@ -72,6 +72,7 @@ static int gplh_handler(FAR struct ioexpander_dev_s *ioe, static int gplh_read(FAR struct gpio_dev_s *gpio, FAR bool *value); static int gplh_write(FAR struct gpio_dev_s *gpio, bool value); +static int gplh_toggle(FAR struct gpio_dev_s *gpio, FAR bool *value); #ifdef CONFIG_IOEXPANDER_INT_ENABLE static int gplh_attach(FAR struct gpio_dev_s *gpio, pin_interrupt_t callback); @@ -104,6 +105,7 @@ static const struct gpio_operations_s g_gplh_ops = gplh_setpintype, gplh_setdebounce, gplh_setmask, + gplh_toggle, }; /* Identifies the type of the GPIO pin */ @@ -223,6 +225,25 @@ static int gplh_write(FAR struct gpio_dev_s *gpio, bool value) return IOEXP_WRITEPIN(priv->ioe, priv->pin, value); } +/**************************************************************************** + * Name: gplh_toggle + * + * Description: + * Atomically toggle the value of an I/O expander output pin. + * + ****************************************************************************/ + +static int gplh_toggle(FAR struct gpio_dev_s *gpio, FAR bool *value) +{ + FAR struct gplh_dev_s *priv = (FAR struct gplh_dev_s *)gpio; + + DEBUGASSERT(priv != NULL && priv->ioe != NULL && value != NULL); + + gpioinfo("pin%u: toggle\n", priv->pin); + + return IOEXP_TOGGLE(priv->ioe, priv->pin, value); +} + /**************************************************************************** * Name: gplh_attach * diff --git a/include/nuttx/ioexpander/ioexpander.h b/include/nuttx/ioexpander/ioexpander.h index f9fba627495..91d81614a68 100644 --- a/include/nuttx/ioexpander/ioexpander.h +++ b/include/nuttx/ioexpander/ioexpander.h @@ -189,6 +189,26 @@ #define IOEXP_READBUF(dev,pin,valptr) ((dev)->ops->ioe_readbuf(dev,pin,valptr)) +/**************************************************************************** + * Name: IOEXP_TOGGLE + * + * Description: + * Atomically toggle a pin and return the new level. Optional. + * If the lower half does not support this, -ENOTSUP is returned. + * + * Input Parameters: + * dev - Device-specific state data + * pin - The index of the pin to toggle + * valptr - Pointer to a buffer where the new level is stored. + * + * Returned Value: + * 0 on success, else a negative error code + * + ****************************************************************************/ + +#define IOEXP_TOGGLE(dev,pin,valptr) \ + ((dev)->ops->ioe_toggle ? (dev)->ops->ioe_toggle(dev,pin,valptr) : -ENOTSUP) + #ifdef CONFIG_IOEXPANDER_MULTIPIN /**************************************************************************** @@ -384,6 +404,8 @@ struct ioexpander_ops_s CODE int (*ioe_setpwm)(FAR struct ioexpander_dev_s *dev, uint8_t pin, uint16_t duty); #endif + CODE int (*ioe_toggle)(FAR struct ioexpander_dev_s *dev, uint8_t pin, + FAR bool *value); }; struct ioexpander_dev_s
