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 c32466a873757231be334009ec6592220cdaae0d
Author: p-szafonimateusz <[email protected]>
AuthorDate: Mon May 25 16:58:48 2026 +0200

    drivers/i2c: add rpmsg support
    
    Add I2C RPMsg client and server drivers. The client forwards I2C
    transfers to a remote NuttX instance over RPMsg. The server exports
    a local I2C lower-half to remote RPMsg I2C clients.
    
    Assisted-by: opencode:zhipuai/glm-5.3
    
    Signed-off-by: p-szafonimateusz <[email protected]>
---
 Documentation/os/drivers/special/i2c.rst |  18 ++
 drivers/i2c/CMakeLists.txt               |   8 +
 drivers/i2c/Kconfig                      |  15 +
 drivers/i2c/Make.defs                    |   8 +
 drivers/i2c/i2c_rpmsg.c                  | 516 +++++++++++++++++++++++++++++++
 drivers/i2c/i2c_rpmsg.h                  |  90 ++++++
 drivers/i2c/i2c_rpmsg_server.c           | 323 +++++++++++++++++++
 include/nuttx/i2c/i2c_rpmsg.h            |  88 ++++++
 8 files changed, 1066 insertions(+)

diff --git a/Documentation/os/drivers/special/i2c.rst 
b/Documentation/os/drivers/special/i2c.rst
index b53677df908..320b40bc8f9 100644
--- a/Documentation/os/drivers/special/i2c.rst
+++ b/Documentation/os/drivers/special/i2c.rst
@@ -228,3 +228,21 @@ The frontend and backend share the OASIS VirtIO I2C 
wire-format definitions in
 ``include/nuttx/virtio/virtio-i2c.h``. The frontend can be validated against a
 host vhost-user backend, or against a second NuttX instance running the vhost
 backend over rptun/IVSHMEM.
+
+==========
+RPMsg I2C
+==========
+
+NuttX can expose an I2C controller across a RPMsg link. Enable
+``CONFIG_I2C_RPMSG`` on the client side and ``CONFIG_I2C_RPMSG_SERVER`` on
+the side that owns the real I2C bus.
+
+The client creates a normal ``struct i2c_master_s`` instance with
+``i2c_rpmsg_initialize(cpuname, name)``. The server exports a local I2C
+controller with ``i2c_rpmsg_server_initialize(name, i2c)``. Board code may
+then pass the client instance to device drivers, or register it with
+``i2c_register()`` so NSH tools can access it as ``/dev/i2cN``.
+
+RPMsg is only the transport. I2C transfers use a private RPMsg payload
+format carrying each NuttX ``struct i2c_msg_s`` with its address, flags, and
+frequency.
diff --git a/drivers/i2c/CMakeLists.txt b/drivers/i2c/CMakeLists.txt
index bb1365faf01..003c024b5c4 100644
--- a/drivers/i2c/CMakeLists.txt
+++ b/drivers/i2c/CMakeLists.txt
@@ -44,6 +44,14 @@ if(CONFIG_I2C)
     list(APPEND SRCS i2c_dummy.c)
   endif()
 
+  if(CONFIG_I2C_RPMSG)
+    list(APPEND SRCS i2c_rpmsg.c)
+  endif()
+
+  if(CONFIG_I2C_RPMSG_SERVER)
+    list(APPEND SRCS i2c_rpmsg_server.c)
+  endif()
+
   # Include the selected I2C multiplexer drivers
 
   if(CONFIG_I2CMULTIPLEXER_PCA9540BDP)
diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig
index 03642d7a1ed..59fc8373d04 100644
--- a/drivers/i2c/Kconfig
+++ b/drivers/i2c/Kconfig
@@ -152,6 +152,21 @@ config I2C_DUMMY
                dummy targets on this bus without adding hardcoded devices to 
the
                common driver.
 
+config I2C_RPMSG
+       bool "RPMSG I2C controller"
+       default n
+       depends on RPMSG
+       ---help---
+               Enable an I2C lower-half that forwards I2C transfers to a remote
+               NuttX instance over RPMSG.
+
+config I2C_RPMSG_SERVER
+       bool "RPMSG I2C server"
+       default n
+       depends on RPMSG
+       ---help---
+               Export a local I2C lower-half to remote RPMSG I2C clients.
+
 config I2C_DRIVER
        bool "I2C character driver"
        default n
