Difference between revisions of "STM32 Jump to System Memory Bootloader"
Line 18: | Line 18: | ||
== The easy solution == | == The easy solution == | ||
+ | |||
+ | Fortunately, there is in fact an easier way to do this. If we look at the code generated by [[STM32CubeMX]] the startup is like this: | ||
+ | |||
+ | <pre> | ||
+ | int main(void) | ||
+ | { | ||
+ | /* USER CODE BEGIN 1 */ | ||
+ | /* USER CODE END 1 */ | ||
+ | |||
+ | /* MCU Configuration--------------------------------------------------------*/ | ||
+ | |||
+ | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ | ||
+ | HAL_Init(); | ||
+ | |||
+ | /* USER CODE BEGIN Init */ | ||
+ | |||
+ | /* USER CODE END Init */ | ||
+ | |||
+ | /* Configure the system clock */ | ||
+ | SystemClock_Config(); | ||
+ | |||
+ | /* USER CODE BEGIN SysInit */ | ||
+ | |||
+ | /* USER CODE END SysInit */ | ||
+ | |||
+ | /* Initialize all configured peripherals */ | ||
+ | MX_GPIO_Init(); | ||
+ | MX_USB_DEVICE_Init(); | ||
+ | |||
+ | ... | ||
+ | |||
+ | </pre> | ||
== Source == | == Source == |
Revision as of 04:55, 16 March 2022
All STM32 MCUs has a builtin bootloader stored in so-called system memory. The system memory is a ROM (read-only memory) which is created during the production of the MCU and can never be changed. When the MCU startup the Boot0 pin is asserted and if high (pulled up to VCC) the MCU will execute the bootloader.
The "problem" (or challenge)
But what if we would like to execute this bootloader programmatically based on some other event for example the press of a user button or a command in a serial console. While this is entirely possible it is surprisingly difficult. The problem is that the built-in bootloader make a lot of assumptions and it is necessary to make certain all these assumptions are met before jumping to it. In general that would look something like:
- Find system memory location for specific STM32 in AN2606
- Set RCC to default values (the same as on startup) [Internal clock, no PLL, etc.)
- Disable SysTick interrupt and reset it to default
- Disable all interrupts
- Map system memory to 0x00000000 location
- Set jump location to memory location + 4 bytes offset
- Set main stack pointer to value stored at system memory location address
- Call virtual function assigned before
Depending on which peripherals is being used, the above can be quite complicated and changes in code can easily screw this up.
The easy solution
Fortunately, there is in fact an easier way to do this. If we look at the code generated by STM32CubeMX the startup is like this:
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USB_DEVICE_Init(); ...
Source
The source for this example can be found on our github.