From 48b07eb9912769e19a79d5df981c320d723fe04e Mon Sep 17 00:00:00 2001 From: Dylan Laduranty Date: Tue, 7 Mar 2023 12:00:37 +0100 Subject: [PATCH] bootloaders: fix bootloader button logic Signed-off-by: Dylan Laduranty --- bootloaders/riotboot_dfu/main.c | 14 ++++++++++++-- bootloaders/riotboot_tinyusb_dfu/main.c | 14 ++++++++++++-- sys/include/riotboot/bootloader_selection.h | 17 +++++------------ sys/riotboot/serial.c | 7 +++++-- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/bootloaders/riotboot_dfu/main.c b/bootloaders/riotboot_dfu/main.c index e8be5935de..275213c4b6 100644 --- a/bootloaders/riotboot_dfu/main.c +++ b/bootloaders/riotboot_dfu/main.c @@ -36,9 +36,19 @@ static bool _bootloader_alternative_mode(void) { -#ifdef BTN_BOOTLOADER_PIN +#if defined (BTN_BOOTLOADER_PIN) && defined (BTN_BOOTLOADER_MODE) + bool state; + gpio_init(BTN_BOOTLOADER_PIN, BTN_BOOTLOADER_MODE); - return (bool)gpio_read(BTN_BOOTLOADER_PIN) != BTN_BOOTLOADER_INVERTED; + state = gpio_read(BTN_BOOTLOADER_PIN); + /* If button configures w/ internal or external pullup, then it is an + active-low, thus reverts the logic */ + if (BTN_BOOTLOADER_EXT_PULLUP || BTN_BOOTLOADER_MODE == GPIO_IN_PU || + BTN_BOOTLOADER_MODE == GPIO_OD_PU ) { + return !state; + } else { + return state; + } #else return false; #endif diff --git a/bootloaders/riotboot_tinyusb_dfu/main.c b/bootloaders/riotboot_tinyusb_dfu/main.c index e8be5935de..275213c4b6 100644 --- a/bootloaders/riotboot_tinyusb_dfu/main.c +++ b/bootloaders/riotboot_tinyusb_dfu/main.c @@ -36,9 +36,19 @@ static bool _bootloader_alternative_mode(void) { -#ifdef BTN_BOOTLOADER_PIN +#if defined (BTN_BOOTLOADER_PIN) && defined (BTN_BOOTLOADER_MODE) + bool state; + gpio_init(BTN_BOOTLOADER_PIN, BTN_BOOTLOADER_MODE); - return (bool)gpio_read(BTN_BOOTLOADER_PIN) != BTN_BOOTLOADER_INVERTED; + state = gpio_read(BTN_BOOTLOADER_PIN); + /* If button configures w/ internal or external pullup, then it is an + active-low, thus reverts the logic */ + if (BTN_BOOTLOADER_EXT_PULLUP || BTN_BOOTLOADER_MODE == GPIO_IN_PU || + BTN_BOOTLOADER_MODE == GPIO_OD_PU ) { + return !state; + } else { + return state; + } #else return false; #endif diff --git a/sys/include/riotboot/bootloader_selection.h b/sys/include/riotboot/bootloader_selection.h index b0fc8ff669..246d695e15 100644 --- a/sys/include/riotboot/bootloader_selection.h +++ b/sys/include/riotboot/bootloader_selection.h @@ -58,19 +58,12 @@ extern "C" { /** @brief Interpretation of @ref BTN_BOOTLOADER_PIN. * - * Set to true for active-low buttons (go to DFU if the pin is low), otherwise - * to false (go to DFU if the pin is high). - * - * The default value for all boards is inverted (active-low), except if - * BTN0_MODE is defined as GPIO_IN_PD. In this case the value is not - * inverted (high-active). + * This value should be set to true if the button has an *external* pull-up and + * thus, works as an active-low button. + * If the button has an internal pull-up, the default value should remains false */ -#ifndef BTN_BOOTLOADER_INVERTED -#if (BTN0_MODE == GPIO_IN_PD) -#define BTN_BOOTLOADER_INVERTED false -#else -#define BTN_BOOTLOADER_INVERTED true -#endif +#ifndef BTN_BOOTLOADER_EXT_PULLUP +#define BTN_BOOTLOADER_EXT_PULLUP false #endif /** @brief LED pin for bootloader indication diff --git a/sys/riotboot/serial.c b/sys/riotboot/serial.c index 502f204e9d..ae6f578088 100644 --- a/sys/riotboot/serial.c +++ b/sys/riotboot/serial.c @@ -57,8 +57,11 @@ static inline void uart_write_byte(uart_t uart, uint8_t data) static inline bool _boot_pin(void) { -#ifdef BTN_BOOTLOADER_PIN - if (BTN_BOOTLOADER_INVERTED) { +#if defined (BTN_BOOTLOADER_PIN) && defined(BTN_BOOTLOADER_MODE) + /* Reverts the logic if the button has an internal or external pullup and + thus, is an active-low button */ + if (BTN_BOOTLOADER_EXT_PULLUP || BTN_BOOTLOADER_MODE == GPIO_IN_PU || + BTN_BOOTLOADER_MODE == GPIO_OD_PU ) { return !gpio_read(BTN_BOOTLOADER_PIN); } else {