I am writing a multithreaded communication interface where one function (in my main thread) pushes data into a queue (VxWorks msgQLib
) and another function in my communication task, fetches data from the queue to forward to the hardware interface.
Now, it seems like my push data doesn't align with my fetch data (or vice versa). I inserted some debugging messages to find out what's going on but have failed to find the culprit thus far. The code I have:
a little explanation. I built the functions so, that I insert a message tag with some meta data before the actual message i.e. each message will insert two items into the queue.
Please don't worry about the "reply" part yet, that still needs some work on either side. I'm now just worried about the outgoing part of the equation.
The function that pushes data into the Queue (in the application task):
static STATUS SPIQWriteRead_TDM(UINT8 cmdlen, UINT8 *cmd, UINT8 retdatlen, UINT8 *retdat)
{
UINT8 BytesRead;
UINT8 *msgTag[MSGTAGLEN]={0};
static UINT spi_init = 0;
UINT i=0;
//assemble message tag
msgTag[0] = 0x01; //FrameID
msgTag[1] = cmdlen/255; //MsgLen MSB
msgTag[2] = cmdlen%255; //MsgLen LSB
msgTag[3] = CS_TDM; // ChipSelect
msgTag[4] = 0; //SlotID
//copy message tag into queue
for(i=0;i<MSGTAGLEN;i++)
printf("<= msgTag[%d] 0x%x\n",i,msgTag[i]);
msgQSend(TDMTxQ,msgTag,MSGTAGLEN,NO_WAIT,MSG_PRI_NORMAL);
printf("<= TxQueue(0x%x) contains %d items\n",TDMTxQ, msgQNumMsgs(TDMTxQ));
for(i=0;i<cmdlen;i++)
printf("<= msg[%d] 0x%x\n",i,cmd[i]);
// copy message into queue
msgQSend(TDMTxQ,cmd,cmdlen,NO_WAIT,MSG_PRI_NORMAL);
printf("<= TxQueue(0x%x) contains %d items\n",TDMTxQ, msgQNumMsgs(TDMTxQ));
// wait for a maximum of 10 ticks (~1600ms) for the Reply
BytesRead = msgQReceive(TDMRxQ,retdat,retdatlen,100);
for(i=0;i<retdatlen;i++)
printf("retdat[%d] 0x%x\n",i,retdat[i]);
printf("BytesRead 0x%x\n",BytesRead);
// compare reply and return
if (BytesRead != retdatlen)
return ERROR;
else
return OK;
}
and the function that fetches data from the queue (in the communication task):
static FROM_Q_DAT *GetFromQueue(MSG_Q_ID TxQId)
{
UINT8 msgTag[MSGTAGLEN]= {0};
UINT8 FrameID = 0;
UINT16 MsgLen = 0;
UINT8 ChipSelect = 99;
UINT8 SlotID = 99;
static UINT8 *cmd=NULL;
UINT8 retdat[2]={0};
UINT8 cs=99;
static FROM_Q_DAT Qdat;
int retval = 0;
UINT8 i=0;
// read message tag from queue
msgQReceive(TxQId, msgTag, MSGTAGLEN, NO_WAIT);
for(i=0;i<MSGTAGLEN;i++)
printf("=> msgTag[%d] 0x%x\n",i,msgTag[i]);
// parse received message tag
FrameID = msgTag[0];
MsgLen = (((UINT16)msgTag[1])<<8) | (msgTag[2]);
ChipSelect = msgTag[3];
SlotID = msgTag[4];
printf("FrameID: %d\n",FrameID);
printf("MsgLen: %d\n",MsgLen);
printf("ChipSelect: %d\n",ChipSelect);
printf("SlotID: %d\n",SlotID);
printf("=> TxQueue(0x%x) contains %d items\n",TxQId, msgQNumMsgs(TxQId));
cmd = (UINT8*) calloc(MsgLen, sizeof(UINT8));
// Get the message from the queue
msgQReceive(TxQId, cmd, MsgLen, NO_WAIT);
for(i=0;i<MsgLen;i++)
printf("=> cmd[%d] 0x%x\n",i,cmd[i]);
printf("=> TxQueue(0x%x) contains %d items\n",TxQId, msgQNumMsgs(TxQId));
Qdat.cs = ChipSelect;
Qdat.len = MsgLen;
Qdat.data = cmd;
return &Qdat;
}
And a screen capture looks like this:
<= msgTag[0] 0x1
<= msgTag[1] 0x0
<= msgTag[2] 0x9
<= msgTag[3] 0x2
<= msgTag[4] 0x0
<= TxQueue(0x261c6010) contains 1 items
<= msg[0] 0x5e
<= msg[1] 0x8
<= msg[2] 0x40
<= msg[3] 0x6
<= msg[4] 0x0
<= msg[5] 0x0
<= msg[6] 0x0
<= msg[7] 0x0
<= msg[8] 0x0
<= TxQueue(0x261c6010) contains 2 items
//the next message (with '==' is from the communication engine that checks for content in the communication Queue:
== TxQueue(1) contains 2 items
=> msgTag[0] 0x0
=> msgTag[1] 0x0
=> msgTag[2] 0x0
=> msgTag[3] 0x1
=> msgTag[4] 0x0
FrameID: 0
MsgLen: 0
ChipSelect: 1
SlotID: 0
=> TxQueue(0x261c6010) contains 1 items
=> TxQueue(0x261c6010) contains 0 items
I don't see why my data that goes into the queue wouldn't come out...