diff --git a/src/platforms/f4discovery/Makefile.inc b/src/platforms/f4discovery/Makefile.inc index e1f22e2..8596882 100644 --- a/src/platforms/f4discovery/Makefile.inc +++ b/src/platforms/f4discovery/Makefile.inc @@ -1,4 +1,5 @@ CROSS_COMPILE ?= arm-none-eabi- +BMP_BOOTLOADER ?= CC = $(CROSS_COMPILE)gcc OBJCOPY = $(CROSS_COMPILE)objcopy @@ -7,12 +8,20 @@ CFLAGS += -Istm32/include -mcpu=cortex-m4 -mthumb \ -DSTM32F4 -DF4DISCOVERY -I../libopencm3/include \ -Iplatforms/stm32 -LDFLAGS = -lopencm3_stm32f4 \ +LDFLAGS_BOOT = -lopencm3_stm32f4 \ -Wl,-T,platforms/stm32/f4discovery.ld -nostartfiles -lc -lnosys \ -Wl,-Map=mapfile -mthumb -mcpu=cortex-m4 -Wl,-gc-sections \ -mfloat-abi=hard -mfpu=fpv4-sp-d16 \ -L../libopencm3/lib +ifeq ($(BMP_BOOTLOADER), 1) +$(info Load address 0x08004000 for BMPBootloader) +LDFLAGS = $(LDFLAGS_BOOT) -Wl,-Ttext=0x8004000 +CFLAGS += -DUSE_BMP_SERIAL +else +LDFLAGS = $(LDFLAGS_BOOT) +endif + VPATH += platforms/stm32 SRC += cdcacm.c \ @@ -23,7 +32,18 @@ SRC += cdcacm.c \ timing.c \ timing_stm32.c \ +ifneq ($(BMP_BOOTLOADER), 1) all: blackmagic.bin +else +all: blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex +blackmagic_dfu: usbdfu.o dfucore.o dfu_f4.o + $(CC) $^ -o $@ $(LDFLAGS_BOOT) +blackmagic_dfu.bin: blackmagic_dfu + $(OBJCOPY) -O binary $^ $@ + +blackmagic_dfu.hex: blackmagic_dfu + $(OBJCOPY) -O ihex $^ $@ +endif host_clean: -$(Q)$(RM) blackmagic.bin diff --git a/src/platforms/f4discovery/platform.c b/src/platforms/f4discovery/platform.c index 04ac378..5370513 100644 --- a/src/platforms/f4discovery/platform.c +++ b/src/platforms/f4discovery/platform.c @@ -71,8 +71,8 @@ void platform_init(void) rcc_periph_clock_enable(RCC_CRC); /* Set up USB Pins and alternate function*/ - gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12); - gpio_set_af(GPIOA, GPIO_AF10, GPIO11 | GPIO12); + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9 | GPIO11 | GPIO12); + gpio_set_af(GPIOA, GPIO_AF10, GPIO9 | GPIO10 | GPIO11 | GPIO12); GPIOC_OSPEEDR &=~0xF30; GPIOC_OSPEEDR |= 0xA20; diff --git a/src/platforms/f4discovery/platform.h b/src/platforms/f4discovery/platform.h index dde94ed..29e2b86 100644 --- a/src/platforms/f4discovery/platform.h +++ b/src/platforms/f4discovery/platform.h @@ -33,6 +33,7 @@ #define PLATFORM_HAS_TRACESWO #define BOARD_IDENT "Black Magic Probe (F4Discovery), (Firmware " FIRMWARE_VERSION ")" +#define BOARD_IDENT_DFU "Black Magic Firmware Upgrade (F4Discovery)" #define DFU_IDENT "Black Magic Firmware Upgrade (F4Discovery)" /* Important pin mappings for STM32 implementation: diff --git a/src/platforms/f4discovery/usbdfu.c b/src/platforms/f4discovery/usbdfu.c new file mode 100644 index 0000000..4b493ea --- /dev/null +++ b/src/platforms/f4discovery/usbdfu.c @@ -0,0 +1,73 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2013 Gareth McMullin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "usbdfu.h" +#include "general.h" +#include "platform.h" + +uint32_t app_address = 0x08004000; +extern char _ebss[]; + +void dfu_detach(void) +{ + scb_reset_system(); +} + +int main(void) +{ + volatile uint32_t *magic = (uint32_t *)_ebss; + rcc_periph_clock_enable(RCC_GPIOA); + if (gpio_get(GPIOA, GPIO0) || + ((magic[0] == BOOTMAGIC0) && (magic[1] == BOOTMAGIC1))) { + magic[0] = 0; + magic[1] = 0; + } else { + dfu_jump_app_if_valid(); + } + rcc_clock_setup_pll(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]); + + /* Assert blue LED as indicator we are in the bootloader */ + rcc_periph_clock_enable(RCC_GPIOD); + gpio_mode_setup(LED_PORT, GPIO_MODE_OUTPUT, + GPIO_PUPD_NONE, LED_BOOTLOADER); + gpio_set(LED_PORT, LED_BOOTLOADER); + + /* Enable peripherals */ + rcc_periph_clock_enable(RCC_OTGFS); + + /* Set up USB Pins and alternate function*/ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12); + gpio_set_af(GPIOA, GPIO_AF10, GPIO11 | GPIO12); + + dfu_protect(false); + dfu_init(&USB_DRIVER); + dfu_main(); + +} + +void dfu_event(void) +{ +} + diff --git a/src/platforms/stm32/dfu_f4.c b/src/platforms/stm32/dfu_f4.c index 0e3f935..229f562 100644 --- a/src/platforms/stm32/dfu_f4.c +++ b/src/platforms/stm32/dfu_f4.c @@ -53,15 +53,14 @@ static void get_sector_num(uint32_t addr) void dfu_check_and_do_sector_erase(uint32_t addr) { if(addr == sector_addr[sector_num]) { - flash_erase_sector((sector_num & 0x1f)<<3, FLASH_PROGRAM_X32); + flash_erase_sector((sector_num & 0x1f)<<3, FLASH_CR_PROGRAM_X32); } } void dfu_flash_program_buffer(uint32_t baseaddr, void *buf, int len) { for(int i = 0; i < len; i += 4) - flash_program_word(baseaddr + i, *(uint32_t*)(buf+i), - FLASH_PROGRAM_X32); + flash_program_word(baseaddr + i, *(uint32_t*)(buf+i)); } uint32_t dfu_poll_timeout(uint8_t cmd, uint32_t addr, uint16_t blocknum) @@ -78,14 +77,16 @@ uint32_t dfu_poll_timeout(uint8_t cmd, uint32_t addr, uint16_t blocknum) return 26; } -void dfu_protect_enable(void) +void dfu_protect(bool enable) { + if (enable) { #ifdef DFU_SELF_PROTECT - if ((FLASH_OPTCR & 0x10000) != 0) { - flash_program_option_bytes(FLASH_OPTCR & ~0x10000); - flash_lock_option_bytes(); - } + if ((FLASH_OPTCR & 0x10000) != 0) { + flash_program_option_bytes(FLASH_OPTCR & ~0x10000); + flash_lock_option_bytes(); + } #endif + } } void dfu_jump_app_if_valid(void) @@ -93,14 +94,17 @@ void dfu_jump_app_if_valid(void) /* Boot the application if it's valid */ /* Vector table may be anywhere in 128 kByte RAM CCM not handled*/ - if((*(volatile uint32_t*)APP_ADDRESS & 0x2FFC0000) == 0x20000000) { - /* Set vector table base address */ - SCB_VTOR = APP_ADDRESS & 0x1FFFFF; /* Max 2 MByte Flash*/ + if((*(volatile uint32_t*)app_address & 0x2FFC0000) == 0x20000000) { +#if defined(STM32F7) /* Set vector table base address */ + SCB_VTOR = app_address & 0xFFFFFF00; +#else + SCB_VTOR = app_address & 0x1FFFFF; /* Max 2 MByte Flash*/ +#endif /* Initialise master stack pointer */ asm volatile ("msr msp, %0"::"g" - (*(volatile uint32_t*)APP_ADDRESS)); + (*(volatile uint32_t*)app_address)); /* Jump to application */ - (*(void(**)())(APP_ADDRESS + 4))(); + (*(void(**)())(app_address + 4))(); } } diff --git a/src/platforms/stm32/dfucore.c b/src/platforms/stm32/dfucore.c index d90f6ea..84e588e 100644 --- a/src/platforms/stm32/dfucore.c +++ b/src/platforms/stm32/dfucore.c @@ -29,10 +29,11 @@ # define DFU_IFACE_STRING "@Internal Flash /0x08000000/8*001Ka,000*001Kg" # define DFU_IFACE_STRING_OFFSET 38 # define DFU_IFACE_PAGESIZE 1 -#elif defined(STM32F4) -# define DFU_IFACE_STRING "/0x08000000/1*016Ka,3*016Kg,1*064Kg,7*128Kg" -#elif defined(STM32F7) -# define DFU_IFACE_STRING "/0x08000000/1*016Ka,3*016Kg,1*064Kg,3*128Kg" +#elif defined(STM32F4) || defined(STM32F7) +# define FLASH_BASE 0x08000000U +# define DFU_IFACE_PAGESIZE 128 +# define DFU_IFACE_STRING_OFFSET 54 +# define DFU_IFACE_STRING "@Internal Flash /0x08000000/1*016Ka,3*016Kg,1*064Kg,000*128Kg" #endif #include @@ -329,7 +330,7 @@ static void set_dfu_iface_string(uint32_t size) *p++ = size + '0'; } #else -# define set_dfu_iface_string() +# define set_dfu_iface_string(x) #endif static char *get_dev_unique_id(char *s) diff --git a/src/platforms/stm32/serialno.c b/src/platforms/stm32/serialno.c index 6950380..23cda10 100644 --- a/src/platforms/stm32/serialno.c +++ b/src/platforms/stm32/serialno.c @@ -21,7 +21,7 @@ char *serial_no_read(char *s, int max) { -#if defined(STM32F1) +#if defined(STM32F1) || defined(USE_BMP_SERIAL) /* Only STM32F103 has no DFU Bootloader. Generate a ID comatible * with the BMP Bootloader since ages. */