esp32[s2|s3]: Add I2C bitbang support

This commit is contained in:
Eren Terzioglu 2024-10-22 12:36:50 +02:00 committed by Alan C. Assis
parent 4afaef1a30
commit 3796f56748
17 changed files with 466 additions and 6 deletions

View file

@ -23,6 +23,23 @@ config ESPRESSIF_TEMP
---help---
A built-in sensor used to measure the chip's internal temperature.
config ESPRESSIF_I2C_PERIPH
bool
depends on (ESP32S3_I2C0 || ESP32S3_I2C1) || (ESP32_I2C0 || ESP32_I2C1) || (ESP32S2_I2C0 || ESP32S2_I2C1)
default n
config ESPRESSIF_I2C_BITBANG
bool "I2C Bitbang"
default n
select I2C_BITBANG
select ESP32S3_I2C if ARCH_CHIP_ESP32S3
select ESP32S2_I2C if ARCH_CHIP_ESP32S2
select ESP32_I2C if ARCH_CHIP_ESP32
select I2C
select I2C_BITBANG
---help---
Software implemented I2C peripheral with GPIOs. Suggested to use if I2C peripherals are already in use.
config ESPRESSIF_SPIFLASH
bool "SPI Flash"
depends on ARCH_CHIP_ESP32S2
@ -72,6 +89,21 @@ config ESPRESSIF_TEMP_THREAD_STACKSIZE
endmenu # ESPRESSIF_TEMP
menu "I2C bitbang configuration"
depends on ESPRESSIF_I2C_BITBANG
config ESPRESSIF_I2C_BITBANG_SCLPIN
int "I2C Bitbang SCL Pin"
default 0
range 0 21
config ESPRESSIF_I2C_BITBANG_SDAPIN
int "I2C Bitbang SDA Pin"
default 1
range 0 21
endmenu # I2C bitbang configuration
config ESPRESSIF_HAVE_OTA_PARTITION
bool
default n

View file

@ -40,6 +40,10 @@ ifeq ($(CONFIG_ESPRESSIF_TEMP),y)
CHIP_CSRCS += esp_temperature_sensor.c
endif
ifeq ($(CONFIG_ESPRESSIF_I2C_BITBANG),y)
CHIP_CSRCS += esp_i2c_bitbang.c
endif
ifeq ($(CONFIG_ESPRESSIF_SPIFLASH),y)
CHIP_CSRCS += esp_spiflash.c
ifeq ($(CONFIG_ESPRESSIF_MTD),y)

View file

