Index: firmware/App/Services/SystemComm.c =================================================================== diff -u -rc47140a246754809db1b8a44eac46be0139c66f7 -rcd2ba56ddf9443fc624c21764e6478766b7fd6ed --- firmware/App/Services/SystemComm.c (.../SystemComm.c) (revision c47140a246754809db1b8a44eac46be0139c66f7) +++ firmware/App/Services/SystemComm.c (.../SystemComm.c) (revision cd2ba56ddf9443fc624c21764e6478766b7fd6ed) @@ -110,7 +110,7 @@ static CAN_MESSAGE_BOX_T lastCANPacketSentChannel = (CAN_MESSAGE_BOX_T)0; ///< Keep channel last packet was sent on CAN bus in case we need to re-send. static U32 lastCANPacketSentTimeStamp = 0; ///< Keep time last packet sent on CAN bus so we can timeout on transmission attempt. -static PENDING_ACK_RECORD_T pendingAckList[ PENDING_ACK_LIST_SIZE ]; ///< list of outgoing messages that are awaiting an ACK +static volatile PENDING_ACK_RECORD_T pendingAckList[ PENDING_ACK_LIST_SIZE ]; ///< list of outgoing messages that are awaiting an ACK static volatile BOOL hdIsOnlyCANNode = TRUE; ///< flag indicating whether HD is alone on CAN bus. static U32 canXmitRetryCtr = 0; ///< counter for CAN transmit retries. @@ -227,11 +227,7 @@ *************************************************************************/ BOOL isDGCommunicating( void ) { - BOOL result = dgIsCommunicating; - - dgIsCommunicating = FALSE; - - return result; + return dgIsCommunicating; } /*********************************************************************//** @@ -313,44 +309,54 @@ *************************************************************************/ void execSystemCommTx( void ) { - // if CAN transmitter is idle, start transmitting any pending packets - if ( ( FALSE == hdIsOnlyCANNode ) && ( FALSE == isCAN1TransmitInProgress() ) ) - { - transmitNextCANPacket(); - } - else - { // generally, transmitter should not be busy at time of this function call - check timeout just in case so we don't get stuck waiting forever - if ( TRUE == didTimeout( lastCANPacketSentTimeStamp, CAN_XMIT_PACKET_TIMEOUT_MS ) ) + // don't bother with transmitting if no other nodes on CAN bus + if ( FALSE == hdIsOnlyCANNode ) + { + // if CAN transmitter is idle, start transmitting any pending packets + if ( FALSE == isCAN1TransmitInProgress() ) { - // assume last packet was not successfully transmitted. Re-send last packet. - if ( ++canXmitRetryCtr <= MAX_XMIT_RETRIES ) + transmitNextCANPacket(); + } + else + { + // generally, transmitter should not be busy at time of this function call - check timeout just in case so we don't get stuck waiting forever + if ( TRUE == didTimeout( lastCANPacketSentTimeStamp, CAN_XMIT_PACKET_TIMEOUT_MS ) ) { - canTransmit( canREG1, lastCANPacketSentChannel, lastCANPacketSent ); -#ifdef DEBUG_ENABLED + // assume last packet was not successfully transmitted. Re-send last packet. + if ( ++canXmitRetryCtr <= MAX_XMIT_RETRIES ) { - char debugStr[100]; - sprintf( debugStr, "SystemComm-HD resend Last Frame. %2d\n", lastCANPacketSentChannel ); - sendDebugDataToUI( (U08*)debugStr ); - } -#endif - } - // we must be only node on CAN bus - nobody is ACKing our transmitted frames - else - { - hdIsOnlyCANNode = TRUE; // set only CAN node flag - canXmitRetryCtr = MAX_XMIT_RETRIES; - signalCANXmitsCompleted(); // clear pending xmit flag - clearCANXmitBuffers(); // clear xmit buffers - nothing is going out right now + // ensure we have a previous CAN packet/channel to resend - canTransmit() channel param MUST be valid + if ( ( lastCANPacketSentChannel > COMM_BUFFER_NOT_USED ) && ( lastCANPacketSentChannel <= COMM_BUFFER_LAST_CAN_BUFFER ) ) + { + canTransmit( canREG1, lastCANPacketSentChannel, lastCANPacketSent ); + } #ifdef DEBUG_ENABLED - { - char debugStr[100]; - sprintf( debugStr, "SystemComm-HD is only node.\n" ); - sendDebugDataToUI( (U08*)debugStr ); + { + char debugStr[100]; + sprintf( debugStr, "SystemComm-HD resend Last Frame. %2d\n", lastCANPacketSentChannel ); + sendDebugData( (U08*)debugStr, strlen(debugStr) ); + sendDebugDataToUI( (U08*)debugStr ); + } +#endif } + // we must be only node on CAN bus - nobody is ACKing our transmitted frames + else + { + hdIsOnlyCANNode = TRUE; // set only CAN node flag + canXmitRetryCtr = MAX_XMIT_RETRIES; + signalCANXmitsCompleted(); // clear pending xmit flag + clearCANXmitBuffers(); // clear xmit buffers - nothing is going out right now +#ifdef DEBUG_ENABLED + { + char debugStr[100]; + sprintf( debugStr, "SystemComm-HD is only node.\n" ); + sendDebugData( (U08*)debugStr, strlen(debugStr) ); + } #endif + } } - } - } + } + } #ifdef DEBUG_ENABLED // if UART transmitter is idle, start transmitting any pending packets @@ -774,7 +780,13 @@ // copy CRC portion of message data to the new message rcvMsg.crc = *dataPtr; // add new message to queue for later processing - addToMsgQueue( MSG_Q_IN, &rcvMsg ); + addToMsgQueue( MSG_Q_IN, &rcvMsg ); + // if message from DG broadcast channel, update DG comm status + if ( COMM_BUFFER_IN_CAN_DG_BROADCAST == MSG_IN_BUFFERS[ i ] ) + { + dgIsCommunicating = TRUE; + timeOfLastDGCheckIn = getMSTimerCount(); + } } else if ( -1 == msgSize ) // candidate message with bad CRC found? { @@ -947,8 +959,11 @@ { activateAlarmNoData( ALARM_ID_UI_COMM_TIMEOUT ); } + } + if ( TRUE == didTimeout( timeOfLastDGCheckIn, DG_COMM_TIMEOUT_IN_MS ) ) + { + dgIsCommunicating = FALSE; } - // TODO - check DG comm timeout } /*********************************************************************//** @@ -1011,15 +1026,17 @@ pendingAckList[ i ].timeStamp = getMSTimerCount(); pendingAckList[ i ].retries = MSG_NOT_ACKED_MAX_RETRIES; pendingAckList[ i ].msgSize = len; - memcpy( pendingAckList[ i ].msg, msgData, len ); + memcpy( (U08*)pendingAckList[ i ].msg, msgData, len ); result = TRUE; break; } else { _enable_IRQ(); } - } + } + + // TODO - if no open slot found, s/w fault return result; } @@ -1070,21 +1087,23 @@ // find expired messages pending ACK for ( i = 0; i < PENDING_ACK_LIST_SIZE; i++ ) - { + { // pending ACK expired? if ( ( TRUE == pendingAckList[ i ].used ) && ( TRUE == didTimeout( pendingAckList[ i ].timeStamp, MSG_NOT_ACKED_TIMEOUT_MS ) ) ) - { + { // if retries left, reset and resend pending message if ( pendingAckList[ i ].retries > 0 ) { // re-queue message for transmit pendingAckList[ i ].retries--; pendingAckList[ i ].timeStamp = getMSTimerCount(); - addToCommBuffer( pendingAckList[ i ].channel, pendingAckList[ i ].msg, pendingAckList[ i ].msgSize ); - } + addToCommBuffer( pendingAckList[ i ].channel, (U08*)pendingAckList[ i ].msg, pendingAckList[ i ].msgSize ); + } + // if no retries left, alarm else { U16 msgID; - memcpy( &msgID, &pendingAckList[ i ].msg[ sizeof( U08 ) + sizeof( U16) ], sizeof( U16 ) ); + memcpy( &msgID, (U08*)&pendingAckList[ i ].msg[ sizeof( U08 ) + sizeof( U16) ], sizeof( U16 ) ); SET_ALARM_WITH_1_U32_DATA( ALARM_ID_CAN_MESSAGE_NOT_ACKED, (U32)msgID ); + pendingAckList[ i ].used = FALSE; // take pending message off of list } } }