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/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..5dd65cb1001 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,16 +67,16 @@ void platform_init(void) SYSCFG_MEMRM |= 1U; scb_reset_core(); } -#pragma GCC diagnostic pop - rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]); +#endif + rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[PLATFORM_CLOCK_FREQ]); /* Enable peripherals */ 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; @@ -130,19 +131,18 @@ 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) { - uint32_t *magic = (uint32_t *)&_ebss; magic[0] = BOOTMAGIC0; magic[1] = BOOTMAGIC1; 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 24a96fe61c1..a116e41e904 100644 --- a/src/platforms/common/blackpill-f4/usbdfu.c +++ b/src/platforms/common/blackpill-f4/usbdfu.c @@ -22,40 +22,37 @@ #include #include #include +#include #include "usbdfu.h" #include "general.h" #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 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[PLATFORM_CLOCK_FREQ]); /* 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,14 @@ 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(); } -#pragma GCC diagnostic pop - void dfu_event(void) { }