第5课 混合编程和芯片手册阅读

5.1 C语言和汇编混合编程

C语言和汇编的混合编程,实现混合编程的一个重要方面就是要实现C代码和汇编代码的数据、函数共享。

汇编程序以.S结尾,在/work/uClinux-dist/user/下新建一个scu1的文件夹。在文件夹里新建一个文件testasm.S,在testasm.S里用汇编写两个函数,实现两个数的加减。如下

.text
.global MyAdd
.global MySub
MyAdd:
add r0, r0, r1
mov pc, lr
MySub:
sub r0, r0, r1
mov pc, lr

然后,在C语言里调用这两个函数,在scu1里新建一个test.c文件。内容如下:

#include <stdio.h>
extern int MyAdd(int a, int b);
extern int MySub(int a,int b);
int main(void)
{
      printf(“\nMyAdd=%d”, MyAdd(5,6));
      printf(“\nMySub=%d”, MySub(8,3));
      return 0;
}

其中,extern在汇编中用来引用一个在其他模块中定义过的符号名,使得这个符号名所表示的数据或函数能在该模块中被使用。Extern申明为外部函数。最后需要一个Makefile文件,

EXECS = test
OBJS = testasm.o test.o
AFLAGS := -mapcs-32
all: $(EXECS)
testasm.o: testasm.S
      $(CC) $(AFLAGS) -c -o $@ $<
$(EXECS): $(OBJS)
      $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)
romfs:
      $(ROMFSINST) /bin/test

但这三个文件代码写好了以后,在/work/uClinux-dist目录下执行sudo make,检查是否报错。如果没有报错,下到开发板里面去运行一下,再开发板里的执行结果如下:

 

5.2 阅读cpu_arm946_reset(loc)

在路径/work/uClinux-dist /linux-2.4.x/arch/armnommu/mm/proc-arm946.S下,找到该函数,函数如下:

* cpu_arm946_reset(loc)

 * Perform a soft reset of the system. Put the CPU into the

 * same state as it would be if it had been reset, and branch

 * to what would be the reset vector.

* loc: location to jump to for soft reset

 */

.align      5 

ENTRY(cpu_arm946_reset)

       mov ip, #0

       mcr p15, 0, ip, c7, c7, 0       @ invalidate I,D caches

       mrc p15, 0, ip, c1, c0, 0       @ ctrl register

       bic   ip, ip, #0x000f             @ ............wcam

       bic   ip, ip, #0x1100            @ ...i...s........

       mcr p15, 0, ip, c1, c0, 0       @ ctrl register

       mov pc, r0

这个程序是对arm946CPU进行软件复位,首先分配5个字节空间存放代码,MCR 传送 ARM 寄存器 Rd 的内容到协处理器,MRC 从协处理器传送一个单一的字并把它放置到 ARM 寄存器 Rd 中,bic是在一个字中清除位的一种方法,与 OR 位设置是相反的操作,这条指令的目的是清除位0,1,2,3,保持其他位不变。

5.3 问题解答

通过阅读芯片手册Part 1 - FireFox-Overview-Rev0-1 – 120602和Part 2 - FirefoxCPU-Benjamin_Nov_01_02可以找到相应问题的答案。

(1) Which pin is used to select boot chip?

BOOTCSn(Pin65)

(2) Which pins are used to select SDRAM?

M_CS[2:0]n(Pin56,Pin57,Pin85)

(3) What is the base address of boot device?

0xFFC0_0000

(4) What is the base address of GPIO registers?

0x8000_D000

(5) What is the base address of UART registers?

0x8000_C840

5.4 总结

这节课主要讲了汇编和C语言的混合编程,同时也主要阅读相关的文档来加深理解,尤其是做嵌入式的,芯片手册的阅读是一个必须经过的的过程,只有通过阅读手册,才能对芯片有一个深入的了解,这节课讲解的内容不多,但阅读量是非常大的,用一个周都是看不完的,接下来还得继续。

猜你喜欢

转载自www.cnblogs.com/LiuFarrell/p/9836737.html