mini2440 裸机编程 -led

  又重新做回了嵌入式,想把以前学到的东西从头复习一下。首先从裸机编程开始。

本系列使用的硬件环境是友善之臂的 mini2440,百问网的OpenJtag,所有程序在 linux gcc下编译, 具体硬件设置  软件环境搭建可见openjtag 文档:

编译器使用友善之臂的 4.4.3 。编译器配置 /etc/profile:


[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. PATH="$PATH:/opt/FriendlyARM/toolschain/4.4.3/bin"  
  2. export PATH  
     注意如果在 /etc/provile 里面修改了编译器 之后,只是 source /etc/profile 还是不够的,无法调整所用编译器路径。 正确做法是 先 source /etc/environment  然后再 source /etc/profile


第一个程序是 led 灯控制:

开头 汇编 文件:

[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. @******************************************************************************  
  2. @ File£ºcrt0.S  
  3. @ ¹¦ÄÜ£ºÍ¨¹ýËüתÈëC³ÌÐò  
  4. @******************************************************************************  
  5.   
  6. #define  PXT                            0x12  
  7.   
  8. .text  
  9. .global _start  
  10. _start:  
  11.             ldr     r0, =0x53000000     @ disable WATCHDOG  
  12.             mov     r1, #0x0                       
  13.             str   r1, [r0]              @   
  14.   
  15.             ldr     sp, =1024*4         @ stack pointer point to 4K  
  16.   
  17.             bl      main                  
  18. halt_loop:  
  19.             b       halt_loop  

主函数文件:

  1. #define GPBCON      (*(volatile unsigned long *)0x56000010)  
  2. #define GPBDAT      (*(volatile unsigned long *)0x56000014)  
  3.   
  4. #define GPB5_out    (1<<(5*2))  
  5. #define GPB6_out    (1<<(6*2))  
  6. #define GPB7_out    (1<<(7*2))  
  7. #define GPB8_out    (1<<(8*2))  
  8.   
  9. void  wait(unsigned long dly)  
  10. {  
  11.     for(; dly > 0; dly--);  
  12. }  
  13.   
  14. int main(void)  
  15. {  
  16.     unsigned long i = 0;  
  17.       
  18.     GPBCON = GPB5_out|GPB6_out|GPB7_out|GPB8_out;     
  19.   
  20.     while(1){  
  21.         wait(100000);  
  22.         GPBDAT = (1<< ( (i%4) + 5) );   
  23.         if(++i == 16)  
  24.             i = 0;  
  25.     }  
  26.   
  27.     return 0;  
  28. }  

这个程序实现的功能是点亮led 灯,并实现流水效果。其中高电平熄灭 led 灯。


链接脚本把两个文件链接成一个独立的二进制文件: ( 使用开始的 4K 字节内存)


[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. SECTIONS {  
  2.         . = 0x00000000;  
  3.         .text          :   { *(.text) }  
  4.         .rodata ALIGN(4) : {*(.rodata)}  
  5.         .data ALIGN(4) : { *(.data) }  
  6.         .bss ALIGN(4)  : { *(.bss)  *(COMMON) }  
  7. }  

Makefile 脚本

[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. CFLAGS  := -Wall -Wstrict-prototypes -g -fomit-frame-pointer -ffreestanding  
  2. all : crt0.S  leds.c  
  3.         arm-linux-gcc $(CFLAGS) -c -o crt0.o crt0.S  
  4.         arm-linux-gcc $(CFLAGS) -c -o leds.o leds.c  
  5.         arm-linux-ld -Tleds.lds  crt0.o leds.o -o leds_elf  
  6.         arm-linux-objcopy -O binary -S leds_elf leds.bin  
  7.         arm-linux-objdump -D -m arm  leds_elf > leds.dis  
  8. clean:  
  9.         rm -f   leds.dis leds.bin leds_elf *.o  


首先运行脚本openocd.sh

[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. openocd -f /etc/openocd/interface/openjtag.cfg -f /etc/openocd/target/samsung_s3c2440.cfg  
保持开启不退出状态。

然后另开启一个终端启动 elf 文件调试

[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. arm-linux-gdb -x gdb.init leds_elf  

其中 gdb.init 文件内容:

[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. target remote 127.0.0.1:3333  
  2. monitor halt  
  3. monitor arm920t cp15 2 0  
  4. monitor step   
  5. load  

此时就可以像调试普通程序一样调试这个裸机程序。


工程文件地址:




有一点特别需要注意:

   汇编语言文件开头的位置标号必须是 _start:  如果是其它的则会导致 OpenJtag 无法识别,load 之后 用si指令执行导致 PC指针到未知的位置!!。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值