From 7942301c6be56bfe2619a15aeffbacc35238eaa5 Mon Sep 17 00:00:00 2001 From: ALTracer Date: Tue, 6 Jun 2023 20:11:35 +0300 Subject: [PATCH 1/4] platforms/blackpill-f4: Enable BMP DFU build variant * BMP DFU uploads are ~2.4x faster than STM32F4x1 BootROM DFU * Use 84MHz clocks in DFU (and in APP), revisit for F411 & 96MHz * Use the PA0 pushbutton active-low and pulled-up to control DFU entry platforms/blackpill-f4: Makefile-switchable BMP DFU * Use makeflags to adjust CFLAGS to pass a macro definition platforms/blackpill-f4: Move bootloader flags to .noinit section * blackpill-f4 inherited _ebss and related hacks from f4discovery * This will not cooperate with BMP DFU, but libopencm3 has a .noinit * Keep #ifdef guards for pure ST DFU by default --- .../common/blackpill-f4/Makefile.inc | 1 + .../common/blackpill-f4/blackpill-f4.c | 14 +++++------ src/platforms/common/blackpill-f4/usbdfu.c | 24 ++++++++----------- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/platforms/common/blackpill-f4/Makefile.inc b/src/platforms/common/blackpill-f4/Makefile.inc index 62a6cf65700..5ddae9aa620 100644 --- a/src/platforms/common/blackpill-f4/Makefile.inc +++ b/src/platforms/common/blackpill-f4/Makefile.inc @@ -31,6 +31,7 @@ LDFLAGS_BOOT = \ ifeq ($(BMP_BOOTLOADER), 1) $(info Load address 0x08004000 for BMPBootloader) LDFLAGS = $(LDFLAGS_BOOT) -Wl,-Ttext=0x8004000 +CFLAGS += -DAPP_START=0x08004000 -DBMP_BOOTLOADER CFLAGS += -DDFU_SERIAL_LENGTH=9 else LDFLAGS += $(LDFLAGS_BOOT) diff --git a/src/platforms/common/blackpill-f4/blackpill-f4.c b/src/platforms/common/blackpill-f4/blackpill-f4.c index 0824dd93966..c1996331a63 100644 --- a/src/platforms/common/blackpill-f4/blackpill-f4.c +++ b/src/platforms/common/blackpill-f4/blackpill-f4.c @@ -38,20 +38,21 @@ #include jmp_buf fatal_error_jmpbuf; -extern uint32_t _ebss; // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) +volatile uint32_t magic[2] __attribute__((section(".noinit"))); void platform_init(void) { - volatile uint32_t *magic = (uint32_t *)&_ebss; /* Enable GPIO peripherals */ rcc_periph_clock_enable(RCC_GPIOA); rcc_periph_clock_enable(RCC_GPIOC); rcc_periph_clock_enable(RCC_GPIOB); -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Warray-bounds" +#ifndef BMP_BOOTLOADER + /* Blackpill board has a floating button on PA0. Pull it up and use as active-low. */ + gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO0); + /* Check the USER button */ - if (gpio_get(GPIOA, GPIO0) || (magic[0] == BOOTMAGIC0 && magic[1] == BOOTMAGIC1)) { + if (!gpio_get(GPIOA, GPIO0) || (magic[0] == BOOTMAGIC0 && magic[1] == BOOTMAGIC1)) { magic[0] = 0; magic[1] = 0; /* Assert blue LED as indicator we are in the bootloader */ @@ -66,7 +67,7 @@ void platform_init(void) SYSCFG_MEMRM |= 1U; scb_reset_core(); } -#pragma GCC diagnostic pop +#endif rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]); /* Enable peripherals */ @@ -135,7 +136,6 @@ const char *platform_target_voltage(void) void platform_request_boot(void) { - uint32_t *magic = (uint32_t *)&_ebss; magic[0] = BOOTMAGIC0; magic[1] = BOOTMAGIC1; scb_reset_system(); diff --git a/src/platforms/common/blackpill-f4/usbdfu.c b/src/platforms/common/blackpill-f4/usbdfu.c index 24a96fe61c1..2943bd86d86 100644 --- a/src/platforms/common/blackpill-f4/usbdfu.c +++ b/src/platforms/common/blackpill-f4/usbdfu.c @@ -28,34 +28,31 @@ #include "platform.h" uintptr_t app_address = 0x08004000U; -extern uint32_t _ebss; // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) +volatile uint32_t magic[2] __attribute__((section(".noinit"))); void dfu_detach(void) { scb_reset_system(); } -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Warray-bounds" - int main(void) { - volatile uint32_t *magic = (uint32_t *)&_ebss; rcc_periph_clock_enable(RCC_GPIOA); -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Warray-bounds" - if (gpio_get(GPIOA, GPIO0) || (magic[0] == BOOTMAGIC0 && magic[1] == BOOTMAGIC1)) { + /* Blackpill board has a floating button on PA0. Pull it up and use as active-low. */ + gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO0); + + if (!gpio_get(GPIOA, GPIO0) || (magic[0] == BOOTMAGIC0 && magic[1] == BOOTMAGIC1)) { magic[0] = 0; magic[1] = 0; - } else + } else { dfu_jump_app_if_valid(); -#pragma GCC diagnostic pop + } - rcc_clock_setup_pll(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]); + rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]); /* Assert blue LED as indicator we are in the bootloader */ - rcc_periph_clock_enable(RCC_GPIOD); + rcc_periph_clock_enable(RCC_GPIOC); gpio_mode_setup(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_BOOTLOADER); gpio_set(LED_PORT, LED_BOOTLOADER); @@ -68,11 +65,10 @@ int main(void) dfu_protect(false); dfu_init(&USB_DRIVER); + dfu_main(); } -#pragma GCC diagnostic pop - void dfu_event(void) { } From 5554b6e8f3c25369a47d8cf2832a7d7e853b4333 Mon Sep 17 00:00:00 2001 From: ALTracer Date: Tue, 6 Jun 2023 20:18:16 +0300 Subject: [PATCH 2/4] platforms/blackpill-f4: Fix DWC USB init V-Bus Sense Disable is required both in DFU and in FW. --- src/platforms/common/blackpill-f4/blackpill-f4.c | 6 +++--- src/platforms/common/blackpill-f4/usbdfu.c | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/platforms/common/blackpill-f4/blackpill-f4.c b/src/platforms/common/blackpill-f4/blackpill-f4.c index c1996331a63..9080ad1a183 100644 --- a/src/platforms/common/blackpill-f4/blackpill-f4.c +++ b/src/platforms/common/blackpill-f4/blackpill-f4.c @@ -74,9 +74,9 @@ void platform_init(void) rcc_periph_clock_enable(RCC_OTGFS); rcc_periph_clock_enable(RCC_CRC); - /* Set up USB Pins and alternate function */ - gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9 | GPIO11 | GPIO12); - gpio_set_af(GPIOA, GPIO_AF10, GPIO9 | GPIO10 | GPIO11 | GPIO12); + /* Set up DM/DP pins. PA9/PA10 are not routed to USB-C. */ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12); + gpio_set_af(GPIOA, GPIO_AF10, GPIO11 | GPIO12); GPIOA_OSPEEDR &= 0x3c00000cU; GPIOA_OSPEEDR |= 0x28000008U; diff --git a/src/platforms/common/blackpill-f4/usbdfu.c b/src/platforms/common/blackpill-f4/usbdfu.c index 2943bd86d86..312c61b836e 100644 --- a/src/platforms/common/blackpill-f4/usbdfu.c +++ b/src/platforms/common/blackpill-f4/usbdfu.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "usbdfu.h" #include "general.h" @@ -66,6 +67,10 @@ int main(void) dfu_protect(false); dfu_init(&USB_DRIVER); + /* https://github.com/libopencm3/libopencm3/pull/1256#issuecomment-779424001 */ + OTG_FS_GCCFG |= OTG_GCCFG_NOVBUSSENS | OTG_GCCFG_PWRDWN; + OTG_FS_GCCFG &= ~(OTG_GCCFG_VBUSBSEN | OTG_GCCFG_VBUSASEN); + dfu_main(); } From 85e285ea57c94100dbc7f7fda01596c738635c78 Mon Sep 17 00:00:00 2001 From: ALTracer Date: Wed, 7 Jun 2023 04:31:41 +0300 Subject: [PATCH 3/4] platforms/blackpill-f4: Set different core clocks * libopencm3 has presets for 84 & 96mhz, use them for f401 and f411 respectively. Same for DFU. --- src/platforms/blackpill-f401cc/platform.h | 3 ++- src/platforms/blackpill-f401ce/platform.h | 3 ++- src/platforms/blackpill-f411ce/platform.h | 3 ++- src/platforms/common/blackpill-f4/blackpill-f4.c | 2 +- src/platforms/common/blackpill-f4/usbdfu.c | 2 +- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/platforms/blackpill-f401cc/platform.h b/src/platforms/blackpill-f401cc/platform.h index 9e0aebe86f5..c920a930413 100644 --- a/src/platforms/blackpill-f401cc/platform.h +++ b/src/platforms/blackpill-f401cc/platform.h @@ -23,7 +23,8 @@ #ifndef PLATFORMS_BLACKPILL_F401CC_PLATFORM_H #define PLATFORMS_BLACKPILL_F401CC_PLATFORM_H -#define PLATFORM_IDENT "(BlackPill-F401CC) " +#define PLATFORM_IDENT "(BlackPill-F401CC) " +#define PLATFORM_CLOCK_FREQ RCC_CLOCK_3V3_84MHZ #include "blackpill-f4.h" diff --git a/src/platforms/blackpill-f401ce/platform.h b/src/platforms/blackpill-f401ce/platform.h index c7ffce261f6..556fe53a735 100644 --- a/src/platforms/blackpill-f401ce/platform.h +++ b/src/platforms/blackpill-f401ce/platform.h @@ -23,7 +23,8 @@ #ifndef PLATFORMS_BLACKPILL_F401CE_PLATFORM_H #define PLATFORMS_BLACKPILL_F401CE_PLATFORM_H -#define PLATFORM_IDENT "(BlackPill-F401CE) " +#define PLATFORM_IDENT "(BlackPill-F401CE) " +#define PLATFORM_CLOCK_FREQ RCC_CLOCK_3V3_84MHZ #include "blackpill-f4.h" diff --git a/src/platforms/blackpill-f411ce/platform.h b/src/platforms/blackpill-f411ce/platform.h index 7586067707f..db1ff9162b2 100644 --- a/src/platforms/blackpill-f411ce/platform.h +++ b/src/platforms/blackpill-f411ce/platform.h @@ -23,7 +23,8 @@ #ifndef PLATFORMS_BLACKPILL_F411CE_PLATFORM_H #define PLATFORMS_BLACKPILL_F411CE_PLATFORM_H -#define PLATFORM_IDENT "(BlackPill-F411CE) " +#define PLATFORM_IDENT "(BlackPill-F411CE) " +#define PLATFORM_CLOCK_FREQ RCC_CLOCK_3V3_96MHZ #include "blackpill-f4.h" diff --git a/src/platforms/common/blackpill-f4/blackpill-f4.c b/src/platforms/common/blackpill-f4/blackpill-f4.c index 9080ad1a183..ba670968c7f 100644 --- a/src/platforms/common/blackpill-f4/blackpill-f4.c +++ b/src/platforms/common/blackpill-f4/blackpill-f4.c @@ -68,7 +68,7 @@ void platform_init(void) scb_reset_core(); } #endif - rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]); + rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[PLATFORM_CLOCK_FREQ]); /* Enable peripherals */ rcc_periph_clock_enable(RCC_OTGFS); diff --git a/src/platforms/common/blackpill-f4/usbdfu.c b/src/platforms/common/blackpill-f4/usbdfu.c index 312c61b836e..1a36f69aa3b 100644 --- a/src/platforms/common/blackpill-f4/usbdfu.c +++ b/src/platforms/common/blackpill-f4/usbdfu.c @@ -50,7 +50,7 @@ int main(void) dfu_jump_app_if_valid(); } - rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]); + rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[PLATFORM_CLOCK_FREQ]); /* Assert blue LED as indicator we are in the bootloader */ rcc_periph_clock_enable(RCC_GPIOC); From ea55377ca99a4b6a3b307e517fa6085aba4d6de6 Mon Sep 17 00:00:00 2001 From: ALTracer Date: Tue, 13 Jun 2023 20:07:59 +0300 Subject: [PATCH 4/4] platforms/blackpill-f4: DFU cleanup * Reset unnecessary braces change * Drop last unneeded `#pragma`s * Add a clarification on reboot flows --- src/platforms/common/blackpill-f4/blackpill-f4.c | 10 +++++----- src/platforms/common/blackpill-f4/usbdfu.c | 3 +-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/platforms/common/blackpill-f4/blackpill-f4.c b/src/platforms/common/blackpill-f4/blackpill-f4.c index ba670968c7f..5dd65cb1001 100644 --- a/src/platforms/common/blackpill-f4/blackpill-f4.c +++ b/src/platforms/common/blackpill-f4/blackpill-f4.c @@ -131,9 +131,11 @@ const char *platform_target_voltage(void) return NULL; } -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Warray-bounds" - +/* + * Write the bootloader flag and reboot. + * The platform_init() will see this and reboot a second time into ST BootROM. + * If BMPBootloader is enabled, then it will see this and initialize its DFU. + */ void platform_request_boot(void) { magic[0] = BOOTMAGIC0; @@ -141,8 +143,6 @@ void platform_request_boot(void) scb_reset_system(); } -#pragma GCC diagnostic pop - #ifdef PLATFORM_HAS_POWER_SWITCH bool platform_target_get_power(void) { diff --git a/src/platforms/common/blackpill-f4/usbdfu.c b/src/platforms/common/blackpill-f4/usbdfu.c index 1a36f69aa3b..a116e41e904 100644 --- a/src/platforms/common/blackpill-f4/usbdfu.c +++ b/src/platforms/common/blackpill-f4/usbdfu.c @@ -46,9 +46,8 @@ int main(void) if (!gpio_get(GPIOA, GPIO0) || (magic[0] == BOOTMAGIC0 && magic[1] == BOOTMAGIC1)) { magic[0] = 0; magic[1] = 0; - } else { + } else dfu_jump_app_if_valid(); - } rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[PLATFORM_CLOCK_FREQ]);