Simple STM32 Assembler-flashing LEDs

Source: Internet
Author: User
Tags bitwise

To transplant the operating system, the assembly is the way to cross the past. So take the idea of the previous article, I am ready to use the sink to write a simple flashing LED light program. Practice the compilation to prepare for the operating system.

The first step is to create an empty folder as in the previous article.

The second step, because it is to use the assembly to write the program, so do not need to start the code, here Choose No.

The third step is to create a. s file and add the file to the project.

Fourth step, add the following code in the Led.s file.

LED0 EQU 0x422101a0 rcc_apb2enr EQU 0x40021018gpioa_crh EQU 0x40010804stack_size EQU 0x00000400 Area STACK, Noinit, READWRITE, align=3stack_mem SPACE stack_size__initial_sp area RESET, DATA, readonly__vectors DCD __i NITIAL_SP;Top of StackDCD Reset_handler;Reset HandlerArea |.                                    Text|, CODE, READONLY THUMB REQUIRE8 PRESERVE8                Entryreset_handler bl led_initmainloop 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_apb2enrSTRR0,[r1] Ldr r0,=gpioa_crh BIC r0,r0, #0x0F Ldr R1,=gpioa_ CRHSTRR0,[r1] Ldr r0,=gpioa_crh ORR r0,r0, #0x03 Ldr R1,=gpioa_ CRHSTRR0,[R1]MOVr0,#1LDR r1,=led0STRR0,[R1]POP{r0,r1,pc} led_onPUSH{r0,r1, LR}MOVr0,#0LDR r1,=led0STRR0,[R1]POP{r0,r1,pc} led_offPUSH{r0,r1, LR}MOVr0,#1LDR r1,=led0STRR0,[R1]POP{r0,r1,pc} DelayPUSH{r0,r1, LR} MOVS r0,#0MOVS r1,#0MOVS r2,#0DelayLoop0 ADDS r0,r0,#1                CMPr0,# theBCC DelayLoop0 MOVS r0,#0ADDS r1,r1,#1                CMPr1,# theBCC DelayLoop0 MOVS r0,#0MOVS r1,#0ADDS r2,r2,#1                CMPr2,# theBCC DelayLoop0POP{R0,R1,PC};NOPEND

///////////////////////////////////////////////////////

A simple explanation of the code

1, pre-defined

LED0 EQU 0x422101a0 ; The Bit-bond address of the PA8.

Rcc_apb2enr EQU 0x40021018

GPIOA_CRH EQU 0x40010804

For ease of operation, define a name for each register address that needs to be used, similar to the C # define. The calculation method of the Bit-bond address of PA8 can be calculated according to the algorithm of C language in the previous article. The following two addresses are fixed and can be queried from the STM32 manual, or calculated based on St's official library files.

2, allocating stack space

Stack_size EQU 0x00000400

Area STACK, Noinit, READWRITE, align=3

Stack_mem SPACE Stack_size

__initial_sp

This paragraph is excerpted from the startup file. To read this code, first understand the two commands.

Area Command : The area Command instructs the assembler to assemble a new code snippet or data segment. Segments are independent, specified, invisible code or blocks of data that are handled by the linker. The format is as follows:

Area segment name, segment attribute 1, segment attribute 2, Segment Property 3 ...

Area STACK, Noinit, READWRITE, align=3

Noinit: = no Init, not initialized.

READWRITE: readable, writable.

ALIGN =3:2^3 Alignment, which is 8-byte alignment.

Space Command: The SPACE command retains a memory block populated with 0.

So the whole paragraph means: Assign a stack segment that is uninitialized, read-write, and 8-byte aligned. Allocate a storage space of size stack_size, and make the address of the top of the stack __initial_sp.

3, Allocation vector table

Area RESET, DATA, READONLY

__vectors DCD __initial_sp; Top of Stack

DCD Reset_handler; Reset Handler

The vectors here refer to the detailed analysis of theSTM32 vector table I wrote earlier.

4, Start code snippet

Area |. Text|, CODE, READONLY

Notifies the assembler to begin the code snippet.

THUMB

REQUIRE8

PRESERVE8

The meaning of this paragraph is that the assembler supports thumb instructions, and the code snippet is aligned by 8 bytes.

ENTRY Command: Declares an entry point for the entire program, with only one entry point. In either language, the compiler has to have an entry point, which is nothing to say.

5, the program officially started.

The following code is used for standard THUMB2 assembly instructions. First understand the instructions that are used in the code.

BL: Jump instruction with link. When you use this command to jump, the current address (PC) is automatically fed into the LR register.

B: Unconditional jump.

Push and Pop: As you can see, all subroutines are wrapped up by push and pop. Use a picture to explain the two instructions.

It is known that the meaning of push {r0,r1, LR} is to put the value in R0,R1,LR into the stack. Because the BL jump instruction is used in the main program, the value in LR is actually the value of the current PC. The meaning of pop {r0,r1,pc} is to return the value of the previously stored r0,r1,lr in the stack to r0,r1,pc. This completes the return of the subroutine.

Ldr and str: Load and store instructions for registers.

LDR is the loading of addresses into registers (such as R0).

STR is storing the value in the address referred to by the Register.

As an example:

MOV R0, #1; will be sent immediately to the number 1 into R0.

LDR r1,=led0; PA8 Bit-bond address is sent to R1.

STR R0,[R1]; the value of R0, which is 1, is given to the address that the value in R1 points to, that is, the Bit-bond address of PA8.

The above three sentences mean to put PA8 1.

Orr and Bic:

ORR bitwise OR operation. ORR r0,r0, #0x04意思即将R0中的数或上0x04, then sent the results to R0. The actual meaning is to place the second position of the R0 1, the other bits unchanged.

The BIC first reversed the immediate number, then bitwise AND.

cmp and Bcc: CMP is a comparison of two numbers, equal or greater than the flag bit C is set, otherwise C is zeroed. BCC is a combined instruction, actually b+cc, meaning if c=0 jumps.

CMP R2, #15; Calculate the value of r2-15, if r2<15, then c=0; if r2>=15, then c=1.

BCC DelayLoop0; If c=0, then jump to DelayLoop0, if c=1, do not jump.

The above is the description of the code snippet related directives, I believe that understand the meaning of these instructions, to understand the code is not difficult.

The structure of the entire code and the previous article in C language is basically the same. Reference to Understanding

////////////////////////////////////////////////////

Fifth step, compile, download.

After compiling, there will be a warning No section matches pattern ... No need to control. After downloading, the LED light flashes normally.

Simple STM32 Assembler-flashing LEDs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.