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, \