diff --git a/boards/calliope-mini/board.c b/boards/calliope-mini/board.c deleted file mode 100644 index 9c7e7a8d5f..0000000000 --- a/boards/calliope-mini/board.c +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2016 Freie Universität Berlin - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @ingroup boards_calliope-mini - * @{ - * - * @file - * @brief Board initialization code for the Calliope mini - * - * @author Hauke Petersen - * - * @} - */ - -#include "board.h" -#include "periph/gpio.h" - -void board_init(void) -{ - /* initialize the mini's buttons */ - gpio_init(BTN0_PIN, BTN0_MODE); - gpio_init(BTN1_PIN, BTN1_MODE); -} diff --git a/boards/microbit-v2/board.c b/boards/microbit-v2/board.c deleted file mode 100644 index 7a84fb4a99..0000000000 --- a/boards/microbit-v2/board.c +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2021 Inria - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @ingroup boards_microbit_v2 - * @{ - * - * @file - * @brief Board initialization code for the BBC micro:bit v2 - * - * @author Alexandre Abadie - * - * @} - */ - -#include "board.h" -#include "periph/gpio.h" - -void board_init(void) -{ - /* initialize the on board buttons */ - gpio_init(BTN0_PIN, BTN0_MODE); - gpio_init(BTN1_PIN, BTN1_MODE); -} diff --git a/boards/microbit/board.c b/boards/microbit/board.c deleted file mode 100644 index 3a0e2ea411..0000000000 --- a/boards/microbit/board.c +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2016 Freie Universität Berlin - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @ingroup boards_microbit - * @{ - * - * @file - * @brief Board initialization code for the BBC micro:bit - * - * @author Hauke Petersen - * - * @} - */ - -#include "board.h" -#include "periph/gpio.h" - -void board_init(void) -{ - /* initialize the micro:bit's buttons */ - gpio_init(BTN0_PIN, BTN0_MODE); - gpio_init(BTN1_PIN, BTN1_MODE); -} diff --git a/boards/nrf51dk/board.c b/boards/nrf51dk/board.c deleted file mode 100644 index f39d6d3ee9..0000000000 --- a/boards/nrf51dk/board.c +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 2018 Inria - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @ingroup boards_nrf51dk - * @{ - * - * @file - * @brief Board initialization code for the nRF51DK - * - * @author Alexandre Abadie - * - * @} - */ - -#include "board.h" -#include "periph/gpio.h" - -void board_init(void) -{ - /* initialize the board buttons */ - gpio_init(BTN0_PIN, BTN0_MODE); - gpio_init(BTN1_PIN, BTN1_MODE); - gpio_init(BTN2_PIN, BTN2_MODE); - gpio_init(BTN3_PIN, BTN3_MODE); -} diff --git a/boards/nrf9160dk/board.c b/boards/nrf9160dk/board.c deleted file mode 100644 index 3da2963715..0000000000 --- a/boards/nrf9160dk/board.c +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (C) 2021 Mesotic SAS - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @ingroup boards_nrf9160dk - * @{ - * - * @file - * @brief Board initialization for the nRF9160DK - * - * @author Dylan Laduranty - * - * @} - */ - -#include "cpu.h" -#include "board.h" -#include "periph/gpio.h" - -void board_init(void) -{ - - gpio_init(BTN0_PIN, BTN0_MODE); - gpio_init(BTN1_PIN, BTN1_MODE); - gpio_init(BTN2_PIN, BTN2_MODE); - gpio_init(BTN3_PIN, BTN3_MODE); -} diff --git a/boards/olimexino-stm32/board.c b/boards/olimexino-stm32/board.c deleted file mode 100644 index 57b9eb4e45..0000000000 --- a/boards/olimexino-stm32/board.c +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2020 Scallog - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @ingroup boards_olimexino-stm32 - * @{ - * - * @file - * @brief Board specific implementations for the OLIMEXINO STM32 board - * - * @author Corentin Vigourt - */ - -#include "board.h" -#include "periph/gpio.h" - -void board_init(void) -{ - - /* initialize the button */ - gpio_init(BTN0_PIN, BTN0_MODE); -} -/** @} */ diff --git a/boards/omote/board.c b/boards/omote/board.c index 55e6e5fb9b..3c6f7f2863 100644 --- a/boards/omote/board.c +++ b/boards/omote/board.c @@ -22,6 +22,4 @@ void board_init(void) { /* The boot pin must be set to input otherwise it may lock the bootloader */ gpio_init(BOOT_PIN, GPIO_IN); - - gpio_init(BTN0_PIN, BTN0_MODE); } diff --git a/boards/openmote-b/board.c b/boards/openmote-b/board.c index 9550cebef8..31e358086b 100644 --- a/boards/openmote-b/board.c +++ b/boards/openmote-b/board.c @@ -27,7 +27,6 @@ void board_init(void) /* The boot pin must be set to input otherwise it may lock the bootloader */ gpio_init(BOOT_PIN, GPIO_IN); - gpio_init(BTN0_PIN, BTN0_MODE); gpio_init(RF24_SWITCH_CC2538_PIN, GPIO_OUT); gpio_init(RF24_SWITCH_AT86RF215_PIN, GPIO_OUT); diff --git a/boards/qn9080dk/board.c b/boards/qn9080dk/board.c index 6af89542d4..7c53642757 100644 --- a/boards/qn9080dk/board.c +++ b/boards/qn9080dk/board.c @@ -57,11 +57,3 @@ static mtd_spi_nor_t mtd_nor_dev = { mtd_dev_t *mtd0 = (mtd_dev_t *)&mtd_nor_dev; #endif /* MODULE_MTD */ - -void board_init(void) -{ - /* Initialize LEDs and Buttons. */ - gpio_init(BTN1_PIN, BTN1_MODE); - gpio_init(BTN2_PIN, BTN2_MODE); - -} diff --git a/boards/samd10-xmini/board.c b/boards/samd10-xmini/board.c index 8cccd775f4..da221650e8 100644 --- a/boards/samd10-xmini/board.c +++ b/boards/samd10-xmini/board.c @@ -23,15 +23,10 @@ void board_init(void) { - - /* initialize the on-board button */ - gpio_init(BTN0_PIN, BTN0_MODE); - /* If the PA24 and PA25 pins are not connected, it is recommended * to enable a pull-up on PA24 and PA25 through input GPIO mode. * (those have external pull-ups on the board that would leak current) */ gpio_init(GPIO_PIN(PA, 24), GPIO_IN_PU); gpio_init(GPIO_PIN(PA, 25), GPIO_IN_PU); - } diff --git a/boards/samd20-xpro/board.c b/boards/samd20-xpro/board.c deleted file mode 100644 index 9c5ee9c8b5..0000000000 --- a/boards/samd20-xpro/board.c +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (C) 2020 ML!PA Consulting GmbH - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @ingroup boards_samd20-xpro - * @{ - * - * @file - * @brief Board specific implementations for the Atmel SAM D20 Xplained - * Pro board - * - * @author Benjamin Valentin - * @} - */ - -#include "board.h" -#include "periph/gpio.h" - -void board_init(void) -{ - - /* initialize the on-board button */ - gpio_init(BTN0_PIN, BTN0_MODE); - -} diff --git a/boards/samd21-xpro/board.c b/boards/samd21-xpro/board.c deleted file mode 100644 index c0d9e9693b..0000000000 --- a/boards/samd21-xpro/board.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2017 Travis Griggs - * Copyright (C) 2017 Dan Evans - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @ingroup boards_samd21-xpro - * @{ - * - * @file - * @brief Board specific implementations for the Atmel SAM D21 Xplained - * Pro board - * - * @author Travis Griggs - * @author Dan Evans - * @author Sebastian Meiling - * @} - */ - -#include "board.h" -#include "periph/gpio.h" - -void board_init(void) -{ - - /* initialize the on-board button */ - gpio_init(BTN0_PIN, BTN0_MODE); - -} diff --git a/boards/same54-xpro/board.c b/boards/same54-xpro/board.c index 0b8597d004..4a0ff7a140 100644 --- a/boards/same54-xpro/board.c +++ b/boards/same54-xpro/board.c @@ -67,9 +67,3 @@ mtd_dev_t *mtd1 = (mtd_dev_t *)&at24mac_dev; VFS_AUTO_MOUNT(littlefs2, VFS_MTD(same54_nor_dev), "/nvm", 0); #endif #endif /* MODULE_MTD */ - -void board_init(void) -{ - /* initialize the on-board button */ - gpio_init(BTN0_PIN, BTN0_MODE); -} diff --git a/boards/sensebox_samd21/board.c b/boards/sensebox_samd21/board.c index 6fcc85c19a..918bd4b14a 100644 --- a/boards/sensebox_samd21/board.c +++ b/boards/sensebox_samd21/board.c @@ -44,11 +44,6 @@ mtd_dev_t *mtd0 = (mtd_dev_t *)&sensebox_sd_dev; void board_init(void) { - - - /* initialize the on-board button on pin PA20 */ - gpio_init(BTN0_PIN, BTN0_MODE); - /* initialize XBEE1 bus enable pin */ gpio_init(XBEE1_EN_PIN, XBEE1_EN_MODE); diff --git a/boards/stm32g0316-disco/board.c b/boards/stm32g0316-disco/board.c deleted file mode 100644 index b546982f30..0000000000 --- a/boards/stm32g0316-disco/board.c +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2021 BISSELL Homecare, Inc. - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @ingroup boards_stm32g0316-disco - * @{ - * - * @file - * @brief Board specific implementations for the STM32G0316-DISCO board - * - * @author Dave VanKampen - * - * @} - */ - -#include "cpu.h" -#include "board.h" -#include "periph/gpio.h" - -void board_init(void) -{ - - gpio_init(BTN0_PIN, BTN0_MODE); -} diff --git a/drivers/periph_common/Kconfig b/drivers/periph_common/Kconfig index 14219ec373..4add675808 100644 --- a/drivers/periph_common/Kconfig +++ b/drivers/periph_common/Kconfig @@ -28,6 +28,12 @@ config MODULE_PERIPH_INIT_ADC default y if MODULE_PERIPH_INIT depends on MODULE_PERIPH_ADC +config MODULE_PERIPH_INIT_BUTTONS + bool + depends on TEST_KCONFIG + help + Enable auto initialization of on-board buttons + config MODULE_PERIPH_CPUID bool "CPU unique ID" depends on HAS_PERIPH_CPUID diff --git a/drivers/periph_common/init.c b/drivers/periph_common/init.c index 9d88170b72..f547852cf5 100644 --- a/drivers/periph_common/init.c +++ b/drivers/periph_common/init.c @@ -63,6 +63,11 @@ void periph_init(void) extern void led_init(void); led_init(); } + /* initialize buttonss */ + if (IS_USED(MODULE_PERIPH_INIT_BUTTONS)) { + extern void button_init(void); + button_init(); + } /* initialize configured I2C devices */ #ifdef MODULE_PERIPH_INIT_I2C for (unsigned i = 0; i < I2C_NUMOF; i++) { diff --git a/drivers/periph_common/init_buttons.c b/drivers/periph_common/init_buttons.c new file mode 100644 index 0000000000..1a2b85cfe5 --- /dev/null +++ b/drivers/periph_common/init_buttons.c @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2022 ML!PA Consulting GmbH + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup drivers_periph_init + * @{ + * + * @file + * @brief Init on board Buttons as input + * + * @author Benjamin Valentin + * @} + */ + +#include "board.h" +#include "periph/gpio.h" +#include "kernel_defines.h" + +__attribute__ ((weak)) +void button_init(void) +{ + if (!IS_USED(MODULE_PERIPH_GPIO)) { + return; + } + +#ifdef BTN0_PIN + gpio_init(BTN0_PIN, BTN0_MODE); +#endif +#ifdef BTN1_PIN + gpio_init(BTN1_PIN, BTN1_MODE); +#endif +#ifdef BTN2_PIN + gpio_init(BTN2_PIN, BTN2_MODE); +#endif +#ifdef BTN3_PIN + gpio_init(BTN3_PIN, BTN3_MODE); +#endif +#ifdef BTN4_PIN + gpio_init(BTN4_PIN, BTN4_MODE); +#endif +#ifdef BTN5_PIN + gpio_init(BTN5_PIN, BTN5_MODE); +#endif +#ifdef BTN6_PIN + gpio_init(BTN6_PIN, BTN6_MODE); +#endif +#ifdef BTN7_PIN + gpio_init(BTN7_PIN, BTN7_MODE); +#endif +} diff --git a/tests/leds/Makefile b/tests/leds/Makefile index 76108073c0..ef98b85d96 100644 --- a/tests/leds/Makefile +++ b/tests/leds/Makefile @@ -3,4 +3,7 @@ include ../Makefile.tests_common # Some boards do not initialize LED0 by default CFLAGS=-DPERIPH_INIT_LED0 +# auto-init on board buttons as well +USEMODULE += periph_init_buttons + include $(RIOTBASE)/Makefile.include diff --git a/tests/leds/README.md b/tests/leds/README.md index ce2423cd7a..447eba4f2d 100644 --- a/tests/leds/README.md +++ b/tests/leds/README.md @@ -5,6 +5,9 @@ endless loop. Each LED will light up once long, and twice short, where the long interval is roughly 4 times as long as the short interval. The length of the interval is not specified and differs for each platform. +Afterwards the test will connect each on-board button to an LED (if available), +so that that LED state mirrors the button state. + Background ========== Running this test shows if all the direct access macros for all on-board LEDs diff --git a/tests/leds/main.c b/tests/leds/main.c index 417637e1b8..143983495b 100644 --- a/tests/leds/main.c +++ b/tests/leds/main.c @@ -24,6 +24,7 @@ #include "clk.h" #include "board.h" #include "periph_conf.h" +#include "periph/gpio.h" #define DELAY_SHORT (coreclk() / 50) #define DELAY_LONG (DELAY_SHORT * 4) @@ -81,10 +82,10 @@ int main(void) } else { printf("Available LEDs: %i\n\n", numof); - puts("Will now light up each LED once short and twice long in a loop"); + puts("Will now light up each LED once short and twice long"); } - while (1) { + for (unsigned i = 0; i < 4; ++i) { #ifdef LED0_ON LED0_ON; dumb_delay(DELAY_LONG); @@ -199,5 +200,34 @@ int main(void) #endif } + puts("Mapping each LED to a button (if available)"); + + while (1) { +#if defined(LED0_PIN) && defined(BTN0_PIN) + gpio_write(LED0_PIN, gpio_read(BTN0_PIN)); +#endif +#if defined(LED1_PIN) && defined(BTN1_PIN) + gpio_write(LED1_PIN, gpio_read(BTN1_PIN)); +#endif +#if defined(LED2_PIN) && defined(BTN2_PIN) + gpio_write(LED2_PIN, gpio_read(BTN2_PIN)); +#endif +#if defined(LED3_PIN) && defined(BTN3_PIN) + gpio_write(LED3_PIN, gpio_read(BTN3_PIN)); +#endif +#if defined(LED4_PIN) && defined(BTN4_PIN) + gpio_write(LED4_PIN, gpio_read(BTN4_PIN)); +#endif +#if defined(LED5_PIN) && defined(BTN5_PIN) + gpio_write(LED5_PIN, gpio_read(BTN5_PIN)); +#endif +#if defined(LED6_PIN) && defined(BTN6_PIN) + gpio_write(LED6_PIN, gpio_read(BTN6_PIN)); +#endif +#if defined(LED7_PIN) && defined(BTN7_PIN) + gpio_write(LED7_PIN, gpio_read(BTN7_PIN)); +#endif + } + return 0; }