Файл: Real-time processing with the Philips LPC ARM mcu using GCC and uCOS II RTOS (D.W. Hawkins, 2006).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 480
Скачиваний: 2
AR1803 |
May 10, 2006 |
/ −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Constants
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
/
/ LED c o n t r o l |
r e g i s t e r s |
/ |
||
IOSET1 : |
. word |
0 xE0028014 |
||
IODIR1 : |
. word |
0 xE0028018 |
||
IOCLR1: |
. word |
0xE002801C |
||
IODIR1 |
VALUE : |
. word |
0x00FF0000 |
|
IOSET1 |
VALUE : |
. word |
0 x00550000 |
|
. end
Example 1 is a short assembler program that sets up basic exception vectors (they drop into an infinite loop), and then turns the MCB2130 even LEDs on (where indexing is LED[7:0]) and the odd LEDs o (a high value on an I/O pin turns an LED on). The example does not setup stacks (since we do not need them). The successful compilation and download of this example checks the compiler and linker setup.
A linker script is used to define the memory map of a processor. If GCC is not passed a linker script as part of the command line arguments, a default linker script is used. The linker script can be displayed by passing -Wl,--verbose to arm-elf-gcc or --verbose to arm-elf-ld. The default linker script defines an entry point symbol called _start and loads to a default address of 0x8000. The example assembler program uses the expected entry symbol _start, and since the Philips LPC microcontroller reset vector is address 0, a command line option, -Ttext=0, is needed to link the .text section to address 0 (note that this just overrides the linker script .text section start address, it does not replace the linker script). The example needs to setup the exception vector table correctly, as the Philips LPC microcontroller expects the reserved exception vector (the old 26-bit ARM processor address exception) to contain a checksum word. The Philips FlashUtils downloader fills in this checksum.
The example is compiled using
arm-elf-gcc -mcpu=arm7tdmi -c ex1.s
and is linked using
arm-elf-ld -Ttext=0 ex1.o -o ex1.elf
The linker -T option links the code starting at physical address 0.
The compilation and link step can be combined using
arm-elf-gcc -mcpu=arm7tdmi -nostartfiles -Ttext=0 ex1.s -o ex1.elf
The -nostartfiles option tells the compiler not to use its startup assembler routine. The executable can be disassembled using
arm-elf-objdump -d ex1.elf
which simply shows the original source. The disassembled code covers addresses 0 through 54h (inclusive), i.e., 58h bytes = 88 bytes. The amount of Flash and SRAM needed by a program can be summarized using
arm-elf-size ex1.elf |
||||
text |
data |
bss |
dec |
hex filename |
88 |
0 |
0 |
88 |
58 ex1.elf |
7
AR1803 |
May 10, 2006 |
A key point to note about this example is that there is only a text section, i.e., the example will only use Flash RAM.
The FlashUtils downloader expects an Intel hex format file. This file is created using
arm-elf-objcopy -O ihex ex1.elf ex1.hex
Start LPC210x_ISP.exe and download the program; the even LEDs should be on, the odd o (LED[0] is closest to the corner of the board).
3.2Example 2: A simple C program
The following startup routine sets up exception vectors, with the reset vector jumping directly to the C coded main application
/ e x 2 |
s t a r t . s / |
|||||||||||||||||
. t e x t |
||||||||||||||||||
. arm |
||||||||||||||||||
. g l o b a l |
main |
|||||||||||||||||
. g l o b a l |
s t a r t |
|||||||||||||||||
s t a r t : |
||||||||||||||||||
/ Vectors (8 t o t a l ) / |
||||||||||||||||||
l d r |
pc , |
main |
addr |
/ r e s e t / |
||||||||||||||
l d r pc , l o o p |
a d d r |
/ u n defin ed i n s t r u c t i o n / |
||||||||||||||||
l d r pc , l o o p |
a d d r |
/ s o f t w a r e i n t e r r u p t / |
||||||||||||||||
l d r pc , l o o p |
a d d r |
/ p r e f e t c h a b o r t / |
||||||||||||||||
l d r pc , l o o p |
a d d r |
/ data a b o r t / |
||||||||||||||||
nop |
/ r e s e r v e d f o r t he b o o t l d r checksum / |
|||||||||||||||||
l d r |
pc , |
l o o p |
a d d r |
/ IRQ / |
||||||||||||||
l o o p |
l d r |
pc , |
l o o p |
a d d r |
/ FIQ / |
|||||||||||||
a d d r : |
. word |
l o o p |
||||||||||||||||
main |
addr : |
. word |
main |
|||||||||||||||
l o o p : |
b |
l o o p |
||||||||||||||||
. end |
||||||||||||||||||
The exception vectors are setup slightly di erently than those in example 1. Instead of branching to the loop address, they load the program counter with the address of the loop.
The main C code is
/ ex2 main . c /
#define IOSET1 ( ( ( volatile unsigned long ) 0 xE0028014 ) ) #define IODIR1 ( ( ( volatile unsigned long ) 0 xE0028018 ) ) #define IOCLR1 ( ( ( volatile unsigned long ) 0xE002801C ) )
int main ( void ) |
|
{ |
ou t pu t / |
/ Define t he LED pin s P1 . [ 1 6 . . 2 3 ] as |
|
IODIR1 = 0x00FF0000 ; |
|
/ Clear a l l pin s / |
|
IOCLR1 = 0x00FF0000 ; |
|
/ LED [ 7 : 0 ] ; even on ( high ) , odd o f f |
( low ) / |
8
AR1803 |
May 10, 2006 |
IOSET1 = 0 x00550000 ;
while ( 1 ) ; return 0 ;
}
Compilation and disassembly of just the main code using
arm-elf-gcc -mcpu=arm7tdmi -c ex2_main.c arm-elf-objdump -d ex2_main.o
will show assembly code that uses the stack pointer. However, a stack pointer is not setup by ex2_start.s, so instead the code should be compiled with optimization level 2, -O2, as that eliminates the stack references for this particular example. The application can be compiled using
arm-elf-gcc -O2 -mcpu=arm7tdmi -nostartfiles -Ttext=0 \ ex2_start.s ex2_main.c -o ex2.elf
The order of the files here is important, the startup code must come first. Disassembly of the code using
arm-elf-objdump -d ex2.elf
shows the startup code followed by the main code. The code covers addresses 0 through 48h (4Ch bytes). The size of the standard sections in the file are (all the sections can be viewed using arm-elf-objdump -h ex2.elf)
arm-elf-size ex2.elf |
||||
text |
data |
bss |
dec |
hex filename |
76 |
0 |
0 |
76 |
4c ex2.elf |
As with example 1, the code produces only a text section, so only Flash RAM is used.
Conversion of the elf file into hex format, and then download using FlashUtils produces the same result as example 1.
3.3Examples 3(a) and (b): C program stack setup
The LPC2138 contains 32Kbytes (i.e., 8000h) SRAM starting at address 40000000h. The ARM stack grows down, so the ex3_start.s file initializes the stack pointer to the end of SRAM, i.e., 40008000h, and then jumps to main
/ e x 3 s t a r t . s /
. t e x t |
||||
. arm |
||||
. g l o b a l |
main |
|||
. g l o b a l |
s t a r t |
|||
s t a r t : |
||||
/ Vectors (8 t o t a l ) / |
||||
b r e s e t |
/ r e s e t / |
|||
b l o o p |
/ u n defin ed i n s t r u c t i o n / |
|||
b l o o p |
/ s o f t w a r e i n t e r r u p t / |
|||
b l o o p |
/ p r e f e t c h a b o r t / |
|||
b l o o p |
/ data a b o r t / |
|||
nop |
/ |
r e s e r v e d f o r t he b o o t l o a d e r checksum / |
||
b l o o p |
/ |
IRQ / |
||
9
AR1803 |
May 10, 2006 |
|||||||||
b l o o p |
/ FIQ / |
|||||||||
/ Setup |
t he |
s t a c k |
p o i n t e r |
and then jump t o |
main / |
|||||
r e s e t : |
||||||||||
l d r sp , s t a c k |
a d d r |
|||||||||
b l |
main |
|||||||||
/ Catch |
r et u r n from |
main |
/ |
|||||||
l o o p : |
b |
l o o p |
||||||||
/ Constants / |
||||||||||
/ LPC SRAM |
s t a r t s |
at |
0x40000000 , and t h e r e |
i s 32Kb = 8000 h / |
||||||
s t a c k |
a d d r : |
. word |
0 x40008000 |
|||||||
. end |
||||||||||
The Example 3(a) main application turns on the MCB2130 LEDs as in the previous examples, but uses a function call. A function call requires the use of a stack (the usuage of the stack by the main code in the final executable is shown shortly).
/ ex3a main . c /
#include ” l e d . h”
int main ( void )
{
l e d i n i t ( ) ; l e d ( 0 x55 ) ; while ( 1 ) ; return 0 ;
}
Since the LED routines are used in multiple programs, they are placed in a separate files; a header
/ l e d . h |
/ |
|||||||
#ifndef |
LED |
H |
||||||
#define LED |
H |
|||||||
/ I n i t i a l i z e t he LEDs |
/ |
|||||||
void l e d |
i n i t ( ) ; |
|||||||
/ Control t he LEDs / |
||||||||
void l e d ( unsigned long |
v a l ) ; |
|||||||
/ Set LEDs / |
||||||||
void l e d |
s e t ( unsigned |
long |
s e t ) ; |
|||||
/ Clear LEDs / |
||||||||
void l e d |
c l r ( unsigned |
long |
c l r ) ; |
|||||
#endif
10