Skip to main content
Werner Dähn
Associate III
June 1, 2018
Solved

STM32F4: jump to MCU bootloader

  • June 1, 2018
  • 16 replies
  • 5022 views
Posted on June 01, 2018 at 07:54

I think I got the concept of how to jump to the MCU built-in bootloader but I never got it working. And often information is opposing each other.

My requirement is simple and very common, hence I hope somebody has code at hand. I am fed up with trying and trying.

I have a USB CDC connection, and enter commands there. One command should be 'boot to MCU bootloader' which would disconnect the USB and make the MCU appear in the DfuSeDemo tool. As if I manually disconnected the the board, pressed the Boot0 button and power it while button pressed.

Source Code can be found here: 

https://github.com/wernerdaehn/CC3D-CableCam-Controller

  

Nothing special, standard memory layout etc.

#stm32f4-dfu-usb-bootloader
This topic has been closed for replies.
Best answer by Tesla DeLorean
...
 .text
 .thumb
 
 .align 2
 .thumb_func
 .globl Reboot_Loader
 .type Reboot_Loader, %function
Reboot_Loader:
 // Device specific, if in doubt RTFM, here F2/F4
 
 LDR R0, =0x40023844 // RCC_APB2ENR
 LDR R1, =0x00004000 // ENABLE SYSCFG CLOCK
 STR R1, [R0, #0]
 LDR R0, =0x40013800 // SYSCFG_MEMRMP
 LDR R1, =0x00000001 // MAP ROM AT ZERO
 STR R1, [R0, #0]
 LDR R0, =0x1FFF0000 // ROM BASE
 LDR SP,[R0, #0] // SP @ +0
 LDR R0,[R0, #4] // PC @ +4
 BX R0
 .pool // sourcer32@gmail.com
 .size Reboot_Loader, . - Reboot_Loader
 
 .align 2
 .thumb_func
 .globl Reset_Handler
 .type Reset_Handler, %function
Reset_Handler:
 
 LDR R0, =0x2001FFF0 // End of SRAM for your CPU
 LDR R1, =0xDEADBEEF
 LDR R2, [R0, #0]
 STR R0, [R0, #0] // Invalidate
 CMP R2, R1
 BEQ Reboot_Loader
 
/* Loop to copy data from read only memory to RAM. The ranges
 * of copy from/to are specified by following symbols evaluated in
 * linker script.
 * __etext: End of code section, i.e., begin of data sections to copy from.
 * __data_start__/__data_end__: RAM address range that data should be
 * copied to. Both must be aligned to 4 bytes boundary. */
...

16 replies

Tobi as
Associate
September 11, 2018

Hi Clive,

thanks for your answer. I have now set the following lines as comment:

//   __disable_irq();

//   __set_PRIMASK(1);

But I have the same effect as before...

Werner Dähn
Associate III
September 30, 2018

The provided code did work for 5 times, but after that Windows was not able to recognize the USB device after the system boot any longer. I had to again unplug the cable and reconnect.

As I guessed it was a timing issue, I added some code for disabling USB first and a short delay. Since then no more problems. So my C code is at the moment.

void JumpToBootloader(void) {
 *((unsigned long *)0x2001FFF0) = 0xDEADBEEF; // End of RAM
 
 USBD_Stop(&hUsbDeviceFS);
 USBD_DeInit(&hUsbDeviceFS);
 HAL_Delay(5000); // Wait for 5 seconds so that it looks to Windows as if the USB cable has been unplugged.
 
 NVIC_SystemReset();
}

Tobi as
Associate
October 8, 2018

Hi Werner,

thanks for the very helpful information. My code run, when I unplug and reconnect the cable. :)

This is sufficient for my application.

But can you tell me what the function USBD_Stop(&hUsbDeviceFS); does in detail? This function isn't available in my code.