risc-v/espressif/i2c: Update common source code functions
Updates the common source code for the I2C peripheral used by Espressif's RISC-Vs SoCs. This enables newer SoCs to be supported in the future while maintaining backwards compatibility. Signed-off-by: Tiago Medicci Serrano <tiago.medicci@espressif.com>
This commit is contained in:
parent
d7b2df6d85
commit
1519d6c80b
7 changed files with 244 additions and 202 deletions
|
|
@ -441,40 +441,6 @@ config ESPRESSIF_LP_I2C0
|
|||
select ESPRESSIF_I2C_PERIPH_MASTER_MODE
|
||||
select I2C
|
||||
|
||||
choice ESPRESSIF_I2C0_MODE
|
||||
prompt "I2C0 Mode"
|
||||
depends on ESPRESSIF_I2C0
|
||||
default ESPRESSIF_I2C0_MASTER_MODE
|
||||
|
||||
config ESPRESSIF_I2C0_MASTER_MODE
|
||||
bool "I2C0 Master Mode"
|
||||
select ESPRESSIF_I2C_PERIPH_MASTER_MODE
|
||||
|
||||
config ESPRESSIF_I2C0_SLAVE_MODE
|
||||
bool "I2C0 Slave Mode"
|
||||
select I2C_SLAVE
|
||||
select I2C_SLAVE_DRIVER
|
||||
select ESPRESSIF_I2C_PERIPH_SLAVE_MODE
|
||||
|
||||
endchoice # ESPRESSIF_I2C0_MODE
|
||||
|
||||
choice ESPRESSIF_I2C1_MODE
|
||||
prompt "I2C1 Mode"
|
||||
depends on ESPRESSIF_I2C1
|
||||
default ESPRESSIF_I2C1_MASTER_MODE
|
||||
|
||||
config ESPRESSIF_I2C1_MASTER_MODE
|
||||
bool "I2C1 Master Mode"
|
||||
select ESPRESSIF_I2C_PERIPH_MASTER_MODE
|
||||
|
||||
config ESPRESSIF_I2C1_SLAVE_MODE
|
||||
bool "I2C1 Slave Mode"
|
||||
select I2C_SLAVE
|
||||
select I2C_SLAVE_DRIVER
|
||||
select ESPRESSIF_I2C_PERIPH_SLAVE_MODE
|
||||
|
||||
endchoice # ESPRESSIF_I2C1_MODE
|
||||
|
||||
config ESPRESSIF_I2C_BITBANG
|
||||
bool "I2C Bitbang"
|
||||
default n
|
||||
|
|
@ -2229,6 +2195,23 @@ menu "I2C Configuration"
|
|||
|
||||
if ESPRESSIF_I2C0
|
||||
|
||||
choice ESPRESSIF_I2C0_MODE
|
||||
prompt "I2C0 Mode"
|
||||
depends on ESPRESSIF_I2C0
|
||||
default ESPRESSIF_I2C0_MASTER_MODE
|
||||
|
||||
config ESPRESSIF_I2C0_MASTER_MODE
|
||||
bool "I2C0 Master Mode"
|
||||
select ESPRESSIF_I2C_PERIPH_MASTER_MODE
|
||||
|
||||
config ESPRESSIF_I2C0_SLAVE_MODE
|
||||
bool "I2C0 Slave Mode"
|
||||
select I2C_SLAVE
|
||||
select I2C_SLAVE_DRIVER
|
||||
select ESPRESSIF_I2C_PERIPH_SLAVE_MODE
|
||||
|
||||
endchoice # ESPRESSIF_I2C0_MODE
|
||||
|
||||
config ESPRESSIF_I2C0_SCLPIN
|
||||
int "I2C0 SCL Pin"
|
||||
default 6 if !ESPRESSIF_LPI2C
|
||||
|
|
@ -2244,6 +2227,23 @@ endif # ESPRESSIF_I2C0
|
|||
|
||||
if ESPRESSIF_I2C1
|
||||
|
||||
choice ESPRESSIF_I2C1_MODE
|
||||
prompt "I2C1 Mode"
|
||||
depends on ESPRESSIF_I2C1
|
||||
default ESPRESSIF_I2C1_MASTER_MODE
|
||||
|
||||
config ESPRESSIF_I2C1_MASTER_MODE
|
||||
bool "I2C1 Master Mode"
|
||||
select ESPRESSIF_I2C_PERIPH_MASTER_MODE
|
||||
|
||||
config ESPRESSIF_I2C1_SLAVE_MODE
|
||||
bool "I2C1 Slave Mode"
|
||||
select I2C_SLAVE
|
||||
select I2C_SLAVE_DRIVER
|
||||
select ESPRESSIF_I2C_PERIPH_SLAVE_MODE
|
||||
|
||||
endchoice # ESPRESSIF_I2C1_MODE
|
||||
|
||||
config ESPRESSIF_I2C1_SCLPIN
|
||||
int "I2C1 SCL Pin"
|
||||
default 2
|
||||
|
|
@ -2256,8 +2256,41 @@ config ESPRESSIF_I2C1_SDAPIN
|
|||
|
||||
endif # ESPRESSIF_I2C1
|
||||
|
||||
if ESPRESSIF_LPI2C
|
||||
|
||||
choice ESPRESSIF_LPI2C_MODE
|
||||
prompt "LPI2C Mode"
|
||||
default ESPRESSIF_LPI2C_MASTER_MODE
|
||||
|
||||
config ESPRESSIF_LPI2C_MASTER_MODE
|
||||
bool "I2C0 Master Mode"
|
||||
select ESPRESSIF_I2C_PERIPH_MASTER_MODE
|
||||
|
||||
endchoice # ESPRESSIF_LPI2C_MODE
|
||||
|
||||
config ESPRESSIF_LPI2C_SCLPIN
|
||||
int "LPI2C SCL Pin"
|
||||
default 7
|
||||
range 7 7
|
||||
|
||||
config ESPRESSIF_LPI2C_SDAPIN
|
||||
int "LPI2C SDA Pin"
|
||||
default 6
|
||||
range 6 6
|
||||
|
||||
endif # ESPRESSIF_LPI2C
|
||||
|
||||
if ESPRESSIF_I2C_BITBANG
|
||||
|
||||
choice ESPRESSIF_I2C_BITBANG_MODE
|
||||
prompt "I2C Bitbang Mode"
|
||||
default ESPRESSIF_I2C_BITBANG_MASTER_MODE
|
||||
|
||||
config ESPRESSIF_I2C_BITBANG_MASTER_MODE
|
||||
bool "I2C Bitbang Master Mode"
|
||||
|
||||
endchoice # ESPRESSIF_I2C_BITBANG_MODE
|
||||
|
||||
config ESPRESSIF_I2C_BITBANG_SCLPIN
|
||||
int "I2C Bitbang SCL Pin"
|
||||
default 0
|
||||
|
|
|
|||
|
|
@ -82,18 +82,6 @@
|
|||
# define ESP_LP_I2C0_ID I2C_NUM_MAX
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ARCH_CHIP_ESP32H2
|
||||
# define SYSTEM_I2C_EXT0_CLK_EN PCR_I2C0_CLK_EN
|
||||
# define SYSTEM_I2C_EXT0_RST PCR_I2C0_RST_EN
|
||||
# define SYSTEM_I2C_EXT1_CLK_EN PCR_I2C1_CLK_EN
|
||||
# define SYSTEM_I2C_EXT1_RST PCR_I2C1_RST_EN
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ARCH_CHIP_ESP32C6
|
||||
# define SYSTEM_I2C_EXT0_CLK_EN PCR_I2C_CLK_EN
|
||||
# define SYSTEM_I2C_EXT0_RST PCR_I2C_RST_EN
|
||||
#endif
|
||||
|
||||
#define GET_STATUS(hw) hw->sr.val
|
||||
|
||||
#define ESPRESSIF_I2CTIMEOTICKS \
|
||||
|
|
@ -147,6 +135,26 @@
|
|||
|
||||
#define SCL_TIMEOUT_VAL_US (CONFIG_ESPRESSIF_I2CTIMEOMS * 1000)
|
||||
|
||||
#if SOC_PERIPH_CLK_CTRL_SHARED
|
||||
# define I2C_CLOCK_SRC_ATOMIC() PERIPH_RCC_ATOMIC()
|
||||
#else
|
||||
# define I2C_CLOCK_SRC_ATOMIC()
|
||||
#endif
|
||||
|
||||
#if !SOC_RCC_IS_INDEPENDENT
|
||||
# define I2C_RCC_ATOMIC() PERIPH_RCC_ATOMIC()
|
||||
#else
|
||||
# define I2C_RCC_ATOMIC()
|
||||
#endif
|
||||
|
||||
#if SOC_LP_I2C_SUPPORTED
|
||||
# define LP_I2C_SRC_CLK_ATOMIC() PERIPH_RCC_ATOMIC()
|
||||
# define LP_I2C_BUS_CLK_ATOMIC() PERIPH_RCC_ATOMIC()
|
||||
#endif
|
||||
|
||||
#define SCL_PIN_ATTR (FUNCTION_2 || INPUT_PULLUP || OUTPUT_OPEN_DRAIN)
|
||||
#define SDA_PIN_ATTR (FUNCTION_2 || INPUT_PULLUP || OUTPUT_OPEN_DRAIN)
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
|
@ -208,23 +216,8 @@ struct esp_trace_s
|
|||
struct esp_i2c_config_s
|
||||
{
|
||||
uint32_t clk_freq; /* Clock frequency */
|
||||
|
||||
uint8_t scl_pin; /* GPIO configuration for SCL as SCL */
|
||||
uint8_t sda_pin; /* GPIO configuration for SDA as SDA */
|
||||
|
||||
#ifndef CONFIG_I2C_POLLED
|
||||
uint8_t periph; /* Peripheral ID */
|
||||
uint8_t irq; /* Interrupt ID */
|
||||
#endif
|
||||
|
||||
uint32_t clk_bit; /* Clock enable bit */
|
||||
uint32_t rst_bit; /* I2C reset bit */
|
||||
|
||||
uint32_t scl_insig; /* I2C SCL input signal index */
|
||||
uint32_t scl_outsig; /* I2C SCL output signal index */
|
||||
|
||||
uint32_t sda_insig; /* I2C SDA input signal index */
|
||||
uint32_t sda_outsig; /* I2C SDA output signal index */
|
||||
};
|
||||
|
||||
/* I2C Device Private Data */
|
||||
|
|
@ -239,7 +232,7 @@ struct esp_i2c_priv_s
|
|||
|
||||
/* Port configuration */
|
||||
|
||||
const struct esp_i2c_config_s *config;
|
||||
struct esp_i2c_config_s *config;
|
||||
int refs; /* Reference count */
|
||||
mutex_t lock; /* Mutual exclusion mutex */
|
||||
|
||||
|
|
@ -351,21 +344,11 @@ i2c_hal_context_t i2c0_ctx =
|
|||
|
||||
/* I2C device structure */
|
||||
|
||||
static const struct esp_i2c_config_s esp_i2c0_config =
|
||||
static struct esp_i2c_config_s esp_i2c0_config =
|
||||
{
|
||||
.clk_freq = I2C_CLK_FREQ_DEF,
|
||||
.scl_pin = CONFIG_ESPRESSIF_I2C0_SCLPIN,
|
||||
.sda_pin = CONFIG_ESPRESSIF_I2C0_SDAPIN,
|
||||
#ifndef CONFIG_I2C_POLLED
|
||||
.periph = ETS_I2C_EXT0_INTR_SOURCE,
|
||||
.irq = ESP_IRQ_I2C_EXT0,
|
||||
#endif
|
||||
.clk_bit = SYSTEM_I2C_EXT0_CLK_EN,
|
||||
.rst_bit = SYSTEM_I2C_EXT0_RST,
|
||||
.scl_insig = I2CEXT0_SCL_IN_IDX,
|
||||
.scl_outsig = I2CEXT0_SCL_OUT_IDX,
|
||||
.sda_insig = I2CEXT0_SDA_IN_IDX,
|
||||
.sda_outsig = I2CEXT0_SDA_OUT_IDX
|
||||
};
|
||||
|
||||
static struct esp_i2c_priv_s esp_i2c0_priv =
|
||||
|
|
@ -395,7 +378,7 @@ static struct esp_i2c_priv_s esp_i2c0_priv =
|
|||
#endif
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_I2C1_MASTER_MODE
|
||||
|
||||
# if SOC_HP_I2C_NUM > 1
|
||||
i2c_hal_context_t i2c1_ctx =
|
||||
{
|
||||
0
|
||||
|
|
@ -403,21 +386,11 @@ i2c_hal_context_t i2c1_ctx =
|
|||
|
||||
/* I2C device structure */
|
||||
|
||||
static const struct esp_i2c_config_s esp_i2c1_config =
|
||||
static struct esp_i2c_config_s esp_i2c1_config =
|
||||
{
|
||||
.clk_freq = I2C_CLK_FREQ_DEF,
|
||||
.scl_pin = CONFIG_ESPRESSIF_I2C1_SCLPIN,
|
||||
.sda_pin = CONFIG_ESPRESSIF_I2C1_SDAPIN,
|
||||
#ifndef CONFIG_I2C_POLLED
|
||||
.periph = ETS_I2C_EXT1_INTR_SOURCE,
|
||||
.irq = ESP_IRQ_I2C_EXT1,
|
||||
#endif
|
||||
.clk_bit = SYSTEM_I2C_EXT1_CLK_EN,
|
||||
.rst_bit = SYSTEM_I2C_EXT1_RST,
|
||||
.scl_insig = I2CEXT1_SCL_IN_IDX,
|
||||
.scl_outsig = I2CEXT1_SCL_OUT_IDX,
|
||||
.sda_insig = I2CEXT1_SDA_IN_IDX,
|
||||
.sda_outsig = I2CEXT1_SDA_OUT_IDX
|
||||
};
|
||||
|
||||
static struct esp_i2c_priv_s esp_i2c1_priv =
|
||||
|
|
@ -444,9 +417,13 @@ static struct esp_i2c_priv_s esp_i2c1_priv =
|
|||
.clk_freq = 0,
|
||||
.ctx = &i2c1_ctx
|
||||
};
|
||||
# else
|
||||
# error "This device contains only a single HP I2C port."
|
||||
# endif /* SOC_HP_I2C_NUM > 0 */
|
||||
#endif /* CONFIG_ESPRESSIF_I2C1 */
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_LP_I2C0
|
||||
# if SOC_LP_I2C_NUM > 0
|
||||
i2c_hal_context_t lp_i2c0_ctx =
|
||||
{
|
||||
0
|
||||
|
|
@ -454,21 +431,11 @@ i2c_hal_context_t lp_i2c0_ctx =
|
|||
|
||||
/* I2C device structure */
|
||||
|
||||
static const struct esp_i2c_config_s esp_lp_i2c0_config =
|
||||
static struct esp_i2c_config_s esp_lp_i2c0_config =
|
||||
{
|
||||
.clk_freq = I2C_CLK_FREQ_DEF,
|
||||
.scl_pin = 7,
|
||||
.sda_pin = 6,
|
||||
#ifndef CONFIG_I2C_POLLED
|
||||
.periph = ETS_LP_I2C_INTR_SOURCE,
|
||||
.irq = ESP_IRQ_LP_I2C,
|
||||
#endif
|
||||
.clk_bit = SYSTEM_I2C_EXT0_CLK_EN,
|
||||
.rst_bit = SYSTEM_I2C_EXT0_RST,
|
||||
.scl_insig = 0,
|
||||
.scl_outsig = 0,
|
||||
.sda_insig = 0,
|
||||
.sda_outsig = 0
|
||||
};
|
||||
|
||||
static struct esp_i2c_priv_s esp_lp_i2c0_priv =
|
||||
|
|
@ -495,6 +462,9 @@ static struct esp_i2c_priv_s esp_lp_i2c0_priv =
|
|||
.clk_freq = 0,
|
||||
.ctx = &lp_i2c0_ctx
|
||||
};
|
||||
# else
|
||||
# error "This device does not contain a LP I2C port."
|
||||
# endif /* SOC_LP_I2C_NUM > 0 */
|
||||
#endif /* CONFIG_ESPRESSIF_LP_I2C0 */
|
||||
|
||||
/* Trace events strings */
|
||||
|
|
@ -796,16 +766,26 @@ static void esp_i2c_init_clock(struct esp_i2c_priv_s *priv,
|
|||
esp_clk_tree_src_get_freq_hz(priv->clk_src,
|
||||
ESP_CLK_TREE_SRC_FREQ_PRECISION_APPROX,
|
||||
&src_clk_frequency);
|
||||
i2c_hal_set_bus_timing(priv->ctx, priv->config->clk_freq,
|
||||
priv->clk_src, src_clk_frequency);
|
||||
|
||||
I2C_CLOCK_SRC_ATOMIC()
|
||||
{
|
||||
i2c_hal_set_bus_timing(priv->ctx, priv->config->clk_freq,
|
||||
priv->clk_src, src_clk_frequency);
|
||||
}
|
||||
|
||||
i2c_hal_master_set_scl_timeout_val(priv->ctx, SCL_TIMEOUT_VAL_US,
|
||||
src_clk_frequency);
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_LPI2C
|
||||
PERIPH_RCC_ATOMIC()
|
||||
LP_I2C_SRC_CLK_ATOMIC()
|
||||
{
|
||||
lp_i2c_ll_set_source_clk(priv->ctx->dev, priv->clk_src);
|
||||
}
|
||||
#else
|
||||
I2C_CLOCK_SRC_ATOMIC()
|
||||
{
|
||||
i2c_ll_set_source_clk(priv->ctx->dev, priv->clk_src);
|
||||
}
|
||||
#endif
|
||||
|
||||
i2c_ll_update(priv->ctx->dev);
|
||||
|
|
@ -861,27 +841,39 @@ static void esp_i2c_init(struct esp_i2c_priv_s *priv)
|
|||
|
||||
if (priv->id < ESP_LP_I2C0_ID)
|
||||
{
|
||||
uint32_t scl_in_sig = i2c_periph_signal[priv->id].scl_in_sig;
|
||||
uint32_t scl_out_sig = i2c_periph_signal[priv->id].scl_out_sig;
|
||||
uint32_t sda_in_sig = i2c_periph_signal[priv->id].sda_in_sig;
|
||||
uint32_t sda_out_sig = i2c_periph_signal[priv->id].sda_out_sig;
|
||||
|
||||
I2C_RCC_ATOMIC()
|
||||
{
|
||||
i2c_ll_enable_bus_clock(priv->id, true);
|
||||
i2c_ll_reset_register(priv->id);
|
||||
}
|
||||
|
||||
I2C_CLOCK_SRC_ATOMIC()
|
||||
{
|
||||
i2c_hal_init(priv->ctx, priv->id);
|
||||
}
|
||||
|
||||
/* Configure GPIO signals for I2C SCL and SDA pins */
|
||||
|
||||
esp_gpiowrite(config->scl_pin, 1);
|
||||
esp_gpiowrite(config->sda_pin, 1);
|
||||
|
||||
esp_configgpio(config->scl_pin, INPUT_PULLUP | OUTPUT_OPEN_DRAIN);
|
||||
esp_gpio_matrix_out(config->scl_pin, config->scl_outsig, 0, 0);
|
||||
esp_gpio_matrix_in(config->scl_pin, config->scl_insig, 0);
|
||||
esp_configgpio(config->scl_pin, SCL_PIN_ATTR);
|
||||
esp_gpio_matrix_out(config->scl_pin, scl_out_sig, 0, 0);
|
||||
esp_gpio_matrix_in(config->scl_pin, scl_in_sig, 0);
|
||||
|
||||
esp_configgpio(config->sda_pin, INPUT_PULLUP | OUTPUT_OPEN_DRAIN);
|
||||
esp_gpio_matrix_out(config->sda_pin, config->sda_outsig, 0, 0);
|
||||
esp_gpio_matrix_in(config->sda_pin, config->sda_insig, 0);
|
||||
|
||||
/* Enable I2C hardware */
|
||||
|
||||
periph_module_enable(priv->module);
|
||||
esp_configgpio(config->sda_pin, SDA_PIN_ATTR);
|
||||
esp_gpio_matrix_out(config->sda_pin, sda_out_sig, 0, 0);
|
||||
esp_gpio_matrix_in(config->sda_pin, sda_in_sig, 0);
|
||||
}
|
||||
#ifdef CONFIG_ESPRESSIF_LPI2C
|
||||
else
|
||||
{
|
||||
PERIPH_RCC_ATOMIC()
|
||||
LP_I2C_BUS_CLK_ATOMIC()
|
||||
{
|
||||
/* Enable LP I2C bus clock */
|
||||
|
||||
|
|
@ -892,6 +884,11 @@ static void esp_i2c_init(struct esp_i2c_priv_s *priv)
|
|||
lp_i2c_ll_reset_register(priv->id - LP_I2C_NUM_0);
|
||||
}
|
||||
|
||||
I2C_CLOCK_SRC_ATOMIC()
|
||||
{
|
||||
i2c_hal_init(priv->ctx, priv->id);
|
||||
}
|
||||
|
||||
esp_lp_i2c_config_io(priv, config->scl_pin);
|
||||
esp_lp_i2c_config_io(priv, config->sda_pin);
|
||||
const i2c_signal_conn_t *p_i2c_pin = &i2c_periph_signal[priv->id];
|
||||
|
|
@ -907,16 +904,20 @@ static void esp_i2c_init(struct esp_i2c_priv_s *priv)
|
|||
}
|
||||
#endif
|
||||
|
||||
i2c_hal_init(priv->ctx, priv->id);
|
||||
/* Initialize I2C Master */
|
||||
|
||||
i2c_hal_master_init(priv->ctx);
|
||||
|
||||
i2c_ll_update(priv->ctx->dev);
|
||||
|
||||
/* Initialize I2C bus clock */
|
||||
|
||||
esp_i2c_init_clock(priv, config->clk_freq);
|
||||
|
||||
/* Disable I2C interrupts */
|
||||
|
||||
esp_i2c_intr_disable(priv);
|
||||
|
||||
/* Initialize I2C Master */
|
||||
|
||||
i2c_hal_master_init(priv->ctx);
|
||||
|
||||
/* Configure the hardware filter function */
|
||||
|
||||
i2c_ll_master_set_filter(priv->ctx->dev, I2C_FILTER_CYC_NUM_DEF);
|
||||
|
|
@ -931,10 +932,6 @@ static void esp_i2c_init(struct esp_i2c_priv_s *priv)
|
|||
|
||||
i2c_ll_master_rx_full_ack_level(priv->ctx->dev, 1);
|
||||
#endif
|
||||
|
||||
/* Initialize I2C bus clock */
|
||||
|
||||
esp_i2c_init_clock(priv, config->clk_freq);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
|
|
@ -950,11 +947,17 @@ static void esp_i2c_init(struct esp_i2c_priv_s *priv)
|
|||
|
||||
static void esp_i2c_deinit(struct esp_i2c_priv_s *priv)
|
||||
{
|
||||
const struct esp_i2c_config_s *config = priv->config;
|
||||
|
||||
priv->clk_freq = 0;
|
||||
i2c_hal_deinit(priv->ctx);
|
||||
periph_module_disable(priv->module);
|
||||
|
||||
I2C_CLOCK_SRC_ATOMIC()
|
||||
{
|
||||
i2c_hal_deinit(priv->ctx);
|
||||
}
|
||||
|
||||
I2C_RCC_ATOMIC()
|
||||
{
|
||||
i2c_ll_enable_bus_clock(priv->id, false);
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
|
|
@ -1665,7 +1668,6 @@ struct i2c_master_s *esp_i2cbus_initialize(int port)
|
|||
{
|
||||
struct esp_i2c_priv_s *priv;
|
||||
#ifndef CONFIG_I2C_POLLED
|
||||
const struct esp_i2c_config_s *config;
|
||||
int ret;
|
||||
#endif
|
||||
|
||||
|
|
@ -1703,16 +1705,15 @@ struct i2c_master_s *esp_i2cbus_initialize(int port)
|
|||
}
|
||||
|
||||
#ifndef CONFIG_I2C_POLLED
|
||||
config = priv->config;
|
||||
if (priv->cpuint != -ENOMEM)
|
||||
{
|
||||
/* Disable the previous IRQ */
|
||||
|
||||
up_disable_irq(config->irq);
|
||||
esp_teardown_irq(config->periph, priv->cpuint);
|
||||
up_disable_irq(ESP_SOURCE2IRQ(i2c_periph_signal[priv->id].irq));
|
||||
esp_teardown_irq(i2c_periph_signal[priv->id].irq, priv->cpuint);
|
||||
}
|
||||
|
||||
priv->cpuint = esp_setup_irq(config->periph,
|
||||
priv->cpuint = esp_setup_irq(i2c_periph_signal[priv->id].irq,
|
||||
ESP_IRQ_PRIORITY_DEFAULT,
|
||||
ESP_IRQ_TRIGGER_LEVEL);
|
||||
if (priv->cpuint < 0)
|
||||
|
|
@ -1725,12 +1726,13 @@ struct i2c_master_s *esp_i2cbus_initialize(int port)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
ret = irq_attach(config->irq, esp_i2c_irq, priv);
|
||||
ret = irq_attach(ESP_SOURCE2IRQ(i2c_periph_signal[priv->id].irq),
|
||||
esp_i2c_irq, priv);
|
||||
if (ret != OK)
|
||||
{
|
||||
/* Failed to attach IRQ, free the allocated CPU interrupt */
|
||||
|
||||
esp_teardown_irq(config->periph, priv->cpuint);
|
||||
esp_teardown_irq(i2c_periph_signal[priv->id].irq, priv->cpuint);
|
||||
priv->cpuint = -ENOMEM;
|
||||
priv->refs--;
|
||||
nxmutex_unlock(&priv->lock);
|
||||
|
|
@ -1740,7 +1742,7 @@ struct i2c_master_s *esp_i2cbus_initialize(int port)
|
|||
|
||||
/* Enable the CPU interrupt that is linked to the I2C device. */
|
||||
|
||||
up_enable_irq(config->irq);
|
||||
up_enable_irq(ESP_SOURCE2IRQ(i2c_periph_signal[priv->id].irq));
|
||||
#endif
|
||||
|
||||
esp_i2c_init(priv);
|
||||
|
|
@ -1786,8 +1788,8 @@ int esp_i2cbus_uninitialize(struct i2c_master_s *dev)
|
|||
}
|
||||
|
||||
#ifndef CONFIG_I2C_POLLED
|
||||
up_disable_irq(priv->config->irq);
|
||||
esp_teardown_irq(priv->config->periph, priv->cpuint);
|
||||
up_disable_irq(ESP_SOURCE2IRQ(i2c_periph_signal[priv->id].irq));
|
||||
esp_teardown_irq(i2c_periph_signal[priv->id].irq, priv->cpuint);
|
||||
priv->cpuint = -ENOMEM;
|
||||
#endif
|
||||
|
||||
|
|
|
|||
|
|
@ -80,12 +80,21 @@
|
|||
#define I2C_SLAVE_POLL_RATE 10
|
||||
#endif
|
||||
|
||||
#if SOC_PERIPH_CLK_CTRL_SHARED
|
||||
#define I2C_CLOCK_SRC_ATOMIC() PERIPH_RCC_ATOMIC()
|
||||
#else
|
||||
#define I2C_CLOCK_SRC_ATOMIC()
|
||||
#endif
|
||||
|
||||
#if !SOC_RCC_IS_INDEPENDENT
|
||||
# define I2C_RCC_ATOMIC() PERIPH_RCC_ATOMIC()
|
||||
#else
|
||||
# define I2C_RCC_ATOMIC()
|
||||
#endif
|
||||
|
||||
#define SCL_PIN_ATTR (FUNCTION_2 | INPUT_PULLUP)
|
||||
#define SDA_PIN_ATTR (FUNCTION_2 | INPUT_PULLUP | OUTPUT_OPEN_DRAIN)
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
|
@ -96,16 +105,6 @@ struct esp_i2c_config_s
|
|||
{
|
||||
uint8_t scl_pin; /* GPIO configuration for SCL as SCL */
|
||||
uint8_t sda_pin; /* GPIO configuration for SDA as SDA */
|
||||
|
||||
#ifndef CONFIG_I2C_POLLED
|
||||
uint8_t periph; /* Peripheral ID */
|
||||
uint8_t irq; /* Interrupt ID */
|
||||
#endif
|
||||
|
||||
uint32_t scl_insig; /* I2C SCL input signal index */
|
||||
|
||||
uint32_t sda_insig; /* I2C SDA input signal index */
|
||||
uint32_t sda_outsig; /* I2C SDA output signal index */
|
||||
};
|
||||
|
||||
/* I2C Device Private Data */
|
||||
|
|
@ -193,13 +192,6 @@ static const struct esp_i2c_config_s esp_i2c0_config =
|
|||
{
|
||||
.scl_pin = CONFIG_ESPRESSIF_I2C0_SCLPIN,
|
||||
.sda_pin = CONFIG_ESPRESSIF_I2C0_SDAPIN,
|
||||
#ifndef CONFIG_I2C_POLLED
|
||||
.periph = ETS_I2C_EXT0_INTR_SOURCE,
|
||||
.irq = ESP_IRQ_I2C_EXT0,
|
||||
#endif
|
||||
.scl_insig = I2CEXT0_SCL_IN_IDX,
|
||||
.sda_insig = I2CEXT0_SDA_IN_IDX,
|
||||
.sda_outsig = I2CEXT0_SDA_OUT_IDX
|
||||
};
|
||||
|
||||
static struct esp_i2c_priv_s esp_i2c0_priv =
|
||||
|
|
@ -249,13 +241,6 @@ static const struct esp_i2c_config_s esp_i2c1_config =
|
|||
{
|
||||
.scl_pin = CONFIG_ESPRESSIF_I2C1_SCLPIN,
|
||||
.sda_pin = CONFIG_ESPRESSIF_I2C1_SDAPIN,
|
||||
#ifndef CONFIG_I2C_POLLED
|
||||
.periph = ETS_I2C_EXT1_INTR_SOURCE,
|
||||
.irq = ESP_IRQ_I2C_EXT1,
|
||||
#endif
|
||||
.scl_insig = I2CEXT1_SCL_IN_IDX,
|
||||
.sda_insig = I2CEXT1_SDA_IN_IDX,
|
||||
.sda_outsig = I2CEXT1_SDA_OUT_IDX
|
||||
};
|
||||
|
||||
static struct esp_i2c_priv_s esp_i2c1_priv =
|
||||
|
|
@ -532,17 +517,22 @@ static void esp_i2c_slave_init(struct esp_i2c_priv_s *priv)
|
|||
{
|
||||
const struct esp_i2c_config_s *config = priv->config;
|
||||
|
||||
uint32_t scl_in_sig = i2c_periph_signal[priv->id].scl_in_sig;
|
||||
uint32_t scl_out_sig = i2c_periph_signal[priv->id].scl_out_sig;
|
||||
uint32_t sda_in_sig = i2c_periph_signal[priv->id].sda_in_sig;
|
||||
uint32_t sda_out_sig = i2c_periph_signal[priv->id].sda_out_sig;
|
||||
|
||||
/* Configure GPIO signals for I2C SCL and SDA pins */
|
||||
|
||||
esp_gpiowrite(config->scl_pin, 1);
|
||||
esp_gpiowrite(config->sda_pin, 1);
|
||||
|
||||
esp_configgpio(config->scl_pin, INPUT_PULLUP);
|
||||
esp_gpio_matrix_in(config->scl_pin, config->scl_insig, 0);
|
||||
esp_configgpio(config->scl_pin, SCL_PIN_ATTR);
|
||||
esp_gpio_matrix_in(config->scl_pin, scl_in_sig, 0);
|
||||
|
||||
esp_configgpio(config->sda_pin, INPUT_PULLUP | OUTPUT_OPEN_DRAIN);
|
||||
esp_gpio_matrix_out(config->sda_pin, config->sda_outsig, 0, 0);
|
||||
esp_gpio_matrix_in(config->sda_pin, config->sda_insig, 0);
|
||||
esp_configgpio(config->sda_pin, SDA_PIN_ATTR);
|
||||
esp_gpio_matrix_out(config->sda_pin, sda_out_sig, 0, 0);
|
||||
esp_gpio_matrix_in(config->sda_pin, sda_in_sig, 0);
|
||||
|
||||
/* Enable I2C hardware */
|
||||
|
||||
|
|
@ -552,7 +542,10 @@ static void esp_i2c_slave_init(struct esp_i2c_priv_s *priv)
|
|||
i2c_ll_reset_register(priv->id);
|
||||
}
|
||||
|
||||
i2c_hal_init(priv->ctx, priv->id);
|
||||
I2C_CLOCK_SRC_ATOMIC()
|
||||
{
|
||||
i2c_hal_init(priv->ctx, priv->id);
|
||||
}
|
||||
|
||||
/* Disable I2C interrupts */
|
||||
|
||||
|
|
@ -562,7 +555,11 @@ static void esp_i2c_slave_init(struct esp_i2c_priv_s *priv)
|
|||
|
||||
i2c_hal_slave_init(priv->ctx);
|
||||
i2c_ll_slave_enable_auto_start(priv->ctx->dev, true);
|
||||
i2c_ll_set_source_clk(priv->ctx->dev, I2C_CLK_SRC_DEFAULT);
|
||||
I2C_CLOCK_SRC_ATOMIC()
|
||||
{
|
||||
i2c_ll_set_source_clk(priv->ctx->dev, I2C_CLK_SRC_DEFAULT);
|
||||
}
|
||||
|
||||
i2c_ll_set_slave_addr(priv->ctx->dev, priv->addr, false);
|
||||
i2c_ll_set_rxfifo_full_thr(priv->ctx->dev, I2C_FIFO_FULL_THRESH_VAL);
|
||||
i2c_ll_set_txfifo_empty_thr(priv->ctx->dev, I2C_FIFO_EMPTY_THRESH_VAL);
|
||||
|
|
@ -590,9 +587,11 @@ static void esp_i2c_slave_init(struct esp_i2c_priv_s *priv)
|
|||
|
||||
static void esp_i2c_slave_deinit(struct esp_i2c_priv_s *priv)
|
||||
{
|
||||
const struct esp_i2c_config_s *config = priv->config;
|
||||
I2C_CLOCK_SRC_ATOMIC()
|
||||
{
|
||||
i2c_hal_deinit(priv->ctx);
|
||||
}
|
||||
|
||||
i2c_hal_deinit(priv->ctx);
|
||||
I2C_RCC_ATOMIC()
|
||||
{
|
||||
i2c_ll_enable_bus_clock(priv->id, false);
|
||||
|
|
@ -834,12 +833,6 @@ struct i2c_slave_s *esp_i2cbus_slave_initialize(int port, int addr)
|
|||
{
|
||||
struct esp_i2c_priv_s *priv;
|
||||
int ret;
|
||||
#ifndef CONFIG_I2C_POLLED
|
||||
const struct esp_i2c_config_s *config;
|
||||
#else
|
||||
char *argv[2];
|
||||
char arg1[32];
|
||||
#endif
|
||||
|
||||
switch (port)
|
||||
{
|
||||
|
|
@ -870,16 +863,15 @@ struct i2c_slave_s *esp_i2cbus_slave_initialize(int port, int addr)
|
|||
}
|
||||
|
||||
#ifndef CONFIG_I2C_POLLED
|
||||
config = priv->config;
|
||||
if (priv->cpuint != -ENOMEM)
|
||||
{
|
||||
/* Disable the previous IRQ */
|
||||
|
||||
up_disable_irq(config->irq);
|
||||
esp_teardown_irq(config->periph, priv->cpuint);
|
||||
up_disable_irq(ESP_SOURCE2IRQ(i2c_periph_signal[priv->id].irq));
|
||||
esp_teardown_irq(i2c_periph_signal[priv->id].irq, priv->cpuint);
|
||||
}
|
||||
|
||||
priv->cpuint = esp_setup_irq(config->periph,
|
||||
priv->cpuint = esp_setup_irq(i2c_periph_signal[priv->id].irq,
|
||||
ESP_IRQ_PRIORITY_DEFAULT,
|
||||
ESP_IRQ_TRIGGER_LEVEL);
|
||||
if (priv->cpuint < 0)
|
||||
|
|
@ -892,12 +884,13 @@ struct i2c_slave_s *esp_i2cbus_slave_initialize(int port, int addr)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
ret = irq_attach(config->irq, esp_i2c_slave_irq, priv);
|
||||
ret = irq_attach(ESP_SOURCE2IRQ(i2c_periph_signal[priv->id].irq),
|
||||
esp_i2c_slave_irq, priv);
|
||||
if (ret != OK)
|
||||
{
|
||||
/* Failed to attach IRQ, free the allocated CPU interrupt */
|
||||
|
||||
esp_teardown_irq(config->periph, priv->cpuint);
|
||||
esp_teardown_irq(i2c_periph_signal[priv->id].irq, priv->cpuint);
|
||||
priv->cpuint = -ENOMEM;
|
||||
priv->refs--;
|
||||
nxmutex_unlock(&priv->lock);
|
||||
|
|
@ -907,7 +900,7 @@ struct i2c_slave_s *esp_i2cbus_slave_initialize(int port, int addr)
|
|||
|
||||
/* Enable the CPU interrupt that is linked to the I2C device. */
|
||||
|
||||
up_enable_irq(config->irq);
|
||||
up_enable_irq(ESP_SOURCE2IRQ(i2c_periph_signal[priv->id].irq));
|
||||
#else
|
||||
/* Create thread for polling sensor data */
|
||||
|
||||
|
|
@ -976,8 +969,8 @@ int esp_i2cbus_slave_uninitialize(struct i2c_slave_s *dev)
|
|||
}
|
||||
|
||||
#ifndef CONFIG_I2C_POLLED
|
||||
up_disable_irq(priv->config->irq);
|
||||
esp_teardown_irq(priv->config->periph, priv->cpuint);
|
||||
up_disable_irq(ESP_SOURCE2IRQ(i2c_periph_signal[priv->id].irq));
|
||||
esp_teardown_irq(i2c_periph_signal[priv->id].irq, priv->cpuint);
|
||||
priv->cpuint = -ENOMEM;
|
||||
#endif
|
||||
|
||||
|
|
|
|||
|
|
@ -37,13 +37,8 @@
|
|||
|
||||
#ifndef __ASSEMBLY__
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_I2C0_SLAVE_MODE
|
||||
# define ESPRESSIF_I2C0_SLAVE 0
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_I2C1_SLAVE_MODE
|
||||
# define ESPRESSIF_I2C1_SLAVE 1
|
||||
#endif
|
||||
#define ESPRESSIF_I2C0_SLAVE 0
|
||||
#define ESPRESSIF_I2C1_SLAVE 1
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
|
|
|
|||
|
|
@ -149,15 +149,21 @@ int board_i2c_init(void)
|
|||
int ret = OK;
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE
|
||||
# ifdef CONFIG_ESPRESSIF_I2C0_MASTER_MODE
|
||||
ret = i2c_driver_init(ESPRESSIF_I2C0);
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_I2C_BITBANG
|
||||
# ifdef CONFIG_ESPRESSIF_I2C_BITBANG_MASTER_MODE
|
||||
ret = i2c_bitbang_driver_init(ESPRESSIF_I2C_BITBANG);
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
|
||||
# ifdef CONFIG_ESPRESSIF_I2C0_SLAVE_MODE
|
||||
ret = i2c_slave_driver_init(ESPRESSIF_I2C0_SLAVE, I2C0_SLAVE_ADDR);
|
||||
# endif
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
|
|
|
|||
|
|
@ -148,17 +148,22 @@ int board_i2c_init(void)
|
|||
{
|
||||
int ret = OK;
|
||||
|
||||
#if defined(CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE) && \
|
||||
defined(CONFIG_ESPRESSIF_I2C0)
|
||||
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE
|
||||
# ifdef CONFIG_ESPRESSIF_I2C0_MASTER_MODE
|
||||
ret = i2c_driver_init(ESPRESSIF_I2C0);
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_I2C_BITBANG
|
||||
# ifdef CONFIG_ESPRESSIF_I2C_BITBANG_MASTER_MODE
|
||||
ret = i2c_bitbang_driver_init(ESPRESSIF_I2C_BITBANG);
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
|
||||
# ifdef CONFIG_ESPRESSIF_I2C0_SLAVE_MODE
|
||||
ret = i2c_slave_driver_init(ESPRESSIF_I2C0_SLAVE, I2C0_SLAVE_ADDR);
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_LP_I2C0
|
||||
|
|
|
|||
|
|
@ -47,8 +47,10 @@
|
|||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
|
||||
#define I2C0_SLAVE_ADDR 0x28
|
||||
#define I2C0_SLAVE_NBITS 7
|
||||
# define I2C0_SLAVE_ADDR 0x28
|
||||
# define I2C0_SLAVE_NBITS 7
|
||||
# define I2C1_SLAVE_ADDR 0x28
|
||||
# define I2C1_SLAVE_NBITS 7
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
|
|
@ -115,12 +117,14 @@ static int i2c_slave_driver_init(int bus, int addr)
|
|||
i2c = esp_i2cbus_slave_initialize(bus, addr);
|
||||
if (i2c == NULL)
|
||||
{
|
||||
syslog(LOG_ERR, "Failed to get I2C%d interface\n", bus);
|
||||
syslog(LOG_ERR, "Failed to get I2C%d slave interface\n", bus);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_I2C_DRIVER
|
||||
ret = i2c_slave_register(i2c, bus, addr, I2C0_SLAVE_NBITS);
|
||||
ret = i2c_slave_register(i2c, bus, addr,
|
||||
bus == ESPRESSIF_I2C0_SLAVE ? \
|
||||
I2C0_SLAVE_NBITS : I2C1_SLAVE_NBITS);
|
||||
if (ret < 0)
|
||||
{
|
||||
syslog(LOG_ERR, "Failed to register I2C%d driver: %d\n", bus, ret);
|
||||
|
|
@ -149,25 +153,29 @@ int board_i2c_init(void)
|
|||
int ret = OK;
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE
|
||||
#ifdef CONFIG_ESPRESSIF_I2C0
|
||||
# ifdef CONFIG_ESPRESSIF_I2C0_MASTER_MODE
|
||||
ret = i2c_driver_init(ESPRESSIF_I2C0);
|
||||
if (ret != OK)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_I2C1
|
||||
# ifdef CONFIG_ESPRESSIF_I2C1_MASTER_MODE
|
||||
ret = i2c_driver_init(ESPRESSIF_I2C1);
|
||||
#endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_I2C_BITBANG
|
||||
# ifdef CONFIG_ESPRESSIF_I2C_BITBANG_MASTER_MODE
|
||||
ret = i2c_bitbang_driver_init(ESPRESSIF_I2C_BITBANG);
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE
|
||||
# ifdef CONFIG_ESPRESSIF_I2C0_SLAVE_MODE
|
||||
ret = i2c_slave_driver_init(ESPRESSIF_I2C0_SLAVE, I2C0_SLAVE_ADDR);
|
||||
# endif
|
||||
|
||||
# ifdef CONFIG_ESPRESSIF_I2C1_SLAVE_MODE
|
||||
ret = i2c_slave_driver_init(ESPRESSIF_I2C1_SLAVE, I2C1_SLAVE_ADDR);
|
||||
# endif
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue