From f5dd3006fba631d9d0ae2bbdb27540cd9e513ab3 Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Sat, 15 Apr 2017 15:24:53 +0200 Subject: [PATCH 1/4] STM32: Generate same serial number as internal DFU bootloader. --- src/platforms/common/cdcacm.c | 4 ++++ src/platforms/stm32/serialno.c | 13 ++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/platforms/common/cdcacm.c b/src/platforms/common/cdcacm.c index a44703f..5974cd4 100644 --- a/src/platforms/common/cdcacm.c +++ b/src/platforms/common/cdcacm.c @@ -390,7 +390,11 @@ static const struct usb_config_descriptor config = { .interface = ifaces, }; +#if defined(STM32L0) || defined(STM32F3) || defined(STM32F4) +char serial_no[13]; +#else char serial_no[9]; +#endif static const char *usb_strings[] = { "Black Sphere Technologies", diff --git a/src/platforms/stm32/serialno.c b/src/platforms/stm32/serialno.c index ef28ed2..ae02f8a 100644 --- a/src/platforms/stm32/serialno.c +++ b/src/platforms/stm32/serialno.c @@ -21,11 +21,17 @@ char *serialno_read(char *s) { -#if defined(STM32F4) - volatile uint32_t *unique_id_p = (volatile uint32_t *)0x1FFF7A10; +#if defined(STM32L0) || defined(STM32F3) || defined(STM32F4) + volatile uint16_t *uid = (volatile uint16_t *)DESIG_UNIQUE_ID_BASE; +# if defined(STM32F4) + int offset = 3; +# elif defined(STM32L0) || defined(STM32F4) + int offset = 5; +#endif + sprintf(s, "%04X%04X%04X", + uid[1] + uid[5], uid[0] + uid[4], uid[offset]); #else volatile uint32_t *unique_id_p = (volatile uint32_t *)0x1FFFF7E8; -#endif uint32_t unique_id = *unique_id_p + *(unique_id_p + 1) + *(unique_id_p + 2); @@ -40,6 +46,7 @@ char *serialno_read(char *s) s[i] += 'A' - '9' - 1; s[8] = 0; +#endif return s; } From 814eb0e3a269b43e08712ee6017a375dcc07ce43 Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Sat, 15 Apr 2017 16:51:58 +0200 Subject: [PATCH 2/4] f4discovery: Make DFU work (again?) --- src/platforms/f4discovery/platform.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/platforms/f4discovery/platform.c b/src/platforms/f4discovery/platform.c index 5d24503..f31cd5b 100644 --- a/src/platforms/f4discovery/platform.c +++ b/src/platforms/f4discovery/platform.c @@ -34,6 +34,8 @@ #include #include #include +#include +#include jmp_buf fatal_error_jmpbuf; @@ -88,16 +90,35 @@ const char *platform_target_voltage(void) void platform_request_boot(void) { /* Disconnect USB cable */ - usbd_disconnect(usbdev, 1); - nvic_disable_irq(USB_IRQ); + if (RCC_AHB2ENR & RCC_AHB2ENR_OTGFSEN) { + usbd_disconnect(usbdev, 1); + nvic_disable_irq(USB_IRQ); + rcc_peripheral_disable_clock(&RCC_AHB2ENR, RCC_AHB2ENR_OTGFSEN); + } /* Assert blue LED as indicator we are in the bootloader */ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); gpio_mode_setup(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_BOOTLOADER); gpio_set(LED_PORT, LED_BOOTLOADER); + /* Disable used ports beside PORTD.*/ + rcc_peripheral_disable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); + rcc_peripheral_disable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPCEN); + rcc_periph_clock_disable(USBUSART_CLK); + /* Reset Systick.*/ + systick_interrupt_disable(); + STK_CSR = 0; + STK_RVR = 0; + STK_CVR = 0; + /*Disable Interrupts.*/ + cm_disable_interrupts(); + /* Switch back to HSI.*/ + while (!(RCC_CR & RCC_CR_HSIRDY)) + RCC_CR |= RCC_CR_HSION; + while (RCC_CFGR & (RCC_CFGR_SWS_HSE | RCC_CFGR_SWS_PLL)) + RCC_CFGR &= ~(RCC_CFGR_SWS_HSE | RCC_CFGR_SWS_PLL); - /* Jump to the built in bootloader by mapping System flash */ + /* Map System flash at 0.*/ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_SYSCFGEN); SYSCFG_MEMRM &= ~3; SYSCFG_MEMRM |= 1; From a0e47c392bf9b65396f3f967ba43cec1443af81a Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Tue, 18 Apr 2017 20:01:28 +0200 Subject: [PATCH 3/4] f4_discovery: Clean up platform.h and Readme. --- src/platforms/f4discovery/Readme | 4 +- src/platforms/f4discovery/platform.c | 19 ++++--- src/platforms/f4discovery/platform.h | 10 ++-- src/platforms/f4discovery/usbdfu.c | 75 ---------------------------- 4 files changed, 14 insertions(+), 94 deletions(-) delete mode 100644 src/platforms/f4discovery/usbdfu.c diff --git a/src/platforms/f4discovery/Readme b/src/platforms/f4discovery/Readme index 9bc1557..bbcfc4f 100644 --- a/src/platforms/f4discovery/Readme +++ b/src/platforms/f4discovery/Readme @@ -1,6 +1,6 @@ System vs BMP Bootloader ======================== -For the BMP bootloader, flashing qas not reliable. So we use the system +For the BMP bootloader, flashing was not reliable. So we use the system bootloder unconditional. Connections: @@ -17,7 +17,7 @@ PC6: TDO/TRACESWO PC1: TRST PC8: SRST -Blue Led: Indicator that system bootloader is entered via BMP +PD15/Blue Led: Indicator that system bootloader is entered via BMP Bootstrapping the F4Discovery on-board ST-Link ============================================== diff --git a/src/platforms/f4discovery/platform.c b/src/platforms/f4discovery/platform.c index f31cd5b..01813bf 100644 --- a/src/platforms/f4discovery/platform.c +++ b/src/platforms/f4discovery/platform.c @@ -42,7 +42,7 @@ jmp_buf fatal_error_jmpbuf; void platform_init(void) { /* Check the USER button*/ - rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); + rcc_periph_clock_enable(RCC_GPIOA); if(gpio_get(GPIOA, GPIO0)) { platform_request_boot(); scb_reset_core(); @@ -51,10 +51,10 @@ void platform_init(void) rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_48MHZ]); /* Enable peripherals */ - rcc_peripheral_enable_clock(&RCC_AHB2ENR, RCC_AHB2ENR_OTGFSEN); - rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPCEN); - rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); - rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_CRCEN); + rcc_periph_clock_enable(RCC_OTGFS); + rcc_periph_clock_enable(RCC_GPIOC); + rcc_periph_clock_enable(RCC_GPIOD); + 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); @@ -93,17 +93,16 @@ void platform_request_boot(void) if (RCC_AHB2ENR & RCC_AHB2ENR_OTGFSEN) { usbd_disconnect(usbdev, 1); nvic_disable_irq(USB_IRQ); - rcc_peripheral_disable_clock(&RCC_AHB2ENR, RCC_AHB2ENR_OTGFSEN); + rcc_periph_clock_disable(RCC_OTGFS); } /* Assert blue LED as indicator we are in the bootloader */ - rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); gpio_mode_setup(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_BOOTLOADER); gpio_set(LED_PORT, LED_BOOTLOADER); /* Disable used ports beside PORTD.*/ - rcc_peripheral_disable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); - rcc_peripheral_disable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPCEN); + rcc_periph_clock_disable(RCC_GPIOA); + rcc_periph_clock_disable(RCC_GPIOC); rcc_periph_clock_disable(USBUSART_CLK); /* Reset Systick.*/ systick_interrupt_disable(); @@ -119,7 +118,7 @@ void platform_request_boot(void) RCC_CFGR &= ~(RCC_CFGR_SWS_HSE | RCC_CFGR_SWS_PLL); /* Map System flash at 0.*/ - rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_SYSCFGEN); + rcc_periph_clock_enable(RCC_SYSCFG); SYSCFG_MEMRM &= ~3; SYSCFG_MEMRM |= 1; } diff --git a/src/platforms/f4discovery/platform.h b/src/platforms/f4discovery/platform.h index e274a38..9cd1082 100644 --- a/src/platforms/f4discovery/platform.h +++ b/src/platforms/f4discovery/platform.h @@ -32,8 +32,8 @@ #include #define PLATFORM_HAS_TRACESWO -#define BOARD_IDENT "Black Magic Probe (F4Discovery), (Firmware " FIRMWARE_VERSION ")" -#define DFU_IDENT "Black Magic Firmware Upgrade (F4Discovery" +#define BOARD_IDENT "Black Magic Probe (F4Discovery), (Firmware " FIRMWARE_VERSION ")" +#define DFU_IDENT "Black Magic Firmware Upgrade (F4Discovery)" /* Important pin mappings for STM32 implementation: * @@ -42,7 +42,6 @@ * LED2 = PD12 (Red LED : Error) * LED3 = PD15 (Blue LED : Bootloader active) * - * TPWR = XXX (input) -- analogue on mini design ADC1, ch8 * nTRST = PC1 * SRST_OUT = PC8 * TDI = PC2 @@ -51,10 +50,7 @@ * TDO = PC6 (input for TRACESWO * nSRST = * - * USB cable pull-up: PA8 - * USB VBUS detect: PB13 -- New on mini design. - * Enable pull up for compatibility. - * Force DFU mode button: PB12 + * Force DFU mode button: PA0 */ /* Hardware definitions... */ diff --git a/src/platforms/f4discovery/usbdfu.c b/src/platforms/f4discovery/usbdfu.c deleted file mode 100644 index 101824f..0000000 --- a/src/platforms/f4discovery/usbdfu.c +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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 "general.h" -#include "usbdfu.h" - -#include -#include -#include -#include - -void dfu_detach(void) -{ - /* USB device must detach, we just reset... */ - scb_reset_system(); -} - -int main(void) -{ - /* Check the force bootloader pin*/ - rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); - if(!gpio_get(GPIOA, GPIO0)) - dfu_jump_app_if_valid(); - - dfu_protect_enable(); - - /* Set up clock*/ - rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]); - systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8); - systick_set_reload(2100000); - - systick_interrupt_enable(); - systick_counter_enable(); - - /* Handle LEDs */ - rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); - gpio_clear(GPIOD, GPIO12 | GPIO13 | GPIO14 |GPIO15); - gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, - GPIO12 | GPIO13 | GPIO14 |GPIO15); - - /* Set up USB*/ - rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); - rcc_peripheral_enable_clock(&RCC_AHB2ENR, RCC_AHB2ENR_OTGFSEN); - - /* Set up USB Pins and alternate function*/ - gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, - GPIO10 | GPIO11 | GPIO12); - gpio_set_af(GPIOA, GPIO_AF10, GPIO10 | GPIO11 | GPIO12); - dfu_init(&stm32f107_usb_driver); - - dfu_main(); -} - - -void sys_tick_handler(void) -{ - gpio_toggle(GPIOD, GPIO12); /* Green LED on/off */ -} - From 8cc2c44dbfe5770b5e05ecdda49fc2a5c19c28e8 Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Tue, 18 Apr 2017 20:02:01 +0200 Subject: [PATCH 4/4] f4discovery: Use Magic constant in RAM for Bootloader entry. --- src/platforms/f4discovery/Makefile.inc | 2 +- src/platforms/f4discovery/platform.c | 54 ++++++++++---------------- src/platforms/f4discovery/platform.h | 2 + 3 files changed, 23 insertions(+), 35 deletions(-) diff --git a/src/platforms/f4discovery/Makefile.inc b/src/platforms/f4discovery/Makefile.inc index a1ec121..b04cf41 100644 --- a/src/platforms/f4discovery/Makefile.inc +++ b/src/platforms/f4discovery/Makefile.inc @@ -7,7 +7,7 @@ CFLAGS += -Istm32/include -mcpu=cortex-m4 -mthumb \ -DSTM32F4 -DF4DISCOVERY -I../libopencm3/include \ -Iplatforms/stm32 -LDFLAGS = -lopencm3_stm32f4 -Wl,--defsym,_stack=0x20006000 \ +LDFLAGS = -lopencm3_stm32f4 -Wl,--defsym,_stack=0x20010000 \ -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 \ diff --git a/src/platforms/f4discovery/platform.c b/src/platforms/f4discovery/platform.c index 01813bf..bc781e2 100644 --- a/src/platforms/f4discovery/platform.c +++ b/src/platforms/f4discovery/platform.c @@ -38,13 +38,27 @@ #include jmp_buf fatal_error_jmpbuf; +extern uint32_t _ebss; void platform_init(void) { + volatile uint32_t *magic = (uint32_t *) &_ebss; /* Check the USER button*/ rcc_periph_clock_enable(RCC_GPIOA); - if(gpio_get(GPIOA, GPIO0)) { - platform_request_boot(); + 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 */ + 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); + /* Jump to the built in bootloader by mapping System flash. + As we just come out of reset, no other deinit is needed!*/ + rcc_periph_clock_enable(RCC_SYSCFG); + SYSCFG_MEMRM &= ~3; + SYSCFG_MEMRM |= 1; scb_reset_core(); } @@ -89,36 +103,8 @@ const char *platform_target_voltage(void) void platform_request_boot(void) { - /* Disconnect USB cable */ - if (RCC_AHB2ENR & RCC_AHB2ENR_OTGFSEN) { - usbd_disconnect(usbdev, 1); - nvic_disable_irq(USB_IRQ); - rcc_periph_clock_disable(RCC_OTGFS); - } - - /* Assert blue LED as indicator we are in the bootloader */ - gpio_mode_setup(LED_PORT, GPIO_MODE_OUTPUT, - GPIO_PUPD_NONE, LED_BOOTLOADER); - gpio_set(LED_PORT, LED_BOOTLOADER); - /* Disable used ports beside PORTD.*/ - rcc_periph_clock_disable(RCC_GPIOA); - rcc_periph_clock_disable(RCC_GPIOC); - rcc_periph_clock_disable(USBUSART_CLK); - /* Reset Systick.*/ - systick_interrupt_disable(); - STK_CSR = 0; - STK_RVR = 0; - STK_CVR = 0; - /*Disable Interrupts.*/ - cm_disable_interrupts(); - /* Switch back to HSI.*/ - while (!(RCC_CR & RCC_CR_HSIRDY)) - RCC_CR |= RCC_CR_HSION; - while (RCC_CFGR & (RCC_CFGR_SWS_HSE | RCC_CFGR_SWS_PLL)) - RCC_CFGR &= ~(RCC_CFGR_SWS_HSE | RCC_CFGR_SWS_PLL); - - /* Map System flash at 0.*/ - rcc_periph_clock_enable(RCC_SYSCFG); - SYSCFG_MEMRM &= ~3; - SYSCFG_MEMRM |= 1; + uint32_t *magic = (uint32_t *) &_ebss; + magic[0] = BOOTMAGIC0; + magic[1] = BOOTMAGIC1; + scb_reset_system(); } diff --git a/src/platforms/f4discovery/platform.h b/src/platforms/f4discovery/platform.h index 9cd1082..971ebc2 100644 --- a/src/platforms/f4discovery/platform.h +++ b/src/platforms/f4discovery/platform.h @@ -80,6 +80,8 @@ #define LED_IDLE_RUN GPIO13 #define LED_ERROR GPIO14 #define LED_BOOTLOADER GPIO15 +#define BOOTMAGIC0 0xb007da7a +#define BOOTMAGIC1 0xbaadfeed #define TMS_SET_MODE() \ gpio_mode_setup(TMS_PORT, GPIO_MODE_OUTPUT, \