From 384cbc201335f109adba4d7560c82e21f73a6aa4 Mon Sep 17 00:00:00 2001 From: raiden00pl Date: Tue, 14 Mar 2023 10:38:08 +0100 Subject: [PATCH] boards/nrf52840-dk: add buttons example --- boards/Kconfig | 1 + .../arm/nrf52/nrf52832-dk/src/nrf52_bringup.c | 14 +++++ .../nrf52840-dk/configs/buttons/defconfig | 51 +++++++++++++++++++ .../arm/nrf52/nrf52840-dk/src/nrf52_bringup.c | 14 +++++ .../arm/nrf52/nrf52840-dk/src/nrf52_buttons.c | 38 +++++++------- .../arm/nrf52/nrf52840-dk/src/nrf52_sx127x.c | 3 +- 6 files changed, 101 insertions(+), 20 deletions(-) create mode 100644 boards/arm/nrf52/nrf52840-dk/configs/buttons/defconfig diff --git a/boards/Kconfig b/boards/Kconfig index 7654b8f6f1..bad6f227be 100644 --- a/boards/Kconfig +++ b/boards/Kconfig @@ -1013,6 +1013,7 @@ config ARCH_BOARD_NRF52840_DK depends on ARCH_CHIP_NRF52 select ARCH_HAVE_LEDS select ARCH_HAVE_BUTTONS + select ARCH_HAVE_IRQBUTTONS ---help--- This option selects the Nordic nRF52840 Development Kit (PCA10056) diff --git a/boards/arm/nrf52/nrf52832-dk/src/nrf52_bringup.c b/boards/arm/nrf52/nrf52832-dk/src/nrf52_bringup.c index d79ccff4d3..a4327b7035 100644 --- a/boards/arm/nrf52/nrf52832-dk/src/nrf52_bringup.c +++ b/boards/arm/nrf52/nrf52832-dk/src/nrf52_bringup.c @@ -37,6 +37,10 @@ # include #endif +#ifdef CONFIG_INPUT_BUTTONS +# include +#endif + #ifdef CONFIG_NRF52_SOFTDEVICE_CONTROLLER # include "nrf52_sdc.h" #endif @@ -96,6 +100,16 @@ int nrf52_bringup(void) } #endif +#ifdef CONFIG_INPUT_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 + #ifdef CONFIG_NRF52_SOFTDEVICE_CONTROLLER ret = nrf52_sdc_initialize(); diff --git a/boards/arm/nrf52/nrf52840-dk/configs/buttons/defconfig b/boards/arm/nrf52/nrf52840-dk/configs/buttons/defconfig new file mode 100644 index 0000000000..8332f5ce4d --- /dev/null +++ b/boards/arm/nrf52/nrf52840-dk/configs/buttons/defconfig @@ -0,0 +1,51 @@ +# +# 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_FPU is not set +# CONFIG_NSH_DISABLE_IFCONFIG is not set +# CONFIG_NSH_DISABLE_PS is not set +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="nrf52840-dk" +CONFIG_ARCH_BOARD_NRF52840_DK=y +CONFIG_ARCH_BUTTONS=y +CONFIG_ARCH_CHIP="nrf52" +CONFIG_ARCH_CHIP_NRF52840=y +CONFIG_ARCH_CHIP_NRF52=y +CONFIG_ARCH_IRQBUTTONS=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_STDARG_H=y +CONFIG_BOARD_LOOPSPERMSEC=5500 +CONFIG_BUILTIN=y +CONFIG_EXAMPLES_BUTTONS=y +CONFIG_FAT_LCNAMES=y +CONFIG_FAT_LFN=y +CONFIG_FS_FAT=y +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INPUT=y +CONFIG_INPUT_BUTTONS=y +CONFIG_INPUT_BUTTONS_LOWER=y +CONFIG_INTELHEX_BINARY=y +CONFIG_MM_REGIONS=2 +CONFIG_NRF52_GPIOTE=y +CONFIG_NRF52_UART0=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=65535 +CONFIG_RAM_START=0x20000000 +CONFIG_RAW_BINARY=y +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_WAITPID=y +CONFIG_START_DAY=26 +CONFIG_START_MONTH=3 +CONFIG_SYMTAB_ORDEREDBYNAME=y +CONFIG_SYSTEM_NSH=y +CONFIG_TASK_NAME_SIZE=0 +CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/arm/nrf52/nrf52840-dk/src/nrf52_bringup.c b/boards/arm/nrf52/nrf52840-dk/src/nrf52_bringup.c index b055243e1d..807fbfb724 100644 --- a/boards/arm/nrf52/nrf52840-dk/src/nrf52_bringup.c +++ b/boards/arm/nrf52/nrf52840-dk/src/nrf52_bringup.c @@ -33,6 +33,10 @@ # include #endif +#ifdef CONFIG_INPUT_BUTTONS +# include +#endif + #ifdef CONFIG_NRF52_SOFTDEVICE_CONTROLLER # include "nrf52_sdc.h" #endif @@ -142,6 +146,16 @@ int nrf52_bringup(void) } #endif +#ifdef CONFIG_INPUT_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 + #ifdef CONFIG_SENSORS_LSM6DSL ret = nrf52_lsm6dsl_initialize("/dev/lsm6dsl0"); if (ret < 0) diff --git a/boards/arm/nrf52/nrf52840-dk/src/nrf52_buttons.c b/boards/arm/nrf52/nrf52840-dk/src/nrf52_buttons.c index f57bffe1e5..9b52fac6d1 100644 --- a/boards/arm/nrf52/nrf52840-dk/src/nrf52_buttons.c +++ b/boards/arm/nrf52/nrf52840-dk/src/nrf52_buttons.c @@ -24,14 +24,16 @@ #include -#include +#include #include +#include #include #include #include #include "nrf52_gpio.h" +#include "nrf52_gpiote.h" #include "nrf52840-dk.h" @@ -89,27 +91,22 @@ uint32_t board_button_initialize(void) uint32_t board_buttons(void) { uint32_t ret = 0; + int i; /* Check that state of each key */ - if (!nrf52_gpio_read(g_buttons[BUTTON_BTN1])) + for (i = 0; i < NUM_BUTTONS; i++) { - ret |= BUTTON_BTN1_BIT; - } + /* A LOW value means that the key is pressed. */ - if (!nrf52_gpio_read(g_buttons[BUTTON_BTN2])) - { - ret |= BUTTON_BTN2_BIT; - } + bool released = nrf52_gpio_read(g_buttons[i]); - if (!nrf52_gpio_read(g_buttons[BUTTON_BTN3])) - { - ret |= BUTTON_BTN3_BIT; - } + /* Accumulate the set of depressed (not released) keys */ - if (!nrf52_gpio_read(g_buttons[BUTTON_BTN4])) - { - ret |= BUTTON_BTN4_BIT; + if (!released) + { + ret |= (1 << i); + } } return ret; @@ -140,11 +137,16 @@ uint32_t board_buttons(void) #ifdef CONFIG_ARCH_IRQBUTTONS int board_button_irq(int id, xcpt_t irqhandler, void *arg) { - int ret = -ENOSYS; + int ret = OK; -#warning Missing Implementation! + ret = nrf52_gpiote_set_event(g_buttons[id], true, true, irqhandler, arg); + if (ret < 0) + { + ierr("ERROR: nrf52_gpiote_set_event failed %d\n", ret); + return ret; + } - return ret; + return OK; } #endif diff --git a/boards/arm/nrf52/nrf52840-dk/src/nrf52_sx127x.c b/boards/arm/nrf52/nrf52840-dk/src/nrf52_sx127x.c index 062d12430b..e65a24bc27 100644 --- a/boards/arm/nrf52/nrf52840-dk/src/nrf52_sx127x.c +++ b/boards/arm/nrf52/nrf52840-dk/src/nrf52_sx127x.c @@ -88,8 +88,7 @@ static int sx127x_irq0_attach(xcpt_t isr, void *arg) /* IRQ on rising edge */ - nrf52_gpiote_set_ch_event(GPIO_SX127X_DIO0, 0, true, false, isr, arg); - return OK; + return nrf52_gpiote_set_event(GPIO_SX127X_DIO0, true, false, isr, arg); } /****************************************************************************