diff --git a/drivers/i2c/Make.defs b/drivers/i2c/Make.defs
index fec4e1e42e1..dede064ebe9 100644
--- a/drivers/i2c/Make.defs
+++ b/drivers/i2c/Make.defs
@@ -47,6 +47,14 @@ ifeq ($(CONFIG_I2C_DUMMY),y)
 CSRCS += i2c_dummy.c
 endif
 
+ifeq ($(CONFIG_I2C_RPMSG),y)
+CSRCS += i2c_rpmsg.c
+endif
+
+ifeq ($(CONFIG_I2C_RPMSG_SERVER),y)
+CSRCS += i2c_rpmsg_server.c
+endif
+
 # Include the selected I2C multiplexer drivers
 
 ifeq ($(CONFIG_I2CMULTIPLEXER_PCA9540BDP),y)
diff --git a/drivers/i2c/i2c_rpmsg.c b/drivers/i2c/i2c_rpmsg.c
new file mode 100644
index 00000000000..6334e314e7b
--- /dev/null
+++ b/drivers/i2c/i2c_rpmsg.c
@@ -0,0 +1,516 @@
+/****************************************************************************
+ * drivers/i2c/i2c_rpmsg.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 <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/param.h>
+
+#include <nuttx/i2c/i2c_rpmsg.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/nuttx.h>
+#include <nuttx/rpmsg/rpmsg.h>
+#include <nuttx/semaphore.h>
+
+#include "i2c_rpmsg.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct i2c_rpmsg_cookie_s
+{
+  FAR void *response;
+  size_t response_len;
+  int result;
+  sem_t sem;
+};
+
+struct i2c_rpmsg_client_s
+{
+  struct i2c_master_s i2c;
+  FAR const char *cpuname;
+  FAR const char *name;
+  sem_t sem;
+  struct rpmsg_endpoint ept;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int i2c_rpmsg_transfer(FAR struct i2c_master_s *dev,
+                              FAR struct i2c_msg_s *msgs, int count);
+#ifdef CONFIG_I2C_RESET
+static int i2c_rpmsg_reset(FAR struct i2c_master_s *dev);
+#endif
+static int i2c_rpmsg_setup(FAR struct i2c_master_s *dev);
+static int i2c_rpmsg_shutdown(FAR struct i2c_master_s *dev);
+static uint8_t i2c_rpmsg_status(FAR struct i2c_master_s *dev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct i2c_ops_s g_i2c_rpmsg_ops =
+{
+  i2c_rpmsg_transfer,
+#ifdef CONFIG_I2C_RESET
+  i2c_rpmsg_reset,
+#endif
+  i2c_rpmsg_setup,
+  i2c_rpmsg_shutdown,
+  i2c_rpmsg_status
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: i2c_rpmsg_wait_ready
+ ****************************************************************************/
+
+static int i2c_rpmsg_wait_ready(FAR struct rpmsg_endpoint *ept)
+{
+  if (!is_rpmsg_ept_ready(ept))
+    {
+      FAR struct i2c_rpmsg_client_s *priv =
+        container_of(ept, struct i2c_rpmsg_client_s, ept);
+      int ret;
+
+      ret = rpmsg_wait(ept, &priv->sem);
+      if (ret < 0)
+        {
+          return ret;
+        }
+
+      rpmsg_post(ept, &priv->sem);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: i2c_rpmsg_sendrecv
+ ****************************************************************************/
+
+static int i2c_rpmsg_sendrecv(FAR struct rpmsg_endpoint *ept,
+                              uint32_t command,
+                              FAR struct i2c_rpmsg_header_s *msg,
+                              size_t len)
+{
+  struct i2c_rpmsg_cookie_s cookie;
+  int ret;
+
+  cookie.response     = msg;
+  cookie.response_len = len;
+  cookie.result       = -ENXIO;
+
+  ret = i2c_rpmsg_wait_ready(ept);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  nxsem_init(&cookie.sem, 0, 0);
+
+  msg->command = command;
+  msg->result = -ENXIO;
+  msg->cookie = (uintptr_t)&cookie;
+
+  ret = rpmsg_send(ept, msg, len);
+  if (ret >= 0)
+    {
+      ret = rpmsg_wait(ept, &cookie.sem);
+      if (ret >= 0)
+        {
+          ret = cookie.result;
+        }
+    }
+
+  nxsem_destroy(&cookie.sem);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: i2c_rpmsg_sendrecv_nocopy
+ ****************************************************************************/
+
+static int i2c_rpmsg_sendrecv_nocopy(FAR struct rpmsg_endpoint *ept,
+                                     uint32_t command,
+                                     FAR struct i2c_rpmsg_header_s *msg,
+                                     size_t len,
+                                     FAR struct i2c_msg_s *msgs,
+                                     int count)
+{
+  struct i2c_rpmsg_cookie_s cookie;
+  int ret;
+
+  cookie.response     = msgs;
+  cookie.response_len = count;
+  cookie.result       = -ENXIO;
+
+  nxsem_init(&cookie.sem, 0, 0);
+
+  msg->command = command;
+  msg->result = -ENXIO;
+  msg->cookie = (uintptr_t)&cookie;
+
+  ret = rpmsg_send_nocopy(ept, msg, len);
+  if (ret >= 0)
+    {
+      ret = rpmsg_wait(ept, &cookie.sem);
+      if (ret >= 0)
+        {
+          ret = cookie.result;
+        }
+    }
+  else
+    {
+      rpmsg_release_tx_buffer(ept, msg);
+    }
+
+  nxsem_destroy(&cookie.sem);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: i2c_rpmsg_transfer_data
+ ****************************************************************************/
+
+static FAR uint8_t *
+i2c_rpmsg_transfer_data(FAR struct i2c_rpmsg_transfer_s *msg)
+{
+  return (FAR uint8_t *)&msg->msg[msg->count];
+}
+
+/****************************************************************************
+ * Name: i2c_rpmsg_transfer
+ ****************************************************************************/
+
+static int i2c_rpmsg_transfer(FAR struct i2c_master_s *dev,
+                              FAR struct i2c_msg_s *msgs, int count)
+{
+  FAR struct i2c_rpmsg_client_s *priv =
+    (FAR struct i2c_rpmsg_client_s *)dev;
+  FAR struct i2c_rpmsg_transfer_s *msg;
+  FAR uint8_t *data;
+  uint32_t space;
+  size_t datalen = 0;
+  size_t offset = 0;
+  size_t msglen;
+  int ret;
+  int i;
+
+  if (msgs == NULL || count <= 0)
+    {
+      return -EINVAL;
+    }
+
+  for (i = 0; i < count; i++)
+    {
+      if (msgs[i].length < 0 ||
+          (msgs[i].length > 0 && msgs[i].buffer == NULL))
+        {
+          return -EINVAL;
+        }
+
+      if ((msgs[i].flags & I2C_M_READ) == 0)
+        {
+          datalen += msgs[i].length;
+        }
+    }
+
+  /* The request packs write payloads only and must fit a single rpmsg
+   * payload buffer.  The server bounds the response against its own buffer.
+   */
+
+  msglen = sizeof(*msg) + (count - 1) * sizeof(msg->msg[0]) + datalen;
+
+  ret = i2c_rpmsg_wait_ready(&priv->ept);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  msg = rpmsg_get_tx_payload_buffer(&priv->ept, &space, true);
+  if (msg == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  if (msglen > space)
+    {
+      rpmsg_release_tx_buffer(&priv->ept, msg);
+      return -E2BIG;
+    }
+
+  msg->count = count;
+  data = i2c_rpmsg_transfer_data(msg);
+
+  for (i = 0; i < count; i++)
+    {
+      msg->msg[i].frequency = msgs[i].frequency;
+      msg->msg[i].addr = msgs[i].addr;
+      msg->msg[i].flags = msgs[i].flags;
+      msg->msg[i].length = msgs[i].length;
+
+      /* A write carries its payload; a read has no request slot. */
+
+      if ((msgs[i].flags & I2C_M_READ) == 0 && msgs[i].length > 0)
+        {
+          memcpy(&data[offset], msgs[i].buffer, msgs[i].length);
+          offset += msgs[i].length;
+        }
+    }
+
+  return i2c_rpmsg_sendrecv_nocopy(&priv->ept, I2C_RPMSG_TRANSFER,
+                                   &msg->header, msglen, msgs, count);
+}
+
+#ifdef CONFIG_I2C_RESET
+/****************************************************************************
+ * Name: i2c_rpmsg_reset
+ ****************************************************************************/
+
+static int i2c_rpmsg_reset(FAR struct i2c_master_s *dev)
+{
+  FAR struct i2c_rpmsg_client_s *priv =
+    (FAR struct i2c_rpmsg_client_s *)dev;
+  struct i2c_rpmsg_simple_s msg;
+
+  return i2c_rpmsg_sendrecv(&priv->ept, I2C_RPMSG_RESET,
+                            &msg.header, sizeof(msg));
+}
+#endif
+
+/****************************************************************************
+ * Name: i2c_rpmsg_setup
+ ****************************************************************************/
+
+static int i2c_rpmsg_setup(FAR struct i2c_master_s *dev)
+{
+  FAR struct i2c_rpmsg_client_s *priv =
+    (FAR struct i2c_rpmsg_client_s *)dev;
+  struct i2c_rpmsg_simple_s msg;
+
+  return i2c_rpmsg_sendrecv(&priv->ept, I2C_RPMSG_SETUP,
+                            &msg.header, sizeof(msg));
+}
+
+/****************************************************************************
+ * Name: i2c_rpmsg_shutdown
+ ****************************************************************************/
+
+static int i2c_rpmsg_shutdown(FAR struct i2c_master_s *dev)
+{
+  FAR struct i2c_rpmsg_client_s *priv =
+    (FAR struct i2c_rpmsg_client_s *)dev;
+  struct i2c_rpmsg_simple_s msg;
+
+  return i2c_rpmsg_sendrecv(&priv->ept, I2C_RPMSG_SHUTDOWN,
+                            &msg.header, sizeof(msg));
+}
+
+/****************************************************************************
+ * Name: i2c_rpmsg_status
+ ****************************************************************************/
+
+static uint8_t i2c_rpmsg_status(FAR struct i2c_master_s *dev)
+{
+  return I2C_STATUS_PRESENT;
+}
+
+/****************************************************************************
+ * Name: i2c_rpmsg_client_ept_cb
+ ****************************************************************************/
+
+static int i2c_rpmsg_client_ept_cb(FAR struct rpmsg_endpoint *ept,
+                                   FAR void *data, size_t len,
+                                   uint32_t src, FAR void *priv)
+{
+  FAR struct i2c_rpmsg_header_s *msg = data;
+  FAR struct i2c_rpmsg_cookie_s *cookie =
+    (FAR struct i2c_rpmsg_cookie_s *)(uintptr_t)msg->cookie;
+
+  if (msg->command == I2C_RPMSG_TRANSFER)
+    {
+      FAR struct i2c_rpmsg_transfer_s *rsp = data;
+      FAR struct i2c_msg_s *msgs = cookie->response;
+      int count = cookie->response_len;
+      FAR uint8_t *buf = NULL;
+      size_t datalen = 0;
+      size_t offset = 0;
+      size_t hdrlen;
+      int i;
+
+      /* Validate the server-controlled response geometry against the
+       * actual received rpmsg length before reading any payload, so a
+       * mismatched or corrupted response cannot drive an out-of-bounds
+       * read of the vring buffer.
+       */
+
+      hdrlen = sizeof(*rsp) - sizeof(rsp->msg[0]);
+      if (len < hdrlen ||
+          rsp->count > (len - hdrlen) / sizeof(rsp->msg[0]))
+        {
+          msg->result = -EINVAL;
+        }
+      else
+        {
+          hdrlen += rsp->count * sizeof(rsp->msg[0]);
+          datalen = len - hdrlen;
+          buf = i2c_rpmsg_transfer_data(rsp);
+        }
+
+      for (i = 0; msg->result >= 0 && i < count && i < rsp->count; i++)
+        {
+          if (rsp->msg[i].length < msgs[i].length)
+            {
+              msg->result = -EINVAL;
+              break;
+            }
+
+          if ((msgs[i].flags & I2C_M_READ) != 0 && msgs[i].length > 0)
+            {
+              if (rsp->msg[i].length > datalen - offset)
+                {
+                  msg->result = -EINVAL;
+                  break;
+                }
+
+              memcpy(msgs[i].buffer, &buf[offset], msgs[i].length);
+              offset += rsp->msg[i].length;
+            }
+        }
+    }
+  else if (cookie->response != NULL)
+    {
+      memcpy(cookie->response, data, MIN(len, cookie->response_len));
+    }
+
+  cookie->result = msg->result;
+  rpmsg_post(ept, &cookie->sem);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: i2c_rpmsg_client_created
+ ****************************************************************************/
+
+static void i2c_rpmsg_client_created(FAR struct rpmsg_device *rdev,
+                                     FAR void *priv)
+{
+  FAR struct i2c_rpmsg_client_s *client = priv;
+
+  if (strcmp(client->cpuname, rpmsg_get_cpuname(rdev)) == 0)
+    {
+      char eptname[RPMSG_NAME_SIZE];
+
+      snprintf(eptname, sizeof(eptname), I2C_RPMSG_EPT_FORMAT,
+               client->name);
+
+      client->ept.priv = client;
+      rpmsg_create_ept(&client->ept, rdev, eptname, RPMSG_ADDR_ANY,
+                       RPMSG_ADDR_ANY, i2c_rpmsg_client_ept_cb, NULL);
+      rpmsg_post(&client->ept, &client->sem);
+    }
+}
+
+/****************************************************************************
+ * Name: i2c_rpmsg_client_destroy
+ ****************************************************************************/
+
+static void i2c_rpmsg_client_destroy(FAR struct rpmsg_device *rdev,
+                                     FAR void *priv)
+{
+  FAR struct i2c_rpmsg_client_s *client = priv;
+
+  if (strcmp(client->cpuname, rpmsg_get_cpuname(rdev)) == 0)
+    {
+      rpmsg_wait(&client->ept, &client->sem);
+      rpmsg_destroy_ept(&client->ept);
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: i2c_rpmsg_initialize
+ *
+ * Description:
+ *   Create an I2C client lower-half that forwards transfers to a remote
+ *   RPMsg I2C server.
+ *
+ * Input Parameters:
+ *   cpuname - Name of the remote CPU to connect to.
+ *   name    - RPMsg I2C service name.
+ *
+ * Returned Value:
+ *   A pointer to the I2C lower-half on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct i2c_master_s *i2c_rpmsg_initialize(FAR const char *cpuname,
+                                              FAR const char *name)
+{
+  FAR struct i2c_rpmsg_client_s *client;
+  int ret;
+
+  if (cpuname == NULL || name == NULL)
+    {
+      return NULL;
+    }
+
+  client = kmm_zalloc(sizeof(*client));
+  if (client == NULL)
+    {
+      return NULL;
+    }
+
+  client->i2c.ops = &g_i2c_rpmsg_ops;
+  client->cpuname = cpuname;
+  client->name = name;
+
+  nxsem_init(&client->sem, 0, 0);
+
+  ret = rpmsg_register_callback(client, i2c_rpmsg_client_created,
+                                i2c_rpmsg_client_destroy, NULL, NULL);
+  if (ret < 0)
+    {
+      nxsem_destroy(&client->sem);
+      kmm_free(client);
+      return NULL;
+    }
+
+  return &client->i2c;
+}
diff --git a/drivers/i2c/i2c_rpmsg.h b/drivers/i2c/i2c_rpmsg.h
new file mode 100644
index 00000000000..60c97fabcdc
--- /dev/null
+++ b/drivers/i2c/i2c_rpmsg.h
@@ -0,0 +1,90 @@
+/****************************************************************************
+ * drivers/i2c/i2c_rpmsg.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 __DRIVERS_I2C_I2C_RPMSG_H
+#define __DRIVERS_I2C_I2C_RPMSG_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+
+#include <nuttx/compiler.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Native rpmsg wire protocol between i2c_rpmsg client and i2c_rpmsg_server.
+ * This is a private NuttX-to-NuttX format (no VirtIO/spec constraints): each
+ * message carries its own frequency, slave address and native I2C_M_* flags
+ * in host byte order, and the whole sequence is handed to a single
+ * I2C_TRANSFER() on the server.  Keep client and server in sync by defining
+ * the command codes and message layout in this single private header.
+ */
+
+#define I2C_RPMSG_EPT_FORMAT "rpmsg-i2c-%s"
+
+#define I2C_RPMSG_TRANSFER   0
+#define I2C_RPMSG_RESET      1
+#define I2C_RPMSG_SETUP      2
+#define I2C_RPMSG_SHUTDOWN   3
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+begin_packed_struct struct i2c_rpmsg_header_s
+{
+  uint32_t command;
+  int32_t result;
+  uint64_t cookie;
+} end_packed_struct;
+
+/* Per-message descriptor.  Data follows the descriptor array: the request
+ * packs write payloads only; the response has one slot per message.
+ */
+
+begin_packed_struct struct i2c_rpmsg_msg_hdr_s
+{
+  uint32_t frequency;
+  uint16_t addr;
+  uint16_t flags;
+  uint32_t length;
+} end_packed_struct;
+
+begin_packed_struct struct i2c_rpmsg_transfer_s
+{
+  struct i2c_rpmsg_header_s header;
+  uint32_t count;
+  struct i2c_rpmsg_msg_hdr_s msg[1];
+} end_packed_struct;
+
+begin_packed_struct struct i2c_rpmsg_simple_s
+{
+  struct i2c_rpmsg_header_s header;
+} end_packed_struct;
+
+#endif /* __DRIVERS_I2C_I2C_RPMSG_H */
diff --git a/drivers/i2c/i2c_rpmsg_server.c b/drivers/i2c/i2c_rpmsg_server.c
new file mode 100644
index 00000000000..c936bfc4289
--- /dev/null
+++ b/drivers/i2c/i2c_rpmsg_server.c
@@ -0,0 +1,323 @@
+/****************************************************************************
+ * drivers/i2c/i2c_rpmsg_server.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 <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/param.h>
+
+#include <nuttx/i2c/i2c_rpmsg.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/rpmsg/rpmsg.h>
+
+#include "i2c_rpmsg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define I2C_RPMSG_MAX_MSGS   8
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct i2c_rpmsg_server_s
+{
+  FAR const char *name;
+  FAR struct i2c_master_s *i2c;
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: i2c_rpmsg_transfer_handler
+ ****************************************************************************/
+
+static int i2c_rpmsg_transfer_handler(FAR struct rpmsg_endpoint *ept,
+                                      FAR void *data, size_t len)
+{
+  FAR struct i2c_rpmsg_server_s *server = ept->priv;
+  FAR struct i2c_rpmsg_transfer_s *req = data;
+  FAR struct i2c_rpmsg_transfer_s *rsp;
+  struct i2c_msg_s local[I2C_RPMSG_MAX_MSGS];
+  FAR uint8_t *rxdata;
+  FAR uint8_t *txdata;
+  size_t reqoff = 0;
+  size_t rspoff = 0;
+  size_t datalen;
+  size_t hdrlen;
+  uint32_t space;
+  uint32_t i;
+
+  hdrlen = sizeof(*req) - sizeof(req->msg[0]);
+  if (len < hdrlen || req->count == 0 || req->count > I2C_RPMSG_MAX_MSGS)
+    {
+      req->header.result = -EINVAL;
+      return rpmsg_send(ept, req, len);
+    }
+
+  hdrlen += req->count * sizeof(req->msg[0]);
+  if (len < hdrlen)
+    {
+      req->header.result = -EINVAL;
+      return rpmsg_send(ept, req, len);
+    }
+
+  datalen = len - hdrlen;
+  rxdata = (FAR uint8_t *)&req->msg[req->count];
+
+  rsp = rpmsg_get_tx_payload_buffer(ept, &space, true);
+  if (rsp == NULL)
+    {
+      req->header.result = -ENOMEM;
+      return rpmsg_send(ept, req, len);
+    }
+
+  if (hdrlen > space)
+    {
+      rpmsg_release_tx_buffer(ept, rsp);
+      req->header.result = -E2BIG;
+      return rpmsg_send(ept, req, len);
+    }
+
+  memcpy(rsp, req, hdrlen);
+  txdata = (FAR uint8_t *)&rsp->msg[rsp->count];
+
+  for (i = 0; i < req->count; i++)
+    {
+      local[i].frequency = req->msg[i].frequency;
+      local[i].addr = req->msg[i].addr;
+      local[i].flags = req->msg[i].flags;
+      local[i].length = req->msg[i].length;
+      local[i].callback = NULL;
+      local[i].arg = NULL;
+
+      if ((req->msg[i].flags & I2C_M_READ) != 0)
+        {
+          /* Only reads own a response slot; only writes carry
+           * request data.
+           */
+
+          if (req->msg[i].length > space - hdrlen - rspoff)
+            {
+              rpmsg_release_tx_buffer(ept, rsp);
+              req->header.result = -E2BIG;
+              return rpmsg_send(ept, req, len);
+            }
+
+          local[i].buffer = &txdata[rspoff];
+          rspoff += req->msg[i].length;
+        }
+      else
+        {
+          if (req->msg[i].length > datalen - reqoff)
+            {
+              rpmsg_release_tx_buffer(ept, rsp);
+              req->header.result = -EINVAL;
+              return rpmsg_send(ept, req, len);
+            }
+
+          local[i].buffer = &rxdata[reqoff];
+          reqoff += req->msg[i].length;
+        }
+    }
+
+  rsp->header.result = I2C_TRANSFER(server->i2c, local, req->count);
+  return rpmsg_send_nocopy(ept, rsp, hdrlen + rspoff);
+}
+
+/****************************************************************************
+ * Name: i2c_rpmsg_simple_handler
+ ****************************************************************************/
+
+static int i2c_rpmsg_simple_handler(FAR struct rpmsg_endpoint *ept,
+                                    FAR void *data, size_t len)
+{
+  FAR struct i2c_rpmsg_server_s *server = ept->priv;
+  FAR struct i2c_rpmsg_header_s *msg = data;
+
+  if (len != sizeof(struct i2c_rpmsg_simple_s))
+    {
+      msg->result = -EINVAL;
+      return rpmsg_send(ept, msg, len);
+    }
+
+  switch (msg->command)
+    {
+      case I2C_RPMSG_SETUP:
+        msg->result = I2C_SETUP(server->i2c);
+        break;
+
+      case I2C_RPMSG_SHUTDOWN:
+        msg->result = I2C_SHUTDOWN(server->i2c);
+        break;
+
+#ifdef CONFIG_I2C_RESET
+      case I2C_RPMSG_RESET:
+        msg->result = I2C_RESET(server->i2c);
+        break;
+#endif
+
+      default:
+        msg->result = -ENOTTY;
+        break;
+    }
+
+  return rpmsg_send(ept, msg, len);
+}
+
+/****************************************************************************
+ * Name: i2c_rpmsg_server_ept_cb
+ ****************************************************************************/
+
+static int i2c_rpmsg_server_ept_cb(FAR struct rpmsg_endpoint *ept,
+                                   FAR void *data, size_t len,
+                                   uint32_t src, FAR void *priv)
+{
+  FAR struct i2c_rpmsg_header_s *msg = data;
+
+  if (len < sizeof(*msg))
+    {
+      return -EINVAL;
+    }
+
+  switch (msg->command)
+    {
+      case I2C_RPMSG_TRANSFER:
+        return i2c_rpmsg_transfer_handler(ept, data, len);
+
+      case I2C_RPMSG_SETUP:
+      case I2C_RPMSG_SHUTDOWN:
+      case I2C_RPMSG_RESET:
+        return i2c_rpmsg_simple_handler(ept, data, len);
+
+      default:
+        msg->result = -ENOTTY;
+        return rpmsg_send(ept, msg, len);
+    }
+}
+
+/****************************************************************************
+ * Name: i2c_rpmsg_server_match
+ ****************************************************************************/
+
+static bool i2c_rpmsg_server_match(FAR struct rpmsg_device *rdev,
+                                   FAR void *priv, FAR const char *name,
+                                   uint32_t dest)
+{
+  FAR struct i2c_rpmsg_server_s *server = priv;
+  char eptname[RPMSG_NAME_SIZE];
+
+  snprintf(eptname, sizeof(eptname), I2C_RPMSG_EPT_FORMAT, server->name);
+  return strcmp(name, eptname) == 0;
+}
+
+/****************************************************************************
+ * Name: i2c_rpmsg_server_ept_release
+ ****************************************************************************/
+
+static void i2c_rpmsg_server_ept_release(FAR struct rpmsg_endpoint *ept)
+{
+  kmm_free(ept);
+}
+
+/****************************************************************************
+ * Name: i2c_rpmsg_server_bind
+ ****************************************************************************/
+
+static void i2c_rpmsg_server_bind(FAR struct rpmsg_device *rdev,
+                                  FAR void *priv, FAR const char *name,
+                                  uint32_t dest)
+{
+  FAR struct rpmsg_endpoint *ept;
+
+  ept = kmm_zalloc(sizeof(*ept));
+  if (ept == NULL)
+    {
+      return;
+    }
+
+  ept->priv = priv;
+  ept->release_cb = i2c_rpmsg_server_ept_release;
+
+  rpmsg_create_ept(ept, rdev, name, RPMSG_ADDR_ANY, RPMSG_ADDR_ANY,
+                   i2c_rpmsg_server_ept_cb, rpmsg_destroy_ept);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: i2c_rpmsg_server_initialize
+ *
+ * Description:
+ *   Export a local I2C lower-half to remote RPMsg I2C clients.
+ *
+ * Input Parameters:
+ *   name - RPMsg I2C service name.
+ *   i2c  - Local I2C lower-half to export.
+ *
+ * Returned Value:
+ *   Zero on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int i2c_rpmsg_server_initialize(FAR const char *name,
+                                FAR struct i2c_master_s *i2c)
+{
+  FAR struct i2c_rpmsg_server_s *server;
+  int ret;
+
+  if (name == NULL || i2c == NULL)
+    {
+      return -EINVAL;
+    }
+
+  server = kmm_zalloc(sizeof(*server));
+  if (server == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  server->name = name;
+  server->i2c = i2c;
+
+  ret = rpmsg_register_callback(server, NULL, NULL,
+                                i2c_rpmsg_server_match,
+                                i2c_rpmsg_server_bind);
+  if (ret < 0)
+    {
+      kmm_free(server);
+    }
+
+  return ret;
+}
diff --git a/include/nuttx/i2c/i2c_rpmsg.h b/include/nuttx/i2c/i2c_rpmsg.h
new file mode 100644
index 00000000000..6a9abcdb802
--- /dev/null
+++ b/include/nuttx/i2c/i2c_rpmsg.h
@@ -0,0 +1,88 @@
+/****************************************************************************
+ * include/nuttx/i2c/i2c_rpmsg.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_RPMSG_H
+#define __INCLUDE_NUTTX_I2C_I2C_RPMSG_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <nuttx/i2c/i2c_master.h>
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#ifdef CONFIG_I2C_RPMSG
+/****************************************************************************
+ * Name: i2c_rpmsg_initialize
+ *
+ * Description:
+ *   Create an I2C client lower-half that forwards transfers to a remote
+ *   RPMsg I2C server.
+ *
+ * Input Parameters:
+ *   cpuname - Name of the remote CPU to connect to.
+ *   name    - RPMsg I2C service name.
+ *
+ * Returned Value:
+ *   A pointer to the I2C lower-half on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct i2c_master_s *i2c_rpmsg_initialize(FAR const char *cpuname,
+                                              FAR const char *name);
+#endif
+
+#ifdef CONFIG_I2C_RPMSG_SERVER
+/****************************************************************************
+ * Name: i2c_rpmsg_server_initialize
+ *
+ * Description:
+ *   Export a local I2C lower-half to remote RPMsg I2C clients.
+ *
+ * Input Parameters:
+ *   name - RPMsg I2C service name.
+ *   i2c  - Local I2C lower-half to export.
+ *
+ * Returned Value:
+ *   Zero on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int i2c_rpmsg_server_initialize(FAR const char *name,
+                                FAR struct i2c_master_s *i2c);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __INCLUDE_NUTTX_I2C_I2C_RPMSG_H */

Reply via email to