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 06ef4a8084aaedec63f2df65940cd9c9a940922c Author: Eren Terzioglu <eren.terzio...@espressif.com> AuthorDate: Wed Feb 26 09:18:51 2025 +0100 boards/xtensa/esp32[s2|s3]: Add I2C slave support Add I2C slave board support for Xtensa based Espressif devices Signed-off-by: Eren Terzioglu <eren.terzio...@espressif.com> --- .../esp32s2-saola-1/src/esp32s2_board_i2c.c | 11 +++-- .../xtensa/esp32s3/common/src/esp32s3_board_i2c.c | 54 ++++++++++++++++++++-- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_board_i2c.c b/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_board_i2c.c index 395f3a73c9..37e909b3f6 100644 --- a/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_board_i2c.c +++ b/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_board_i2c.c @@ -77,19 +77,20 @@ int board_i2c_init(void) { int ret = OK; -#ifdef CONFIG_ESP32S2_I2C0 +#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE +# ifdef CONFIG_ESP32S2_I2C0_MASTER_MODE ret = i2c_driver_init(ESP32S2_I2C0); if (ret != OK) { goto done; } -#endif +# endif -#ifdef CONFIG_ESP32S2_I2C1 +# ifdef CONFIG_ESP32S2_I2C1_MASTER_MODE ret = i2c_driver_init(ESP32S2_I2C1); -#endif +# endif +#endif /* #ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE */ done: return ret; } - diff --git a/boards/xtensa/esp32s3/common/src/esp32s3_board_i2c.c b/boards/xtensa/esp32s3/common/src/esp32s3_board_i2c.c index 52091caa5e..290c14ff01 100644 --- a/boards/xtensa/esp32s3/common/src/esp32s3_board_i2c.c +++ b/boards/xtensa/esp32s3/common/src/esp32s3_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 "esp32s3_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,22 +142,30 @@ int board_i2c_init(void) { int ret = OK; -#ifdef CONFIG_ESP32S3_I2C0 +#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE +# ifdef CONFIG_ESP32S3_I2C0_MASTER_MODE ret = i2c_driver_init(ESP32S3_I2C0); if (ret != OK) { goto done; } -#endif +# endif -#ifdef CONFIG_ESP32S3_I2C1 +# ifdef CONFIG_ESP32S3_I2C1_MASTER_MODE ret = i2c_driver_init(ESP32S3_I2C1); +# endif #endif #ifdef CONFIG_ESPRESSIF_I2C_BITBANG ret = i2c_bitbang_driver_init(ESPRESSIF_I2C_BITBANG); #endif +#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE +# ifdef CONFIG_ESP32S3_I2C0_SLAVE_MODE + ret = i2c_slave_driver_init(ESPRESSIF_I2C0_SLAVE, I2C0_SLAVE_ADDR); + #endif +#endif + #ifdef CONFIG_ESP32S3_I2C0 done: #endif