Three BOOT modes of STM32 and assembly programming design



1. BOOT

STM32 BOOT overview:
The storage media corresponding to the three boot modes of STM32 are all built-in chips. They are:
1) User flash memory = Flash built-in chip.
2) SRAM = RAM area built into the chip, which is the memory.
3) System memory = a specific area inside the chip, a section of Bootloader is preset in this area when the chip leaves the factory, which is usually called ISP program. No one can modify or erase the content of this area after the chip leaves the factory, that is, it is a ROM area.

There are two pins BOOT0 and BOOT1 on each STM32 chip. The level state of these two pins when the chip is reset determines the area from which the program is executed after the chip is reset:

BOOT1 BOOT0 mode
x 0 Booting from the user flash memory, this is the normal operating mode.
0 1 Start from the system memory. The program function of this mode is set by the manufacturer.
1 1 Starting from the built-in SRAM, this mode can be used for debugging.

Click for details: STM32 BOOT overview

1. Introduction to three BOOT modes

The so-called startup generally refers to the value of the BOOT pin will be latched on the fourth rising edge of SYSCLK when the chip is restarted after we have finished the program. The user can select the startup mode after reset by setting the state of the BOOT1 and BOOT0 pins.
Insert picture description here

Main Flash memory
is the built-in Flash of STM32. Generally, when we download the program in JTAG or SWD mode, we download it into this, and start the program directly from here after restarting.

System memory is
started from the system memory. The program function of this mode is set by the manufacturer. Generally speaking, this kind of startup method is used less. The system memory is a specific area inside the chip. When STM32 is shipped from the factory, ST presets a section of BootLoader in this area, which is what we often call the ISP program. This is a ROM that
cannot be modified after leaving the factory. Generally speaking, when we choose this startup mode, it is to download the program from the serial port, because in the BootLoader provided by the manufacturer, the firmware of the serial port downloading program is provided, and the program can be downloaded to the Flash of the system through this BootLoader. However, this download method requires the following steps:
Step1: Set BOOT0 to 1, BOOT1 to 0, and then press the reset button, so that BootLoader can be started from the system memory.
Step2: Finally, with the help of BootLoader, download the program to Flash through the serial port
Step3: After the program download is completed, it is necessary to set BOOT0 to GND and manually reset it, so that STM32 can be started from Flash and can be seen. It is quite troublesome to download the program using the serial port, and it is necessary to jump around. Very little attention to user experience.

Embedded Memory has
built-in SRAM. Since it is SRAM, there is no program storage capability. This mode is generally used for program debugging. If I only modify a small part of the code, then I need to erase the entire Flash again. It takes more time. Consider starting the code from this mode (that is, in the STM32 memory) for fast program debugging, etc. After the program is debugged, download the program to SRAM.

2. Develop BOOT mode selection.

1. The program code is usually stored in the main flash memory, the configuration method: BOOT0=0, BOOT1=X;

2. Flash lock solution:

During the development and debugging process, the internal Flash is locked due to some reasons, and the SWD and Jtag debugging cannot be connected, and the device cannot be read. You can refresh the code by modifying the BOOT mode.

Modify it to BOOT0=1, BOOT1=0 to boot from the system memory, ST comes with a Bootloader program when it leaves the factory, and SWD and JTAG debugging interfaces are dedicated. After re-programming, the BOOT mode can be changed to BOOT0=0, BOOT1=X and it can be used normally.

Introduction to the three BOOT modes of STM32

2. Create a pure assembly language STM32 project based on MDK

1. Complete the compilation of the assembler

1. New project
Insert picture description here
2. Chip selection STM32F103ZE
Insert picture description here3. Configuration environment is as follows:
Insert picture description here
4. Add .s file
Insert picture description here
Insert picture description here
5. Write code The
Insert picture description here
code is as follows:

 AREA MYDATA, DATA
	
 AREA MYCODE, CODE
	ENTRY
	EXPORT __main

__main
	MOV R0, #10
	MOV R1, #11
	MOV R2, #12
	MOV R3, #13
	;LDR R0, =func01

	BL	func01
	;LDR R1, =func02
	BL	func02
	
	BL 	func03
	LDR LR, =func01
	LDR PC, =func03
	B .
		
