裸机程序二:GPIO点亮led灯 c语言

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/heyuqian_csdn/article/details/53002717

开发板:  JZ2440 V3 + EasyOpenJTAG

参考资料:《CPU三星S3C2440A芯片手册.pdf

                   《第2课 GPIO实验.avi》


我们在main函数中点亮led,但是c语言之行的第一句代码并不在main中,需要在之前加上一些必须的初始化代码,这部分代码称之为启动文件

启动文件的作用主要分为硬件相关和软件相关初始化两部份,如下:




代码如下:

$ ls
led_on.c  Makefile  setup.S


启动文件放在汇编代码setup.S中,首先关闭看门狗,设置堆栈指针,然后调用main函数
$ cat setup.S 
.text
.global _start
_start:
	ldr r0,=0x53000000 @看门狗寄存器地址
	mov r1,#0x0
	str r1,[r0]        @关闭看门狗

	ldr sp,=4*1024     @设置堆栈指针

	bl main            @调用main函数
halt_loop:
	b halt_loop

$ cat led_on.c 
#define GPFCON	(*(volatile unsigned long*)0x56000050)
#define GPFDAT  (*(volatile unsigned long*)0x56000054)

void main()
{
	GPFCON = 0x00001500;
	GPFDAT = 0x000000ff;

	return; 
}
 $ cat Makefile 
led_on.bin:led_on.c setup.S
	arm-linux-gcc -c -o led_on.o led_on.c
	arm-linux-gcc -c -o setup.o setup.S

	arm-linux-ld -Ttext 0x00000000 -g led_on.o setup.o -o led_on_elf
	arm-linux-objcopy -O binary -S led_on_elf led_on.bin

clean:
	rm -rf *.o led_on_elf led_on.bin

板子初始led1是点亮的,修改上述代码关闭led1,运行后发现,led1熄灭后,led1又被点亮了,此时看门狗已经正确关闭了

反汇编arm-linux-objdump -D -m arm led_on_elf > led_on_dis,发现0x0位置放的是main函数:

原因是上述Makefile链接时将led_on.o写到setup.o前面了,默认是按照输入文件的先后次序链接文件的,需要将setup.o放在最前面

$ cat led_on_dis

led_on_elf:     file format elf32-littlearm

Disassembly of section .text:

00000000 <main>:
   0:	e1a0c00d 	mov	ip, sp
   4:	e92dd800 	stmdb	sp!, {fp, ip, lr, pc}
   8:	e24cb004 	sub	fp, ip, #4	; 0x4
   c:	e3a03456 	mov	r3, #1442840576	; 0x56000000
  10:	e2833050 	add	r3, r3, #80	; 0x50
  14:	e3a02c15 	mov	r2, #5376	; 0x1500
  18:	e5832000 	str	r2, [r3]
  1c:	e3a03456 	mov	r3, #1442840576	; 0x56000000
  20:	e2833054 	add	r3, r3, #84	; 0x54
  24:	e3a020ff 	mov	r2, #255	; 0xff
  28:	e5832000 	str	r2, [r3]
  2c:	e89da800 	ldmia	sp, {fp, sp, pc}

00000030 <_start>:
  30:	e3a00453 	mov	r0, #1392508928	; 0x53000000
  34:	e3a01000 	mov	r1, #0	; 0x0
  38:	e5801000 	str	r1, [r0]
  3c:	e3a0da01 	mov	sp, #4096	; 0x1000
  40:	ebffffee 	bl	0 <main>

00000044 <halt_loop>:
  44:	eafffffe 	b	44 <halt_loop>
Disassembly of section .comment:

00000000 <.comment>:
   0:	43434700 	cmpmi	r3, #0	; 0x0
   4:	4728203a 	undefined
   8:	2029554e 	eorcs	r5, r9, lr, asr #10
   c:	2e342e33 	mrccs	14, 1, r2, cr4, cr3, {1}
  10:	Address 0x10 is out of bounds.


猜你喜欢

转载自blog.csdn.net/heyuqian_csdn/article/details/53002717