Skip to content

Commit

Permalink
riotboot_dfu: Stay in DFU mode if button is pressed
Browse files Browse the repository at this point in the history
Boards can override this; by default it's BTN0
  • Loading branch information
chrysn committed Mar 18, 2021
1 parent a68cfac commit d770917
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
78 changes: 78 additions & 0 deletions bootloaders/riotboot_dfu/bootloader_selection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (C) 2020 Christian Amsüss <[email protected]>
*
* 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 bootloaders
*
* @{
*
* @file
* @brief Configuration for the riotboot_dfu bootloader
*
* @author Christian Amsüss <[email protected]>
*/

/* Include guards and cplusplus are more of a formality; this header is local
* to the riotboot_dfu application that isn't written in C++ and not included
* from anywhere else either, but still here for consistency (and because
* otherwise the checks complain) */
#ifndef BOOTLOADER_SELECTION_H
#define BOOTLOADER_SELECTION_H

#ifdef __cplusplus
extern "C" {
#endif

/* Not including GPIO headers: we're not actually *doing* anything on GPIO, and
* if no BTN0_PIN is defined we don't define anything either */
#include <board.h>


/** @brief Button pin for bootloader selection
*
* This pin (typically connected to a button) is checked by the riotboot_dfu
* bootloader to decide whether to enter DFU mode even if a valid image is
* present.
*
* The default value for all boards is BTN0, if one is defined.
*
* Boards that insist on not using any button even though they have some can
* define BTN_BOOTLOADER_NONE in their `board.h`.
*
* */
#if (!defined(BTN_BOOTLOADER_PIN) && defined(BTN0_PIN) && !defined(BTN_BOOTLOADER_NONE)) || DOXYGEN
#define BTN_BOOTLOADER_PIN BTN0_PIN
#endif

/** @brief Pin mode for @ref BTN_BOOTLOADER_PIN
*
* Mode into which the riotboot_dfu bootloader will configure @ref
* BTN_BOOTLOADER_PIN before reading it.
*
* */
#ifndef BTN_BOOTLOADER_MODE
#define BTN_BOOTLOADER_MODE BTN0_MODE
#endif

/** @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).
*
* */
#ifndef BTN_BOOTLOADER_INVERTED
#define BTN_BOOTLOADER_INVERTED true
#endif

#ifdef __cplusplus
}
#endif

#endif /* BOOTLOADER_SELECTION_H */

/** @} */
18 changes: 17 additions & 1 deletion bootloaders/riotboot_dfu/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@
#include "riotboot/slot.h"
#include "riotboot/usb_dfu.h"

#include "bootloader_selection.h"

#ifdef BTN_BOOTLOADER_PIN
#include "periph/gpio.h"
#endif

static bool _bootloader_alternative_mode(void)
{
#ifdef BTN_BOOTLOADER_PIN
gpio_init(BTN_BOOTLOADER_PIN, BTN_BOOTLOADER_MODE);
return (bool)gpio_read(BTN_BOOTLOADER_PIN) != BTN_BOOTLOADER_INVERTED;
#else
return false;
#endif
}

void kernel_init(void)
{
uint32_t version = 0;
Expand All @@ -50,7 +66,7 @@ void kernel_init(void)
/* Flash the unused slot if magic word is set */
riotboot_usb_dfu_init(0);

if (slot != -1) {
if (slot != -1 && !_bootloader_alternative_mode()) {
riotboot_slot_jump(slot);
}

Expand Down

0 comments on commit d770917

Please sign in to comment.