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

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

commit 0e83c53afdc022d7547c923b02090d0cad2b58d1
Author: raiden00pl <[email protected]>
AuthorDate: Sun Nov 22 10:11:05 2026 +0100

    drivers/leds: Add LED1642GW 16-channel LED driver.
    
    PWM lower-half driver for the STMicroelectronics LED1642GW constant
    current LED driver, one PWM channel for each output.  The serial
    interface is bit-banged with board callbacks because LE selects the
    command in the middle of a word.  The board provides the PWCLK clock.
    
    Tested on nucleo-c071rb with X-NUCLEO-LED16A1.
    
    Assisted-by: Claude:claude-fable-5-2
    Signed-off-by: raiden00pl <[email protected]>
---
 Documentation/os/drivers/character/leds/index.rst  |   1 +
 .../os/drivers/character/leds/led1642gw.rst        |  66 ++++
 drivers/leds/CMakeLists.txt                        |   4 +
 drivers/leds/Kconfig                               |   9 +
 drivers/leds/Make.defs                             |   6 +
 drivers/leds/led1642gw.c                           | 383 +++++++++++++++++++++
 include/nuttx/leds/led1642gw.h                     | 113 ++++++
 7 files changed, 582 insertions(+)

diff --git a/Documentation/os/drivers/character/leds/index.rst 
b/Documentation/os/drivers/character/leds/index.rst
index cb3f723bd6e..0aa5b2a56c0 100644
--- a/Documentation/os/drivers/character/leds/index.rst
+++ b/Documentation/os/drivers/character/leds/index.rst
@@ -127,3 +127,4 @@ The ``include/nuttx/board.h`` includes the following 
declarations:
   userled.rst
   ws2812.rst
   ktd2052.rst
+  led1642gw.rst
diff --git a/Documentation/os/drivers/character/leds/led1642gw.rst 
b/Documentation/os/drivers/character/leds/led1642gw.rst
new file mode 100644
index 00000000000..6769876c183
--- /dev/null
+++ b/Documentation/os/drivers/character/leds/led1642gw.rst
@@ -0,0 +1,66 @@
+=========
+LED1642GW
+=========
+
+This is a driver for the STMicroelectronics LED1642GW, a 16-channel constant
+current LED driver with a 4-wire serial interface and 12/16-bit PWM
+brightness control for each channel. The driver is a PWM lower-half driver
+with one PWM channel for each output.
+
+Driver Configuration
+====================
+
+To use the driver, enable the following option in menuconfig:
+
+- ``Device Drivers -> LED Support -> LED1642GW LED Driver``
+  (``CONFIG_LEDS_LED1642GW``)
+
+``CONFIG_PWM_NCHANNELS`` sets how many outputs can be changed with one
+request.
+
+Driver Usage
+============
+
+Board-specific logic must call ``led1642gw_initialize`` during the board
+initialization sequence. This function takes the board configuration, writes
+the configuration register, turns all outputs off and returns the PWM
+lower-half instance. The instance can be registered with ``pwm_register``
+or used by other drivers, e.g. ``rgbled_register``.
+
+The serial interface is bit-banged because the command is selected by the
+number of CLK rising edges with LE high at the end of each 16-bit word.
+The board configuration provides the functions that set the state of the
+CLK, SDI and LE lines, the current gain, the current range and the
+brightness resolution. The board must also provide a free-running clock on
+the PWCLK pin: the PWM frequency of the outputs is the PWCLK frequency
+divided by 4096 (12-bit mode) or by 65536 (16-bit mode).
+
+.. code-block:: c
+
+    #include <nuttx/leds/led1642gw.h>
+    #include <nuttx/leds/rgbled.h>
+
+    static const struct led1642gw_config_s g_config =
+    {
+      .clk   = board_led1642gw_clk,
+      .sdi   = board_led1642gw_sdi,
+      .le    = board_led1642gw_le,
+      .gain  = LED1642GW_GAIN_MAX,
+      .range = false,
+      .pwm16 = false,
+    };
+
+    /* In your board's initialization logic */
+
+    pwm = led1642gw_initialize(&g_config);
+
+    /* RGB LED on outputs 0, 1 and 2 */
+
+    rgbled_register("/dev/rgbled0", pwm, pwm, pwm, 1, 2, 3);
+
+The PWM channel ``n`` is the output ``n - 1`` and the duty is the brightness
+of the output. A duty of 0 turns the output off. The outputs that are not in
+the channel list of a request are not changed. The requested PWM frequency is
+ignored. Stopping the PWM turns all outputs off.
+
+The error detection and the SDO line are not supported.
diff --git a/drivers/leds/CMakeLists.txt b/drivers/leds/CMakeLists.txt
index 70ffdbbaaac..76079eb24fe 100644
--- a/drivers/leds/CMakeLists.txt
+++ b/drivers/leds/CMakeLists.txt
@@ -58,6 +58,10 @@ if(CONFIG_NCP5623C)
   list(APPEND SRCS ncp5623c.c)
 endif()
 
