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 9b15cec2fc6064399c6d2e56d72f80aea07ec388
Author: p-szafonimateusz <[email protected]>
AuthorDate: Mon May 25 16:59:22 2026 +0200

    drivers/i2c: add dummy bus support
    
    Add a callback-backed dummy I2C lower-half driver intended for transport
    and command-line validation. Board logic can register callback-backed
    dummy targets on this bus without adding hardcoded devices to the
    common driver.
    
    Signed-off-by: p-szafonimateusz <[email protected]>
---
 drivers/i2c/CMakeLists.txt    |   4 +
 drivers/i2c/Kconfig           |   9 ++
 drivers/i2c/Make.defs         |   4 +
 drivers/i2c/i2c_dummy.c       | 210 ++++++++++++++++++++++++++++++++++++++++++
 include/nuttx/i2c/i2c_dummy.h |  82 +++++++++++++++++
 5 files changed, 309 insertions(+)

diff --git a/drivers/i2c/CMakeLists.txt b/drivers/i2c/CMakeLists.txt
index e882fae1ab8..bb1365faf01 100644
--- a/drivers/i2c/CMakeLists.txt
+++ b/drivers/i2c/CMakeLists.txt
@@ -40,6 +40,10 @@ if(CONFIG_I2C)
     list(APPEND SRCS i2c_slave_driver.c)
   endif()
 
+  if(CONFIG_I2C_DUMMY)
+    list(APPEND SRCS i2c_dummy.c)
+  endif()
+
   # Include the selected I2C multiplexer drivers
 
   if(CONFIG_I2CMULTIPLEXER_PCA9540BDP)
diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig
index d1453ce930e..03642d7a1ed 100644
--- a/drivers/i2c/Kconfig
+++ b/drivers/i2c/Kconfig
@@ -143,6 +143,15 @@ config I2C_BITBANG_IOEXPANDER
 
 endif # I2C_BITBANG
 
+config I2C_DUMMY
+       bool "Callback-backed dummy I2C bus"
+       default n
+       ---help---
+               Enable a dummy I2C lower-half intended for transport and
+               command-line validation. Board logic can register 
callback-backed
+               dummy targets on this bus without adding hardcoded devices to 
the
+               common driver.
+
 config I2C_DRIVER
        bool "I2C character driver"
        default n
diff --git a/drivers/i2c/Make.defs b/drivers/i2c/Make.defs
index 5d7e276493b..fec4e1e42e1 100644
--- a/drivers/i2c/Make.defs
+++ b/drivers/i2c/Make.defs
@@ -43,6 +43,10 @@ ifeq ($(CONFIG_I2C_SLAVE_DRIVER),y)
 CSRCS += i2c_slave_driver.c
 endif
 
+ifeq ($(CONFIG_I2C_DUMMY),y)
+CSRCS += i2c_dummy.c
+endif
+
 # Include the selected I2C multiplexer drivers
 
 ifeq ($(CONFIG_I2CMULTIPLEXER_PCA9540BDP),y)
