From 036e3375e8da4317f708a3c45cd72f51b6cb627c Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Nihei Date: Tue, 28 Jun 2022 13:51:52 -0300 Subject: [PATCH] esp32s3-devkit: Add support for I2C chardev driver Signed-off-by: Gustavo Henrique Nihei --- boards/xtensa/esp32s3/common/src/Make.defs | 4 + .../esp32s3/common/src/esp32s3_board_i2c.c | 93 +++++++++++++++++++ .../esp32s3-devkit/configs/i2c/defconfig | 60 ++++++++++++ .../esp32s3-devkit/src/esp32s3-devkit.h | 16 ++++ .../esp32s3-devkit/src/esp32s3_bringup.c | 14 +++ 5 files changed, 187 insertions(+) create mode 100644 boards/xtensa/esp32s3/common/src/esp32s3_board_i2c.c create mode 100644 boards/xtensa/esp32s3/esp32s3-devkit/configs/i2c/defconfig diff --git a/boards/xtensa/esp32s3/common/src/Make.defs b/boards/xtensa/esp32s3/common/src/Make.defs index 26685e6768..91cc34ee20 100644 --- a/boards/xtensa/esp32s3/common/src/Make.defs +++ b/boards/xtensa/esp32s3/common/src/Make.defs @@ -32,6 +32,10 @@ ifeq ($(CONFIG_ESP32S3_SPIFLASH),y) CSRCS += esp32s3_board_spiflash.c endif +ifeq ($(CONFIG_I2C_DRIVER),y) + CSRCS += esp32s3_board_i2c.c +endif + DEPPATH += --dep-path src VPATH += :src CFLAGS += $(shell $(INCDIR) "$(CC)" $(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src) diff --git a/boards/xtensa/esp32s3/common/src/esp32s3_board_i2c.c b/boards/xtensa/esp32s3/common/src/esp32s3_board_i2c.c new file mode 100644 index 0000000000..04280ac459 --- /dev/null +++ b/boards/xtensa/esp32s3/common/src/esp32s3_board_i2c.c @@ -0,0 +1,93 @@ +/**************************************************************************** + * boards/xtensa/esp32s3/common/src/esp32s3_board_i2c.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 + +#include +#include +#include + +#include + +#include "esp32s3_i2c.h" +#include "esp32s3-devkit.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +static int i2c_driver_init(int bus) +{ + struct i2c_master_s *i2c; + int ret; + + i2c = esp32s3_i2cbus_initialize(bus); + 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); + esp32s3_i2cbus_uninitialize(i2c); + } + + return ret; +} + +/**************************************************************************** + * Name: board_i2c_init + * + * Description: + * Configure the I2C driver. + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +int board_i2c_init(void) +{ + int ret = OK; + +#ifdef CONFIG_ESP32S3_I2C0 + ret = i2c_driver_init(ESP32S3_I2C0); + if (ret != OK) + { + goto done; + } +#endif + +#ifdef CONFIG_ESP32S3_I2C1 + ret = i2c_driver_init(ESP32S3_I2C1); +#endif + +done: + return ret; +} + diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/configs/i2c/defconfig b/boards/xtensa/esp32s3/esp32s3-devkit/configs/i2c/defconfig new file mode 100644 index 0000000000..80665dca93 --- /dev/null +++ b/boards/xtensa/esp32s3/esp32s3-devkit/configs/i2c/defconfig @@ -0,0 +1,60 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_ARCH_LEDS is not set +# CONFIG_NSH_ARGCAT is not set +# CONFIG_NSH_CMDOPT_HEXDUMP is not set +# CONFIG_NSH_CMDPARMS is not set +CONFIG_ARCH="xtensa" +CONFIG_ARCH_BOARD="esp32s3-devkit" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_ESP32S3_DEVKIT=y +CONFIG_ARCH_CHIP="esp32s3" +CONFIG_ARCH_CHIP_ESP32S3=y +CONFIG_ARCH_CHIP_ESP32S3WROOM1=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_XTENSA=y +CONFIG_BOARD_LOOPSPERMSEC=16717 +CONFIG_BUILTIN=y +CONFIG_DEBUG_ASSERTIONS=y +CONFIG_DEBUG_ERROR=y +CONFIG_DEBUG_FEATURES=y +CONFIG_DEBUG_I2C=y +CONFIG_DEBUG_I2C_ERROR=y +CONFIG_DEBUG_I2C_INFO=y +CONFIG_DEBUG_I2C_WARN=y +CONFIG_DEBUG_INFO=y +CONFIG_DEBUG_WARN=y +CONFIG_ESP32S3_I2C0=y +CONFIG_ESP32S3_I2C1=y +CONFIG_ESP32S3_UART0=y +CONFIG_FS_PROCFS=y +CONFIG_HAVE_CXX=y +CONFIG_HAVE_CXXINITIALIZE=y +CONFIG_I2CTOOL_DEFFREQ=100000 +CONFIG_I2CTOOL_MAXBUS=1 +CONFIG_I2C_RESET=y +CONFIG_I2C_TRACE=y +CONFIG_IDLETHREAD_STACKSIZE=3072 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INTELHEX_BINARY=y +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_LINELEN=64 +CONFIG_NSH_READLINE=y +CONFIG_PREALLOC_TIMERS=4 +CONFIG_RAM_SIZE=114688 +CONFIG_RAM_START=0x20000000 +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_WAITPID=y +CONFIG_START_DAY=28 +CONFIG_START_MONTH=6 +CONFIG_START_YEAR=2022 +CONFIG_SYSTEM_I2CTOOL=y +CONFIG_SYSTEM_NSH=y +CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3-devkit.h b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3-devkit.h index be394764ff..a2b7965139 100644 --- a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3-devkit.h +++ b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3-devkit.h @@ -81,5 +81,21 @@ int esp32s3_bringup(void); int board_spiflash_init(void); #endif +/**************************************************************************** + * Name: board_i2c_init + * + * Description: + * Configure the I2C driver. + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_I2C_DRIVER +int board_i2c_init(void); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __BOARDS_XTENSA_ESP32S3_ESP32S3_DEVKIT_SRC_ESP32S3_DEVKIT_H */ diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c index 555c010b85..2fb9dc3120 100644 --- a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c +++ b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c @@ -46,6 +46,10 @@ # include "esp32s3_rt_timer.h" #endif +#ifdef CONFIG_ESP32S3_I2C +# include "esp32s3_i2c.h" +#endif + #ifdef CONFIG_WATCHDOG # include "esp32s3_board_wdt.h" #endif @@ -127,6 +131,16 @@ int esp32s3_bringup(void) } #endif +#ifdef CONFIG_I2C_DRIVER + /* Configure I2C peripheral interfaces */ + + ret = board_i2c_init(); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to initialize I2C driver: %d\n", ret); + } +#endif + #ifdef CONFIG_INPUT_BUTTONS /* Register the BUTTON driver */