+if(CONFIG_LEDS_LED1642GW)
+  list(APPEND SRCS led1642gw.c)
+endif()
+
 if(CONFIG_WS2812)
   list(APPEND SRCS ws2812.c)
 endif()
diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
index f4cb58ca50e..edb20c739d8 100644
--- a/drivers/leds/Kconfig
+++ b/drivers/leds/Kconfig
@@ -190,6 +190,15 @@ config NCP5623C
                Enable support for the onsemi NCP5623C LED driver which can be
                utilized to drive up to 3 LED's.
 
+config LEDS_LED1642GW
+       bool "LED1642GW LED Driver"
+       default n
+       depends on PWM
+       ---help---
+               Enable support for the STMicroelectronics LED1642GW LED driver
+               which can be utilized to drive up to 16 LED's.  The device is
+               a PWM lower-half driver with one channel for each output.
+
 config WS2812
        bool "WS2812 LED Driver"
        default n
diff --git a/drivers/leds/Make.defs b/drivers/leds/Make.defs
index 7567f1d5e27..f555b8655fe 100644
--- a/drivers/leds/Make.defs
+++ b/drivers/leds/Make.defs
@@ -72,6 +72,12 @@ ifeq ($(CONFIG_NCP5623C),y)
   LEDVPATH = :leds
 endif
 
+ifeq ($(CONFIG_LEDS_LED1642GW),y)
+  CSRCS += led1642gw.c
+  LEDDEPATH = --dep-path leds
+  LEDVPATH = :leds
+endif
+
 ifeq ($(CONFIG_WS2812),y)
   CSRCS += ws2812.c
   LEDDEPATH = --dep-path leds
