Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Blackpill-F4 and BMP DFU #1508

Merged
merged 4 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 16 additions & 16 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 @@ -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)
{
Expand Down
26 changes: 13 additions & 13 deletions src/platforms/common/blackpill-f4/usbdfu.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,37 @@
#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
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 +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)
{
}