Source code analysis of the bootloader of the ARM7 lpc2210

Source: Internet
Author: User
Tags prefetch

Analyze and interpret the bootlaoder written in chenmingji, easyarm lpc2210 Development Board.

I. Variable (constant) Volume statement
First declare the stack size
; Define the stack size
; Defines the size of the stack
Svc_stack_le1_equ 0
Fiq_stack_le1_equ 0
Irq_stack_le1_equ 256; IRQ interrupt Stack
Abt_stack_le1_equ 0
Und_stack_le1_equ 0
Processor mode statement
Noint equ 0x80

Usr32mode equ 0x10
Svc32mode equ 0x13
Sys32mode equ 0x1f
Irq32mode equ 0x12
Fiq32mode equ 0x11
External Storage configuration statement
Pinsel2 equ 0xe002c014

B00000 equ 0xffe00000
Bcfg1 equ 0xffe00004
Bcfg2 equ 0xffe00008
Bcfg3 equ 0xffe0000c

Import _ use_no_semihosting_swi
Import _ use_two_region_memory

; The imported labels
; The introduced external label is declared here
Import fiq_exception; fast interrupt exceptions handler fast interrupt exception handling program
Import _ main; the entry point to the main function C main program Portal
Import targetresetinit; initialize the target board basic Initialization

; The emported labels
The label used for external use is declared here
Export bottom_of_heap; bottom of heap
Export bottom_of_stacks; bottom of the stack
Export top_of_heap; top of heap
Export stackusr

Export reset; Reset
Export _ user_initial_stackheap; User-initiated stack and heap

2. Establish an interruption vector table (the 32-bit sum of all data in the table is 0)
Reset
Ldr pc, resetaddr; Execution address starting After resetting
Ldr pc, undefinedaddr; undefined command exception
Ldr pc, swi_addr; software interruption
Ldr pc, prefetchaddr; prefetch aborted
Ldr pc, dataabortaddr; Data prefetch aborted
DCD 0xb9205f80; reserved exception
Ldr pc, [PC, #-0xff0]; IRQ (this command reads the value of the vicvectaddr register, and then puts the PC pointer)
Ldr pc, fiq_addr; Fiq

; Assign continuous word storage units to each vector
Resetaddr DCD resetinit
Undefinedaddr DCD undefined
Swi_addr DCD softwareinterrupt
Prefetchaddr DCD prefetchabort
Dataabortaddr DCD dataabort
Nouse DCD 0
Irq_addr DCD 0
Fiq_addr DCD fiq_handler

; When the following exception occurs, the program is suspended (except FIQ)
; Undefined command
Undefined
B undefined

; Soft Interrupt
Softwareinterrupt
B softwareinterrupt

; Abort the command.
Prefetchabort
B prefetchabort

; Stop retrieving data
Dataabort
B dataabort

; Fast interruption
Fiq_handler
Stmfd SP !, {R0-R3, LR}; store the values of R0-R3 and LR into the stack
BL fiq_exception; jump to the FIQ interrupt program
Ldmfd SP !, {R0-R3, LR}; restore R0-R3, LR Value
Subs PC, LR, #4; PC pointer jump to the LR-4

Iii. Reset procedure
According to the pin connection method of the external memory controller, the gpio is set (the pins connected to the external memory are reused with the gpio functions of P1, P2, and P3 ports ), then, set the four groups of memory groups used.
Resetinit
; Initial extenal bus controller.
Initialize the External Bus Controller and configure it according to the target board.

LDR r0, = pinsel2
If: Def: en_crp
LDR R1, = 0x0f814910
Else
LDR R1, = 0x0f814914
Endif
STR R1, [R0]
Set four groups of memory configurations
LDR r0, = b1_0
LDR R1, = 0x1000ffef
STR R1, [R0]

LDR r0, = bcfg1
LDR R1, = 0x1000ffef
STR R1, [R0]

; LDR r0, = bcfg2; the Development Board does not use the following two groups of memory Interfaces
; LDR R1, = 0x2000ffef
; STR R1, [R0]

; LDR r0, = bcfg3
; LDR R1, = 0x2000ffef
; STR R1, [R0]

BL initstack; initialize the stack
BL targetresetinit; the target board basically initializes initialize the target board
Jump to the C language portal jump to the entry point of C program
B _ main

Iv. Stack Initialization
Initialize each mode Stack
Initstack
MoV r0, LR
; Build the SVC Stack
; Set the management mode Stack
MSR cpsr_c, #0xd3
LDR sp, stacksvc
; Build the IRQ Stack
; Set the interrupt mode Stack
MSR cpsr_c, #0xd2
LDR sp, stackirq
; Build the FIQ Stack
; Set the stack of the fast interrupt mode
MSR cpsr_c, #0xd1
LDR sp, stackfiq
; Build the dataabort Stack
; Set the abort mode Stack
MSR cpsr_c, #0xd7
LDR sp, stackabt
; Build the UDF Stack
; Set the undefined mode Stack
MSR cpsr_c, # 0xdb
LDR sp, stackund
; Build the Sys Stack
; Set the system mode Stack
MSR cpsr_c, # 0xdf; change to 0x5d to enable IRQ interruption
LDR sp, = stackusr

MoV PC, R0; subroutine return

V. Target Board Initialization
Void targetresetinit (void)
{
// Set the memory ing mode, which must be set based on hardware. Here is the setting in ads1.2.
# Ifdef _ debug
Memmap = 0x3; // remap
# Endif

# Ifdef _ out_chip
Memmap = 0x3; // remap
# Endif

# Ifdef _ in_chip
Memmap = 0x1; // remap
# Endif

/* Set the clock of each part of the system */
Pllcon = 1;
# If (fpclk/(fcclk/4) = 1
Vpbdiv = 0;
# Endif
# If (fpclk/(fcclk/4) = 2
Vpbdiv = 2;
# Endif
# If (fpclk/(fcclk/4) = 4
Vpbdiv = 1;
# Endif

# If (fcco/fcclk) = 2
Pllcfg = (fcclk/fosc)-1) | (0 <5 );
# Endif
# If (fcco/fcclk) = 4
Pllcfg = (fcclk/fosc)-1) | (1 <5 );
# Endif
# If (fcco/fcclk) = 8
Pllcfg = (fcclk/fosc)-1) | (2 <5 );
# Endif
# If (fcco/fcclk) = 16
Pllcfg = (fcclk/fosc)-1) | (3 <5 );
# Endif
Pllfeed = 0xaa;
Pllfeed = 0x55;
While (pllstat & (1 <10) = 0 );
Pllcon = 3; // set the PLL control register according to the specific setting method
Pllfeed = 0xaa;
Pllfeed = 0x55;