func01
	MOV R5, #05
	BX LR
	
func02
	MOV R6, #06
	BX LR
	
func03
	MOV R7, #07
	MOV R8, #08	
	BX LR
	END

6. Follow-up configuration
Insert picture description here

I have not connected the device, so I need to tick as shown in the figure:
Insert picture description here

7. Compile and debug
First compile, and then start debugging.
Insert picture description here
Insert picture description here
Click Run and
Insert picture description here
you can see that there is an error.
Insert picture description here
Let us reconfigure here:

Dialog DLL is changed to DARMSTM.DLL
-pCM3 is changed to -pSTM32F103ZE
Insert picture description here
, there is nothing wrong with running

2. The size of each segment of the hex file, and the first 8 bytes of the Hex file

Check Create HEX File
Insert picture description here
and then compile to generate a hex file.
Insert picture description here
Open the hex file and
Insert picture description here
divide the first line of code into five parts
020000040800F2——>02 0000 04 0800 F2

The first byte is the data length, the
second and the third two bytes represent the address offset, the
fourth byte is the data type, and the next is the data area. The
last byte is the check code.

Three, LED assembler

1. Create the project as above
Insert picture description here
2. Add code

LED0 EQU 0x40010c00
RCC_APB2ENR EQU 0x40021018
GPIOA_CRH EQU 0x40010804



Stack_Size      EQU     0x00000400

                AREA    STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem       SPACE   Stack_Size
__initial_sp




                AREA    RESET, DATA, READONLY

__Vectors       DCD     __initial_sp               ; Top of Stack
                DCD     Reset_Handler              ; Reset Handler
                    
                    
                AREA    |.text|, CODE, READONLY
                    
                THUMB
                REQUIRE8
                PRESERVE8
                    
                ENTRY
Reset_Handler 
                BL LED_Init
MainLoop        BL LED_ON
                BL Delay
                BL LED_OFF
                BL Delay
                
                B MainLoop
             
LED_Init
                PUSH {
    
    R0,R1, LR}
                
                LDR R0,=RCC_APB2ENR
                ORR R0,R0,#0x04
                LDR R1,=RCC_APB2ENR
                STR R0,[R1]
                
                LDR R0,=GPIOA_CRH
                BIC R0,R0,#0x0F
                LDR R1,=GPIOA_CRH
                STR R0,[R1]
                
                LDR R0,=GPIOA_CRH
                ORR R0,R0,#0x03
                LDR R1,=GPIOA_CRH
                STR R0,[R1]
                
                MOV R0,#1 
                LDR R1,=LED0
                STR R0,[R1]
             
                POP {
    
    R0,R1,PC}

             
LED_ON
                PUSH {
    
    R0,R1, LR}    
                
                MOV R0,#0 
                LDR R1,=LED0
                STR R0,[R1]
             
                POP {
    
    R0,R1,PC}
             
LED_OFF
                PUSH {
    
    R0,R1, LR}    
                
                MOV R0,#1 
                LDR R1,=LED0
                STR R0,[R1]
             
                POP {
    
    R0,R1,PC}             
             
Delay
                PUSH {
    
    R0,R1, LR}
                
                MOVS R0,#0
                MOVS R1,#0
                MOVS R2,#0
                
DelayLoop0        
                ADDS R0,R0,#1

                CMP R0,#330
                BCC DelayLoop0
                
                MOVS R0,#0
                ADDS R1,R1,#1
                CMP R1,#330
                BCC DelayLoop0

                MOVS R0,#0
                MOVS R1,#0
                ADDS R2,R2,#1
                CMP R2,#15
                BCC DelayLoop0
                
                
                POP {
    
    R0,R1,PC}    
             
    ;         NOP
             END

3. Check Create HEX File

Insert picture description here
3. Compile and generate hex file
Insert picture description here
4. Observe the LED flicker after burning
Insert picture description here

Guess you like

Origin blog.csdn.net/aiwr_/article/details/111851712