添加查找表
var SharedRegion = xdc.useMoudle(ti.sdo.ipc.SharedRegion);
SharedReginon.numEntries = 4; //最大共享区域数目
SharedReginon。translate = true; //是否需要进行地址转换
修改后的.cfg:
/*
* Copyright (c) 2012, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* */
var MultiProc = xdc.useModule('ti.sdo.utils.MultiProc');
/*
* Get the list of names that the build device supports.
* I.e. ["CORE0", "CORE1", "CORE2" ... ]
*/
var nameList = MultiProc.getDeviceProcNames();
/*
* Since this is a single-image example, we don't (at build-time) which
* processor we're building for. We therefore supply 'null'
* as the local procName and use MultiProc_setLocalId to set the procId
* at runtime.
*/
MultiProc.setConfig(null, nameList);
var System = xdc.useModule('xdc.runtime.System');
var SysStd = xdc.useModule('xdc.runtime.SysStd');
System.SupportProxy = SysStd;
/* Modules explicitly used in the application */
var Notify = xdc.useModule('ti.sdo.ipc.Notify');
var MessageQ = xdc.useModule('ti.sdo.ipc.MessageQ');
var HeapBufMP = xdc.useModule('ti.sdo.ipc.heaps.HeapBufMP');
var Ipc = xdc.useModule('ti.sdo.ipc.Ipc');
var BIOS = xdc.useModule('ti.sysbios.BIOS');
BIOS.heapSize = 0x8000;
var Task = xdc.useModule('ti.sysbios.knl.Task');
var tsk0 = Task.create('&tsk0_func');
tsk0.instance.name = "tsk0";
/* To avoid wasting shared memory for MessageQ transports */
for (var i = 0; i < MultiProc.numProcessors; i++) {
Ipc.setEntryMeta({
remoteProcId: i,
setupMessageQ: true,
});
}
/* Synchronize all processors (this will be done in Ipc_start) */
Ipc.procSync = Ipc.ProcSync_ALL;
/*
* Need to define the shared region. The IPC modules use this
* to make portable pointers. All processors need to add this
* call with their base address of the shared memory region.
* If the processor cannot access the memory, do not add it.
*/
var SharedRegion = xdc.useModule('ti.sdo.ipc.SharedRegion');
/* Shared Memory base address and length */
var SHAREDMEM_0 = 0x0c000000;
var SHAREDMEMSIZE_0 = 0x00800000;
SharedRegion.setEntryMeta(0, // 区域ID
{ base: SHAREDMEM_0, // 基地址
len: SHAREDMEMSIZE_0, // 区域大小
ownerProcId: 0, // 所有者的核ID
isValid: true, // 对于当前核,该区域是否有效
name: "DDR2_RAM_0", // 区域名称
});
/* Create a semaphore with count 0 */
var Semaphore = xdc.useModule('ti.sysbios.knl.Semaphore');
Program.global.semHandle = Semaphore.create(0);
/*
* @(#) ti.sdo.ipc.examples.multicore.evm667x; 1, 0, 0, 0,1; 5-22-2012 16:36:06; /db/vtree/library/trees/ipc/ipc-h32/src/ xlibrary
*/
var execontextInstance0 = xdc.lookup('xdc.platform.ExeContext.Instance#0');
execontextInstance0.memoryMap["MSMCSRAM"].len = 16777216;
b). 动态添加
typedef struct SharedRegion_Entry{
Ptr base;
SizeT len;
UInt16 ownerProcId;
Bool isValid;
Bool cacheLineSize;
Bool createHeap;
String name;
}SharedRegion_Entry
使用共享区域内存
其一般性的使用方法:
heap = (IHeap_Handle)SharedRegion_getHeap(regionId); // 通过区域ID获得共享区域的堆句柄
buf = Memory_alloc(heap, size, align, NULL); // 通过堆分配区域内存
参数:
#include <xdc/runtime/Memory.h>
#include <ti/ipc/SharedRegion.h>
地址转换
在一个拥有共享内存区域的环境中,一个主要的问题是这些共享区域在不同处理器上可能会被映射到不同地址空间,即同样一块共享内存区域在不同处理的本地地址空间的逻辑位置是不同,因此此时就需要在不同处理器之间转换的地址指针。先前在静态设置中将SharedRegion.translate选项设置为true,则需要做地址转换。而设置为false时,不同处理器上同一样的内存缓冲区其对应地址都是相同,所以不需要做地址转换。这里为了描述共享内存间的地址转换过程,我们将其设置为true(默认)。
缓存Cache问题
代码:
#include<xdc/std.h>
#include<stdio.h>
#include<stdlib.h>
/* -----------------------------------XDC.RUNTIME module Headers */
#include <xdc/runtime/System.h>
#include <ti/sysbios/hal/Cache.h>
/* ----------------------------------- IPC module Headers */
#include <ti/ipc/MultiProc.h>
#include <ti/ipc/Notify.h>
#include <xdc/runtime/IHeap.h>
#include <ti/ipc/Ipc.h>
/* ----------------------------------- BIOS6 module Headers */
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/BIOS.h>
/* ----------------------------------- To get globals from .cfg Header */
#include <xdc/cfg/global.h>
#include <xdc/runtime/Memory.h>
#include <ti/ipc/SharedRegion.h>
#define INTERRUPT_LINE 0
#define BufEVENT 10
#define dateNum 8 //76800
unsigned char *inBuf=NULL;
SharedRegion_SRPtr inBuf_srptr;
/*
* ======== cbFxn ========
* 这是Notify模块的注册函数
* procId表示激动注册函数的核ID,或者说该事件是从哪个核来的
*/
Void cbFxn(UInt16 procId, UInt16 lineId,
UInt32 eventId, UArg arg, UInt32 payload)
{
inBuf_srptr=(SharedRegion_SRPtr)payload;
Semaphore_post(semHandle);
}
/*
* ======== tsk0_func ========
* Sends an event to the next processor then pends on a semaphore.
* The semaphore is posted by the callback function.
*/
Void tsk0_func(UArg arg0, UArg arg1)
{
Int status;
Int i;
int coreId=MultiProc_self();
// 分配内存
if (coreId == 0) {
inBuf = (unsigned char*)Memory_alloc(SharedRegion_getHeap(0), dateNum, 128, NULL);
if(inBuf==NULL)
{
System_printf("malloc Buf failed\n");
BIOS_exit(0);
}
for(i=0;i<dateNum;i++){ // 写入数据
inBuf[i]=i*3+5;
}
inBuf_srptr = SharedRegion_getSRPtr(inBuf, 0);
System_printf("inBuf address 0x%x\n",inBuf);
System_printf("outBuf date is ");
for(i=0;i<dateNum;i++)
System_printf("%d ",inBuf[i]);
System_printf("\n");
Cache_wbAll();// Write back all caches();
for(i=1;i<8;i++)
status = Notify_sendEvent(i, INTERRUPT_LINE, BufEVENT, (UInt32)inBuf_srptr, TRUE);
}else{
Semaphore_pend(semHandle, BIOS_WAIT_FOREVER); // 等待从核完成全部任务
Cache_disable(Cache_Type_ALL);
inBuf=SharedRegion_getPtr(inBuf_srptr);
System_printf("inBuf address 0x%x\n",inBuf);
System_printf("regionId is %d\n",SharedRegion_getId(inBuf)); // 打印当前共享区域ID
System_printf("outBuf date is ");
for(i=0;i<dateNum;i++)
System_printf("%d ",inBuf[i]);
System_printf("\n");
}
// 各核完成任务后可以退出了
System_printf("SharedMem is finished\n");
BIOS_exit(0);
}
/*
* ======== main ========
* Synchronizes all processors (in Ipc_start), calls BIOS_start, and registers
* for an incoming event
*/
Int main(Int argc, Char* argv[])
{
Int status;
/*
* Ipc_start() calls Ipc_attach() to synchronize all remote processors
* because 'Ipc.procSync' is set to 'Ipc.ProcSync_ALL' in *.cfg
*/
status = Ipc_start();
if (status < 0) {
System_abort("Ipc_start failed\n");
}
if (MultiProc_self() == 0) {
Int i;
for(i=1;i<8;i++)
status = Notify_registerEvent(i, INTERRUPT_LINE, BufEVENT,
(Notify_FnNotifyCbck)cbFxn, NULL);
}else{
// 从核完成事件注册
status = Notify_registerEvent(0, INTERRUPT_LINE, BufEVENT,
(Notify_FnNotifyCbck)cbFxn, NULL);
}
BIOS_start();
return (0);
}
输出结果:
通过上面结果,可以看出核1-7完美读取了核0写入共享内存的数据。
需要注意的是有些情况下,即使在需要地址转换的情况下,各处理器的本地地址也有可能是相同的,就像本节的例子,但是当共享内存区域的数据成份比较复杂时,同一区域在不同处理器的本地地址就很可能不同了,所以地址转换是非常有必要的。