This is an automated email from the ASF dual-hosted git repository.

masayuki pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 23cfded18fb7f53c3de5fc545b7f032a1b9e9275
Author: Gustavo Henrique Nihei <[email protected]>
AuthorDate: Mon Mar 21 16:32:55 2022 -0300

    esp32s3-devkit: Add support for BOOT button as user button
    
    Signed-off-by: Gustavo Henrique Nihei <[email protected]>
---
 boards/Kconfig                                     |   2 +
 .../esp32s3-devkit/configs/buttons/defconfig       |  57 +++++++
 boards/xtensa/esp32s3/esp32s3-devkit/src/Make.defs |   4 +
 .../esp32s3/esp32s3-devkit/src/esp32s3-devkit.h    |   6 +
 .../esp32s3/esp32s3-devkit/src/esp32s3_bringup.c   |  14 ++
 .../esp32s3/esp32s3-devkit/src/esp32s3_buttons.c   | 164 +++++++++++++++++++++
 6 files changed, 247 insertions(+)

diff --git a/boards/Kconfig b/boards/Kconfig
index 4d8c449..a571070 100644
--- a/boards/Kconfig
+++ b/boards/Kconfig
@@ -283,6 +283,8 @@ config ARCH_BOARD_ESP32S3_DEVKIT
        bool "Espressif ESP32-S3 DevKit"
        depends on ARCH_CHIP_ESP32S3WROOM1 || ARCH_CHIP_ESP32S3MINI1
        select ARCH_HAVE_LEDS
+       select ARCH_HAVE_BUTTONS
+       select ARCH_HAVE_IRQBUTTONS if ESP32S3_GPIO_IRQ
        ---help---
                The ESP32-S3 DevKit features the ESP32-S3 CPU with dual Xtensa 
LX7 cores.
                It comes in two flavors, the ESP32-S3-DevKitM-1 and the 
ESP32-S3-DevKitC-1.
diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/configs/buttons/defconfig 
b/boards/xtensa/esp32s3/esp32s3-devkit/configs/buttons/defconfig
new file mode 100644
index 0000000..3ecc366
--- /dev/null
+++ b/boards/xtensa/esp32s3/esp32s3-devkit/configs/buttons/defconfig
@@ -0,0 +1,57 @@
+#
+# 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_BUTTONS=y
+CONFIG_ARCH_CHIP="esp32s3"
+CONFIG_ARCH_CHIP_ESP32S3=y
+CONFIG_ARCH_CHIP_ESP32S3WROOM1=y
+CONFIG_ARCH_IRQBUTTONS=y
+CONFIG_ARCH_STACKDUMP=y
+CONFIG_ARCH_XTENSA=y
+CONFIG_BOARD_LOOPSPERMSEC=16717
+CONFIG_BUILTIN=y
+CONFIG_DEBUG_FULLOPT=y
+CONFIG_DEBUG_SYMBOLS=y
+CONFIG_ESP32S3_GPIO_IRQ=y
+CONFIG_ESP32S3_UART0=y
+CONFIG_EXAMPLES_BUTTONS=y
+CONFIG_EXAMPLES_BUTTONS_NAME0="BOOT"
+CONFIG_EXAMPLES_BUTTONS_NAMES=y
+CONFIG_EXAMPLES_BUTTONS_QTD=1
+CONFIG_FS_PROCFS=y
+CONFIG_HAVE_CXX=y
+CONFIG_HAVE_CXXINITIALIZE=y
+CONFIG_IDLETHREAD_STACKSIZE=3072
+CONFIG_INIT_ENTRYPOINT="nsh_main"
+CONFIG_INPUT=y
+CONFIG_INPUT_BUTTONS=y
+CONFIG_INPUT_BUTTONS_LOWER=y
+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_RAW_BINARY=y
+CONFIG_RR_INTERVAL=200
+CONFIG_SCHED_WAITPID=y
+CONFIG_START_DAY=6
+CONFIG_START_MONTH=12
+CONFIG_START_YEAR=2011
+CONFIG_SYSTEM_NSH=y
+CONFIG_UART0_SERIAL_CONSOLE=y
diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/src/Make.defs 
b/boards/xtensa/esp32s3/esp32s3-devkit/src/Make.defs
index 25297e8..6599361 100644
--- a/boards/xtensa/esp32s3/esp32s3-devkit/src/Make.defs
+++ b/boards/xtensa/esp32s3/esp32s3-devkit/src/Make.defs
@@ -33,6 +33,10 @@ CSRCS += esp32s3_reset.c
 endif
 endif
 
+ifeq ($(CONFIG_ARCH_BUTTONS),y)
+CSRCS += esp32s3_buttons.c
+endif
+
 SCRIPTIN = $(SCRIPTDIR)$(DELIM)esp32s3.template.ld
 SCRIPTOUT = $(SCRIPTDIR)$(DELIM)esp32s3_out.ld
 
diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3-devkit.h 
b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3-devkit.h
index 5588ecc..be39476 100644
--- a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3-devkit.h
+++ b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3-devkit.h
@@ -33,6 +33,12 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
+/* ESP32-S3-DEVKIT GPIOs ****************************************************/
+
+/* BOOT Button */
+
+#define BUTTON_BOOT  0
+
 /****************************************************************************
  * Public Types
  ****************************************************************************/
diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c 
b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c
index 8813a64d..555c010 100644
--- a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c
+++ b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c
@@ -50,6 +50,10 @@
 #  include "esp32s3_board_wdt.h"
 #endif
 
+#ifdef CONFIG_INPUT_BUTTONS
+#  include <nuttx/input/buttons.h>
+#endif
+
 #include "esp32s3-devkit.h"
 
 /****************************************************************************
@@ -123,6 +127,16 @@ int esp32s3_bringup(void)
     }
 #endif
 
+#ifdef CONFIG_INPUT_BUTTONS
+  /* Register the BUTTON driver */
+
+  ret = btn_lower_initialize("/dev/buttons");
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "Failed to initialize button driver: %d\n", ret);
+    }
+#endif
+
 #ifdef CONFIG_ESP32S3_SPIFLASH
   ret = board_spiflash_init();
   if (ret)
diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_buttons.c 
b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_buttons.c
new file mode 100644
index 0000000..b8be81c
--- /dev/null
+++ b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_buttons.c
@@ -0,0 +1,164 @@
+/****************************************************************************
+ * boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_buttons.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>
+
+#include <assert.h>
+#include <debug.h>
+#include <stdbool.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/board.h>
+#include <nuttx/irq.h>
+
+#include "esp32s3_gpio.h"
+#include "hardware/esp32s3_gpio_sigmap.h"
+
+#include "esp32s3-devkit.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_button_initialize
+ *
+ * Description:
+ *   board_button_initialize() must be called to initialize button resources.
+ *   After that, board_buttons() may be called to collect the current state
+ *   of all buttons or board_button_irq() may be called to register button
+ *   interrupt handlers.
+ *
+ ****************************************************************************/
+
+uint32_t board_button_initialize(void)
+{
+  esp32s3_configgpio(BUTTON_BOOT, INPUT_FUNCTION_2 | PULLUP);
+  return 1;
+}
+
+/****************************************************************************
+ * Name: board_buttons
+ *
+ * Description:
+ *   After board_button_initialize() has been called, board_buttons() may be
+ *   called to collect the state of all buttons.  board_buttons() returns an
+ *   8-bit bit set with each bit associated with a button.  See the
+ *   BUTTON_*_BIT  definitions in board.h for the meaning of each bit.
+ *
+ ****************************************************************************/
+
+uint32_t board_buttons(void)
+{
+  uint8_t ret = 0;
+  int i = 0;
+  int n = 0;
+
+  bool b0 = esp32s3_gpioread(BUTTON_BOOT);
+
+  for (i = 0; i < 10; i++)
+    {
+      up_mdelay(1);
+
+      bool b1 = esp32s3_gpioread(BUTTON_BOOT);
+
+      if (b0 == b1)
+        {
+          n++;
+        }
+      else
+        {
+          n = 0;
+        }
+
+      if (3 == n)
+        {
+          break;
+        }
+
+      b0 = b1;
+    }
+
+  iinfo("b=%d n=%d\n", b0, n);
+
+  /* Low value means that the button is pressed */
+
+  if (!b0)
+    {
+      ret = 0x1;
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: board_button_irq
+ *
+ * Description:
+ *   board_button_irq() may be called to register an interrupt handler that
+ *   will be called when a button is depressed or released.  The ID value is
+ *   a button enumeration value that uniquely identifies a button resource.
+ *   See the BUTTON_* definitions in board.h for the meaning of enumeration
+ *   value.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_ARCH_IRQBUTTONS
+int board_button_irq(int id, xcpt_t irqhandler, FAR void *arg)
+{
+  int ret;
+  DEBUGASSERT(id == BUTTON_BOOT);
+
+  int irq = ESP32S3_PIN2IRQ(BUTTON_BOOT);
+
+  if (irqhandler != NULL)
+    {
+      /* Make sure the interrupt is disabled */
+
+      esp32s3_gpioirqdisable(irq);
+
+      ret = irq_attach(irq, irqhandler, arg);
+      if (ret < 0)
+        {
+          syslog(LOG_ERR, "ERROR: irq_attach() failed: %d\n", ret);
+          return ret;
+        }
+
+      gpioinfo("Attach %p\n", irqhandler);
+
+      gpioinfo("Enabling the interrupt\n");
+
+      /* Configure the interrupt for rising and falling edges */
+
+      esp32s3_gpioirqenable(irq, GPIO_INTR_ANYEDGE);
+    }
+  else
+    {
+      gpioinfo("Disable the interrupt\n");
+      esp32s3_gpioirqdisable(irq);
+    }
+
+  return OK;
+}
+#endif

Reply via email to