diff --git a/drivers/leds/led1642gw.c b/drivers/leds/led1642gw.c
new file mode 100644
index 00000000000..b0ee452e416
--- /dev/null
+++ b/drivers/leds/led1642gw.c
@@ -0,0 +1,383 @@
+/****************************************************************************
+ * drivers/leds/led1642gw.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+#include <nuttx/debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/mutex.h>
+#include <nuttx/leds/led1642gw.h>
+#include <nuttx/timers/pwm.h>
+
+#ifdef CONFIG_LEDS_LED1642GW
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Digital keys: the number of CLK rising edges with LE high that ends
+ * a 16-bit word and selects the command.
+ */
+
+#define LED1642GW_KEY_SWITCH   2    /* Write switch */
+#define LED1642GW_KEY_DATA     4    /* Brightness data latch */
+#define LED1642GW_KEY_GLOBAL   6    /* Brightness global latch */
+#define LED1642GW_KEY_WRCFG    7    /* Write configuration register */
+
+/* Configuration register */
+
+#define LED1642GW_CFG_GAIN_MASK 0x003f    /* Bits 0-5: Current gain */
+#define LED1642GW_CFG_RANGE     (1 << 6)  /* Bit 6: Current range */
+#define LED1642GW_CFG_PWM12     (1 << 15) /* Bit 15: 12-bit brightness */
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct led1642gw_dev_s
+{
+  FAR const struct pwm_ops_s *ops;    /* Must be first */
+  FAR const struct led1642gw_config_s *config;
+  mutex_t  lock;
+  uint16_t brightness[LED1642GW_NCHANNELS];
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static void led1642gw_write(FAR struct led1642gw_dev_s *priv, uint16_t data,
+                            int key);
+static void led1642gw_update(FAR struct led1642gw_dev_s *priv);
+
+static int led1642gw_clear(FAR struct led1642gw_dev_s *priv);
+
+static int led1642gw_setup(FAR struct pwm_lowerhalf_s *dev);
+static int led1642gw_shutdown(FAR struct pwm_lowerhalf_s *dev);
+static int led1642gw_start(FAR struct pwm_lowerhalf_s *dev,
+                           FAR const struct pwm_info_s *info);
+static int led1642gw_stop(FAR struct pwm_lowerhalf_s *dev);
+static int led1642gw_ioctl(FAR struct pwm_lowerhalf_s *dev, int cmd,
+                           unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct pwm_ops_s g_led1642gw_ops =
+{
+  led1642gw_setup,              /* setup */
+  led1642gw_shutdown,           /* shutdown */
+  led1642gw_start,              /* start */
+  led1642gw_stop,               /* stop */
+  led1642gw_ioctl,              /* ioctl */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: led1642gw_write
+ *
+ * Description:
+ *   Shift out one 16-bit word, MSB first.  LE is high for the last 'key'
+ *   CLK rising edges.
+ *
+ ****************************************************************************/
+
+static void led1642gw_write(FAR struct led1642gw_dev_s *priv, uint16_t data,
+                            int key)
+{
+  FAR const struct led1642gw_config_s *config = priv->config;
+  int i;
+
+  for (i = 15; i >= 0; i--)
+    {
+      config->sdi(config, (data & (1 << i)) != 0);
+
+      if (i == key - 1)
+        {
+          config->le(config, true);
+        }
+
+      config->clk(config, true);
+      config->clk(config, false);
+    }
+
+  config->le(config, false);
+}
+
+/****************************************************************************
+ * Name: led1642gw_update
+ *
+ * Description:
+ *   Load the brightness of all channels and enable the channels with
+ *   non-zero brightness.
+ *
+ ****************************************************************************/
+
+static void led1642gw_update(FAR struct led1642gw_dev_s *priv)
+{
+  uint16_t mask = 0;
+  uint16_t data;
+  int i;
+
+  /* Channel 15 goes first, the last word latches all channels at once */
+
+  for (i = LED1642GW_NCHANNELS - 1; i >= 0; i--)
+    {
+      data = priv->brightness[i];
+      if (!priv->config->pwm16)
+        {
+          data >>= 4;
+        }
+
+      led1642gw_write(priv, data,
+                      i > 0 ? LED1642GW_KEY_DATA : LED1642GW_KEY_GLOBAL);
+
+      if (data > 0)
+        {
+          mask |= 1 << i;
+        }
+    }
+
+  led1642gw_write(priv, mask, LED1642GW_KEY_SWITCH);
+}
+
+/****************************************************************************
+ * Name: led1642gw_clear
+ *
+ * Description:
+ *   Turn all outputs off.
+ *
+ ****************************************************************************/
+
+static int led1642gw_clear(FAR struct led1642gw_dev_s *priv)
+{
+  int ret;
+
+  ret = nxmutex_lock(&priv->lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  memset(priv->brightness, 0, sizeof(priv->brightness));
+  led1642gw_update(priv);
+  nxmutex_unlock(&priv->lock);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: led1642gw_setup
+ *
+ * Description:
+ *   This method is called when the driver is opened.  The device is
+ *   configured in led1642gw_initialize(), nothing to do here.
+ *
+ ****************************************************************************/
+
+static int led1642gw_setup(FAR struct pwm_lowerhalf_s *dev)
+{
+  return OK;
+}
+
+/****************************************************************************
+ * Name: led1642gw_shutdown
+ *
+ * Description:
+ *   This method is called when the driver is closed.  Turn all outputs off.
+ *
+ ****************************************************************************/
+
+static int led1642gw_shutdown(FAR struct pwm_lowerhalf_s *dev)
+{
+  return led1642gw_clear((FAR struct led1642gw_dev_s *)dev);
+}
+
+/****************************************************************************
+ * Name: led1642gw_start
+ *
+ * Description:
+ *   Set the brightness of the outputs given in the channel list.  Channel n
+ *   is the output n - 1.  The other outputs are not changed.  The frequency
+ *   is ignored: it is set by the PWCLK clock.
+ *
+ ****************************************************************************/
+
+static int led1642gw_start(FAR struct pwm_lowerhalf_s *dev,
+                           FAR const struct pwm_info_s *info)
+{
+  FAR struct led1642gw_dev_s *priv = (FAR struct led1642gw_dev_s *)dev;
+  uint32_t duty;
+  int channel;
+  int ret;
+  int i;
+
+  for (i = 0; i < CONFIG_PWM_NCHANNELS; i++)
+    {
+      channel = info->channels[i].channel;
+      if (channel < 0)
+        {
+          break;
+        }
+
+      if (channel > LED1642GW_NCHANNELS)
+        {
+          lederr("ERROR: Invalid channel: %d\n", channel);
+          return -EINVAL;
+        }
+    }
+
+  ret = nxmutex_lock(&priv->lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  for (i = 0; i < CONFIG_PWM_NCHANNELS; i++)
+    {
+      channel = info->channels[i].channel;
+      if (channel < 0)
+        {
+          break;
+        }
+
+      if (channel == 0)
+        {
+          continue;
+        }
+
+      duty = info->channels[i].duty;
+      if (duty > UINT16_MAX)
+        {
+          duty = UINT16_MAX;
+        }
+
+      priv->brightness[channel - 1] = duty;
+    }
+
+  led1642gw_update(priv);
+  nxmutex_unlock(&priv->lock);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: led1642gw_stop
+ *
+ * Description:
+ *   Turn all outputs off.
+ *
+ ****************************************************************************/
+
+static int led1642gw_stop(FAR struct pwm_lowerhalf_s *dev)
+{
+  return led1642gw_clear((FAR struct led1642gw_dev_s *)dev);
+}
+
+/****************************************************************************
+ * Name: led1642gw_ioctl
+ ****************************************************************************/
+
+static int led1642gw_ioctl(FAR struct pwm_lowerhalf_s *dev, int cmd,
+                           unsigned long arg)
+{
+  return -ENOTTY;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: led1642gw_initialize
+ *
+ * Description:
+ *   Initialize one LED1642GW device: write the configuration register and
+ *   turn all outputs off.  The returned PWM lower half can be passed to
+ *   pwm_register() or rgbled_register().
+ *
+ * Input Parameters:
+ *   config - The board configuration.  It must persist as long as the
+ *            driver persists.
+ *
+ * Returned Value:
+ *   Valid PWM lower half structure reference on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct pwm_lowerhalf_s *
+led1642gw_initialize(FAR const struct led1642gw_config_s *config)
+{
+  FAR struct led1642gw_dev_s *priv;
+  uint16_t regval;
+
+  /* Sanity check */
+
+  DEBUGASSERT(config != NULL && config->clk != NULL &&
+              config->sdi != NULL && config->le != NULL);
+
+  /* Initialize the LED1642GW device structure */
+
+  priv = kmm_zalloc(sizeof(struct led1642gw_dev_s));
+  if (priv == NULL)
+    {
+      lederr("ERROR: Failed to allocate instance of led1642gw_dev_s\n");
+      return NULL;
+    }
+
+  priv->ops    = &g_led1642gw_ops;
+  priv->config = config;
+  nxmutex_init(&priv->lock);
+
+  /* Write the configuration register and turn all outputs off */
+
+  regval = config->gain & LED1642GW_CFG_GAIN_MASK;
+
+  if (config->range)
+    {
+      regval |= LED1642GW_CFG_RANGE;
+    }
+
+  if (!config->pwm16)
+    {
+      regval |= LED1642GW_CFG_PWM12;
+    }
+
+  led1642gw_write(priv, regval, LED1642GW_KEY_WRCFG);
+  led1642gw_update(priv);
+
+  return (FAR struct pwm_lowerhalf_s *)priv;
+}
+
+#endif /* CONFIG_LEDS_LED1642GW */
diff --git a/include/nuttx/leds/led1642gw.h b/include/nuttx/leds/led1642gw.h
new file mode 100644
index 00000000000..0fe85df51f9
--- /dev/null
+++ b/include/nuttx/leds/led1642gw.h
@@ -0,0 +1,113 @@
+/****************************************************************************
+ * include/nuttx/leds/led1642gw.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_LEDS_LED1642GW_H
+#define __INCLUDE_NUTTX_LEDS_LED1642GW_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/timers/pwm.h>
+
+#include <stdbool.h>
+#include <stdint.h>
+
+/* Configuration
+ * CONFIG_LEDS_LED1642GW - Enables support for the LED1642GW driver
+ */
+
+#ifdef CONFIG_LEDS_LED1642GW
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Number of constant current outputs */
+
+#define LED1642GW_NCHANNELS    16
+
+/* Maximum value of the current gain */
+
+#define LED1642GW_GAIN_MAX     63
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/* The LED1642GW serial interface is bit-banged with the board logic that
+ * drives the CLK, SDI and LE lines.  The board must also provide the PWCLK
+ * clock that feeds the brightness counter of the device.
+ */
+
+struct led1642gw_config_s
+{
+  /* Set the state of the CLK, SDI and LE lines */
+
+  CODE void (*clk)(FAR const struct led1642gw_config_s *config, bool state);
+  CODE void (*sdi)(FAR const struct led1642gw_config_s *config, bool state);
+  CODE void (*le)(FAR const struct led1642gw_config_s *config, bool state);
+
+  uint8_t gain;     /* Current gain: 0 - LED1642GW_GAIN_MAX */
+  bool    range;    /* Current range: false = low, true = high */
+  bool    pwm16;    /* Brightness resolution: false = 12-bit, true = 16-bit */
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/****************************************************************************
+ * Name: led1642gw_initialize
+ *
+ * Description:
+ *   Initialize one LED1642GW device: write the configuration register and
+ *   turn all outputs off.  The returned PWM lower half can be passed to
+ *   pwm_register() or rgbled_register().
+ *
+ *   The PWM channel n is the output n - 1 and the duty is the brightness
+ *   of the output.  The PWM frequency is set by the PWCLK clock.
+ *
+ * Input Parameters:
+ *   config - The board configuration.  It must persist as long as the
+ *            driver persists.
+ *
+ * Returned Value:
+ *   Valid PWM lower half structure reference on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct pwm_lowerhalf_s *
+led1642gw_initialize(FAR const struct led1642gw_config_s *config);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CONFIG_LEDS_LED1642GW */
+#endif /* __INCLUDE_NUTTX_LEDS_LED1642GW_H */

Reply via email to