1 /*
2 * Copyright (c) 2012-2015 Texas Instruments Incorporated - http://www.ti.com
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>
48 #include <ti/ipc/transports/TransportRpmsg.h>
50 /* App defines: Must match on remote proc side: */
51 #define HEAPID 0u
52 #define SLAVE_MESSAGEQNAME "SLAVE"
53 #define MPU_MESSAGEQNAME "HOST"
55 #define PROC_ID_DFLT 1 /* Host is zero, remote cores start at 1 */
56 #define NUM_LOOPS_DFLT 100
58 typedef struct SyncMsg {
59 MessageQ_MsgHeader header;
60 UInt32 numLoops; /* also used for msgId */
61 UInt32 print;
62 } SyncMsg ;
64 Int MessageQApp_execute(UInt32 numLoops, UInt16 procId)
65 {
66 Int32 status = 0;
67 MessageQ_Msg msg = NULL;
68 MessageQ_Params msgParams;
69 UInt32 i;
70 MessageQ_QueueId queueId = MessageQ_INVALIDMESSAGEQ;
71 MessageQ_Handle msgqHandle;
72 char remoteQueueName[64];
73 UInt32 msgId;
75 printf("Entered MessageQApp_execute\n");
77 /* Create the local Message Queue for receiving. */
78 MessageQ_Params_init(&msgParams);
79 msgqHandle = MessageQ_create(MPU_MESSAGEQNAME, &msgParams);
80 if (msgqHandle == NULL) {
81 printf("Error in MessageQ_create\n");
82 goto exit;
83 }
84 else {
85 printf("Local MessageQId: 0x%x\n", MessageQ_getQueueId(msgqHandle));
86 }
88 sprintf(remoteQueueName, "%s_%s", SLAVE_MESSAGEQNAME,
89 MultiProc_getName(procId));
91 /* Poll until remote side has it's messageQ created before we send: */
92 do {
93 status = MessageQ_open(remoteQueueName, &queueId);
94 sleep (1);
95 } while (status == MessageQ_E_NOTFOUND);
97 if (status < 0) {
98 printf("Error in MessageQ_open [%d]\n", status);
99 goto cleanup;
100 }
101 else {
102 printf("Remote queueId [0x%x]\n", queueId);
103 }
105 msg = MessageQ_alloc(HEAPID, sizeof(SyncMsg));
106 if (msg == NULL) {
107 printf("Error in MessageQ_alloc\n");
108 MessageQ_close(&queueId);
109 goto cleanup;
110 }
112 /* handshake with remote to set the number of loops */
113 MessageQ_setReplyQueue(msgqHandle, msg);
114 ((SyncMsg *)msg)->numLoops = numLoops;
115 ((SyncMsg *)msg)->print = FALSE;
116 MessageQ_put(queueId, msg);
117 MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
119 printf("Exchanging %d messages with remote processor %s...\n",
120 numLoops, MultiProc_getName(procId));
122 for (i = 1 ; i <= numLoops; i++) {
123 ((SyncMsg *)msg)->numLoops = i;
125 /* Have the remote proc reply to this message queue */
126 MessageQ_setReplyQueue(msgqHandle, msg);
128 status = MessageQ_put(queueId, msg);
129 if (status < 0) {
130 printf("Error in MessageQ_put [%d]\n", status);
131 break;
132 }
134 status = MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
136 if (status < 0) {
137 printf("Error in MessageQ_get [%d]\n", status);
138 break;
139 }
140 else {
141 /* validate the returned message */
142 msgId = ((SyncMsg *)msg)->numLoops;
143 if ((msg != NULL) && (msgId != i)) {
144 printf("Data integrity failure!\n"
145 " Expected %d\n"
146 " Received %d\n",
147 i, msgId);
148 break;
149 }
150 }
152 if (numLoops <= 200) {
153 printf("MessageQ_get #%d Msg = 0x%x\n", i, (UInt)msg);
154 }
155 else if ((i % 1000) == 0) {
156 printf("MessageQ_get #%d Msg = 0x%x\n", i, (UInt)msg);
157 }
158 }
160 printf("Exchanged %d messages with remote processor %s\n",
161 (i-1), MultiProc_getName(procId));
163 if (status >= 0) {
164 printf("Sample application successfully completed!\n");
165 }
167 MessageQ_free(msg);
168 MessageQ_close(&queueId);
170 cleanup:
171 /* Clean-up */
172 status = MessageQ_delete(&msgqHandle);
173 if (status < 0) {
174 printf("Error in MessageQ_delete [%d]\n", status);
175 }
177 exit:
178 printf("Leaving MessageQApp_execute\n\n");
180 return (status);
181 }
183 int main (int argc, char ** argv)
184 {
185 Int status = 0;
186 UInt numLoops = NUM_LOOPS_DFLT;
187 UInt16 procId = PROC_ID_DFLT;
189 /* Parse Args: */
190 switch (argc) {
191 case 1:
192 /* use defaults */
193 break;
194 case 2:
195 numLoops = atoi(argv[1]);
196 break;
197 case 3:
198 numLoops = atoi(argv[1]);
199 procId = atoi(argv[2]);
200 break;
201 default:
202 printf("Usage: %s [<numLoops>] [<ProcId>]\n", argv[0]);
203 printf("\tDefaults: numLoops: %d; ProcId: %d\n",
204 NUM_LOOPS_DFLT, PROC_ID_DFLT);
205 exit(0);
206 }
208 /* configure the transport factory */
209 Ipc_transportConfig(&TransportRpmsg_Factory);
211 /* IPC initialization */
212 status = Ipc_start();
214 if (status < 0) {
215 printf("Error: Ipc_start failed, error=%d\n", status);
216 goto exit;
217 }
219 if ((procId == 0) || (procId >= MultiProc_getNumProcessors())) {
220 printf("ProcId (%d) must be nonzero and less than %d\n",
221 procId, MultiProc_getNumProcessors());
222 Ipc_stop();
223 exit(0);
224 }
225 printf("Using numLoops: %d; procId : %d\n", numLoops, procId);
227 if (status >= 0) {
228 MessageQApp_execute(numLoops, procId);
229 Ipc_stop();
230 }
231 else {
232 printf("Ipc_start failed: status = %d\n", status);
233 }
235 exit:
236 return (status);
237 }