mirror of
https://github.com/apache/nuttx.git
synced 2026-06-02 09:38:37 +08:00
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.terzioglu@espressif.com>
This commit is contained in:
committed by
Alin Jerpelea
parent
59fe350708
commit
939b31b97e
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user