diff --git a/drivers/i2c/i2c_dummy.c b/drivers/i2c/i2c_dummy.c
new file mode 100644
index 00000000000..6684ec50530
--- /dev/null
+++ b/drivers/i2c/i2c_dummy.c
@@ -0,0 +1,210 @@
+/****************************************************************************
+ * drivers/i2c/i2c_dummy.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 <stdint.h>
+#include <string.h>
+
+#include <nuttx/i2c/i2c_dummy.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mutex.h>
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct i2c_dummy_dev_s
+{
+  struct i2c_master_s i2c;
+  mutex_t lock;
+  FAR struct i2c_dummy_device_s * const *devices;
+  int ndevices;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int i2c_dummy_setup(FAR struct i2c_master_s *dev);
+static int i2c_dummy_shutdown(FAR struct i2c_master_s *dev);
+static uint8_t i2c_dummy_status(FAR struct i2c_master_s *dev);
+#ifdef CONFIG_I2C_RESET
+static int i2c_dummy_reset(FAR struct i2c_master_s *dev);
+#endif
+static FAR struct i2c_dummy_device_s *
+  i2c_dummy_find(FAR struct i2c_dummy_dev_s *priv, uint16_t addr);
+static int i2c_dummy_transfer(FAR struct i2c_master_s *dev,
+                              FAR struct i2c_msg_s *msgs, int count);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct i2c_ops_s g_i2c_dummy_ops =
+{
+  i2c_dummy_transfer,
+#ifdef CONFIG_I2C_RESET
+  i2c_dummy_reset,
+#endif
+  i2c_dummy_setup,
+  i2c_dummy_shutdown,
+  i2c_dummy_status
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int i2c_dummy_setup(FAR struct i2c_master_s *dev)
+{
+  return OK;
+}
+
+static int i2c_dummy_shutdown(FAR struct i2c_master_s *dev)
+{
+  return OK;
+}
+
+static uint8_t i2c_dummy_status(FAR struct i2c_master_s *dev)
+{
+  return I2C_STATUS_PRESENT;
+}
+
+#ifdef CONFIG_I2C_RESET
+static int i2c_dummy_reset(FAR struct i2c_master_s *dev)
+{
+  FAR struct i2c_dummy_dev_s *priv = (FAR struct i2c_dummy_dev_s *)dev;
+  FAR struct i2c_dummy_device_s *dummy;
+  int i;
+
+  nxmutex_lock(&priv->lock);
+  for (i = 0; i < priv->ndevices; i++)
+    {
+      dummy = priv->devices[i];
+      if (dummy->reset != NULL)
+        {
+          dummy->reset(dummy);
+        }
+    }
+
+  nxmutex_unlock(&priv->lock);
+
+  return OK;
+}
+#endif
+
+static FAR struct i2c_dummy_device_s *
+i2c_dummy_find(FAR struct i2c_dummy_dev_s *priv, uint16_t addr)
+{
+  FAR struct i2c_dummy_device_s *dummy;
+  int i;
+
+  for (i = 0; i < priv->ndevices; i++)
+    {
+      dummy = priv->devices[i];
+      if (dummy->addr == addr)
+        {
+          return dummy;
+        }
+    }
+
+  return NULL;
+}
+
+static int i2c_dummy_transfer(FAR struct i2c_master_s *dev,
+                             FAR struct i2c_msg_s *msgs, int count)
+{
+  FAR struct i2c_dummy_dev_s *priv = (FAR struct i2c_dummy_dev_s *)dev;
+  FAR struct i2c_dummy_device_s *dummy;
+  int ret = OK;
+  int i;
+
+  DEBUGASSERT(priv != NULL);
+
+  if (msgs == NULL || count <= 0)
+    {
+      return -EINVAL;
+    }
+
+  nxmutex_lock(&priv->lock);
+
+  for (i = 0; i < count; i++)
+    {
+      dummy = i2c_dummy_find(priv, msgs[i].addr);
+      if (dummy == NULL)
+        {
+          ret = -ENXIO;
+          break;
+        }
+
+      if (dummy->transfer == NULL)
+        {
+          ret = -ENOSYS;
+          break;
+        }
+
+      ret = dummy->transfer(dummy, &msgs[i]);
+      if (ret < 0)
+        {
+          break;
+        }
+    }
+
+  nxmutex_unlock(&priv->lock);
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR struct i2c_master_s *
+i2c_dummy_initialize(FAR struct i2c_dummy_device_s * const *devices,
+                     int ndevices)
+{
+  FAR struct i2c_dummy_dev_s *priv;
+
+  if (ndevices < 0 || (devices == NULL && ndevices > 0))
+    {
+      return NULL;
+    }
+
+  priv = kmm_zalloc(sizeof(*priv));
+  if (priv == NULL)
+    {
+      return NULL;
+    }
+
+  priv->i2c.ops = &g_i2c_dummy_ops;
+  priv->devices = devices;
+  priv->ndevices = ndevices;
+  nxmutex_init(&priv->lock);
+
+  return &priv->i2c;
+}
diff --git a/include/nuttx/i2c/i2c_dummy.h b/include/nuttx/i2c/i2c_dummy.h
new file mode 100644
index 00000000000..49557229e2a
--- /dev/null
+++ b/include/nuttx/i2c/i2c_dummy.h
@@ -0,0 +1,82 @@
+/****************************************************************************
+ * include/nuttx/i2c/i2c_dummy.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_I2C_I2C_DUMMY_H
+#define __INCLUDE_NUTTX_I2C_I2C_DUMMY_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+
+#include <nuttx/i2c/i2c_master.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct i2c_dummy_device_s
+{
+  CODE int (*transfer)(FAR struct i2c_dummy_device_s *dev,
+                       FAR struct i2c_msg_s *msg);
+#ifdef CONFIG_I2C_RESET
+  CODE void (*reset)(FAR struct i2c_dummy_device_s *dev);
+#endif
+  uint16_t addr;
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/****************************************************************************
+ * Name: i2c_dummy_initialize
+ *
+ * Description:
+ *   Initialize a dummy I2C bus for transport and command-line validation.
+ *   The device pointer table must remain valid for the lifetime of the bus.
+ *
+ * Returned Value:
+ *   A valid I2C lower-half instance on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_I2C_DUMMY
+FAR struct i2c_master_s *
+i2c_dummy_initialize(FAR struct i2c_dummy_device_s * const *devices,
+                     int ndevices);
+
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __INCLUDE_NUTTX_I2C_I2C_DUMMY_H */

Reply via email to