Published Dec. 1, 2024, 11:37 a.m. by Ezra
This guide will explore the various boot modes of STM32 microcontrollers and explain how to configure the Boot0 and Boot1 pin states to select the desired mode. Additionally, we'll cover how to locate these pins, set them to HIGH or LOW, and more. Let’s start now!
We will cover the following:
STM32 Boot Modes
STM32 microcontrollers offer several boot modes that dictate how the device starts up and the source of the initial code execution. Here, we will explore the main boot modes available for STM32 microcontrollers.
STM32 Boot Mode Options
1) Main Flash Memory Boot: In this mode, the microcontroller starts from the main flash memory, where the user application resides. It is the default and most widely used boot mode in production environments.
2) System Memory Boot: Here, the microcontroller boots from the system memory, which houses the built-in bootloader. This mode is commonly utilized for tasks like firmware updates or initial programming via interfaces such as UART, USB, or SPI.
3) Embedded SRAM Boot: The microcontroller initializes from the internal SRAM, a mode primarily used for debugging or specific purposes.
The Boot0 and Boot1 pin states during a reset determine the boot mode the STM32 microcontroller enters upon power-up. For instance, the STM32F103 offers specific boot modes based on the Boot0 and Boot1 pin combinations, a pattern similar across most STM32 microcontrollers.
The Boot0 pin plays a key role in determining the boot mode of an STM32 microcontroller. Its state is read during a reset, and together with the Boot1 pin, it decides the boot mode.
Locating the Boot0 pin on an STM32 microcontroller is straightforward, as shown in the diagram below. This dedicated pin is exclusively used for its boot function and is not shared with any GPIO functionality.
For custom PCB designs, it is advisable to connect the STM32 Boot0 pin to GND through a 10kΩ resistor if you don’t anticipate using a bootloader (e.g., DFU mode).
If there’s uncertainty about future requirements, such as the potential need for a bootloader, consider adding flexibility. You can achieve this by connecting the Boot0 pin to a jumper or creating a solder pad, allowing for modifications later if needed.
The Boot1 pin works alongside the Boot0 pin to determine the microcontroller's boot mode. The specific boot mode is dictated by the states of both pins during a reset, as summarized in the accompanying table.
In many STM32 microcontrollers, the Boot1 pin is shared with a GPIO function, meaning it is multiplexed. To use it for boot mode selection, you must configure the GPIO pin properly. Be sure to consult the specific STM32 datasheet for detailed instructions on setting up the Boot1 pin for this purpose.
If you have connected the Boot0 pin to GND to ensure your STM32 always boots from FLASH memory, the Boot1 pin can be left unconnected and used as a regular GPIO pin.
However, when using both Boot0 and Boot1 pins to select a custom boot mode, you should connect the Boot1 pin to a jumper for state selection (0 or 1). Additionally, remember to include a 10kΩ pull-up or pull-down resistor as recommended by STM32’s hardware design guidelines.
In certain STM32 microcontrollers, like the STM32G0 series, the Boot0 pin is disabled by default, which forces the microcontroller to always boot from the main flash memory regardless of the Boot0 pin state. To enable the Boot0 pin functionality, you need to flash a specific piece of code to modify the control registers responsible for this setting.
Below is an example code that you can compile and flash to your target microcontroller. After running this code once, the Boot0 pin will be enabled and can be used for boot mode selection, similar to other STM32 microcontroller series.
Code To Re-Enable A Disabled Boot0 Pin in STM32 G0
#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 (;;); }
Conclusion
To sum up, we have examined the various boot modes available in STM32 microcontrollers, explained how the Boot0 and Boot1 pins are used to choose the boot mode, shown how to identify these pins on any STM32 device, and provided guidance on designing a circuit to control these pins as per your application's requirements.
If you're new to working with STM32 microcontrollers, be sure to check out the STM32 Getting Started Tutorial. Continue exploring the STM32 Tutorial Series to deepen your understanding of STM32 microcontroller programming.
In today's guide, we’ll explore how to use PWM …Introduction