From 9b16a3631dd976c4e8de103d8c421ccc1d6b5f02 Mon Sep 17 00:00:00 2001 From: Tercio Gaudencio Filho Date: Fri, 9 Aug 2019 09:11:20 -0300 Subject: [PATCH 1/4] Fixes issue #13. Added a delay(Approximately 20ms) before reading the button. --- src/stm32f103/target_stm32f103.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/stm32f103/target_stm32f103.c b/src/stm32f103/target_stm32f103.c index 10b07d9..69c21db 100644 --- a/src/stm32f103/target_stm32f103.c +++ b/src/stm32f103/target_stm32f103.c @@ -161,6 +161,10 @@ bool target_get_force_bootloader(void) { backup_write(BKP0, 0); #if HAVE_BUTTON + /* Wait sometime in case the button has some debounce capacitor */ + for (int i = 0; i < 1440000; i++) { + __asm__("nop"); + } /* Check if the user button is held down */ if (BUTTON_ACTIVE_HIGH) { if (gpio_get(BUTTON_GPIO_PORT, BUTTON_GPIO_PIN)) { From 721650827b90c7d3a7a601a94f31134374b3ff96 Mon Sep 17 00:00:00 2001 From: Tercio Gaudencio Filho Date: Fri, 9 Aug 2019 13:22:38 -0300 Subject: [PATCH 2/4] Added a variable to control the button sample delay. --- src/stm32f103/generic/config.h | 4 ++++ src/stm32f103/target_stm32f103.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/stm32f103/generic/config.h b/src/stm32f103/generic/config.h index 9588a6f..960d852 100644 --- a/src/stm32f103/generic/config.h +++ b/src/stm32f103/generic/config.h @@ -33,6 +33,10 @@ #define HAVE_BUTTON 0 #endif +#ifndef BUTTON_SAMPLE_DELAY_CYCLES +#define BUTTON_SAMPLE_DELAY_CYCLES 1440000 +#endif + #ifndef HAVE_USB_PULLUP_CONTROL #define HAVE_USB_PULLUP_CONTROL 0 #endif diff --git a/src/stm32f103/target_stm32f103.c b/src/stm32f103/target_stm32f103.c index 69c21db..41f2382 100644 --- a/src/stm32f103/target_stm32f103.c +++ b/src/stm32f103/target_stm32f103.c @@ -162,7 +162,7 @@ bool target_get_force_bootloader(void) { #if HAVE_BUTTON /* Wait sometime in case the button has some debounce capacitor */ - for (int i = 0; i < 1440000; i++) { + for (int i = 0; i < BUTTON_SAMPLE_DELAY_CYCLES; i++) { __asm__("nop"); } /* Check if the user button is held down */ From b1f4a08edb959b0eef6c8084ce76971a90e33634 Mon Sep 17 00:00:00 2001 From: Tercio Gaudencio Filho Date: Fri, 9 Aug 2019 13:49:49 -0300 Subject: [PATCH 3/4] Added a README entry explaining the button usage. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f860799..b0b61c6 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ The bootloader can be built to look for arbitrary patterns, but the default for The bootloader currently looks for `0x544F` in RTC backup register 1 and `0x4F42` in RTC backup register 0 (together they spell "BOOT" in ASCII). +You can also use a button to stay in bootloader while booting. It's configured using `HAVE_BUTTON` define. If your button has a debounce capacitor, you can use `BUTTON_SAMPLE_DELAY_CYCLES` define to specify how many cycles to wait before sampling the I/O pin, by default it is approximately 20ms in a 72Mhz MCU. + ### WebUSB This bootloader implements the draft [WebUSB](https://wicg.github.io/webusb/) specification, which allows web pages to access the bootloader (after presenting the user with a device picker dialog). From e3a77a80f1b63f6e6faef53f6f591834396bad4b Mon Sep 17 00:00:00 2001 From: Tercio Gaudencio Filho Date: Fri, 9 Aug 2019 14:09:58 -0300 Subject: [PATCH 4/4] Fix C89 compatibility. --- src/stm32f103/target_stm32f103.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/stm32f103/target_stm32f103.c b/src/stm32f103/target_stm32f103.c index 41f2382..281f1cb 100644 --- a/src/stm32f103/target_stm32f103.c +++ b/src/stm32f103/target_stm32f103.c @@ -162,7 +162,8 @@ bool target_get_force_bootloader(void) { #if HAVE_BUTTON /* Wait sometime in case the button has some debounce capacitor */ - for (int i = 0; i < BUTTON_SAMPLE_DELAY_CYCLES; i++) { + int i; + for (i = 0; i < BUTTON_SAMPLE_DELAY_CYCLES; i++) { __asm__("nop"); } /* Check if the user button is held down */