From 1d2dfa955d679b43e0fd976fb1c621171f609311 Mon Sep 17 00:00:00 2001 From: Daniel Thompson Date: Sat, 18 Jan 2020 20:53:15 +0000 Subject: [PATCH] boards: Add support for active high buttons The button on the PineTime isn't a simple pull low design. Instead the switch is connected across two GPIO pins *and* has external pull downs on both pins. This circuit requires special handling since it must be active high and we need to configure the other pin as an output for the pin to work correctly. --- src/boards.c | 24 +++++++++++++----------- src/boards.h | 4 ++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/boards.c b/src/boards.c index ab83a71..0521b2b 100644 --- a/src/boards.c +++ b/src/boards.c @@ -42,20 +42,22 @@ //------------- IMPLEMENTATION -------------// void button_init(uint32_t pin) { - if ( BUTTON_PULL == NRF_GPIO_PIN_PULLDOWN ) - { - nrf_gpio_cfg_sense_input(pin, BUTTON_PULL, NRF_GPIO_PIN_SENSE_HIGH); - } - else - { - nrf_gpio_cfg_sense_input(pin, BUTTON_PULL, NRF_GPIO_PIN_SENSE_LOW); - } + nrf_gpio_cfg_sense_input(pin, BUTTON_PULL, NRF_GPIO_PIN_SENSE_LOW); +#ifdef BUTTON_ENABLE + /* + * BUTTON_ENABLE is used when a switch is connected to another GPIO pin + * rather than the Vcc or ground. In the bootloader we'll just permanently + * push the enable pin to whatever value the sense pin thinks is the + * active one. + */ + nrf_gpio_cfg_output(BUTTON_ENABLE); + nrf_gpio_pin_write(BUTTON_ENABLE, BUTTON_ACTIVE); +#endif } bool button_pressed(uint32_t pin) { - uint32_t const active_state = (BUTTON_PULL == NRF_GPIO_PIN_PULLDOWN ? 1 : 0); - return nrf_gpio_pin_read(pin) == active_state; + return (nrf_gpio_pin_read(pin) == BUTTON_ACTIVE) ? true : false; } void board_init(void) @@ -68,7 +70,7 @@ void board_init(void) NRF_CLOCK->TASKS_LFCLKSTART = 1UL; button_init(BUTTON_DFU); -#ifndef BUTTON_FRESET +#ifdef BUTTON_FRESET button_init(BUTTON_FRESET); #endif NRFX_DELAY_US(100); // wait for the pin state is stable diff --git a/src/boards.h b/src/boards.h index 1c6d8ed..28cc5c6 100644 --- a/src/boards.h +++ b/src/boards.h @@ -39,6 +39,10 @@ #if !defined(BUTTON_FRESET) && BUTTONS_NUMBER > 1 #define BUTTON_FRESET BUTTON_2 #endif +#ifndef BUTTON_ACTIVE +/* default to active-low signals */ +#define BUTTON_ACTIVE 0 +#endif // The primary LED is usually Red but not in all cases. #define LED_PRIMARY 0