Cleaned up code repetitions of messaging and shared memory read/write.
[processor-sdk/performance-audio-sr.git] / pasdk / test_dsp / framework / aspMsg_master.c
index 8cd870326e85b959d4c65f81aae3ebc3b167780a..6a84fb9deeaed72dfb54013b97a4ffde5f730ea2 100644 (file)
@@ -40,7 +40,7 @@ All rights reserved.
 #include <xdc/runtime/Log.h>
 #include <xdc/runtime/Memory.h>
 #include <xdc/runtime/System.h>
-
+#include <string.h>
 #include <ti/sysbios/heaps/HeapBuf.h>
 
 #include <ti/ipc/MessageQ.h>
@@ -49,6 +49,8 @@ All rights reserved.
 
 #include "aspMsg_common.h"
 #include "aspMsg_master.h"
+#include "audioStreamProc_common.h"
+#include "common.h"
 
 #define AspMsg_MasterToSlaveMsgHeapId ( 0 )
 
@@ -166,3 +168,77 @@ Int AspMsgMaster_init(
     Log_print1(Diags_EXIT, "<-- AspMsgMaster_init(): %d", (IArg)status);    
     return 0;
 }
+
+/************************************************************************************
+ * ASP message sending function. Refer to aspMsg_master.h for detailed description.
+************************************************************************************/
+Int AspMsgSend(UInt32 sndCmd, UInt32 ackCmd, char *sndMsgBuf, char *ackMsgBuf)
+{
+    ASP_Msg* pAspMsg;                   /* Messaging */
+    Int status, retVal;
+
+    // Init return vallue - no error in messaging operation, even though there
+    // may be error in returned (acknowledgement) message.
+    retVal = ASP_MSG_NO_ERR;
+
+    // allocate message
+    pAspMsg = (ASP_Msg *)MessageQ_alloc(hAspMsgMaster->heapId, hAspMsgMaster->msgSize);
+    if (pAspMsg == NULL) {
+        TRACE_TERSE0("MessageQ_alloc() failure.");
+        SW_BREAKPOINT; // temporary
+        retVal = ASP_MSG_ERR_QUEUE_ALLOC;
+    }
+
+    // set the return address in the message header and fill in message payload
+    MessageQ_setReplyQueue(hAspMsgMaster->masterQue, (MessageQ_Msg)pAspMsg);
+    pAspMsg->cmd = sndCmd;
+    pAspMsg->procId = hAspMsgMaster->masterProcId;
+    pAspMsg->messageId = hAspMsgMaster->messageId & ~(1<<31);
+    pAspMsg->expectResp = TRUE;
+
+    if(sndMsgBuf != NULL) {
+        memcpy(pAspMsg->buf, sndMsgBuf, ASP_MSG_BUF_LEN);
+    }
+
+    // send the message
+    TRACE_TERSE3("ASP message: procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
+    status = MessageQ_put(hAspMsgMaster->slaveQue, (MessageQ_Msg)pAspMsg);
+    if (status != MessageQ_S_SUCCESS) {
+        SW_BREAKPOINT;
+        retVal = ASP_MSG_ERR_QUEUE_PUT;
+    }
+
+    // wait for complete message from slave
+    status = MessageQ_get(hAspMsgMaster->masterQue, (MessageQ_Msg *)&pAspMsg, MessageQ_FOREVER);
+    if (status != MessageQ_S_SUCCESS) {
+        TRACE_TERSE0("MessageQ_get() failure.");
+        SW_BREAKPOINT; // temporary
+        retVal = ASP_MSG_ERR_QUEUE_GET;
+    }
+
+    // check if returned message is valid
+    if ((pAspMsg->procId != hAspMsgMaster->slaveProcId) ||
+        (pAspMsg->cmd != ackCmd) ||
+        (pAspMsg->messageId != (hAspMsgMaster->messageId | ((UInt32)1<<31)))) {
+        TRACE_TERSE3("ERROR: ASP message: procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
+        SW_BREAKPOINT;
+        retVal = ASP_MSG_ERR_ACKNOWLEDGE;
+    }
+
+    hAspMsgMaster->messageId = (hAspMsgMaster->messageId + 1) & ~(1<<31);
+    TRACE_TERSE3("ASP message: procId=%d, cmd=%d, messageId=0x%04x", pAspMsg->procId, pAspMsg->cmd, pAspMsg->messageId);
+
+    if(ackMsgBuf != NULL) {
+        memcpy(ackMsgBuf, pAspMsg->buf, ASP_MSG_BUF_LEN);
+    }
+
+    /* free the message */
+    status = MessageQ_free((MessageQ_Msg)pAspMsg);
+    if (status != MessageQ_S_SUCCESS) {
+        SW_BREAKPOINT;
+        retVal = ASP_MSG_ERR_QUEUE_FREE;
+    }
+
+    return retVal;
+} /* AspMsgSend */
+