Search code examples
stm32microcontrollerbootloader

STM32 Bootloader


I am a real newbie in the field of micro controllers. So I am sorry if I missed something or mess up terminology.

There are many really specific questions to specific problems on that topic, but I want to understand it a bit more on a general level.

My reference is a STM32F1 on a Bluepill.

I know that this controller as all? in the STM32 family has system memory starting at address 0x00000000 holding the bootloader and the main flash memory starting at 0x08000000 holding my firmware.

When I have BOOT0 and BOOT1 on GND the processor will boot from main flash memory and if I set BOOT0 to HIGH it will boot the bootloader.

So bootloader has a different meaning compared to computers and operating systems, because for final use of the device I don't need the bootloader, I only need the correct pin setup of BOOT0 and BOOT1.

My understanding was that if I want to change the firmware I should set the controller up to boot the bootloader and then connect via one of the many interfaces and flash the main flash memory. When I set it up like this and connect it via USB to my PC, nothing happens.

What I successfully adapted from introductions to embedded rust was to set the controller up to boot from main flash memory and connect a ST-LinkV2 to the Bluepill. Then I can connect via gdb with the ST-LinkV2 and flash my firmware.

So I have a couple of questions:

  1. Why is the ST-LinkV2 used with the memory setup to boot the main firmware and not the bootloader?

  2. Why do I see no USB device when I set the controller to boot into bootloader and connect it via USB?

  3. I see many people that want to use Bluepills with Arduino IDE. They flash a new bootloader onto the controller. Are they replacing the ST bootloader? Will the USB interface after the be similar to programming over ST-Link?


Solution

  • The STM32F103's SRAM is at address 0x20000000, and its flash is at address 0x08000000 (note the leading zero). It also has a bootloader (in ROM) at address 0x1FFFxxxx.

    Depending on the setting of the BOOTx pins, one of those three memory areas will be aliased to 0x00000000. e.g. if you are booting from flash, the flash memory will be visible both at address 0x08000000 and also at address 0x00000000.

    Because the CPU, when it comes out of reset, always fetches the reset vector from address 0x00000004, this aliasing allows you to boot from either SRAM, flash, or the bootloader.

    The system bootloader is in ROM and cannot be changed or updated.

    When you program your board with an ST-Link, you are not using the bootloader on the STM32 (or any software on the STM32), the programming happens entirely over the JTAG or SWD interface.

    The STM32F103's bootloader does not support USB, it only supports USART1. See table 3 in AN2606.

    It's possible to write your own bootloader, and many people do. However, this would still live in normal flash memory, and does not overwrite the inbuilt bootloader.