因为要移植一个微内核到6410的体系上,我手里正好有一块友坚恒天的UT6410BV04的开发板,在这个板子上有D4、D5、D7、D8四个LED灯,经过查看原理图,知道它们分别对应GPM0~GPM3,查看Datasheet,知道GPMCON的物理地址是0x7F008820,GPMDAT的地址是0x7F008824,下面编写程序控制LED灯亮灭,程序有两种写法,一是汇编实现,一是C实现:
一、汇编实现:
# touch led_on.S
# gedit led_on.S
.text
.global _start
_start:
ldr r0, =0x7f008820
mov r1, #0x00000001
str r1, [r0]
ldr r0, =0x7f008824
mov r1, #0x00000000
str r1, [r0]
main_loop:
b main_loop
Makefile的内容:
CROSS_COMPILE := /usr/local/arm/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
led_on.bin : led_on.S
$(CC) -g -c -o led_on.o led_on.S
$(LD) -Ttext 0x0000000 -g led_on.o -o led_on_elf
$(OBJCOPY) -O binary -S led_on_elf led_on.bin
clean:
rm -f led_on.bin led_on_elf *.o
二、C实现
ARM Linux C环境需要crt0.S构建运行环境,诸如SP之类的,因此先建立crt0.S,下面是crt0.S的内容:
.text
.global _start
_start:
ldr r0, =0x53000000
mov r1, #0x0
str r1, [r0]
ldr sp, =1024*4
bl main
halt_loop:
b halt_loop
然后建立C文件:led_on_c.c,其内容为:
#define GPMCON (*(volatile unsigned long *)0x7F008820)
#define GPMDAT (*(volatile unsigned long *)0x7F008824)
int main()
{
GPMCON = 0x00000101;
GPMDAT = 0x00000000;
return 0;
}
下面是Makefile:
CROSS_COMPILE := /usr/local/arm/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
led_on_c.bin : crt0.S led_on_c.c
$(CC) -g -c -o crt0.o crt0.S
$(CC) -g -c -o led_on_c.o led_on_c.c
$(LD) -Ttext 0x0000000 -g crt0.o led_on_c.o -o led_on_c_elf
$(OBJCOPY) -O binary -S led_on_c_elf led_on_c.bin
$(OBJDUMP) -D -m arm led_on_c_elf > led_on_c.dis
clean:
rm -f led_on_c.dis led_on_c.bin led_on_c_elf *.o
然后编译之后,使用dnw,开发板加电,在uboot下使用usb OTG下载程序到开发板内存0xc0008000,然后:
go 0xc0008000启动程序运行,可以看到LED灯根据控制变亮或者灭掉。
转自:http://hi.baidu.com/ylfan2006/blog/item/792e53d01be1bc27960a1623.html