本文是 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 間交互了。介紹結束。