Index: App/Services/SystemComm.c =================================================================== diff -u -r83de6e3e3de4767fd50713e18b0bd75a06479fc7 -r29f1ba03faefd982327916590818a260a3e4aa48 --- App/Services/SystemComm.c (.../SystemComm.c) (revision 83de6e3e3de4767fd50713e18b0bd75a06479fc7) +++ App/Services/SystemComm.c (.../SystemComm.c) (revision 29f1ba03faefd982327916590818a260a3e4aa48) @@ -24,12 +24,9 @@ #include "SystemCommMessages.h" #include "SystemComm.h" -// ***************************** TEST CODE ****************************** -// TODO - remove later -#if 1 -#include "CPLD.h" +#ifdef RM46_EVAL_BOARD_TARGET + #include "CPLD.h" #endif -// ************************** END TEST CODE ****************************** // ********** private definitions ********** @@ -58,6 +55,11 @@ // ********** private function prototypes ********** +static void handleCANPacketReceivedInt( CAN_MESSAGE_BOX_T srcCANBox ); +static void handleCANXmitCompleteInt( void ); +static BOOL isCANBoxForXmit( CAN_MESSAGE_BOX_T srcCANBox ); +static BOOL isCANBoxForRecv( CAN_MESSAGE_BOX_T srcCANBox ); + static COMM_BUFFER_T findNextHighestPriorityCANPacketToTransmit( void ); static void transmitNextCANPacket( void ); static void transmitPendingUARTData( void ); @@ -125,8 +127,92 @@ } } +/************************************************************************* + * @brief handleCANMsgInterrupt + * The handleCANMsgInterrupt function handles a CAN message interrupt. \n + * This may have occurred because a CAN packet transmission has completed \n + * or because a CAN packet has been received. The appropriate handler is \n + * called. + * @details + * Inputs : none + * Outputs : message interrupt handled + * @param srcCANBox : which CAN message box triggered this interrupt + * @return none + *************************************************************************/ +void handleCANMsgInterrupt( CAN_MESSAGE_BOX_T srcCANBox ) +{ + // message interrupt is for a transmit message box? + if ( TRUE == isCANBoxForXmit( srcCANBox ) ) + { + handleCANXmitCompleteInt(); + } + else if ( TRUE == isCANBoxForRecv( srcCANBox ) ) + { + handleCANPacketReceivedInt( srcCANBox ); + } + else + { + // shouldn't get here - not an active message box + // s/w fault? + } +} /************************************************************************* + * @brief isCANBoxForXmit + * The isCANBoxForXmit function determines whether a given CAN message box \n + * is configured for transmit. + * @details + * Inputs : CAN_OUT_BUFFERS[] + * Outputs : none + * @param srcCANBox : which CAN message box to check + * @return TRUE if the given CAN message box is configured for transmit, FALSE if not. + *************************************************************************/ +static BOOL isCANBoxForXmit( CAN_MESSAGE_BOX_T srcCANBox ) +{ + BOOL result = FALSE; + U32 i; + + for ( i = 0; i < NUM_OF_CAN_OUT_BUFFERS; i++ ) + { + if ( CAN_OUT_BUFFERS[i] == srcCANBox ) + { + result = TRUE; + break; + } + } + + return result; +} + +/************************************************************************* + * @brief isCANBoxForRecv + * The isCANBoxForRecv function determines whether a given CAN message box \n + * is configured for receiving. + * @details + * Inputs : CAN_IN_BUFFERS[] + * Outputs : none + * @param srcCANBox : which CAN message box to check + * @return TRUE if the given CAN message box is configured for receiving, FALSE if not. + *************************************************************************/ +static BOOL isCANBoxForRecv( CAN_MESSAGE_BOX_T srcCANBox ) +{ + BOOL result = FALSE; + U32 i; + + for ( i = 0; i < NUM_OF_CAN_IN_BUFFERS; i++ ) + { + if ( CAN_IN_BUFFERS[i] == srcCANBox ) + { + result = TRUE; + break; + } + } + + return result; +} + + +/************************************************************************* ********************** TRANSMIT SUPPORT FUNCTIONS ************************ *************************************************************************/ @@ -143,7 +229,7 @@ * @param none * @return none *************************************************************************/ -void handleCANXmitCompleteInt( void ) +static void handleCANXmitCompleteInt( void ) { transmitNextCANPacket(); } @@ -237,15 +323,15 @@ * @param srcCANBox : CAN message box that the packet arrived in * @return none *************************************************************************/ -void handleCANPacketReceivedInt( CAN_MESSAGE_BOX_T srcCANBox ) +static void handleCANPacketReceivedInt( CAN_MESSAGE_BOX_T srcCANBox ) { U08 data[CAN_MESSAGE_CARGO_SIZE]; - // TODO - get received packets from any/all CAN message boxes that have a new packet - if ( FALSE != canIsRxMessageArrived( canREG1, srcCANBox ) ) // TODO - is this even necessary? when interrupt is calling, don't we already know it's arrived? + // get CAN packet received on given CAN message box + if ( FALSE != canIsRxMessageArrived( canREG1, srcCANBox ) ) { canGetData( canREG1, srcCANBox, data ); - // TODO - select appropriate comm buffer based on the message box it came in on (s/b same #) + // add CAN packet to appropriate comm buffer based on the message box it came in on (s/b same #) addToCommBuffer( srcCANBox, data, CAN_MESSAGE_CARGO_SIZE ); } } @@ -266,7 +352,7 @@ U32 i; // TODO - remove this later - this should be called by CAN receive interrupt handler when implemented - handleCANPacketReceivedInt( COMM_BUFFER_IN_CAN_UI_2_HD ); +// handleCANPacketReceivedInt( COMM_BUFFER_IN_CAN_UI_2_HD ); // queue any received CAN messages for ( i = 0; i < NUM_OF_CAN_IN_BUFFERS; i++ ) @@ -278,18 +364,19 @@ // do we have enough bytes in buffer for smallest message? numOfBytesInBuffer = numberOfBytesInCommBuffer( CAN_IN_BUFFERS[i] ); if ( numOfBytesInBuffer >= MESSAGE_OVERHEAD_SIZE ) - { - U32 bytesPeeked = peekFromCommBuffer( CAN_IN_BUFFERS[i], data, MIN(numOfBytesInBuffer,(sizeof(MESSAGE_WRAPPER_T)+1)) ); + { // peek at minimum of all bytes available or max message size (+1 for sync byte) + U32 bytesPeeked = peekFromCommBuffer( CAN_IN_BUFFERS[i], data, MIN(numOfBytesInBuffer,sizeof(MESSAGE_WRAPPER_T)+1) ); U32 msgSize = parseMessageFromBuffer( data, bytesPeeked ); if ( msgSize > 0 ) - { - msgSize = getFromCommBuffer( CAN_IN_BUFFERS[i], data, msgSize ); - if ( msgSize > 0 ) + { // consume message (+sync byte) + msgSize = getFromCommBuffer( CAN_IN_BUFFERS[i], data, msgSize+1 ); + if ( msgSize > MESSAGE_OVERHEAD_SIZE ) { MESSAGE_WRAPPER_T rcvMsg; - U08 *dataPtr = data; + U08 *dataPtr = data+1; // skip over sync byte + // convert received message data to a message and add to message queue blankMessageInWrapper( &rcvMsg ); memcpy( &(rcvMsg.msg.hdr), dataPtr, sizeof(MESSAGE_HEADER_T) ); dataPtr += sizeof(MESSAGE_HEADER_T); @@ -321,12 +408,16 @@ // consume bytes out of buffer 1 at a time until we find the sync byte or it's empty while ( numOfBytesInBuffer > 0 ) { - getFromCommBuffer( buffer, &data, 1 ); + peekFromCommBuffer( buffer, &data, 1 ); if ( MESSAGE_SYNC_BYTE == data ) { break; // we found a sync - we're done } - numOfBytesInBuffer = numberOfBytesInCommBuffer( buffer ); + else // not a sync byte, so consume it + { + getFromCommBuffer( buffer, &data, 1 ); + numOfBytesInBuffer = numberOfBytesInCommBuffer( buffer ); + } } } @@ -422,12 +513,9 @@ { case MSG_ID_OFF_BUTTON_PRESS: handleOffButtonConfirmMsgFromUI( message ); -// ***************************** TEST CODE ****************************** -// TODO - remove later -#if 1 +#ifdef RM46_EVAL_BOARD_TARGET setUserLED( TRUE ); #endif -// ************************** END TEST CODE ****************************** break; default: