Index: App/Services/SystemComm.c =================================================================== diff -u -r29f1ba03faefd982327916590818a260a3e4aa48 -ra87b6b9e253c6c0fcc84bca6f5de71959ce18bcc --- App/Services/SystemComm.c (.../SystemComm.c) (revision 29f1ba03faefd982327916590818a260a3e4aa48) +++ App/Services/SystemComm.c (.../SystemComm.c) (revision a87b6b9e253c6c0fcc84bca6f5de71959ce18bcc) @@ -243,11 +243,11 @@ * Outputs : none * @param msg : none * @return buffer with highest priority CAN packet to transmit, \n - * COMM_BUFFER_NOT_USED_1 if not CAN packets pending transmit found + * COMM_BUFFER_NOT_USED if not CAN packets pending transmit found *************************************************************************/ static COMM_BUFFER_T findNextHighestPriorityCANPacketToTransmit( void ) { - COMM_BUFFER_T result = COMM_BUFFER_NOT_USED_1; + COMM_BUFFER_T result = COMM_BUFFER_NOT_USED; U32 i; // search for next priority CAN packet to transmit @@ -278,7 +278,7 @@ COMM_BUFFER_T buffer = findNextHighestPriorityCANPacketToTransmit(); // if a buffer is found with a packet to transmit, get packet from buffer and transmit it - if ( buffer != COMM_BUFFER_NOT_USED_1 ) + if ( buffer != COMM_BUFFER_NOT_USED ) { U08 data[CAN_MESSAGE_CARGO_SIZE]; U32 dataSize = getFromCommBuffer( buffer, data, CAN_MESSAGE_CARGO_SIZE ); @@ -351,43 +351,52 @@ U08 data[sizeof(MESSAGE_WRAPPER_T)+1]; 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 ); - // queue any received CAN messages for ( i = 0; i < NUM_OF_CAN_IN_BUFFERS; i++ ) { - U32 numOfBytesInBuffer; + BOOL messagesInBuffer = TRUE; // assume true at first to get into while loop - // since messages can have CAN padding left unconsumed by last get, get padding out of buffer - consumeBufferPaddingBeforeSync( CAN_IN_BUFFERS[i] ); - // do we have enough bytes in buffer for smallest message? - numOfBytesInBuffer = numberOfBytesInCommBuffer( CAN_IN_BUFFERS[i] ); - if ( numOfBytesInBuffer >= MESSAGE_OVERHEAD_SIZE ) - { // 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 ); + while ( TRUE == messagesInBuffer ) + { + U32 numOfBytesInBuffer; - if ( msgSize > 0 ) - { // consume message (+sync byte) - msgSize = getFromCommBuffer( CAN_IN_BUFFERS[i], data, msgSize+1 ); - if ( msgSize > MESSAGE_OVERHEAD_SIZE ) + // assume false so we don't get stuck in loop - only set to true if we find another complete message in buffer + messagesInBuffer = FALSE; + + // since messages can have CAN padding left unconsumed by last get, get padding out of buffer + consumeBufferPaddingBeforeSync( CAN_IN_BUFFERS[i] ); + // do we have enough bytes in buffer for smallest message? + numOfBytesInBuffer = numberOfBytesInCommBuffer( CAN_IN_BUFFERS[i] ); + if ( numOfBytesInBuffer >= MESSAGE_OVERHEAD_SIZE ) + { // 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 ) { - MESSAGE_WRAPPER_T rcvMsg; - U08 *dataPtr = data+1; // skip over sync byte + // 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+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); - memcpy( &(rcvMsg.msg.cargo), dataPtr, rcvMsg.msg.hdr.cargoLen ); - dataPtr += rcvMsg.msg.hdr.cargoLen; - rcvMsg.crc = *dataPtr; - addToMsgQueue( MSG_Q_IN_CAN, &rcvMsg ); - } - } - } - } + messagesInBuffer = TRUE; + // 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); + memcpy( &(rcvMsg.msg.cargo), dataPtr, rcvMsg.msg.hdr.cargoLen ); + dataPtr += rcvMsg.msg.hdr.cargoLen; + rcvMsg.crc = *dataPtr; + addToMsgQueue( MSG_Q_IN_CAN, &rcvMsg ); + } // message is at least as large as minimum size + } // looks like there is a complete message in the comm buffer + } // enough data left in comm buffer to possibly be a complete message + } // while loop to get all complete messages for each comm buffer + } // for loop to check all comm buffers for messages + + // TODO - queue any received UART messages } /************************************************************************* @@ -424,13 +433,13 @@ /************************************************************************* * @brief parseMessageFromBuffer * The parseMessageFromBuffer function looks for a complete message in a \n - * given comm buffer. If found, the given buffer is populated with the \n - * message and its size is returned. + * given buffer. If a message is found, its size is returned. * @details - * Inputs : Comm Buffer + * Inputs : none * Outputs : none - * @param msg : data : pointer to byte array to populate with a message if found - * @return size of message, zero if no complete message found. + * @param data : pointer to byte array to search for a message + * @param len : # of bytes in the data to search + * @return size of message if found, zero if no complete message found. *************************************************************************/ static U32 parseMessageFromBuffer( U08 *data, U32 len ) { @@ -477,22 +486,25 @@ *************************************************************************/ static void processReceivedMessages( void ) { - BOOL isThereMsgRcvd; + BOOL isThereMsgRcvd = TRUE; // assume TRUE at first to get into while loop MESSAGE_WRAPPER_T message; - // see if any messages received - isThereMsgRcvd = getFromMsgQueue( MSG_Q_IN_CAN, &message ); - if ( TRUE == isThereMsgRcvd ) + while ( TRUE == isThereMsgRcvd ) { - // TODO - check CRC before processing a message - if ( 1 ) + // see if any messages received + isThereMsgRcvd = getFromMsgQueue( MSG_Q_IN_CAN, &message ); + if ( TRUE == isThereMsgRcvd ) { - processReceivedMessage( &message.msg ); + // TODO - check CRC before processing a message + if ( 1 ) + { + processReceivedMessage( &message.msg ); + } + else // CRC failed + { + // TODO - probably wouldn't want to fault on this. ignore? + } } - else // CRC failed - { - // TODO - probably wouldn't want to fault on this. ignore? - } } // TODO - process UART (script) messages too