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

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

commit 939b31b97efee54dbe7e9dac09a89a7100aa2bec
Author: Eren Terzioglu <eren.terzio...@espressif.com>
AuthorDate: Wed Feb 26 09:33:02 2025 +0100

    boards/risc-v/esp32[c3|c6|h2]: Add I2C slave support
    
    Add I2C slave support into board layer for risc-v based Espressif devices
    
    Signed-off-by: Eren Terzioglu <eren.terzio...@espressif.com>
---
 boards/risc-v/esp32c3/common/src/esp_board_i2c.c | 46 ++++++++++++++++++++++--
 boards/risc-v/esp32c6/common/src/esp_board_i2c.c | 46 ++++++++++++++++++++++--
 boards/risc-v/esp32h2/common/src/esp_board_i2c.c | 46 ++++++++++++++++++++++--
 3 files changed, 130 insertions(+), 8 deletions(-)

diff --git a/boards/risc-v/esp32c3/common/src/esp_board_i2c.c 
b/boards/risc-v/esp32c3/common/src/esp_board_i2c.c
index 482ec535bf..6f4265f102 100644
--- a/boards/risc-v/esp32c3/common/src/esp_board_i2c.c
+++ b/boards/risc-v/esp32c3/common/src/esp_board_i2c.c
@@ -35,9 +35,21 @@
 #ifdef CONFIG_ESPRESSIF_I2C_BITBANG
 #include "espressif/esp_i2c_bitbang.h"
 #endif
