IPC,Inter-Processor Communication是SYS/BIOS處理核間通信的組件:
IPC的幾種應用方式:
1.最小使用(Minimal use)
這種情況是通過核間的通知機制(notification)來實施的,而一個通知所攜帶的信息是非常小的(一般是32bits),所以稱為最小使用。這種方式一般是用於處理核間的簡單同步,卻無法處理復雜的消息傳遞。
這種情況下,需要利用到Notify模塊的APIs函數,比如通過Notify_sendEvent()函數給某個特定核傳遞一個事件,我們可以給特定事件動態注冊反饋函數。由於一個通知(notification)所攜帶的信息是極少的,所以只能給處理器發送一個事件號,而這個事件號所代表的反饋函數決定之后的動作。另外一些數據以函數參數方式,也可以被送出。
2.增加數據通路(Add data passing)
這種情況是在前面的最小使用機制下,在核間增加了一個傳遞鏈表元素的數據通路,這個鏈表的實施一般是使用共享內容並通過門(gates)來管理同步。

3.增加動態分配(Add dynamic allocation)
這種情況下,增加了從堆中動態分配鏈表元素的功能。
這種情況在上種情況下,增加了一個Heap*MP模塊,這個模塊主要用於從堆中給鏈表動態分配內存。
4.強大但易用的消息機制(Powerful but easy-to-use messaging)
這種情況下利用MessageQ模塊來傳遞消息。
除了Notify通知機制,還可以利用MessageQ來實現更為復雜的核間通信,在這種情況下,只需要配置MultiProc和SharedRegion模塊就可以了,而Ipc_start()函數將自動為我們實現上面灰色模塊的配置。
最小使用(Minimal use)情況舉例
打開CCS自帶例程

選中Group,點擊運行:
結果分析
1.各核打印:
這段是在main()中出現的結果,每個核都會執行各自的main():
System_printf("main: MultiProc id = %d\n", MultiProc_self()); System_printf("main: MultiProc name = %s\n", MultiProc_getName(MultiProc_self()));
2.各核注冊事件,並表明其反饋函數:
status = Notify_registerEvent(srcProc, INTERRUPT_LINE, EVENTID, (Notify_FnNotifyCbck)cbFxn, NULL);
核0執行同時釋放信號量,在核0釋放信號量semHandle之前,其他核都處理等待信號量釋放中
核0通過給核1發送事件,觸發反饋函數,在反饋函數中semHandle歸一,注意這個激活的信號量是在核1中的
status = Notify_sendEvent(dstProc, INTERRUPT_LINE, EVENTID, seq, TRUE);
激活核1的信號量后,核0打印結果,並等待其信號量的結果,所有核的信號量都初始為0:
System_printf("tsk1_func: Sent request #%d to %s\n", seq, MultiProc_getName(dstProc));
/* wait forever on a semaphore, semaphore is posted in callback */ Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
以下是總共八個核,分別執行了NUMLOOPS次(這里設置的是10次)
下一個核信號被激活,開始執行:
/* wait forever on a semaphore, semaphore is posted in callback */ Semaphore_pend(semHandle, BIOS_WAIT_FOREVER); System_printf("tsk1_func: Received request #%d from %s\n", seq, MultiProc_getName(recvProcId));
同時通過反饋函數將當前核的下一個核激活:
status = Notify_sendEvent(dstProc, INTERRUPT_LINE, EVENTID, seq, TRUE);
完成發送事件:
System_printf("tsk1_func: Sent request #%d to %s\n", seq, MultiProc_getName(dstProc));
退出任務循環,同時退出當前核的BIOS:
多核IPC的配置

3.設置同步的核數

