Skip to content
This repository was archived by the owner on Mar 7, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/platforms/blackpill-f401cc/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
3 changes: 2 additions & 1 deletion src/platforms/blackpill-f401ce/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
3 changes: 2 additions & 1 deletion src/platforms/blackpill-f411ce/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions src/platforms/common/blackpill-f4/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions src/platforms/common/blackpill-f4/blackpill-f4.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,21 @@
#include <libopencm3/usb/dwc/otg_fs.h>

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 */
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
29 changes: 15 additions & 14 deletions src/platforms/common/blackpill-f4/usbdfu.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,38 @@
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/cm3/scb.h>
#include <libopencm3/usb/dwc/otg_fs.h>

#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
} else {
Comment thread
ALTracer marked this conversation as resolved.
Outdated
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);

Expand All @@ -68,11 +66,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)
{
}