C语言编程问题
FL2440开发板,裸机编写C语言程序时,为什么前面要加一段用汇编写的init.s呢,请指教! 回复 1# cmz86121361谁说一定要汇编的啊?下面我写的一个项目就是用纯C来控制LED的,因为是下载到S3C2440的内核SRAM中运行,不需要初始化外部SDRAM。因为只是对GPIO进行操作,所以也没有对CPU进行一些初始化。这个程序下载到S3C2440的内部SRAM中就能够正常运行了。
如果你是一些复杂的C代码,如需要设置堆栈等,或需要其他一些外设,如USB等,就需要用汇编对CPU,内存,外设等进行初始化了。
$ ls
led.cled.ldsmakefile
$ cat led.c
/***********************************************************************
* File:led.c
* Version:1.0.0
* Copyright:2011 (c) Guo Wenxue <guowenxue@gmail.com>
* Description:This C code used to turn LED0~LED4 on on FL2440 board
* ChangeLog:1, Release initial version on "Mon Mar 21 21:09:52 CST 2011"
*
***********************************************************************/
#define GPBCON (*(unsigned long volatile *)0x56000010)
#define GPBDAT (*(unsigned long volatile *)0x56000014)
#define GPBUP (*(unsigned long volatile *)0x56000018)
#define LED0 5 /*LED0 use GPB5*/
#define LED1 6 /*LED1 use GPB6*/
#define LED2 8 /*LED2 use GPB8*/
#define LED3 10 /*LED3 use GPB10*/
#define DELAY_TIME 20000000
static inline void delay (unsigned long loops)
{
__asm__ volatile ("1:\n"
"subs %0, %1, #1\n"
"bne 1b":"=r" (loops):"0" (loops));
}
int main(void)
{
/*Set GPB5,GPB6,GPB8,GPB10 as GPIO mode(0x01) */
GPBCON = (GPBCON|0x333C00)&0x111400;
GPBUP = (GPBUP | 0x560);
while(1)
{
/* Set GPB5,GPB6,GPB8,GPB10 as high level, to turn LED0,LED1,LED2,LED3 off */
GPBDAT = (GPBDAT | 0x560);
delay(DELAY_TIME);
/* Turn LED0 on */
GPBDAT = (GPBDAT & (~(1<<LED0)) );
delay(DELAY_TIME);
/* Turn LED1 on */
GPBDAT = (GPBDAT & (~(1<<LED1)) );
delay(DELAY_TIME);
/* Turn LED2 on */
GPBDAT = (GPBDAT & (~(1<<LED2)) );
delay(DELAY_TIME);
/* Turn LED3 on */
GPBDAT = (GPBDAT & (~(1<<LED3)) );
delay(DELAY_TIME);
}
}
$ make
/opt/buildroot-2011.02/arm920t/usr/bin/arm-linux-gcc-c -o led.o led.c
/opt/buildroot-2011.02/arm920t/usr/bin/arm-linux-ld -Tled.lds led.o -o led.elf
/opt/buildroot-2011.02/arm920t/usr/bin/arm-linux-objcopy -O binary -S led.elf led.bin
rm -f *.elf *.o 回复 2# dglwx
我是看FL2440开发板给的ADS编程里的例子是这样的,而且我也试了,用H-JTAG联机调试,去调init.s代码后,例子里的led程序运行没反应,加上init.s代码后就可以了,所以有点疑惑,再次请指教!
页:
[1]