ARM裸机——002C语言实现流水灯

三个LED引脚为GPF4/5/6,点电平亮,高电平灭;

led.c:


void delay(volatile int d)
{
	while(d--);
}

int main()
{
	unsigned int *pGPFCON = (unsigned int *)0x56000050;
	unsigned int *pGPFDAT = (unsigned int *)0x56000054;
	int val=7; /* val:0b000,0b111 */
	
	/* 设置GPFCON让GPF4/5/6配置为输出引脚 */
	*pGPFCON &= ~((3<<8)|(3<<10)|(3<<12)) ; //清零
	*pGPFCON |=  ((1<<8)|(1<<10)|(1<<12));
	
	/*循环点亮*/
	while(1)
	{	
		switch(val)
		{
			case 7:
				break;
			case 6:
				break;
			case 5:
				break;
			case 3:
				break;
			default:
				break;
		}
		*pGPFDAT &= ~(7<<4);
		*pGPFDAT |= (val<<4);
		delay(100000);
		if(val == 5)
		{
			val = 3;
		}
		else if(val == 3)
		{
			val =7;
		}
		else
			val--;
	}
	
	return 0;
}

start.S


.text
.global _start

_start:

	/* 关闭看门狗 */
	ldr r0, =0x53000000
	ldr r1, =0
	str r1, [r0]

	/* 设置内存: sp 栈 */
	/* 分辨是nor/nand启动
	 * 写0到0地址, 再读出来
	 * 如果得到0, 表示0地址上的内容被修改了, 它对应ram, 这就是nand启动
	 * 否则就是nor启动
	 */
	mov r1, #0
	ldr r0, [r1] /* 读出原来的值备份 */
	str r1, [r1] /* 0->[0] */ 
	ldr r2, [r1] /* r2=[0] */
	cmp r1, r2   /* r1==r2? 如果相等表示是NAND启动 */
	ldr sp, =0x40000000+4096 /* 先假设是nor启动 */
	moveq sp, #4096  /* nand启动 */
	streq r0, [r1]   /* 恢复原来的值 */
	

	bl main

halt:
	b halt
	

 

Markfile

all:
	arm-linux-gcc -c -o led.o led.c
	arm-linux-gcc -c -o start.o start.S
	arm-linux-ld -Ttext 0 start.o led.o -o led.elf
	arm-linux-objcopy -O binary -S led.elf led.bin
	arm-linux-objdump -D led.elf > led.dis
clean:
	rm *.bin *.o *.elf *.dis
	

 

你可能感兴趣的:(嵌入式linux学习笔记)