Файл: Embedded system development and labs for ARM (R. Muresan, 2005).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 13.06.2025
Просмотров: 2356
Скачиваний: 5
Embedded Systems Development and Labs; The English Edition
processor from a known state and renders all other pending exceptions irrelevant. The most complex scenario is where a FIQ, an IRQ and a third exception (which is not Reset) happen simultaneously. FIQ has higher priority than IRQ and also masks it out, so the IRQ will be ignored until the FIQ handler explicitly enables IRQ or returns to the user code. If the third exception is a data abort, the processor will enter the data abort handler and then immediately enter the FIQ handler, since data abort entry does not mask FIQs out. The data abort is remembered in the return path and will be processed when the FIQ handler returns. If the third exception is not a data abort, the FIQ will be entered immediately. When the FIQ and IRQ have both completed, the program returns to the instruction which generated the third exception, and in all the remaining cases the exception will recur and be handled accordingly.
From the above, the reset entry is the start point of all the programs. So the first executed line of the program will be executed at 0x00000000. Generally, the following code is used:
# --- Setup interrupt / exception vectors
B |
Reset_Handler |
Undefined_Handler: |
|
B |
Undefined_Handler |
SWI_Handler: |
|
B |
SWI_Handler |
Prefetch_Handler: |
|
B |
Prefetch_Handler |
Abort_Handler: |
|
B |
Abort_Handler |
NOP |
/* Reserved vector */ |
IRQ_Handler: |
|
B |
IRQ_Handler |
FIQ_Handler: |
|
B |
FIQ_Handler |
Reset_Handler: |
|
LDR |
sp, =0x00002000 |
… |
|
2. Linker Script
The Linker Script controls all the linking process. The Linker Script is written using the so called link command language. The main functions of the linker scripts control how to place the programs to the output file and control how to locate the output file in the memory. If needed, the linker script can implement other functions. Most of the linker script files are simple. The simplest linker file has only one command line called SECTIONS. The SECTION command controls the memory distribution of the output file (code).
SECTION command is powerful. For example, consider a program that consists of consists of code, initialized data and un-initialized data are placed in “.text”, “.data” and “.bss” sections. The code of these sections needs to be placed at addresses 0x10000 and 0x8000000, respectively. A simple linker script that performs the above
110
Embedded Systems Development and Labs; The English Edition
tasks is: SECTIONS
{
. = 0x1000;
.text : { *(.text) }
. =0x8000000
.data : { *(.data) }
.bss : { *(.bss) }
}
The starts with the key word SECTIONS. Next is the body of the command encompassed by “{“ and “}”. Within the command The first line inside the SECTIONS command sets the value of the special symbol “.” which is the location counter. If you do not specify the address of an output section in some other way (other ways are described later), the address is set from the current value of the location counter. The location counter is then incremented by the size of the output section. At the start of the SECTIONS command, the location counter has the value 0.
The second line defines an output section “.text”. The colon “:” is required syntax that may be ignored for now. Within the brackets after the output section name, you list the names of the input section that should be placed into this output section. The “*” is a wildcard which matches any file name. The expression *(.text) means all .text input sections of all input files. Since the location counter is 0x10000 when the output section .text is defined, the linker will set the address of the .text section in the output file to be 0x10000.
The remaining lines define the .data and .bss section in the output file. The linker will place the .data output section at address 0x8000000. After the linker places the .data output section, the value of the location counter will be 0x8000000 plus the size of the .data output section. The effect is that the linker will place the .bss output section immediately after the .data output section in memory.
The linker will ensure that each output section has the required alignment, by increasing the location counter if necessary. In this example, the specified addresses for the .text and .data section will probably satisfy any alignment constraints, but the linker may have to create a small gap between the .data and .bss sections.
3. Embedded Assembly Code
The GCC support most of the basic assembly code. The following example shows how the assembly code can be embedded in a C program. An assembly language notation will be inserted to the output stream when the compiler meets this statement.
Example: A basic embedded assembly code.
__asm__(“mov r1, r2”)
3.6.5 Operation Steps
1)Refer to the former Labs and create a new project named c2.
2)Edit the new source files c2.c, init.s and script file ldscript. Add them to the project.
3)Refer to the former Labs and finish the standard settings. Note: In the Linker page shown in Figure 3-11 the ldscript file is used. For the functions of this file please refer to Section 3.6.1.
111
Embedded Systems Development and Labs; The English Edition
Figure 3-11 Embest IDE Linker Script File Settings
Because the concept of initialization file in introduced, the entry file init.o should be specified as shown in Figure 3-12. Please note that the init.o code must be downloaded at address 0x0. The other programs of the project will be automatically downloaded to consecutive address locations. The init.s program initializes the SP register (VERY IMPORTANT !!!) and jumps to the _main () function of the C program.
4)Refer to the former Labs and compile the project. Set the Linker page options as explained in Chapter 2. Also, Figures 3-12a to 3-12d show the correct settings for this project. Build the c2 project. Set the debug options.
5)Download the program, open the Memory/Register/Watch/Variable windows, single step execute the program and analyze the results. In the Watch window, input the variable I that need to be watched. Specially watch and record the changes of the variable I.
6)Combined with the contents of the Lab and related technology materials, watch the program run. Get a deeper understanding of the usage of the registers in different modes.
7)After understanding and mastering the lab, finish the Lab exercises.
112
Embedded Systems Development and Labs; The English Edition
Figure 3-12 Embest IDE Linker Settings
Figure 3-12a. Remote page setting (first step).
113
Embedded Systems Development and Labs; The English Edition
Figure 3-12b. General options for compiler and linker settings. (Note: set the compiler options and do the compile command before you set the linker options. The Linker script file is set to be ldscript)
Figure 3-12c. Image Entry Option and Code Generation Options for the Linker page. (Note: the init.o is the select entry file. The c2.o file will be loaded at the end of the init.o code)
Figure 3-12d. The Debug page option settings. (Note: the download address is set to 0x0 since the init.o starts at
114
Embedded Systems Development and Labs; The English Edition
address 0x0 in the memory)
Figure 3-13 Embest IDE Call Stack Window
3.6.6 Sample Programs
1. c2.c source code void _nop_(){ __asm("mov r0,r0");
}
//------------------------------------------------------------------------------------------------ |
|
//Function Name: delay |
|
//------------------------------------------------------------------------------------------------ |
|
void delay(void) |
//delay |
{ |
|
int i; |
|
for(i=0;i<=10;i++) |
|
{ |
|
_nop_(); |
|
} |
|
} |
|
void delay10(void) |
115
Embedded Systems Development and Labs; The English Edition
{
int i; for(i=0;i<=10;i++)
{
delay();
}
} |
|
//*---------------------------------------------------------------------------- |
|
//* Function Name |
: _start |
//* Input Parameters |
: none |
//* Output Parameters |
: none |
//*---------------------------------------------------------------------------- |
|
__main() |
|
{ |
|
int i=5; |
|
for(;;) |
|
{ |
|
delay10(); |
|
} |
|
} |
3. init.s source code
# *******************************************************
# * NAME |
: 44BINIT.S |
* |
# * Version : 10.April.2000 |
* |
|
# * Description: |
* |
|
# * C start up codes |
* |
|
# * Configure memory, Initialize ISR ,stacks |
* |
|
# * Initialize C-variables |
* |
|
# * Fill zeros into zero-initialized C-variables |
* |
|
#*******************************************************
#Program Entry
#.arm
.global _start
.text _start:
# --- Setup interrupt / exception vectors B Reset_Handler
Undefined_Handler:
B Undefined_Handler
116
Embedded Systems Development and Labs; The English Edition |
|
SWI_Handler: |
|
B |
SWI_Handler |
Prefetch_Handler: |
|
B |
Prefetch_Handler |
Abort_Handler: |
|
B |
Abort_Handler |
NOP |
/* Reserved vector */ |
IRQ_Handler: |
|
B |
IRQ_Handler |
FIQ_Handler: |
|
B |
FIQ_Handler |
Reset_Handler: |
|
LDR |
sp, =0x00002000 |
#------------------------------------------------------------------------------
#- Branch on C code Main function (with interworking)
#----------------------------------------------------
#- Branch must be performed by an interworking call as either an ARM or Thumb
#- main C function must be supported. This makes the code not position-
#- independant. A Branch with link would generate errors
#------------------------------------------------------------------------------ |
|
.extern |
__main |
ldr |
r0, = __main |
mov |
lr, pc |
bx |
r0 |
#------------------------------------------------------------------------------ |
|
#- Loop for ever |
|
#--------------- |
#- End of application. Normally, never occur.
#- Could jump on Software Reset ( B 0x0 ).
#------------------------------------------------------------------------------
End: |
|
b |
End |
.end |
3. ldscript source code
SECTIONS
117