/* Set the memory acceleration module */
Mamcr = 0;
# If fcclk <20000000
Mamtim = 1;
# Else
# If fcclk <40000000
Mamtim = 2;
# Else
Mamtim = 3;
# Endif
# Endif
Mamcr = 2;

/* Initialize Vic */
/* Initialize Vic */
Vicintenclr = 0 xffffffff;
Vicvectaddr = 0;
Vicintselect = 0;

}

Vi. Others

; /*************************************** **************************************** **************************
; ** Function name: _ user_initial_stackheap
; ** Descriptions Function Description: Initial the function library stacks and heaps, can not deleted!
; ** The database function initialization heap and stack cannot be deleted.
; ** Input parameters: reference by function library reference library function manual
; ** Output returned value: reference by function library reference library function manual
; ** Used global variables global variable: none
; ** Calling modules call module: none
; **************************************** **************************************** ************************/
_ User_initial_stackheap
LDR r0, = bottom_of_heap
; LDR R1, = stackusr
LDR R2, = top_of_heap
LDR R3, = bottom_of_stacks
MoV PC, LR

Stacksvc DCD svcstackspace + (svc_stack_le1_1) * 4
Stackirq DCD irqstackspace + (irq_stack_le1_1) * 4
Stackfiq DCD fiqstackspace + (fiq_stack_le1_1) * 4
Stackabt DCD abtstackspace + (abt_stack_le1_1) * 4
Stackund DCD undtstackspace + (und_stack_le1_1) * 4

; /*************************************** **************************************** **************************
; ** Function name: crpdata
; ** Descriptions Function Description: encrypt the chip
; ** Input parameters: None
; ** Output of returned value: none
; ** Used global variables global variable: none
; ** Calling modules call module: none
; **************************************** **************************************** ************************/
If: Def: en_crp
If.> = 0x1fc
Info 1, "/nthe data at 0x000001fc must be 0x87654321./nplease delete some source before this line ."
Endif
Crpdata
While. <0x1fc
NOP
Wend
Crpdata1
DCD 0x87654321;/* when the data is 0x87654321, user code be protected. When this number is 0x87654321, the user program is protected */
Endif

;/* Allocate stack space */
Area mystacks, Data, noinit, align = 2
Svcstackspace space svc_stack_legth * 4; stack spaces for Administration Mode Management Mode stack space
Irqstackspace space irq_stack_legth * 4; stack spaces for interrupt request mode interrupt mode stack space
Fiqstackspace space fiq_stack_legth * 4; stack spaces for fast interrupt request Mode Fast interrupt mode stack space
Abtstackspace space abt_stack_legth * 4; stack spaces for suspend mode stop mode stack space
Undtstackspace space und_stack_legth * 4; stack spaces for undefined mode Stack

Area heap, Data, noinit
Bottom_of_heap space 1

Area stackbottom, Data, noinit
Bottom_of_stacks space 1

Area heaptop, Data, noinit
Top_of_heap

Area stacks, Data, noinit
Stackusr

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.