linux 裸机开发arm程序
@******************************************************************************
@ File:start.s
@ 功能:通过它转入C程序
@******************************************************************************
.text
.global _start
_start:
ldr r0, =0x53000000 @ WATCHDOG寄存器地址
mov r1, #0x0
str r1, [r0] @ 写入0,禁止WATCHDOG,否则CPU会不断重启
ldr sp, =1024*4 @ 设置堆栈,注意:不能大于4k, 因为现在可用的内存只有4K
@ nand flash中的代码在复位后会移到内部ram中,此ram只有4K
bl main @ 调用C程序中的main函数
halt_loop:
b halt_loop
led.c文件
#define GPQCON (*(volatile unsigned long *) 0x7F008180)
#define GPQDAT (*(volatile unsigned long *) 0x7F008184)
void msDelay(int time)
{
volatile unsigned int i,j;
for(i = 0; i < 2000000; i++)
for(j=0; j<time; j++);
}
int main(void)
{
GPQCON &= ~( (0x3<<(2*2)) | (0x3<<(3*2)) | (0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)) );
GPQCON |= ( (0x1<<(2*2)) | (0x1<<(3*2)) | (0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)) );
while(1)
{
GPQDAT |= ( (1<<2) | (1<<3) | (1<<4) | (1<<5) | (1<<6) );
msDelay(2);
GPQDAT &= ~( (1<<2) | (1<<3) | (1<<4) | (1<<5) | (1<<6) );
msDelay(2);
}
return 0;
}
leds.lds文件
SECTIONS {
. = 0x00;
.text : { *(.text) }
.rodata ALIGN(4) : {*(.rodata)}
.data ALIGN(4) : { *(.data) }
.bss ALIGN(4) : { *(.bss) *(COMMON) }
}
makefile文件
all: start.s leds.c
arm-linux-gcc -o start.o start.s -c
arm-linux-gcc -o leds.o leds.c -c
arm-linux-ld -Tleds.lds start.o leds.o -o leds.elf
arm-linux-objcopy -O binary -S leds.elf leds.bin
arm-linux-objdump -D -m arm leds.elf > leds.dis
clean:
rm -f leds.dis leds.bin leds.elf *.o
CFLAGS := -Wall -Wstrict-prototypes -g -fomit-frame-pointer -ffreestanding
all : crt0.S leds.c
arm-linux-gcc $(CFLAGS) -c -o crt0.o crt0.S
arm-linux-gcc $(CFLAGS) -c -o leds.o leds.c
arm-linux-ld -Tleds.lds crt0.o leds.o -o leds_elf
arm-linux-objcopy -O binary -S leds_elf leds.bin
arm-linux-objdump -D -m arm leds_elf > leds.dis
clean:
rm -f leds.dis leds.bin leds_elf *.o
led.bin: start.o
arm-linux-ld -Ttext 0 -o leds.elf start.o
arm-linux-objcopy -O binary -S leds.elf leds.bin
arm-linux-objdump -D -m arm leds.elf > leds.dis
start.o: start.s
arm-linux-gcc -o start.o start.s -c
clean:
rm -f leds.dis leds.bin leds.elf *.o