In this tutorial, we’ll discuss the STM32 Boot Modes, and how to set the STM32 Boot0 Boot1 Pins‘ states to select a desired boot mode for our microcontroller. We’ll also discuss how you can easily identify the STM32 Boot0 Boot1 pins, drive them HIGH or LOW, and much more. Without further ado, let’s get right into it!
Table of Contents
STM32 Boot Modes
STM32 microcontrollers support multiple boot modes that determine how the device initializes and where it fetches the initial code to execute. In this section, we’ll discuss the primary boot modes for STM32 microcontrollers.
STM32 Boot Mode Options
1) Main Flash Memory Boot: The microcontroller boots from the main flash memory, where the user application is stored. This is the most common boot mode used in production devices.
2) System Memory Boot: The microcontroller boots from the system memory, which contains the built-in bootloader. This mode is often used for firmware updates or initial programming through various interfaces such as UART, USB, or SPI.
3) Embedded SRAM Boot: The microcontroller boots from the internal SRAM, which is mainly used for debugging or special purposes.
The states of the Boot0 and Boot1 pins during reset decide which boot mode the STM32 microcontroller will go into when powered up. For example, the STM32F103 device has the following boot modes for each state combination of Boot0 and Boot1 pins. This is almost the same for most of the STM32 microcontrollers.
STM32 Boot0 Pin
The Boot0 pin is crucial in selecting the boot mode of the STM32 microcontroller. It is sampled during reset and, in combination with the Boot1 pin, it determines the boot mode.
STM32 Boot0 Pin States
- (Boot0 = 0): When the Boot0 pin is LOW, the microcontroller typically boots from the main flash memory.
- (Boot0 = 1): When the Boot0 pin is HIGH, the boot mode depends on the state of the Boot1 pin.
STM32 Boot0 Pin Identification
It’s very easy to identify the Boot0 pin in an STM32 microcontroller as you can see in the figure below. This is a special function pin for the microcontroller and is not multiplexed with any GPIO functionality.
STM32 Boot0 Pin Connection
It’s recommended to hook up the STM32 Boot0 pin to GND using a 10kΩ resistor if you’re designing your own PCB and not planning to have any sort of bootloader (DFU) and such.
If you’re not quite sure about it and if your application will need to have a bootloader in the future or not, then you can just hook it up to a jumper or place a solder pad to make some room for a future change in plans, if any.
STM32 Boot1 Pin
The Boot1 pin works in conjunction with the Boot0 pin to select the boot mode. The combination of Boot0 and Boot1 pin states during reset determines the specific boot mode as shown in the table below:
Boot0 | Boot1 | Boot Mode |
---|---|---|
0 | x | Main Flash Memory |
1 | 0 | System Memory |
1 | 1 | Embedded SRAM |
STM32 Boot1 Pin Identification
On most STM32 microcontrollers, the Boot1 pin is multiplexed with a GPIO pin. It is essential to configure this GPIO pin correctly to be able to use it for boot mode selection, if needed. You need to refer to the specific STM32 datasheet for details on configuring the Boot1 pin.
For example, in the STM32F103 microcontroller’s datasheet, I could easily find out that the Boot1 pin is the GPIO pin PB2 as shown in the figure below.
STM32 Boot1 Pin Connection
If you’re pulling-down the Boot0 pin to GND to make your STM32 boot from FLASH memory always, you can just ignore the Boot1 pin and not connect anything to it. You’re free to use it as any other GPIO pin.
However, if you’re using Boot0 + Boot1 pins to select a custom boot mode, you need to connect the Boot1 to a jumper for state selection (0 or 1), and don’t forget to add a pull-up/down resistor of 10kΩ as recommended by the hardware design guidelines of STM32 microcontrollers.
STM32 Boot0 Pin Disabled, How To Enable?
In some STM32 microcontrollers, such as the STM32G0 series, the Boot0 pin functionality is disabled by default. This means that the microcontroller will always boot from the main flash memory regardless of the Boot0 pin state. To re-enable the Boot0 pin, you need to flash some code to manually change this behavior by modifying the control registers responsible for this functionality.
Here is an example code that you can build for your target microcontroller, flash it, and run it once. Then you’ll have the Boot0 enabled and can be used for boot selection just like any other STM32 microcontroller series.
Code Example To Re-Enable A Disabled Boot0 Pin in STM32 G0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
/* * LAB Name: STM32 Boot0 Enable * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com */ #include "stm32g0xx.h" void Flash_Unlock(void) { // Unlock Flash memory FLASH->KEYR = 0x45670123; FLASH->KEYR = 0xCDEF89AB; // Unlock Option Bytes FLASH->OPTKEYR = 0x08192A3B; FLASH->OPTKEYR = 0x4C5D6E7F; } void Boot0_Enable(void) { // Clear the nBOOT_SEL bit to enable BOOT0 pin functionality FLASH->OPTR &= ~FLASH_OPTR_nBOOT_SEL; // Wait for any ongoing flash operation to complete while(FLASH->SR & FLASH_SR_BSY1); // Start the Option Byte programming FLASH->CR |= FLASH_CR_OPTSTRT; // Wait for the programming to complete while(FLASH->SR & FLASH_SR_BSY1); // Launch the option byte loading FLASH->CR |= FLASH_CR_OBL_LAUNCH; } int main(void) { // Check if the nBOOT_SEL bit is already cleared if ((FLASH->OPTR & FLASH_OPTR_nBOOT_SEL) == 0) { // If already cleared, do nothing for (;;); } // Unlock flash memory and option bytes Flash_Unlock(); // Enable BOOT0 pin functionality Boot0_Enable(); // We should never reach this point as the system will reset for (;;); } |
Wrap Up
In conclusion, we’ve explored different boot modes in STM32 microcontrollers, how the STM32 Boot0 Boot1 pins are used to select the boot mode option, how we can easily identify those two pins in any STM32 microcontroller, and how to design the hardware circuit to drive the pins according to our application’s needs.
If you’re just getting started with STM32, you need to check out the STM32 Getting Started Tutorial here. Follow this STM32 Series of Tutorials to learn more about STM32 Microcontrollers Programming.