@ -0,0 +1,277 @@
/****************************************************************************
* arch/xtensa/src/common/espressif/esp_i2c_bitbang.c
*
* 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>
#ifdef CONFIG_ESPRESSIF_I2C_BITBANG
#include <assert.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/i2c/i2c_bitbang.h>
#include <nuttx/kmalloc.h>
#include "espressif/esp_i2c_bitbang.h"
#if defined(CONFIG_ARCH_CHIP_ESP32S3)
#include "esp32s3_gpio.h"
#include "hardware/esp32s3_gpio_sigmap.h"
#elif defined(CONFIG_ARCH_CHIP_ESP32S2)
#include "esp32s2_gpio.h"
#include "esp32s2_gpio_sigmap.h"
#else
#include "esp32_gpio.h"
#include "esp32_gpio_sigmap.h"
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#if defined(CONFIG_ARCH_CHIP_ESP32S3)
#define CONFIG_GPIO(pin, attr) esp32s3_configgpio(pin, attr)
#define GPIO_MATRIX_OUT(pin, idx, inv, en_inv) esp32s3_gpio_matrix_out(pin, \
idx, inv, en_inv)
#define GPIO_WRITE(pin, value) esp32s3_gpiowrite(pin, value)
#define GPIO_READ(pin) esp32s3_gpioread(pin)
#elif defined(CONFIG_ARCH_CHIP_ESP32S2)
#define CONFIG_GPIO(pin, attr) esp32s2_configgpio(pin, attr)
#define GPIO_MATRIX_OUT(pin, idx, inv, en_inv) esp32s2_gpio_matrix_out(pin, \
idx, inv, en_inv)
#define GPIO_WRITE(pin, value) esp32s2_gpiowrite(pin, value)
#define GPIO_READ(pin) esp32s2_gpioread(pin)
#else
#define CONFIG_GPIO(pin, attr) esp32_configgpio(pin, attr)
#define GPIO_MATRIX_OUT(pin, idx, inv, en_inv) esp32_gpio_matrix_out(pin, \
idx, inv, en_inv)
#define GPIO_WRITE(pin, value) esp32_gpiowrite(pin, value)
#define GPIO_READ(pin) esp3_gpioread(pin)
#endif
/****************************************************************************
* Private Types
****************************************************************************/
struct esp_i2c_bitbang_dev_s
{
struct i2c_bitbang_lower_dev_s lower;
int sda_pin;
int scl_pin;
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static void esp_i2c_bitbang_init(struct i2c_bitbang_lower_dev_s *lower);
static void esp_i2c_bitbang_set_scl(struct i2c_bitbang_lower_dev_s *lower,
bool value);
static void esp_i2c_bitbang_set_sda(struct i2c_bitbang_lower_dev_s *lower,
bool value);
static bool esp_i2c_bitbang_get_scl(struct i2c_bitbang_lower_dev_s *lower);
static bool esp_i2c_bitbang_get_sda(struct i2c_bitbang_lower_dev_s *lower);
/****************************************************************************
* Private Data
****************************************************************************/
/* Lower-half I2C bitbang data */
const static struct i2c_bitbang_lower_ops_s g_ops =
{
.initialize = esp_i2c_bitbang_init,
.set_scl = esp_i2c_bitbang_set_scl,
.set_sda = esp_i2c_bitbang_set_sda,
.get_scl = esp_i2c_bitbang_get_scl,
.get_sda = esp_i2c_bitbang_get_sda
};
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: esp_i2c_bitbang_init
*
* Description:
* Initialize the I2C bit-bang driver
*
* Input Parameters:
* lower - A pointer the publicly visible representation of
* the "lower-half" driver state structure.
*
* Returned Value:
* None
*
****************************************************************************/
static void esp_i2c_bitbang_init(struct i2c_bitbang_lower_dev_s *lower)
{
struct esp_i2c_bitbang_dev_s *dev = lower->priv;
GPIO_WRITE(dev->scl_pin, 1);
GPIO_WRITE(dev->sda_pin, 1);
CONFIG_GPIO(dev->scl_pin, INPUT_PULLUP | OUTPUT_OPEN_DRAIN);
GPIO_MATRIX_OUT(dev->scl_pin, SIG_GPIO_OUT_IDX, 0, 0);
CONFIG_GPIO(dev->sda_pin, INPUT_PULLUP | OUTPUT_OPEN_DRAIN);
GPIO_MATRIX_OUT(dev->sda_pin, SIG_GPIO_OUT_IDX, 0, 0);
}
/****************************************************************************
* Name: esp_i2c_bitbang_set_scl
*
* Description:
* Set SCL line value
*
* Input Parameters:
* lower - A pointer the publicly visible representation of
* the "lower-half" driver state structure.
* value - The value to be written (0 or 1).
*
* Returned Value:
* None
*
****************************************************************************/
static void esp_i2c_bitbang_set_scl(struct i2c_bitbang_lower_dev_s *lower,
bool value)
{
struct esp_i2c_bitbang_dev_s *dev =
(struct esp_i2c_bitbang_dev_s *)lower->priv;
GPIO_WRITE(dev->scl_pin, value);
}
/****************************************************************************
* Name: esp_i2c_bitbang_set_sda
*
* Description:
* Set SDA line value
*
* Input Parameters:
* lower - A pointer the publicly visible representation of
* the "lower-half" driver state structure.
* value - The value to be written (0 or 1).
*
* Returned Value:
* None
*
****************************************************************************/
static void esp_i2c_bitbang_set_sda(struct i2c_bitbang_lower_dev_s *lower,
bool value)
{
struct esp_i2c_bitbang_dev_s *dev =
(struct esp_i2c_bitbang_dev_s *)lower->priv;
GPIO_WRITE(dev->sda_pin, value);
}
/****************************************************************************
* Name: esp_i2c_bitbang_get_scl
*
* Description:
* Get value from SCL line
*
* Input Parameters:
* lower - A pointer the publicly visible representation of
* the "lower-half" driver state structure.
*
* Returned Value:
* The boolean representation of the SCL line value (true/false).
*
****************************************************************************/
static bool esp_i2c_bitbang_get_scl(struct i2c_bitbang_lower_dev_s *lower)
{
struct esp_i2c_bitbang_dev_s *dev =
(struct esp_i2c_bitbang_dev_s *)lower->priv;
return GPIO_READ(dev->scl_pin);
}
/****************************************************************************
* Name: esp_i2c_bitbang_get_sda
*
* Description:
* Get value from SDA line
*
* Input Parameters:
* lower - A pointer the publicly visible representation of
* the "lower-half" driver state structure.
*
* Returned Value:
* The boolean representation of the SDA line value (true/false).
*
****************************************************************************/
static bool esp_i2c_bitbang_get_sda(struct i2c_bitbang_lower_dev_s *lower)
{
struct esp_i2c_bitbang_dev_s *dev =
(struct esp_i2c_bitbang_dev_s *)lower->priv;
return GPIO_READ(dev->sda_pin);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: esp_i2cbus_bitbang_initialize
*
* Description:
* Initialize the I2C bitbang driver. And return a unique instance of
* struct struct i2c_master_s. This function may be called to obtain
* multiple instances of the interface, each of which may be set up with
* a different frequency and slave address.
*
* Input Parameters:
* None
*
* Returned Value:
* Valid I2C device structure reference on success; a NULL on failure
*
****************************************************************************/
struct i2c_master_s *esp_i2cbus_bitbang_initialize(void)
{
struct esp_i2c_bitbang_dev_s *dev =
(struct esp_i2c_bitbang_dev_s *)
kmm_malloc(sizeof(struct esp_i2c_bitbang_dev_s));
DEBUGASSERT(dev);
dev->lower.ops = &g_ops;
dev->lower.priv = dev;
dev->scl_pin = CONFIG_ESPRESSIF_I2C_BITBANG_SCLPIN;
dev->sda_pin = CONFIG_ESPRESSIF_I2C_BITBANG_SDAPIN;
return i2c_bitbang_initialize(&dev->lower);
}
#endif /* CONFIG_ESPRESSIF_I2C_BITBANG */

View file

@ -0,0 +1,89 @@
/****************************************************************************
* arch/xtensa/src/common/espressif/esp_i2c_bitbang.h
*
* 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 __ARCH_XTENSA_SRC_COMMON_ESPRESSIF_ESP_I2C_BITBANG_H
#define __ARCH_XTENSA_SRC_COMMON_ESPRESSIF_ESP_I2C_BITBANG_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/i2c/i2c_master.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef CONFIG_ESPRESSIF_I2C_BITBANG
# define ESPRESSIF_I2C_BITBANG 3
#endif
/****************************************************************************
* Public Types
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Inline Functions
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef CONFIG_ESPRESSIF_I2C_BITBANG
/****************************************************************************
* Name: esp_i2cbus_bitbang_initialize
*
* Description:
* Initialize the I2C bitbang driver. And return a unique instance of
* struct struct i2c_master_s. This function may be called to obtain
* multiple instances of the interface, each of which may be set up with
* a different frequency and slave address.
*
* Input Parameters:
* None
*
* Returned Value:
* Valid I2C device structure reference on success; a NULL on failure
*
****************************************************************************/
struct i2c_master_s *esp_i2cbus_bitbang_initialize(void);
#endif
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __ARCH_XTENSA_SRC_COMMON_ESPRESSIF_ESP_I2C_BITBANG_H */

View file

@ -760,11 +760,13 @@ config ESP32_I2C0
bool "I2C 0"
default n
select ESP32_I2C
select ESPRESSIF_I2C_PERIPH
config ESP32_I2C1
bool "I2C 1"
default n
select ESP32_I2C
select ESPRESSIF_I2C_PERIPH
config ESP32_TWAI0
bool "TWAI (CAN) 0"
@ -1135,10 +1137,12 @@ endif # ESP32_I2C1
config ESP32_I2CTIMEOSEC
int "Timeout seconds"
default 0
depends on ESPRESSIF_I2C_PERIPH
config ESP32_I2CTIMEOMS
int "Timeout milliseconds"
default 500
depends on ESPRESSIF_I2C_PERIPH
endmenu # I2C configuration

View file

@ -75,8 +75,10 @@ CHIP_CSRCS += esp32_ledc.c
endif
ifeq ($(CONFIG_ESP32_I2C),y)
ifeq ($(CONFIG_ESPRESSIF_I2C_PERIPH),y)
CHIP_CSRCS += esp32_i2c.c
endif
endif
ifeq ($(CONFIG_ESP32_I2S),y)
CHIP_CSRCS += esp32_i2s.c

View file

@ -24,7 +24,7 @@
#include <nuttx/config.h>
#ifdef CONFIG_ESP32_I2C
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH
#include <sys/types.h>
#include <stdio.h>
@ -1569,4 +1569,4 @@ int esp32_i2cbus_uninitialize(struct i2c_master_s *dev)
return OK;
}
#endif /* CONFIG_ESP32_I2C */
#endif /* CONFIG_ESPRESSIF_I2C_PERIPH */

View file

@ -55,6 +55,7 @@ extern "C"
* Public Function Prototypes
****************************************************************************/
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH
/****************************************************************************
* Name: esp32_i2cbus_initialize
*
@ -90,6 +91,7 @@ struct i2c_master_s *esp32_i2cbus_initialize(int port);
****************************************************************************/
int esp32_i2cbus_uninitialize(struct i2c_master_s *dev);
#endif /* CONFIG_ESPRESSIF_I2C_PERIPH */
#ifdef __cplusplus
}

