原理图中表示了芯片控制LED的硬件电路以及芯片的引脚与LED如何连接。
具体:如何让GPF4输出高低电平?
设置GPFCON
寄存器的第[9:8]位=0b01
。
GPFDAT的第4位为0-低电平,1-高电平。(注:corresponding,相应的)
@ brief: 点亮连接在GPF4的LED
@ author: mculover666
@ date: 2019/3/1
.text
.global _start
_start:
@ 设置GPFCON寄存器,GPF4为输出模式
LDR R0,=0x56000050
LDR R1,=0x0100
STR R1,[R0]
@ 设置GPFDAT寄存器,GPF4输出低电平
LDR R0,=0X56000054
LDR R1,=0
STR R1,[R0]
@程序暂停
halt:
B halt
arm-linux-gcc -c led_on.s -Wall -o led_on.o
arm-linux-ld -Ttext 0 led_on.o -o led_on.elf
arm-linux-objcopy -O binary -S led_on.elf led_on.bin
整个编译步骤可编写为一个makefile:
TARGET = led_on
#输出所有warning
CFLAGS = -Wall
$(TARGET).bin:$(TARGET).elf
arm-linux-objcopy -O binary -S $(TARGET).elf $(TARGET).bin
$(TARGET).elf:$(TARGET).o
arm-linux-ld -Ttext 0 $(TARGET).o -o $(TARGET).elf
$(TARGET).o:$(TARGET).s
arm-linux-gcc -c $(TARGET).s $(CFLAGS) -o $(TARGET).o
clean:
rm -rf *.o *.elf *.bin
download_to_nand:
#下载到nand flash
oflash 0 1 0 0 0 $(TARGET).bin
使用oflash烧写bin文件到Nand Flash的0地址:
oflash 0 1 0 0 0 .\led_on.bin
启动文件start.s:初始化C语言运行环境,引入C程序
@ brief: S3C2440启动文件
@ author: mculover666
.text
.global _start
_start:
@ 关闭看门狗
LDR R0,=0x53000000
MOV R1,#0
STR R1,[R0]
@ 设置栈顶指针SP(从Nand启动)
LDR SP,=4096
@ 调用main函数,保存返回地址,转入C程序
BL main
@ main函数返回,程序暂停
halt:
B halt
int main(void)
{
/* 设置GPFCON寄存器,配置GPF4引脚为输出模式 */
*(unsigned int *)0x56000050 &= ~(3<<(2*4));
*(unsigned int *)0x56000050 |= 1<<(2*4);
/* 设置GPFDAT寄存器,GPF4输出低电平,点亮LED */
*(unsigned int *)0x56000054 &= ~(1<<4);
/* 程序暂停 */
while(1);
}
TARGET = led_on
CFLAGS = -Wall #输出所有warning
$(TARGET).bin:$(TARGET).elf
arm-linux-objcopy -O binary -S $(TARGET).elf $(TARGET).bin
#注意:启动文件必须第一个链接
$(TARGET).elf:start.o $(TARGET).o
arm-linux-ld -Ttext 0 start.o $(TARGET).o -o $(TARGET).elf
$(TARGET).o:$(TARGET).c
arm-linux-gcc -c $(TARGET).c $(CFLAGS) -o $(TARGET).o
start.o:start.s
arm-linux-gcc -c start.s $(CFLAGS) -o start.o
clean:
rm -rf *.o *.elf *.bin
download_to_nand:
#下载到nand flash
oflash 0 1 0 0 0 $(TARGET).bin
oflash 0 1 0 0 0 .\led_on.bin
int main(void)
{
volatile int GPF0_state;
/* 设置GPFCON寄存器 */
//设置GPF4为输出
*(unsigned int *)0x56000050 &= ~(3<<(2*4));
*(unsigned int *)0x56000050 |= (1<<(2*4));
//设置GPF0为输入
*(unsigned int *)0x56000050 &= ~(3<<(2*0));
/* 程序死循环检测按键 */
while(1)
{
/* 读取GPFDAT寄存器 */
GPF0_state = *(unsigned int *)0x56000054;
/* 检测GPF4引脚的状态 */
if(GPF0_state & 0x01)
{
//按键未按下,上拉电阻拉为高电平,熄灭LED
*(unsigned int *)0x56000054 |= (1<<4);
}
else
{
//按键按下,低电平,点亮LED
*(unsigned int *)0x56000054 &= ~(1<<4);
}
}
}
TARGET = key_scan
CFLAGS = -Wall #输出所有warning
$(TARGET).bin:$(TARGET).elf
arm-linux-objcopy -O binary -S $(TARGET).elf $(TARGET).bin
#注意:启动文件必须第一个链接
$(TARGET).elf:start.o $(TARGET).o
arm-linux-ld -Ttext 0 start.o $(TARGET).o -o $(TARGET).elf
$(TARGET).o:$(TARGET).c
arm-linux-gcc -c $(TARGET).c $(CFLAGS) -o $(TARGET).o
start.o:start.s
arm-linux-gcc -c start.s $(CFLAGS) -o start.o
clean:
rm -rf *.o *.elf *.bin
download_to_nand:
#下载到nand flash
oflash 0 1 0 0 0 $(TARGET).bin
oflash 0 1 0 0 0 .\led_on.bin
通过这两个实验: