1 /*
2 * Copyright (c) 2012-2013, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 /* =============================================================================
33 * @file MessageQApp.c
34 *
35 * @brief Sample application for MessageQ module between MPU and Remote Proc
36 *
37 * ============================================================================
38 */
40 /* Standard headers */
41 #include <stdio.h>
42 #include <stdlib.h>
44 /* IPC Headers */
45 #include <ti/ipc/Std.h>
46 #include <ti/ipc/Ipc.h>
47 #include <ti/ipc/MessageQ.h>
49 /* App defines: Must match on remote proc side: */
50 #define HEAPID 0u
51 #define SLAVE_MESSAGEQNAME "SLAVE"
52 #define MPU_MESSAGEQNAME "HOST"
54 #define PROC_ID_DFLT 1 /* Host is zero, remote cores start at 1 */
55 #define NUM_LOOPS_DFLT 100
57 typedef struct SyncMsg {
58 MessageQ_MsgHeader header;
59 unsigned long numLoops;
60 unsigned long print;
61 } SyncMsg ;
63 Int MessageQApp_execute(UInt32 numLoops, UInt16 procId)
64 {
65 Int32 status = 0;
66 MessageQ_Msg msg = NULL;
67 MessageQ_Params msgParams;
68 UInt16 i;
69 MessageQ_QueueId queueId = MessageQ_INVALIDMESSAGEQ;
70 MessageQ_Handle msgqHandle;
71 char remoteQueueName[64];
73 printf("Entered MessageQApp_execute\n");
75 /* Create the local Message Queue for receiving. */
76 MessageQ_Params_init(&msgParams);
77 msgqHandle = MessageQ_create(MPU_MESSAGEQNAME, &msgParams);
78 if (msgqHandle == NULL) {
79 printf("Error in MessageQ_create\n");
80 goto exit;
81 }
82 else {
83 printf("Local MessageQId: 0x%x\n", MessageQ_getQueueId(msgqHandle));
84 }
86 sprintf(remoteQueueName, "%s_%s", SLAVE_MESSAGEQNAME,
87 MultiProc_getName(procId));
89 /* Poll until remote side has it's messageQ created before we send: */
90 do {
91 status = MessageQ_open(remoteQueueName, &queueId);
92 sleep (1);
93 } while (status == MessageQ_E_NOTFOUND);
95 if (status < 0) {
96 printf("Error in MessageQ_open [%d]\n", status);
97 goto cleanup;
98 }
99 else {
100 printf("Remote queueId [0x%x]\n", queueId);
101 }
103 msg = MessageQ_alloc(HEAPID, sizeof(SyncMsg));
104 if (msg == NULL) {
105 printf("Error in MessageQ_alloc\n");
106 MessageQ_close(&queueId);
107 goto cleanup;
108 }
110 /* handshake with remote to set the number of loops */
111 MessageQ_setReplyQueue(msgqHandle, msg);
112 ((SyncMsg *)msg)->numLoops = numLoops;
113 ((SyncMsg *)msg)->print = TRUE;
114 MessageQ_put(queueId, msg);
115 MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
117 printf("Exchanging %d messages with remote processor %s...\n",
118 numLoops, MultiProc_getName(procId));
120 for (i = 0 ; i < numLoops; i++) {
121 MessageQ_setMsgId(msg, i);
123 /* Have the remote proc reply to this message queue */
124 MessageQ_setReplyQueue(msgqHandle, msg);
126 status = MessageQ_put(queueId, msg);
127 if (status < 0) {
128 printf("Error in MessageQ_put [%d]\n", status);
129 break;
130 }
132 status = MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
133 if (status < 0) {
134 printf("Error in MessageQ_get [%d]\n", status);
135 break;
136 }
137 else {
138 printf("MessageQ_get #%d Msg = 0x%x\n", i, (UInt)msg);
140 /* Validate the returned message. */
141 if ((msg != NULL) && (MessageQ_getMsgId (msg) != i)) {
142 printf("Data integrity failure!\n"
143 " Expected %d\n"
144 " Received %d\n",
145 i, MessageQ_getMsgId(msg));
146 break;
147 }
148 }
150 printf("Exchanged %d messages with remote processor %s\n",
151 (i+1), MultiProc_getName(procId));
152 }
154 if (status >= 0) {
155 printf("Sample application successfully completed!\n");
156 }
158 MessageQ_free(msg);
159 MessageQ_close(&queueId);
161 cleanup:
162 /* Clean-up */
163 status = MessageQ_delete(&msgqHandle);
164 if (status < 0) {
165 printf("Error in MessageQ_delete [%d]\n", status);
166 }
168 exit:
169 printf("Leaving MessageQApp_execute\n\n");
171 return (status);
172 }
174 int main (int argc, char ** argv)
175 {
176 Int32 status = 0;
177 UInt32 numLoops = NUM_LOOPS_DFLT;
178 UInt16 procId = PROC_ID_DFLT;
180 /* Parse Args: */
181 switch (argc) {
182 case 1:
183 /* use defaults */
184 break;
185 case 2:
186 numLoops = atoi(argv[1]);
187 break;
188 case 3:
189 numLoops = atoi(argv[1]);
190 procId = atoi(argv[2]);
191 break;
192 default:
193 printf("Usage: %s [<numLoops>] [<ProcId>]\n", argv[0]);
194 printf("\tDefaults: numLoops: %d; ProcId: %d\n",
195 NUM_LOOPS_DFLT, PROC_ID_DFLT);
196 exit(0);
197 }
199 status = Ipc_start();
201 if (procId >= MultiProc_getNumProcessors()) {
202 printf("ProcId must be less than %d\n", MultiProc_getNumProcessors());
203 Ipc_stop();
204 exit(0);
205 }
206 printf("Using numLoops: %d; procId : %d\n", numLoops, procId);
208 if (status >= 0) {
209 MessageQApp_execute(numLoops, procId);
210 Ipc_stop();
211 }
212 else {
213 printf("Ipc_start failed: status = %d\n", status);
214 }
216 return(0);
217 }