本文是 zynq 7000 AMP模式 双裸核CPU同时运行 的继续。本文主要是上文的基础上增加通过共享内存的方式,演示2个裸核的交互。
共享内存前先看看内存地址分布,这个图取自 ug585 4.1 节 address map 的表4-1
本文定义OCM 为共享内存,地址为0xffff_0000。
在2个文件的头部都这样定义共享内存
#include "xil_mmu.h" #define COM_VAL (*(volatile unsigned int*)(0xffff0000)) #define COM_VAL1 (*(volatile unsigned int*)(0xffff0004))在程序中定义0xffff0000 这段内存没有cache,防止写在cache ,另一方看不到。同时初始化变量为0,代码如下:
Xil_SetTlbAttributes(0xffff0000,0x14de2); COM_VAL=0; COM_VAL1=0;在cpu0 中(helloworld工程中)COM_VAL=i, 在cpu1 中显示这个数据
printf("led=%x, cpu0 count=%d\n\r",1<<Ledwidth,COM_VAL);在cpu1 中 COM_VAL1=1<<Ledwidth; 在cpu0 中显示
printf("%d: Hello World! led=%d\n\r",i,COM_VAL1);
2个完整的程序分别是:
cpu0(helloworld 工程)中程序代码
#include <stdio.h> #include "xil_printf.h" #include "sleep.h" #include "xil_mmu.h" #define COM_VAL (*(volatile unsigned int*)(0xffff0000)) #define COM_VAL1 (*(volatile unsigned int*)(0xffff0004)) int main() { int i=0; Xil_SetTlbAttributes(0xffff0000,0x14de2); COM_VAL=0; COM_VAL1=0; while (1) { i++; COM_VAL=i; printf("%d: Hello World! led=%d\n\r",i,COM_VAL1); sleep(2); } return 0; }cpu1 中程序代码
#include <stdio.h> #include "platform.h" #include "xil_printf.h" #include "xparameters.h" #include "xil_io.h" #include "sleep.h" #define MY_IP 0x41200000 #include "xil_mmu.h" #define COM_VAL (*(volatile unsigned int*)(0xffff0000)) #define COM_VAL1 (*(volatile unsigned int*)(0xffff0004)) int main() { u32 Ledwidth; Xil_SetTlbAttributes(0xffff0000,0x14de2); COM_VAL=0; COM_VAL1=0; while (1) { for (Ledwidth = 0x0; Ledwidth < 4; Ledwidth++) { Xil_Out32(MY_IP,1 << Ledwidth); COM_VAL1=1<<Ledwidth; printf("led=%x, cpu0 count=%d\n\r",1<<Ledwidth,COM_VAL); sleep(1); } } return 0; }按照上文介绍的方法调试(debug) 运行,结果如下:
。。。
led=1, cpu0 count=1317 led=2, cpu0 count=1317 1318: Hello World! led=2 led=4, cpu0 count=1318 led=8, cpu0 count=1318 1319: Hello World! led=8 led=1, cpu0 count=1319 led=2, cpu0 count=1319 1320: Hello World! led=2 led=4, cpu0 count=1320 led=8, cpu0 count=1320 1321: Hello World! led=8 led=1, cpu0 count=1321 led=2, cpu0 count=1321 1322: Hello World! led=2 led=4, cpu0 count=1322 led=8, cpu0 count=1322 1323: Hello World! led=8 led=1, cpu0 count=1323 led=2, cpu0 count=1323 1324: Hello World! led=2 led=4, cpu0 count=1324 led=8, cpu0 count=1324 1325: Hello World! led=8
可以看到数据通过共享内存在cpu0 和cpu1 间交互了。介绍结束。