Hello,
I think that the main problem with your code is that you have not met the SPI data format requirements of the slave device, as detailed in its datasheet.
You do not say, but am I correct in assuming that "PORTP0" represents the signal SEN0, and "PORTP1" the signal SEN1? It would appear that SEN0 should remain inactive high for the whole period of a command sequence. Similarly SEN1 should remain inactive high for the whole period of an event interface sequence.
Additionally, a command may have either no operand, or a single 16-bit (word) operand. Your code does not currently allow for either of these alternatives. So we appear to require either a total transmission of 8-bits for no operand, or alternatively a total of 24-bits.
It would appear that you may need a separate function for each case, or a special "invalid" operand value that is outside the normal operand range. A value of 0xFFFF might be such a value. Perhaps something like the following untested code would work -
word SPIcommand( byte command, word operand)
{
byte recvd1;
byte recvd2;
PORTP0_SetVal(); // Ensure SEN0 event interface disabled
PORTP1_ClrVal(); // Enable SEN1 command interface
SPI3_SendChar(command);
if (operand < 0xFFFF) { // Test for operand present
SPI3_SendChar((byte)(operand / 256)); // High operand byte
SPI3_SendChar((byte)(operand % 256)); // Low operand byte
} PORTP1_SetVal(); // Set SEN1 inactive
while (!SRREADY_GetVal()); // Wait until ready for data return
PORTP1_ClrVal(); // Re-enable SEN1 command interface
SPI3_RecvChar(&recvd1); // High return byte
SPI3_RecvChar(&recvd2); // Low return byte
PORTP1_SetVal(); // Set SEN1 inactive
return (recvd1 * 256 + recvd2);
}
Regards,
Mac