From 72d98e645afae40e8351cacc80fedabf9877e96f Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Nihei Date: Mon, 22 Mar 2021 18:18:03 -0300 Subject: [PATCH] boards/esp32-ethernet-kit: Add Button support via the BOOT button --- boards/Kconfig | 2 + .../esp32/esp32-ethernet-kit/src/Make.defs | 4 + .../src/esp32-ethernet-kit.h | 6 + .../esp32-ethernet-kit/src/esp32_bringup.c | 14 ++ .../esp32-ethernet-kit/src/esp32_buttons.c | 166 ++++++++++++++++++ 5 files changed, 192 insertions(+) create mode 100644 boards/xtensa/esp32/esp32-ethernet-kit/src/esp32_buttons.c diff --git a/boards/Kconfig b/boards/Kconfig index 2048ad215a..424533fd62 100644 --- a/boards/Kconfig +++ b/boards/Kconfig @@ -212,6 +212,8 @@ config ARCH_BOARD_ESP32_DEVKITC config ARCH_BOARD_ESP32_ETHERNETKIT bool "Espressif ESP32 Ethernet Kit" depends on ARCH_CHIP_ESP32WROVER + select ARCH_HAVE_BUTTONS + select ARCH_HAVE_IRQBUTTONS if ESP32_GPIO_IRQ ---help--- The ESP32-Ethernet-Kit is an Ethernet-to-Wi-Fi development board that enables Ethernet devices to be interconnected over Wi-Fi. At the same time, to provide diff --git a/boards/xtensa/esp32/esp32-ethernet-kit/src/Make.defs b/boards/xtensa/esp32/esp32-ethernet-kit/src/Make.defs index 8f1f9c667d..9bad435eff 100644 --- a/boards/xtensa/esp32/esp32-ethernet-kit/src/Make.defs +++ b/boards/xtensa/esp32/esp32-ethernet-kit/src/Make.defs @@ -45,6 +45,10 @@ ifeq ($(CONFIG_ESP32_SPIFLASH),y) CSRCS += esp32_spiflash.c endif +ifeq ($(CONFIG_ARCH_BUTTONS),y) +CSRCS += esp32_buttons.c +endif + SCRIPTIN = $(SCRIPTDIR)$(DELIM)esp32.template.ld SCRIPTOUT = $(SCRIPTDIR)$(DELIM)esp32_out.ld diff --git a/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32-ethernet-kit.h b/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32-ethernet-kit.h index 4f40bd7181..3d35f2259b 100644 --- a/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32-ethernet-kit.h +++ b/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32-ethernet-kit.h @@ -33,6 +33,12 @@ * Pre-processor Definitions ****************************************************************************/ +/* ESP32-Ethernet-Kit GPIOs *************************************************/ + +/* BOOT Button */ + +#define BUTTON_BOOT 0 + /**************************************************************************** * Public Types ****************************************************************************/ diff --git a/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32_bringup.c b/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32_bringup.c index 12bc61c910..71a488b5fc 100644 --- a/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32_bringup.c +++ b/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32_bringup.c @@ -55,6 +55,10 @@ # include "esp32_board_wdt.h" #endif +#ifdef CONFIG_BUTTONS +# include +#endif + #include "esp32-ethernet-kit.h" /**************************************************************************** @@ -245,6 +249,16 @@ int esp32_bringup(void) } #endif +#ifdef CONFIG_BUTTONS + /* Register the BUTTON driver */ + + ret = btn_lower_initialize("/dev/buttons"); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: btn_lower_initialize() failed: %d\n", ret); + } +#endif + /* If we got here then perhaps not all initialization was successful, but * at least enough succeeded to bring-up NSH with perhaps reduced * capabilities. diff --git a/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32_buttons.c b/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32_buttons.c new file mode 100644 index 0000000000..e1183c8b27 --- /dev/null +++ b/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32_buttons.c @@ -0,0 +1,166 @@ +/**************************************************************************** + * boards/xtensa/esp32/esp32-ethernet-kit/src/esp32_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 + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "esp32_cpuint.h" +#include "esp32_gpio.h" + +#include "esp32-ethernet-kit.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) +{ + esp32_configgpio(BUTTON_BOOT, INPUT_FUNCTION_3 | PULLDOWN); + 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 = esp32_gpioread(BUTTON_BOOT); + + for (i = 0; i < 10; i++) + { + up_mdelay(1); /* TODO */ + + bool b1 = esp32_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 = ESP32_PIN2IRQ(BUTTON_BOOT); + + if (NULL != irqhandler) + { + /* Make sure the interrupt is disabled */ + + esp32_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 */ + + esp32_gpioirqenable(irq, CHANGE); + } + else + { + gpioinfo("Disable the interrupt\n"); + esp32_gpioirqdisable(irq); + } + + return OK; +} +#endif