4.核間的連接方法Ipc_attach()及Ipc_detach()
Ipc_attach(#coreID),#coreID表示需要連接的核ID號,如Ipc_attach(0)表示連接核0。
不過需要注意的是:
while(Ipc_attach(#coreID)<0) { Task_sleep(1); }
Ipc_detach()的使用方法同Ipc_attach()是類似的,不過它的功能是解除連接。
主從核之間的通信

2.修改源文件為:
#include <xdc/std.h>
/* -----------------------------------XDC.RUNTIME module Headers */ #include <xdc/runtime/System.h>
/* ----------------------------------- IPC module Headers */ #include <ti/ipc/MultiProc.h> #include <ti/ipc/Notify.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>
#define INTERRUPT_LINE 0
/* Notify event number that the app uses */
#define EVENTID 10
/* Number of times to run the loop */
#define NUMLOOPS 3 UInt32 times = 0; UInt16 recvnumes = 0; #define masterProc 0
#define sloverProc1 1
#define sloverProc2 2
#define sloverNum 2
/* * ======== cbFxn ======== * This function was registered with Notify. It is called when any event is * sent to this processor. */ Void cbFxn(UInt16 procId, UInt16 lineId, UInt32 eventId, UArg arg, UInt32 payload) { /* The payload is a sequence number. */
if(procId!=masterProc) // 主核注冊函數
{ recvnumes++; // 接收從核的數目
if(recvnumes==sloverNum) // 當收到全部從核回復的信息
{ recvnumes=0; Semaphore_post(semHandle); } } else { times = 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 i = 1; Int status; if (MultiProc_self() == masterProc) { while (i <= NUMLOOPS) { /* 這里可以添加主核需要執行的任務代碼*/
/* Send an event to the next processor */ status = Notify_sendEvent(sloverProc1, INTERRUPT_LINE, EVENTID, i, TRUE); status = Notify_sendEvent(sloverProc2, INTERRUPT_LINE, EVENTID, i, TRUE); /* Continue until remote side is up */
if (status < 0) { continue; } System_printf("MasterCore Sent Event to SloverCores in %d\n", i); /* Wait to be released by the cbFxn posting the semaphore */ Semaphore_pend(semHandle, BIOS_WAIT_FOREVER); // 主核等待所有從核完成其工作返回
System_printf("MasterCore Received Event from All SloverCores in %d\n",i); /* increment for next iteration */ i++; } } else { while (times < NUMLOOPS) { /* wait forever on a semaphore, semaphore is posted in callback */ Semaphore_pend(semHandle, BIOS_WAIT_FOREVER); // 等待主核通知開始執行任務
System_printf("SloverCore%d Received Event from MasterCore in %d\n", MultiProc_self(),times); /* 這里可以添加從核執行的任務*/
/* Send an event to the next processor */ status = Notify_sendEvent(masterProc, INTERRUPT_LINE, EVENTID, times, TRUE); if (status < 0) { System_abort("sendEvent to MasterCore failed\n"); } System_printf("SloverCore%d sent Event from MasterCore in %d\n", MultiProc_self(),times); } } System_printf("Test completed\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; status = Ipc_start(); if (status < 0) { System_abort("Ipc_start failed\n"); } /* 這里主要根據主核和從核的角色分別添加連接任務:主核同兩個從核都有連接,而從核只與主核有鏈接 在添加核間連接后,分別給核間連接注冊事件 */
if(MultiProc_self()==masterProc) { while(Ipc_attach(sloverProc1)){ Task_sleep(1); }// 完成從核1的連接
while(Ipc_attach(sloverProc2)){ Task_sleep(1); }// 完成從核2的連接
status = Notify_registerEvent(sloverProc1, INTERRUPT_LINE, EVENTID, (Notify_FnNotifyCbck)cbFxn, NULL); if (status < 0) { System_abort("Notify_registerEvent for sloverCore1 failed\n"); }// 完成從核1的事件注冊
status = Notify_registerEvent(sloverProc2, INTERRUPT_LINE, EVENTID, (Notify_FnNotifyCbck)cbFxn, NULL); if (status < 0) { System_abort("Notify_registerEvent for sloverCore2 failed \n"); }// 完成從核2的事件注冊
} else{ while(Ipc_attach(masterProc)) { Task_sleep(1); }// 完成主核0的連接
status = Notify_registerEvent(masterProc, INTERRUPT_LINE, EVENTID, (Notify_FnNotifyCbck)cbFxn, NULL); if (status < 0) { System_abort("Notify_registerEvent for masterCore0 failed\n"); }// 完成主核0的事件注冊
} BIOS_start(); return (0); }
仿真調試的結果:
從結果上看,當從核分別收到了來自主核的事件時,同時開始任務,當從核任務全部完成后,主核才開始其任務。