-#ifdef CONFIG_ESPRESSIF_I2C_PERIPH
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE
 #include "espressif/esp_i2c.h"
 #endif
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
+#include "espressif/esp_i2c_slave.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
+#define I2C0_SLAVE_ADDR   0x28
+#define I2C0_SLAVE_NBITS  7
+#endif
 
 /****************************************************************************
  * Public Functions
@@ -66,7 +78,7 @@ static int i2c_bitbang_driver_init(int bus)
 }
 #endif
 
-#ifdef CONFIG_ESPRESSIF_I2C_PERIPH
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE
 static int i2c_driver_init(int bus)
 {
   struct i2c_master_s *i2c;
@@ -90,6 +102,30 @@ static int i2c_driver_init(int bus)
 }
 #endif
 
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
+static int i2c_slave_driver_init(int bus, int addr)
+{
+  struct i2c_slave_s *i2c;
+  int ret;
+
+  i2c = esp_i2cbus_slave_initialize(bus, addr);
+  if (i2c == NULL)
+    {
+      i2cerr("Failed to get I2C%d interface\n", bus);
+      return -ENODEV;
+    }
+
+  ret = i2c_slave_register(i2c, bus, addr, I2C0_SLAVE_NBITS);
+  if (ret < 0)
+    {
+      i2cerr("Failed to register I2C%d driver: %d\n", bus, ret);
+      esp_i2cbus_slave_uninitialize(i2c);
+    }
+
+  return ret;
+}
+#endif
+
 /****************************************************************************
  * Name: board_i2c_init
  *
@@ -106,7 +142,7 @@ int board_i2c_init(void)
 {
   int ret = OK;
 
-#ifdef CONFIG_ESPRESSIF_I2C0
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE
   ret = i2c_driver_init(ESPRESSIF_I2C0);
 #endif
 
@@ -114,5 +150,9 @@ int board_i2c_init(void)
   ret = i2c_bitbang_driver_init(ESPRESSIF_I2C_BITBANG);
 #endif
 
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
+  ret = i2c_slave_driver_init(ESPRESSIF_I2C0_SLAVE, I2C0_SLAVE_ADDR);
+#endif
+
   return ret;
 }
diff --git a/boards/risc-v/esp32c6/common/src/esp_board_i2c.c 
b/boards/risc-v/esp32c6/common/src/esp_board_i2c.c
index 88f5b16ef6..cbabb41906 100644
--- a/boards/risc-v/esp32c6/common/src/esp_board_i2c.c
+++ b/boards/risc-v/esp32c6/common/src/esp_board_i2c.c
@@ -35,9 +35,21 @@
 #ifdef CONFIG_ESPRESSIF_I2C_BITBANG
 #include "espressif/esp_i2c_bitbang.h"
 #endif
-#ifdef CONFIG_ESPRESSIF_I2C_PERIPH
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE
 #include "espressif/esp_i2c.h"
 #endif
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
+#include "espressif/esp_i2c_slave.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
+#define I2C0_SLAVE_ADDR   0x28
+#define I2C0_SLAVE_NBITS  7
+#endif
 
 /****************************************************************************
  * Public Functions
@@ -66,7 +78,7 @@ static int i2c_bitbang_driver_init(int bus)
 }
 #endif
 
-#ifdef CONFIG_ESPRESSIF_I2C_PERIPH
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE
 static int i2c_driver_init(int bus)
 {
   struct i2c_master_s *i2c;
@@ -90,6 +102,30 @@ static int i2c_driver_init(int bus)
 }
 #endif
 
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
+static int i2c_slave_driver_init(int bus, int addr)
+{
+  struct i2c_slave_s *i2c;
+  int ret;
+
+  i2c = esp_i2cbus_slave_initialize(bus, addr);
+  if (i2c == NULL)
+    {
+      i2cerr("Failed to get I2C%d interface\n", bus);
+      return -ENODEV;
+    }
+
+  ret = i2c_slave_register(i2c, bus, addr, I2C0_SLAVE_NBITS);
+  if (ret < 0)
+    {
+      i2cerr("Failed to register I2C%d driver: %d\n", bus, ret);
+      esp_i2cbus_slave_uninitialize(i2c);
+    }
+
+  return ret;
+}
+#endif
+
 /****************************************************************************
  * Name: board_i2c_init
  *
@@ -106,7 +142,7 @@ int board_i2c_init(void)
 {
   int ret = OK;
 
-#ifdef CONFIG_ESPRESSIF_I2C0
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE
   ret = i2c_driver_init(ESPRESSIF_I2C0);
 #endif
 
@@ -114,5 +150,9 @@ int board_i2c_init(void)
   ret = i2c_bitbang_driver_init(ESPRESSIF_I2C_BITBANG);
 #endif
 
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
+  ret = i2c_slave_driver_init(ESPRESSIF_I2C0_SLAVE, I2C0_SLAVE_ADDR);
+#endif
+
   return ret;
 }
diff --git a/boards/risc-v/esp32h2/common/src/esp_board_i2c.c 
b/boards/risc-v/esp32h2/common/src/esp_board_i2c.c
index 68ab294361..e9108a26f2 100644
--- a/boards/risc-v/esp32h2/common/src/esp_board_i2c.c
+++ b/boards/risc-v/esp32h2/common/src/esp_board_i2c.c
@@ -35,9 +35,21 @@
 #ifdef CONFIG_ESPRESSIF_I2C_BITBANG
 #include "espressif/esp_i2c_bitbang.h"
 #endif
-#ifdef CONFIG_ESPRESSIF_I2C_PERIPH
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE
 #include "espressif/esp_i2c.h"
 #endif
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
+#include "espressif/esp_i2c_slave.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
+#define I2C0_SLAVE_ADDR   0x28
+#define I2C0_SLAVE_NBITS  7
+#endif
 
 /****************************************************************************
  * Public Functions
@@ -66,7 +78,7 @@ static int i2c_bitbang_driver_init(int bus)
 }
 #endif
 
-#ifdef CONFIG_ESPRESSIF_I2C_PERIPH
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE
 static int i2c_driver_init(int bus)
 {
   struct i2c_master_s *i2c;
@@ -90,6 +102,30 @@ static int i2c_driver_init(int bus)
 }
 #endif
 
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
+static int i2c_slave_driver_init(int bus, int addr)
+{
+  struct i2c_slave_s *i2c;
+  int ret;
+
+  i2c = esp_i2cbus_slave_initialize(bus, addr);
+  if (i2c == NULL)
+    {
+      i2cerr("Failed to get I2C%d interface\n", bus);
+      return -ENODEV;
+    }
+
+  ret = i2c_slave_register(i2c, bus, addr, I2C0_SLAVE_NBITS);
+  if (ret < 0)
+    {
+      i2cerr("Failed to register I2C%d driver: %d\n", bus, ret);
+      esp_i2cbus_slave_uninitialize(i2c);
+    }
+
+  return ret;
+}
+#endif
+
 /****************************************************************************
  * Name: board_i2c_init
  *
@@ -106,6 +142,7 @@ int board_i2c_init(void)
 {
   int ret = OK;
 
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE
 #ifdef CONFIG_ESPRESSIF_I2C0
   ret = i2c_driver_init(ESPRESSIF_I2C0);
   if (ret != OK)
@@ -117,10 +154,15 @@ int board_i2c_init(void)
 #ifdef CONFIG_ESPRESSIF_I2C1
   ret = i2c_driver_init(ESPRESSIF_I2C1);
 #endif
+#endif
 
 #ifdef CONFIG_ESPRESSIF_I2C_BITBANG
   ret = i2c_bitbang_driver_init(ESPRESSIF_I2C_BITBANG);
 #endif
 
+#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
+  ret = i2c_slave_driver_init(ESPRESSIF_I2C0_SLAVE, I2C0_SLAVE_ADDR);
+#endif
+
   return ret;
 }

Reply via email to