View file

@ -471,12 +471,14 @@ config ESP32S2_I2C0
default n
select ESP32S2_I2C
select I2C
select ESPRESSIF_I2C_PERIPH
config ESP32S2_I2C1
bool "I2C 1"
default n
select ESP32S2_I2C
select I2C
select ESPRESSIF_I2C_PERIPH
config ESP32S2_TWAI
bool "TWAI (CAN)"
@ -791,10 +793,12 @@ endif # ESP32S2_I2C1
config ESP32S2_I2CTIMEOSEC
int "Timeout seconds"
default 0
depends on ESPRESSIF_I2C_PERIPH
config ESP32S2_I2CTIMEOMS
int "Timeout milliseconds"
default 500
depends on ESPRESSIF_I2C_PERIPH
endmenu # I2C Configuration

View file

@ -54,8 +54,10 @@ CHIP_CSRCS += esp32s2_rng.c
endif
ifeq ($(CONFIG_ESP32S2_I2C),y)
ifeq ($(CONFIG_ESPRESSIF_I2C_PERIPH),y)
CHIP_CSRCS += esp32s2_i2c.c
endif
endif
ifeq ($(CONFIG_ESP32S2_I2S),y)
CHIP_CSRCS += esp32s2_i2s.c

View file

@ -24,7 +24,7 @@
#include <nuttx/config.h>
#ifdef CONFIG_ESP32S2_I2C
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH
#include <assert.h>
#include <debug.h>
@ -1613,4 +1613,4 @@ int esp32s2_i2cbus_uninitialize(struct i2c_master_s *dev)
return OK;
}
#endif /* CONFIG_ESP32S2_I2C */
#endif /* CONFIG_ESPRESSIF_I2C_PERIPH */

