Index: firmware/App/Services/SystemComm.c =================================================================== diff -u -rde5a0d43bdef611d963d11855bc958a8d8899a09 -r194cc4e5fbe048bd60b137c33e68e96a1ae39dc5 --- firmware/App/Services/SystemComm.c (.../SystemComm.c) (revision de5a0d43bdef611d963d11855bc958a8d8899a09) +++ firmware/App/Services/SystemComm.c (.../SystemComm.c) (revision 194cc4e5fbe048bd60b137c33e68e96a1ae39dc5) @@ -32,33 +32,40 @@ #include "CPLD.h" #endif +/** + * @addtogroup SystemComm + * @{ + */ + // ********** private definitions ********** -#define NUM_OF_CAN_OUT_BUFFERS 5 // # of CAN buffers for transmit -#define NUM_OF_CAN_IN_BUFFERS 7 // # of CAN buffers for receiving -#ifdef DEBUG_ENABLED - #define NUM_OF_MSG_IN_BUFFERS 8 // # of Msg buffers for receiving - 1 is UART +#define NUM_OF_CAN_OUT_BUFFERS 5 ///< number of CAN buffers for transmit +#define NUM_OF_CAN_IN_BUFFERS 7 ///< number of CAN buffers for receiving +#ifndef DEBUG_ENABLED + #define NUM_OF_MSG_IN_BUFFERS 7 ///< number of Msg buffers for receiving #else - #define NUM_OF_MSG_IN_BUFFERS 7 // # of Msg buffers for receiving + #define NUM_OF_MSG_IN_BUFFERS 8 + #define SCI1_RECEIVE_DMA_REQUEST 30 + #define SCI1_TRANSMIT_DMA_REQUEST 31 #endif -#define SCI1_RECEIVE_DMA_REQUEST 30 -#define SCI1_TRANSMIT_DMA_REQUEST 31 #define CAN_XMIT_PACKET_TIMEOUT_MS 200 ///< if transmitted CAN frame does not cause a transmit complete interrupt within this time, re-send or move on +#define MAX_XMIT_RETRIES 5 ///< maximum number of retries on no transmit complete interrupt timeout -#define UI_COMM_TIMEOUT_IN_MS 5000 -#define DG_COMM_TIMEOUT_IN_MS 2000 +#define UI_COMM_TIMEOUT_IN_MS 5000 ///< UI has not checked in for this much time +#define DG_COMM_TIMEOUT_IN_MS 2000 ///< DG has not checked in for this much time -#define MAX_COMM_CRC_FAILURES 5 -#define MAX_COMM_CRC_FAILURE_WINDOW_MS (10 * SEC_PER_MIN * MS_PER_SECOND) +#define MAX_COMM_CRC_FAILURES 5 ///< maximum number of CRC errors within window period before alarm +#define MAX_COMM_CRC_FAILURE_WINDOW_MS (10 * SEC_PER_MIN * MS_PER_SECOND) ///< CRC error window -#define MSG_NOT_ACKED_TIMEOUT_MS 150 -#define MSG_NOT_ACKED_MAX_RETRIES 3 -#define PENDING_ACK_LIST_SIZE 25 +#define MSG_NOT_ACKED_TIMEOUT_MS 150 ///< maximum time for a Denali message that requires ACK to be ACK'd +#define MSG_NOT_ACKED_MAX_RETRIES 3 ///< maximum number of times a message that requires ACK that was not ACK'd can be re-sent before alarm +#define PENDING_ACK_LIST_SIZE 25 ///< maximum number of Delanli messages that can be pending ACK at any given time #pragma pack(push, 1) +/// Record for transmitted message that is pending acknowledgement from receiver. typedef struct { BOOL used; @@ -74,6 +81,7 @@ // ********** private data ********** +/// Array of out-going CAN buffers. const COMM_BUFFER_T CAN_OUT_BUFFERS[ NUM_OF_CAN_OUT_BUFFERS ] = { COMM_BUFFER_OUT_CAN_HD_ALARM, @@ -83,6 +91,7 @@ COMM_BUFFER_OUT_CAN_PC }; +/// Array of in-coming CAN buffers. const COMM_BUFFER_T MSG_IN_BUFFERS[ NUM_OF_MSG_IN_BUFFERS ] = { COMM_BUFFER_IN_CAN_DG_ALARM, @@ -97,45 +106,38 @@ #endif }; -static U08 lastCANPacketSent[ CAN_MESSAGE_PAYLOAD_SIZE ]; -static CAN_MESSAGE_BOX_T lastCANPacketSentChannel = (CAN_MESSAGE_BOX_T)0; -static U32 lastCANPacketSentTimeStamp = 0; +static U08 lastCANPacketSent[ CAN_MESSAGE_PAYLOAD_SIZE ]; ///< Keep last packet sent on CAN bus in case we need to re-send. +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. -#ifdef DEBUG_ENABLED - static U08 pcXmitPacket[ 1024 ]; - static U08 pcRecvPacket[ PC_MESSAGE_PACKET_SIZE ] = { 0, 0, 0, 0, 0, 0, 0, 0 }; +static volatile PENDING_ACK_RECORD_T pendingAckList[ PENDING_ACK_LIST_SIZE ]; ///< list of outgoing messages that are awaiting an ACK - // DMA control records - static g_dmaCTRL pcDMAXmitControlRecord; // DMA transmit control record (UART-debug) - static g_dmaCTRL pcDMARecvControlRecord; // DMA receive control record (UART-debug) -#endif +static volatile BOOL hdIsOnlyCANNode = TRUE; ///< flag indicating whether HD is alone on CAN bus. +static U32 canXmitRetryCtr = 0; ///< counter for CAN transmit retries. +static volatile BOOL dgIsCommunicating = FALSE; ///< has DG sent a message since last check +static U32 timeOfLastDGCheckIn = 0; ///< last time DG checked in +static volatile BOOL uiIsCommunicating = FALSE; ///< has UI sent a message since last check +static U32 timeOfLastUICheckIn = 0; ///< last time UI checked in +static volatile BOOL uiDidCommunicate = FALSE; ///< has UI every sent a message -static PENDING_ACK_RECORD_T pendingAckList[ PENDING_ACK_LIST_SIZE ]; // list of outgoing messages that are awaiting an ACK - -static volatile BOOL dgIsCommunicating = FALSE; // has DG sent a message since last check -static U32 timeOfLastDGCheckIn = 0; // last time DG checked in -static volatile BOOL uiIsCommunicating = FALSE; // has UI sent a message since last check -static U32 timeOfLastUICheckIn = 0; // last time UI checked in -static volatile BOOL uiDidCommunicate = FALSE; // has UI every sent a message - -static U32 badCRCTimeStamps[ MAX_COMM_CRC_FAILURES ]; // time of last five bad message CRCs (wrapping list) -static U32 badCRCListIdx = 0; // where next bad message CRC time stamp will go in list -static U32 badCRCListCount = 0; // # of bad CRCs in the list - #ifdef EMC_TEST_BUILD -static U32 badCANCount; // test code in support of EMC testing + static U32 badCANCount; // test code in support of EMC testing #endif +#ifdef DEBUG_ENABLED + // debug buffers + static U08 pcXmitPacket[ 1024 ]; + static U08 pcRecvPacket[ PC_MESSAGE_PACKET_SIZE ] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + + // DMA control records + static g_dmaCTRL pcDMAXmitControlRecord; + static g_dmaCTRL pcDMARecvControlRecord; +#endif + // ********** private function prototypes ********** -#ifdef DEBUG_ENABLED - static void initUARTAndDMA( void ); - static U32 transmitNextUARTPacket( void ); -#endif +static void clearCANXmitBuffers( void ); -static BOOL isCANBoxForXmit( CAN_MESSAGE_BOX_T srcCANBox ); -static BOOL isCANBoxForRecv( CAN_MESSAGE_BOX_T srcCANBox ); - static COMM_BUFFER_T findNextHighestPriorityCANPacketToTransmit( void ); static U32 transmitNextCANPacket( void ); @@ -151,13 +153,17 @@ static BOOL matchACKtoPendingACKList( S16 seqNo ); static void checkPendingACKList( void ); -/************************************************************************* - * @brief initSystemComm +#ifdef DEBUG_ENABLED + static void initUARTAndDMA( void ); + static U32 transmitNextUARTPacket( void ); +#endif + +/*********************************************************************//** + * @brief * The initSystemComm function initializes the SystemComm module. * @details * Inputs : none * Outputs : SystemComm module initialized. - * @param none * @return none *************************************************************************/ void initSystemComm( void ) @@ -169,11 +175,8 @@ initUARTAndDMA(); #endif - // initialize bad message CRC list - for ( i = 0; i < MAX_COMM_CRC_FAILURES; i++ ) - { - badCRCTimeStamps[ i ] = 0; - } + // initialize bad message CRC time windowed count + initTimeWindowedCount( TIME_WINDOWED_COUNT_BAD_MSG_CRC, MAX_COMM_CRC_FAILURES, MAX_COMM_CRC_FAILURE_WINDOW_MS ); // initialize pending ACK list for ( i = 0; i < PENDING_ACK_LIST_SIZE; i++ ) @@ -182,14 +185,13 @@ } } -/************************************************************************* - * @brief checkInFromDG - * The checkInFromDG function checks in the DG with the HD - indicating that \n +/*********************************************************************//** + * @brief + * The checkInFromDG function checks in the DG with the HD - indicating that * the DG is communicating. * @details * Inputs : none * Outputs : dgIsCommunicating - * @param none * @return none *************************************************************************/ void checkInFromDG( void ) @@ -198,14 +200,13 @@ timeOfLastDGCheckIn = getMSTimerCount(); } -/************************************************************************* - * @brief checkInFromUI - * The checkInFromUI function checks in the UI with the HD - indicating that \n +/*********************************************************************//** + * @brief + * The checkInFromUI function checks in the UI with the HD - indicating that * the UI is communicating. * @details * Inputs : none * Outputs : uiIsCommunicating - * @param none * @return none *************************************************************************/ void checkInFromUI( void ) @@ -215,33 +216,27 @@ uiDidCommunicate = TRUE; } -/************************************************************************* - * @brief isDGCommunicating - * The isDGCommunicating function determines whether the DG is communicating \n +/*********************************************************************//** + * @brief + * The isDGCommunicating function determines whether the DG is communicating * with the HD. * @details * Inputs : dgIsCommunicating * Outputs : none - * @param none * @return TRUE if DG has checked in since last call, FALSE if not *************************************************************************/ BOOL isDGCommunicating( void ) { - BOOL result = dgIsCommunicating; - - dgIsCommunicating = FALSE; - - return result; + return dgIsCommunicating; } -/************************************************************************* - * @brief isUICommunicating - * The isUICommunicating function determines whether the UI is communicating \n +/*********************************************************************//** + * @brief + * The isUICommunicating function determines whether the UI is communicating * with the HD. * @details * Inputs : uiIsCommunicating * Outputs : none - * @param none * @return TRUE if UI has checked in since last call, FALSE if not *************************************************************************/ BOOL isUICommunicating( void ) @@ -253,27 +248,39 @@ return result; } -/************************************************************************* - * @brief uiCommunicated +/*********************************************************************//** + * @brief * The uiCommunicated function determines whether the UI has communicated. * @details * Inputs : uiDidCommunicate * Outputs : none - * @param none * @return TRUE if UI has communicated since power up, FALSE if not *************************************************************************/ BOOL uiCommunicated( void ) { return uiDidCommunicate; +} + +/*********************************************************************//** + * @brief + * The isHDOnlyCANNode function determines whether the HD is the only node + * currently on the CAN bus. + * @details + * Inputs : hdIsOnlyCANNode + * Outputs : none + * @return TRUE if HD is only node on CAN bus, FALSE if not + *************************************************************************/ +BOOL isHDOnlyCANNode( void ) +{ + return hdIsOnlyCANNode; } -/************************************************************************* - * @brief execSystemCommRx +/*********************************************************************//** + * @brief * The execSystemCommRx function manages received data from other sub-systems. * @details * Inputs : none - * Outputs : none - * @param none + * Outputs : Incoming messages parsed and processed. * @return none *************************************************************************/ void execSystemCommRx( void ) @@ -291,32 +298,65 @@ checkPendingACKList(); } -/************************************************************************* - * @brief execSystemCommTx - * The execSystemCommTx function manages data to be transmitted to other \n +/*********************************************************************//** + * @brief + * The execSystemCommTx function manages data to be transmitted to other * sub-systems. * @details * Inputs : none - * Outputs : none - * @param none + * Outputs : Outgoing messages transmitted. * @return none *************************************************************************/ void execSystemCommTx( void ) { - // if CAN transmitter is idle, start transmitting any pending packets - if ( 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 ) ) - { - // TODO - depending on why we timed out, we may need to reset CAN controller??? - // assume last packet was not successfully transmitted. TODO - Re-send last packet? Or should we move on? - canTransmit( canREG1, lastCANPacketSentChannel, lastCANPacketSent ); - } - } + // 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() ) + { + 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 ) ) + { + // assume last packet was not successfully transmitted. Re-send last packet. + if ( ++canXmitRetryCtr <= MAX_XMIT_RETRIES ) + { + // 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 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 @@ -327,30 +367,32 @@ #endif } -/************************************************************************* - * @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 +/*********************************************************************//** + * @brief + * The handleCANMsgInterrupt function handles a CAN message interrupt. + * This may have occurred because a CAN packet transmission has completed + * or because a CAN packet has been received. The appropriate handler is * called. * @details * Inputs : none * Outputs : message interrupt handled - * @param srcCANBox : which CAN message box triggered this interrupt + * @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 ) ) { - U32 bytesXmitted = transmitNextCANPacket(); + U32 bytesXmitted; + + bytesXmitted = transmitNextCANPacket(); // if nothing more to send, signal that transmitter is available if ( 0 == bytesXmitted ) { signalCANXmitsCompleted(); - } + } } else if ( TRUE == isCANBoxForRecv( srcCANBox ) ) { @@ -376,14 +418,13 @@ } } -/************************************************************************* - * @brief handleUARTMsgRecvPacketInterrupt - * The handleUARTMsgRecvPacketInterrupt function handles a DMA UART receive \n +/*********************************************************************//*** + * @brief + * The handleUARTMsgRecvPacketInterrupt function handles a DMA UART receive * packet completed interrupt. * @details * Inputs : none - * Outputs : none - * @param none + * Outputs : UART received packet interrupt handled. * @return none *************************************************************************/ #ifdef DEBUG_ENABLED @@ -398,14 +439,13 @@ } #endif -/************************************************************************* - * @brief handleUARTMsgXmitPacketInterrupt - * The handleUARTMsgXmitPacketInterrupt function handles a DMA UART transmit \n +/*********************************************************************//** + * @brief + * The handleUARTMsgXmitPacketInterrupt function handles a DMA UART transmit * packet completed interrupt. * @details * Inputs : none - * Outputs : none - * @param none + * Outputs : UART transmit packet interrupt handled. * @return none *************************************************************************/ #ifdef DEBUG_ENABLED @@ -420,14 +460,13 @@ } #endif -/************************************************************************* - * @brief initUARTAndDMA - * The initUARTAndDMA function initializes the SCI1 peripheral and the DMA \n +/*********************************************************************//** + * @brief + * The initUARTAndDMA function initializes the SCI1 peripheral and the DMA * to go with it for PC communication. * @details * Inputs : none * Outputs : SCI1 and DMA initialized - * @param none * @return none *************************************************************************/ #ifdef DEBUG_ENABLED @@ -487,17 +526,17 @@ } #endif -/************************************************************************* - * @brief isCANBoxForXmit - * The isCANBoxForXmit function determines whether a given CAN message box \n +/*********************************************************************//** + * @brief + * The isCANBoxForXmit function determines whether a given CAN message box * is configured for transmit. * @details * Inputs : CAN_OUT_BUFFERS[] * Outputs : none - * @param srcCANBox : which CAN message box to check + * @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 isCANBoxForXmit( CAN_MESSAGE_BOX_T srcCANBox ) { BOOL result = FALSE; U32 i; @@ -514,17 +553,17 @@ return result; } -/************************************************************************* - * @brief isCANBoxForRecv - * The isCANBoxForRecv function determines whether a given CAN message box \n +/*********************************************************************//** + * @brief + * The isCANBoxForRecv function determines whether a given CAN message box * is configured for receiving. * @details * Inputs : MSG_IN_BUFFERS[] * Outputs : none - * @param srcCANBox : which CAN message box to check + * @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 isCANBoxForRecv( CAN_MESSAGE_BOX_T srcCANBox ) { BOOL result = FALSE; U32 i; @@ -539,6 +578,24 @@ } return result; +} + +/*********************************************************************//** + * @brief + * The clearCANXmitBuffers function clears all CAN transmit buffers. + * @details + * Inputs : CAN_OUT_BUFFERS[] + * Outputs : CAN transmit buffers cleared. + * @return none + *************************************************************************/ +static void clearCANXmitBuffers( void ) +{ + U32 i; + + for ( i = 0; i < NUM_OF_CAN_OUT_BUFFERS; i++ ) + { + clearBuffer( CAN_OUT_BUFFERS[ i ] ); + } } @@ -547,15 +604,14 @@ *************************************************************************/ -/************************************************************************* - * @brief findNextHighestPriorityCANPacketToTransmit - * The findNextHighestPriorityCANPacketToTransmit function gets the next \n - * 8 byte packet and initiates a CAN transmit on the appropriate CAN channel. \n +/*********************************************************************//** + * @brief + * The findNextHighestPriorityCANPacketToTransmit function gets the next + * 8 byte packet and initiates a CAN transmit on the appropriate CAN channel. * @details * Inputs : Output CAN Comm Buffer(s) * Outputs : none - * @param msg : none - * @return buffer with highest priority CAN packet to transmit, \n + * @return buffer with highest priority CAN packet to transmit, * COMM_BUFFER_NOT_USED if not CAN packets pending transmit found *************************************************************************/ static COMM_BUFFER_T findNextHighestPriorityCANPacketToTransmit( void ) @@ -576,15 +632,14 @@ return result; } -/************************************************************************* - * @brief transmitNextCANPacket - * The transmitNextCANPacket function gets the next 8 byte packet and initiates \n +/*********************************************************************//** + * @brief + * The transmitNextCANPacket function gets the next 8 byte packet and initiates * a CAN transmit on the appropriate CAN channel. * @details * Inputs : Output CAN Comm Buffers * Outputs : CAN packet transmit initiated. - * @param msg : none - * @return # of bytes transmitted + * @return number of bytes transmitted *************************************************************************/ static U32 transmitNextCANPacket( void ) { @@ -615,27 +670,26 @@ { signalCANXmitsCompleted(); // TODO - shouldn't get here, but let's see if we do - SET_ALARM_WITH_1_U32_DATA( ALARM_ID_SOFTWARE_FAULT, (U32)mBox ) + SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, (U32)mBox ) } } else { // TODO - shouldn't get here - just testing - set first data to new s/w fault enum later - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_SOFTWARE_FAULT, (U32)buffer, (U32)dataSize ) + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, (U32)buffer, (U32)dataSize ) } } return result; } -/************************************************************************* - * @brief transmitNextUARTPacket - * The transmitNextUARTPacket function sets up and initiates a DMA transmit \n +/*********************************************************************//** + * @brief + * The transmitNextUARTPacket function sets up and initiates a DMA transmit * of the next packet pending transmit (if any) via UART. * @details * Inputs : Output UART Comm Buffer(s) * Outputs : UART DMA transmit initiated. - * @param msg : none - * @return # of bytes transmitted + * @return number of bytes transmitted *************************************************************************/ #ifdef DEBUG_ENABLED static U32 transmitNextUARTPacket( void ) @@ -667,14 +721,13 @@ *************************************************************************/ -/************************************************************************* - * @brief processIncomingData - * The processIncomingData function parses out messages from the Input \n +/*********************************************************************//** + * @brief + * The processIncomingData function parses out messages from the Input * Comm Buffers and adds them to the Received Message Queue. * @details * Inputs : Input Comm Buffers * Outputs : Parsed message(s) added to Received Message Queue - * @param msg : none * @return none *************************************************************************/ static void processIncomingData( void ) @@ -702,7 +755,10 @@ if ( numOfBytesInBuffer >= MESSAGE_OVERHEAD_SIZE ) { // peek at minimum of all bytes available or max message size (+1 for sync byte) U32 bytesPeeked = peekFromCommBuffer( MSG_IN_BUFFERS[ i ], data, MIN( numOfBytesInBuffer, sizeof( MESSAGE_WRAPPER_T ) + 1 ) ); - S32 msgSize = parseMessageFromBuffer( data, bytesPeeked ); + S32 msgSize = parseMessageFromBuffer( data, bytesPeeked ); + + hdIsOnlyCANNode = FALSE; // if we're getting a message, we can't be alone + canXmitRetryCtr = 0; if ( msgSize > 0 ) // valid, complete message found? { @@ -724,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? { @@ -747,14 +809,14 @@ } } -/************************************************************************* - * @brief consumeBufferPaddingBeforeSync - * The consumeBufferPaddingBeforeSync function removes any bytes in a given \n +/*********************************************************************//** + * @brief + * The consumeBufferPaddingBeforeSync function removes any bytes in a given * buffer that lie before a sync byte. * @details * Inputs : none - * Outputs : none - * @param msg : buffer : the comm buffer to process + * Outputs : Any padding at front of buffer is consumed. + * @param buffer the comm buffer to process * @return none *************************************************************************/ static void consumeBufferPaddingBeforeSync( COMM_BUFFER_T buffer ) @@ -778,16 +840,16 @@ } } -/************************************************************************* - * @brief parseMessageFromBuffer - * The parseMessageFromBuffer function looks for a complete message in a \n +/*********************************************************************//** + * @brief + * The parseMessageFromBuffer function looks for a complete message in a * given buffer. If a message is found, its size is returned. * @details * Inputs : none * Outputs : none - * @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, \n + * @param data pointer to byte array to search for a message + * @param len number of bytes in the data to search + * @return size of message if found, zero if no complete message found, * -1 if message found but CRC fails. *************************************************************************/ static S32 parseMessageFromBuffer( U08 *data, U32 len ) @@ -830,14 +892,13 @@ return result; } -/************************************************************************* - * @brief processReceivedMessages - * The processReceivedMessages function processes any messages in the \n +/*********************************************************************//** + * @brief + * The processReceivedMessages function processes any messages in the * received message queues. * @details * Inputs : Received Message Queues * Outputs : Message(s) processed. - * @param msg : none * @return none *************************************************************************/ static void processReceivedMessages( void ) @@ -881,14 +942,13 @@ } } -/************************************************************************* - * @brief checkForCommTimeouts - * The checkForCommTimeouts function checks for sub-system communication \n +/*********************************************************************//** + * @brief + * The checkForCommTimeouts function checks for sub-system communication * timeout errors. * @details * Inputs : timeOfLastDGCheckIn, timeOfLastUICheckIn * Outputs : possibly a comm t/o alarm - * @param none * @return none *************************************************************************/ static void checkForCommTimeouts( void ) @@ -899,14 +959,17 @@ { activateAlarmNoData( ALARM_ID_UI_COMM_TIMEOUT ); } + } + if ( TRUE == didTimeout( timeOfLastDGCheckIn, DG_COMM_TIMEOUT_IN_MS ) ) + { + dgIsCommunicating = FALSE; } - // TODO - check DG comm timeout } -/************************************************************************* - * @brief checkTooManyBadMsgCRCs - * The checkTooManyBadMsgCRCs function checks for too many bad message CRCs \n - * within a set period of time. Assumed function is being called when a new \n +/*********************************************************************//** + * @brief + * The checkTooManyBadMsgCRCs function checks for too many bad message CRCs + * within a set period of time. Assumed function is being called when a new * bad CRC is detected so a new bad CRC will be added to the list. * @details * Inputs : badCRCTimeStamps[], badCRCListIdx, badCRCListCount @@ -915,35 +978,32 @@ *************************************************************************/ static void checkTooManyBadMsgCRCs( void ) { - U32 listTimeInMS; - - // replace oldest bad CRC in list with this new one - badCRCTimeStamps[ badCRCListIdx ] = getMSTimerCount(); - // move list index to next position (may wrap) - badCRCListIdx = INC_WRAP( badCRCListIdx, 0, MAX_COMM_CRC_FAILURES - 1 ); - // update list count - badCRCListCount = INC_CAP( badCRCListCount, MAX_COMM_CRC_FAILURES ); - // check if too many bad CRCs in window of time - listTimeInMS = calcTimeSince( badCRCTimeStamps[ badCRCListIdx ] ); - if ( ( badCRCListCount >= MAX_COMM_CRC_FAILURES ) && ( listTimeInMS <= MAX_COMM_CRC_FAILURE_WINDOW_MS ) ) - { -#ifndef DISABLE_CRC_ERROR - activateAlarmNoData( ALARM_ID_COMM_TOO_MANY_BAD_CRCS ); -#endif - } + if ( TRUE == incTimeWindowedCount( TIME_WINDOWED_COUNT_BAD_MSG_CRC ) ) + { +#ifndef DISABLE_CRC_ERROR + SET_ALARM_WITH_1_U32_DATA( ALARM_ID_COMM_TOO_MANY_BAD_CRCS, (U32)ALARM_SOURCE_HD ); +#endif + } +#ifdef DEBUG_ENABLED + { + char debugStr[100]; + sprintf( debugStr, "SystemComm-HD-Bad Msg CRC.\n" ); + sendDebugDataToUI( (U08*)debugStr ); + } +#endif } -/************************************************************************* - * @brief addMsgToPendingACKList - * The addMsgToPendingACKList function adds a given message to the pending \n - * ACK list. Messages in this list will require receipt of an ACK message \n +/*********************************************************************//** + * @brief + * The addMsgToPendingACKList function adds a given message to the pending + * ACK list. Messages in this list will require receipt of an ACK message * for this particular message within a limited time. * @details * Inputs : pendingAckList[] * Outputs : pendingAckList[] - * @param msg : pointer to msg within the message data - * @param msgData : pointer to message data to add to pending ACK list - * @param len : # of bytes of message data + * @param msg pointer to msg within the message data + * @param msgData pointer to message data to add to pending ACK list + * @param len number of bytes of message data * @return TRUE if message added successfully, FALSE if not *************************************************************************/ BOOL addMsgToPendingACKList( MESSAGE_T *msg, COMM_BUFFER_T channel, U08 *msgData, U32 len ) @@ -966,28 +1026,30 @@ 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; } -/************************************************************************* - * @brief matchACKtoPendingACKList - * The matchACKtoPendingACKList function searches the pending ACK list to \n - * see if the sequence # from a received ACK msg matches any. If found, \n +/*********************************************************************//** + * @brief + * The matchACKtoPendingACKList function searches the pending ACK list to + * see if the sequence # from a received ACK msg matches any. If found, * the list entry is removed. * @details * Inputs : pendingAckList[] * Outputs : pendingAckList[] - * @param seqNo : sequence # to match to an entry in the list + * @param seqNo sequence # to match to an entry in the list * @return TRUE if a match was found, FALSE if not *************************************************************************/ static BOOL matchACKtoPendingACKList( S16 seqNo ) @@ -1009,15 +1071,14 @@ return result; } -/************************************************************************* - * @brief checkPendingACKList - * The checkPendingACKList function searches the pending ACK list to \n - * see if any have expired. Any such messages will be queued for retransmission \n +/*********************************************************************//** + * @brief + * The checkPendingACKList function searches the pending ACK list to + * see if any have expired. Any such messages will be queued for retransmission * and if max retries reached a fault is triggered. * @details * Inputs : pendingAckList[] * Outputs : pendingAckList[] - * @param none * @return none *************************************************************************/ static void checkPendingACKList( void ) @@ -1026,33 +1087,35 @@ // 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 } } } } -/************************************************************************* - * @brief processReceivedMessage +/*********************************************************************//** + * @brief * The processReceivedMessage function processes a given message. * @details * Inputs : none * Outputs : message processed - * @param message : pointer to message to process + * @param message pointer to message to process * @return none *************************************************************************/ static void processReceivedMessage( MESSAGE_T *message ) @@ -1069,6 +1132,14 @@ #endif break; + case MSG_ID_ALARM_TRIGGERED: + handleAlarmTriggered( message ); + break; + + case MSG_ID_ALARM_CLEARED: + handleAlarmCleared( message ); + break; + case MSG_ID_DG_CHECK_IN: handleDGCheckIn( message ); break; @@ -1131,8 +1202,24 @@ case MSG_ID_TESTER_LOGIN_REQUEST: handleTesterLogInRequest( message ); - break; - + break; + + case MSG_ID_UI_NEW_TREATMENT_PARAMS: + handleTreatmentParametersFromUI( message ); + break; + + case MSG_ID_UI_START_TREATMENT: + handleUIStartTreatmentMsg( message ); + break; + + case MSG_ID_UI_USER_CONFIRM_TREATMENT_PARAMS: + handleUIUserConfirmTreatmentParameters( message ); + break; + + case MSG_ID_UI_TREATMENT_END_REQUEST: + handleUIUserEndTreatmentRequest( message ); + break; + default: // TODO - unrecognized message ID received - ignore break; @@ -1143,10 +1230,6 @@ { switch ( msgID ) { - case MSG_ID_HD_MESSAGE: - handleTestHDMessageRequest( message ); - break; - case MSG_ID_OFF_BUTTON_STATE_OVERRIDE: handleTestOffButtonStateOverrideRequest( message ); break; @@ -1295,9 +1378,43 @@ handleTestHDSafetyShutdownOverrideRequest( message ); break; + case MSG_ID_HD_ACCEL_OVERRIDE: + handleTestHDAccelOverrideRequest( message ); + break; + + case MSG_ID_HD_ACCEL_MAX_OVERRIDE: + handleTestHDAccelMaxOverrideRequest( message ); + break; + + case MSG_ID_HD_ACCEL_SEND_INTERVAL_OVERRIDE: + handleTestHDAccelBroadcastIntervalOverrideRequest( message ); + break; + + case MSG_ID_HD_ACCEL_SET_CALIBRATION: + handleSetAccelCalibration( message ); + break; + + case MSG_ID_HD_BLOOD_FLOW_SET_CALIBRATION: + handleSetBloodFlowCalibration( message ); + break; + + case MSG_ID_HD_DIALYSATE_FLOW_SET_CALIBRATION: + handleSetDialysateFlowCalibration( message ); + break; + + case MSG_ID_DIAL_OUT_FLOW_SET_PT_OVERRIDE: + handleTestDialOutFlowSetPointOverrideRequest( message ); + break; + + case MSG_ID_HD_SET_PARAMETER_TREATMENT_PARAMETER: + handleTestSetTreatmentParameter( message ); + break; + default: // TODO - unrecognized message ID received - ignore break; } } } + +/**@}*/