Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 470
FordPrefect Avatar asked Sep 14 '26 23:09

FordPrefect


2 Answers

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.

like image 87
pmacfarlane Avatar answered Sep 19 '26 03:09

pmacfarlane


First, let's define bootloader in context of MCU. Bootloader, as commonly understood, is a dedicate piece of code that allows you to upload or update your application software to the flash so it can later executed from it.

You've got several questions here. Let's go through them one by one.

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

When we are talking about STLink, we are talking about using debugger connection, probably SWD. This is a very low-level connection method which hooks directly into MCU internals. No bootloader is used in this method; in fact, MCU core doesn't work at all, i.e. it is stopped, and no instructions are executed. The programmer can directly erase and write new firmware into main flash without using execution core.

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

I'm going to assume we are talking about STM32F103 MCU, as found on Bluepills. This MCU contains a bootloader which can only operate via USART. It doesn't start USB hardware at all, so you won't see it. If you want to use this bootloader, you have to connect to MCU's USART using some adapter and use that COM port in the programmer (you can find the protocol for it in ST's appnote AN3155 if you are interested in its inner workings - or use one of the programmer applications that support it).

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?

No they are not replacing ST bootloader. It cannot be replaced; it is stored in ROM - proper ROM that cannot be erased and modified.

Arduino bootloader for STM32 (named stm32duino) is a custom bootloader which resides in main flash memory. As far as MCU is concerned, it is just a normal application; however it's a very specialized application and its sole purpose is to allow you to flash another (main) application into main flash memory side by side with itself, and then to run it. Note that it takes a bit of flash memory and your main application has to be aware of that and make room for the bootloader; e.g. stm32duino reserves first 8Kb of flash for itself which must not be touched by an application.

like image 25
Andrey Turkin Avatar answered Sep 19 '26 03:09

Andrey Turkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!