[processor-sdk/performance-audio-sr.git] / processor_audio_sdk_1_00_00_00 / pasdk / test_dsp / framework / aspMsg_master.c
2 /*
3 Copyright (c) 2016, Texas Instruments Incorporated - http://www.ti.com/
4 All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
36 #include <xdc/std.h>
37 #include <xdc/runtime/Diags.h>
38 #include <xdc/runtime/Error.h>
39 #include <xdc/runtime/IHeap.h>
40 #include <xdc/runtime/Log.h>
41 #include <xdc/runtime/Memory.h>
42 #include <xdc/runtime/System.h>
44 #include <ti/sysbios/heaps/HeapBuf.h>
46 #include <ti/ipc/MessageQ.h>
47 #include <ti/ipc/MultiProc.h>
48 #include <ti/ipc/SharedRegion.h>
50 #include "aspMsg_common.h"
51 #include "aspMsg_master.h"
53 #define AspMsg_MasterToSlaveMsgHeapId ( 0 )
55 AspMsgMaster_Module gAspMsgMaster;
56 AspMsgMaster_Handle hAspMsgMaster=&gAspMsgMaster;
58 Uint32 gMessageId;
60 /* Initialize ASP master messaging */
61 Int AspMsgMaster_init(
62 AspMsgMaster_Handle hAspMsgMaster,
63 UInt16 remoteProcId,
64 UInt16 numMsgs
65 )
66 {
67 Int status = ASP_MSG_MASTER_SOK;
68 Int align;
69 Error_Block eb;
70 IHeap_Handle srHeap;
71 HeapBuf_Params heapParams;
72 MessageQ_Params msgqParams;
73 char msgqName[32];
74 UInt16 regionId;
77 Log_print0(Diags_ENTRY, "AspMsgMaster_init(): -->");
78 Log_info0("AspMsgMaster_init(): -->");
80 // set default values
81 hAspMsgMaster->masterQue = NULL;
82 hAspMsgMaster->slaveQue = MessageQ_INVALIDMESSAGEQ;
83 hAspMsgMaster->heapId = AspMsg_MasterToSlaveMsgHeapId;
84 hAspMsgMaster->msgSize = 0;
85 hAspMsgMaster->messageId = 0;
87 // set processor Ids
88 hAspMsgMaster->masterProcId = MultiProc_self();
89 hAspMsgMaster->slaveProcId = remoteProcId;
91 regionId = SharedRegion_getIdByName("SR_0"); // get IPC shared region Id
93 // compute message size to fill entire cache lines
94 align = SharedRegion_getCacheLineSize(regionId);
95 hAspMsgMaster->msgSize = ROUNDUP(sizeof(ASP_Msg), align);
97 // compute message pool size
98 hAspMsgMaster->poolSize = hAspMsgMaster->msgSize * numMsgs;
100 // acquire message pool memory
101 srHeap = (IHeap_Handle)SharedRegion_getHeap(regionId);
102 hAspMsgMaster->store = Memory_alloc(srHeap, hAspMsgMaster->poolSize, align, NULL);
104 // create a heap in shared memory for message pool
105 HeapBuf_Params_init(&heapParams);
106 heapParams.blockSize = hAspMsgMaster->msgSize;
107 heapParams.numBlocks = numMsgs;
108 heapParams.bufSize = hAspMsgMaster->poolSize;
109 heapParams.align = align;
110 heapParams.buf = hAspMsgMaster->store;
111 Error_init(&eb);
113 hAspMsgMaster->heap = HeapBuf_create(&heapParams, &eb);
114 if (hAspMsgMaster->heap == NULL)
115 {
116 Log_error0("AspMsgMaster_init(): failed creating message pool");
117 status = ASP_MSG_MASTER_HEAPBUF_CREATE_FAIL;
118 Log_print1(Diags_INFO, "AspMsgMaster_init(): <-- status=%d", (IArg)status);
119 return status;
120 }
122 // bind message pool to heapId
123 status = MessageQ_registerHeap((Ptr)(hAspMsgMaster->heap), hAspMsgMaster->heapId);
124 if (status != MessageQ_S_SUCCESS)
125 {
126 Log_error0("AspMsgMaster_init(): failed registering heap");
127 status = ASP_MSG_MASTER_MSGQ_REGHEAP_FAIL;
128 Log_print1(Diags_INFO, "AspMsgMaster_init(): <-- status=%d", (IArg)status);
129 return status;
130 }
132 // create local message queue (inbound messages)
133 MessageQ_Params_init(&msgqParams);
135 hAspMsgMaster->masterQue = MessageQ_create(NULL, &msgqParams);
136 if (hAspMsgMaster->masterQue == NULL)
137 {
138 Log_error0("AspMsgMaster_init(): failed creating MessageQ");
139 status = ASP_MSG_MASTER_MASTER_MSGQ_CREATE_FAIL;
140 Log_print1(Diags_INFO, "AspMsgMaster_init(): <-- status=%d", (IArg)status);
141 return status;
142 }
144 // open the remote message queue
145 System_sprintf(msgqName, AspMsg_SlaveMsgQueName, MultiProc_getName(remoteProcId));
147 Log_info0("AspMsgMaster_init(): MessageQ_open() start");
148 do {
149 status = MessageQ_open(msgqName, &hAspMsgMaster->slaveQue);
150 Log_info1("MessageQ_open() status=%d", status);
151 //Task_sleep(1);
152 } while (status == MessageQ_E_NOTFOUND);
153 Log_info0("AspMsgMaster_init(): MessageQ_open() finish");
155 if (status < 0)
156 {
157 Log_error0("AspMsgMaster_init(): failed opening MessageQ");
158 status = ASP_MSG_MASTER_SLAVE_MSGQ_OPEN_FAIL;
159 Log_print1(Diags_INFO, "AspMsgMaster_init(): <-- status=%d", (IArg)status);
160 return status;
161 }
163 Log_print0(Diags_INFO, "AspMsgMaster_init(): ASP Master messaging ready");
164 Log_info0("AspMsgMaster_init(): ASP Master messaging ready");
166 Log_print1(Diags_EXIT, "<-- AspMsgMaster_init(): %d", (IArg)status);
167 return 0;
168 }