View file

@ -51,6 +51,7 @@
* Public Function Prototypes
****************************************************************************/
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH
/****************************************************************************
* Name: esp32s2_i2cbus_initialize
*
@ -86,6 +87,7 @@ struct i2c_master_s *esp32s2_i2cbus_initialize(int port);
****************************************************************************/
int esp32s2_i2cbus_uninitialize(struct i2c_master_s *dev);
#endif /* CONFIG_ESPRESSIF_I2C_PERIPH */
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_I2C_H */

View file

@ -683,12 +683,14 @@ config ESP32S3_I2C0
default n
select ESP32S3_I2C
select I2C
select ESPRESSIF_I2C_PERIPH
config ESP32S3_I2C1
bool "I2C 1"
default n
select ESP32S3_I2C
select I2C
select ESPRESSIF_I2C_PERIPH
config ESP32S3_TWAI
bool "TWAI (CAN)"
@ -1430,10 +1432,12 @@ endif # ESP32S3_I2C1
config ESP32S3_I2CTIMEOSEC
int "Timeout seconds"
default 0
depends on ESPRESSIF_I2C_PERIPH
config ESP32S3_I2CTIMEOMS
int "Timeout milliseconds"
default 500
depends on ESPRESSIF_I2C_PERIPH
endmenu # I2C Configuration

View file

@ -114,8 +114,10 @@ CHIP_CSRCS += esp32s3_adc.c
endif
ifeq ($(CONFIG_ESP32S3_I2C),y)
ifeq ($(CONFIG_ESPRESSIF_I2C_PERIPH),y)
CHIP_CSRCS += esp32s3_i2c.c
endif
endif
ifeq ($(CONFIG_ESP32S3_I2S),y)
CHIP_CSRCS += esp32s3_i2s.c

View file

@ -24,7 +24,7 @@
#include <nuttx/config.h>
#ifdef CONFIG_ESP32S3_I2C
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH
#include <assert.h>
#include <debug.h>
@ -1647,4 +1647,4 @@ int esp32s3_i2cbus_uninitialize(struct i2c_master_s *dev)
return OK;
}
#endif /* CONFIG_ESP32S3_I2C */
#endif /* CONFIG_ESPRESSIF_I2C_PERIPH */

View file

@ -51,6 +51,7 @@
* Public Function Prototypes
****************************************************************************/
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH
/****************************************************************************
* Name: esp32s3_i2cbus_initialize
*
@ -86,6 +87,7 @@ struct i2c_master_s *esp32s3_i2cbus_initialize(int port);
****************************************************************************/
int esp32s3_i2cbus_uninitialize(struct i2c_master_s *dev);
#endif /* CONFIG_ESPRESSIF_I2C_PERIPH */
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_XTENSA_SRC_ESP32S3_ESP32S3_I2C_H */

View file

@ -30,12 +30,41 @@
#include <nuttx/i2c/i2c_master.h>
#ifdef CONFIG_ESPRESSIF_I2C_BITBANG
#include "espressif/esp_i2c_bitbang.h"
#endif
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH
#include "esp32s3_i2c.h"
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
#ifdef CONFIG_ESPRESSIF_I2C_BITBANG
static int i2c_bitbang_driver_init(int bus)
{
struct i2c_master_s *i2c;
int ret;
i2c = esp_i2cbus_bitbang_initialize();
if (i2c == NULL)
{
i2cerr("Failed to get I2C%d interface\n", bus);
return -ENODEV;
}
ret = i2c_register(i2c, bus);
if (ret < 0)
{
i2cerr("Failed to register I2C%d driver: %d\n", bus, ret);
}
return ret;
}
#endif
#ifdef CONFIG_ESPRESSIF_I2C_PERIPH
static int i2c_driver_init(int bus)
{
struct i2c_master_s *i2c;
@ -57,6 +86,7 @@ static int i2c_driver_init(int bus)
return ret;
}
#endif
/****************************************************************************
* Name: board_i2c_init
@ -86,6 +116,10 @@ int board_i2c_init(void)
ret = i2c_driver_init(ESP32S3_I2C1);
#endif
#ifdef CONFIG_ESPRESSIF_I2C_BITBANG
ret = i2c_bitbang_driver_init(ESPRESSIF_I2C_BITBANG);
#endif
#ifdef CONFIG_ESP32S3_I2C0
done:
#endif