S3c2410裸板程序入门流水灯

初学arm,写了几个练习小程序,记录在此,道行低微,贻笑大方

包括: 流水灯, 单按键, pwm驱动蜂鸣器, 串口与PC通信

开发板s3c2410, 开发环境realview + h-jtag

废话少说,开始第一个程序,流水灯对于裸板程序开发的学习,就像hello world对于任何一门高级语言,新手第一课,呵呵。几个文件内容如下:

led.c 

 1 #include "s3c2410.h"
 2 #include "delay.h"
 3 
 4 
 5 
 6 void myblink(void)
 7 {
 8     delay(0);
 9     GPFCON = (GPFCON & ~(0xff<<8))|0x5500;
10 
11     while(1){
12         GPFDAT = 0x7f;
13         delay(1000);
14         GPFDAT = 0xbf;
15         delay(1000);
16         GPFDAT = 0xdf;
17         delay(1000);
18         GPFDAT = 0xef;
19         delay(1000);
20     }
21 }

通过GPFCON配置GPF4, 5, 6,7引脚为输出状态,通过GPFDAT往相应的端口写数据,注意这种位操作的方式。另外,还可以通过GPFUP寄存器配置引脚的上拉,本例未用到。

delay.c

 1 #include "delay.h"
 2 #include "s3c2410.h"
 3 
 4 
 5 static int delayLoopCount = 400;
 6 
 7 /*********************************************************************************************
 8 * name:     delay
 9 * func:     delay time
10 * para:     nTime -- input, nTime=0: nAdjust the delay function by WatchDog timer.
11 *                           nTime>0: the number of loop time, 100us resolution.
12 * ret:      none
13 * modify:
14 * comment:      
15 *********************************************************************************************/
16 void delay(int nTime)
17 {
18      // time=0: adjust the Delay function by WatchDog timer.
19      // time>0: the number of loop time
20      // resolution of time is 100us.
21     int i,adjust=0;
22     if(nTime==0)
23     {
24         nTime   = 200;
25         adjust = 1;
26         delayLoopCount = 400;
27         //PCLK/1M,Watch-dog disable,1/64,interrupt disable,reset disable
28         WTCON = ((PCLK/1000000-1)<<8)|(2<<3);
29         WTDAT = 0xffff;                              //for first update
30         WTCNT = 0xffff;                              //resolution=64us @any PCLK 
31         WTCON = ((PCLK/1000000-1)<<8)|(2<<3)|(1<<5); //Watch-dog timer start
32     }
33     for(;nTime>0;nTime--)
34         for(i=0;i<delayLoopCount;i++);
35     if(adjust==1)
36     {
37         WTCON = ((PCLK/1000000-1)<<8)|(2<<3);   //Watch-dog timer stop
38         i = 0xffff - WTCNT;                     //1count->64us, 200*400 cycle runtime = 64*i us
39         delayLoopCount = 8000000/(i*64);         //200*400:64*i=1*x:100 -> x=80000*100/(64*i)   
40     }
41 }

这个是开发板例程中的延时代码,直接拿过来用了,注释也比较详细。注意watchdog定时器只用了一次,就是delay(0)的时候,计算delayLoopCount的初值时。偶还是不太喜欢这种延时方式,直接让cpu空跑,太浪费了。

fs2410_ram.sct

 1 ; *************************************************************
 2 ; *** Scatter-Loading Description File generated by uVision ***
 3 ; *************************************************************
 4 
 5 LR_ROM1 0x30000000 0x00200000  {    ; load region size_region
 6   ER_ROM1 0x30000000 0x00200000  {  ; load address = execution address
 7    *.o (RESET, +First)
 8    ;*(InRoot$$Sections)
 9    .ANY (+RO)
10   }
11   RW_RAM1 +0  {  ; RW data
12    .ANY (+RW +ZI)
13   }
14 }
15 

fs2410_flash.sct

 1 ; *************************************************************
 2 ; *** Scatter-Loading Description File generated by uVision ***
 3 ; *************************************************************
 4 
 5 LR_ROM1 0x00000000 0x00200000  {    ; load region size_region
 6   ER_ROM1 0x00000000 0x00200000  {  ; load address = execution address
 7    *.o (RESET, +First)
 8    ;*(InRoot$$Sections)
 9    .ANY (+RO)
10   }
11   RW_RAM1 0x30000000 0x04000000  {  ; RW data
12    .ANY (+RW +ZI)
13   }
14 }

DebugInRam.ini

 1 FUNC void Init_Board(void)
 2 {
 3     _WWORD (0x53000000 ,0x00000000);
 4 
 5     _WWORD (0x4A000008 ,0xFFFFFFFF);
 6     _WWORD (0x4A00001C ,0x000007FF);
 7 
 8     _WWORD (0x4C000014 ,0x3);
 9     _WWORD (0x4C000004 ,0x0005c042);
10 
11     _WWORD (0x56000070 ,0x00280000);
12     _WWORD (0x56000078 ,0x00000000);
13 
14     _WWORD (0x48000000 ,0x22111110);
15     _WWORD (0x48000004 ,0x00000700);
16     _WWORD (0x48000008 ,0x00000700);
17     _WWORD (0x4800000C ,0x00000700);
18     _WWORD (0x48000010 ,0x00000700);
19     _WWORD (0x48000014 ,0x00000700);
20     _WWORD (0x48000018 ,0x00000700);
21     _WWORD (0x4800001c ,0x00018005);
22     _WWORD (0x48000020 ,0x00000700);
23     _WWORD (0x48000024 ,0x008e0459);
24     _WWORD (0x48000028 ,0x000000B2);
25     _WWORD (0x4800002c ,0x00000030);
26     _WWORD (0x48000030 ,0x00000030);
27 
28     _WWORD (0x56000014 ,0x1);
29 
30     _WWORD (0x56000020 ,0xaaaa55aa);
31     _WWORD (0x56000028 ,0xffff);
32     _WWORD (0x56000024 ,0x0);
33     pc = 0x30000000;
34 }
35 //map 0x48000000, 0x60000000 read write
36 Init_Board();
37 

其实这第一个工程关键的地方在于熟悉处理器,板子,开发环境。所以工程的配置是重点。

fs2410_ram.sct和fs2410_flash.sct是目标文件分别在ram 和flash上运行的链接脚本,主要用于配置程序代码段和数据段的地址,;*(InRoot$$Sections)注释掉是因为不需要开发板提供的库,写程序前一定要搞清楚板子的地址分配,这一点很关键。这两个文件是realview生成的,一开始没有可以编译一遍工程,自动就产生。

DebugInRam.ini 是目标在ram上运行时的调试脚本,不是我写的哈,是开发板例程里带的,不过这个很重要哦,不然的话内存并没有被初始化,调试程序就有问题,总不能总在flash上跑程序吧。

上面的三个脚本,如果不特殊说明的话,后面的程序都会用到,就不再贴出来了。

main.c很简单,就是包含头文件,然后调用myblink,不贴了哈。这个工程可以不必要工程提供的启动代码

S3C2410A.C,要也可以,不过配置比较复杂,就是其它文章的内容喽。

原文地址:https://www.cnblogs.com/liujiahi/p/2196355.html