Index: firmware/App/Services/CommBuffers.c =================================================================== diff -u -r0dec8744af40d0c87a6d7cd1923920c1c2bd1d2f -rf308cc4c35eab630ebbbde405cfe47d049afeafb --- firmware/App/Services/CommBuffers.c (.../CommBuffers.c) (revision 0dec8744af40d0c87a6d7cd1923920c1c2bd1d2f) +++ firmware/App/Services/CommBuffers.c (.../CommBuffers.c) (revision f308cc4c35eab630ebbbde405cfe47d049afeafb) @@ -17,7 +17,8 @@ #include // for memcpy() -#include "CommBuffers.h" +#include "CommBuffers.h" +#include "SystemComm.h" #include "SystemCommMessages.h" #include "Timers.h" @@ -32,12 +33,10 @@ static volatile U32 commBufferByteCount[ NUM_OF_COMM_BUFFERS ][ DOUBLE_BUFFERS ]; // for each buffer, how many bytes does it contain? (also index to next available) static volatile U32 activeDoubleBuffer[ NUM_OF_COMM_BUFFERS ]; // for each buffer, which double buffer is being fed right now? static U08 commBuffers[ NUM_OF_COMM_BUFFERS ][ DOUBLE_BUFFERS ][ COMM_BUFFER_LENGTH ]; // each is double buffered to avoid thread contention -static volatile BOOL bufferGetLock[ NUM_OF_COMM_BUFFERS ]; // prevent getter from accessing active buffer while add in progress static U32 firstBufferOverflowTimeStamp = 0; // time stamp of a prior overflow event - allows for an overflow persistence check // ********** private function prototypes ********** -static void clearBuffer( COMM_BUFFER_T buffer ); static U32 switchDoubleBuffer( COMM_BUFFER_T buffer ); static void getDataFromInactiveBuffer( COMM_BUFFER_T buffer, U08 *data, U32 len ); @@ -47,7 +46,6 @@ * @details * Inputs : none * Outputs : CommBuffers module initialized. - * @param none * @return none *************************************************************************/ void initCommBuffers( void ) @@ -69,24 +67,27 @@ * @details * Inputs : none * Outputs : given buffer is cleared. - * @param buffer : the buffer to clear + * @param buffer the buffer to clear * @return none *************************************************************************/ -static void clearBuffer( COMM_BUFFER_T buffer ) +void clearBuffer( COMM_BUFFER_T buffer ) { if ( buffer < NUM_OF_COMM_BUFFERS ) { - S32 d,i; + S32 d; + // thread protection for queue operations + _disable_IRQ(); + activeDoubleBuffer[ buffer ] = 0; - for ( d = 0; d < DOUBLE_BUFFERS; d++ ) - { - commBufferByteCount[ buffer ][ d ] = 0; - for ( i = 0; i < COMM_BUFFER_LENGTH; i++ ) - { - commBuffers[ buffer ][ d ][ i ] = 0; - } - } + for ( d = 0; d < DOUBLE_BUFFERS; d++ ) + { + commBufferByteCount[ buffer ][ d ] = 0; + memset( &commBuffers[ buffer ][ d ][ 0 ], 0, COMM_BUFFER_LENGTH ); + } + + // release thread protection + _enable_IRQ(); } } @@ -100,9 +101,9 @@ * @details * Inputs : commBufferByteCount[], activeDoubleBuffer[] * Outputs : commBuffers[], commBufferByteCount[] - * @param buffer : which comm buffer to add data to - * @param data : pointer to byte array containing data to add - * @param len : length of data (in bytes) + * @param buffer which comm buffer to add data to + * @param data pointer to byte array containing data to add + * @param len length of data (in bytes) * @return TRUE if data added to buffer successfully, FALSE if not *************************************************************************/ BOOL addToCommBuffer( COMM_BUFFER_T buffer, U08* data, U32 len ) @@ -114,61 +115,63 @@ { BOOL bufferFull = FALSE; U32 activeBuffer; - U32 currentActiveBufCount; // where to start adding new data to buffer (after existing data) + U32 currentActiveBufCount; // where to start adding new data to buffer (after existing data) + + // prevent adding to out-going CAN buffer if DG is only node on CAN bus + if ( ( FALSE == isDGOnlyCANNode() ) || ( FALSE == isCANBoxForXmit( (CAN_MESSAGE_BOX_T)buffer ) ) ) + { + // thread protection for queue operations + _disable_IRQ(); - // thread protection for queue operations - _disable_IRQ(); - bufferGetLock[ buffer ] = TRUE; + activeBuffer = activeDoubleBuffer[ buffer ]; + currentActiveBufCount = commBufferByteCount[ buffer ][ activeBuffer ]; - activeBuffer = activeDoubleBuffer[ buffer ]; - currentActiveBufCount = commBufferByteCount[ buffer ][ activeBuffer ]; + // check to make sure buffer is not too full to service this add + if ( len <= ( COMM_BUFFER_LENGTH - currentActiveBufCount ) ) + { + U08 *buffPtr; // buffer destination for added data - // check to make sure buffer is not too full to service this add - if ( len <= ( COMM_BUFFER_LENGTH - currentActiveBufCount ) ) - { - U08 *buffPtr; // buffer destination for added data + // set destination pointer to end of active buffer data + buffPtr = &commBuffers[ buffer ][ activeBuffer ][ currentActiveBufCount ]; + // copy source data to destination buffer + memcpy( buffPtr, data, len ); + // adjust buffer count per this data add (also reserves space to add data before releasing thread protection) + commBufferByteCount[ buffer ][ activeBuffer ] += len; + // data successfully added to buffer + result = TRUE; + } + else // buffer too full to add this much data + { + bufferFull = TRUE; + } + // release thread protection + _enable_IRQ(); + } - // set destination pointer to end of active buffer data - buffPtr = &commBuffers[ buffer ][ activeBuffer ][ currentActiveBufCount ]; - // copy source data to destination buffer - memcpy( buffPtr, data, len ); - // adjust buffer count per this data add (also reserves space to add data before releasing thread protection) - commBufferByteCount[ buffer ][ activeBuffer ] += len; - // data successfully added to buffer - result = TRUE; - } - else // buffer too full to add this much data - { - bufferFull = TRUE; - clearBuffer( buffer ); - } - // release thread protection - bufferGetLock[ buffer ] = FALSE; - _enable_IRQ(); // if buffer was full, check persistence - trigger s/w fault if persists if ( TRUE == bufferFull ) { +#ifdef DEBUG_ENABLED + char debugStr[100]; + sprintf( debugStr, "Buf OF:#%3d,%3d, %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n", buffer, len, \ + data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7] ); + sendDebugData( (U08*)debugStr, strlen(debugStr) ); + sendDebugDataToUI( (U08*)debugStr ); +#endif + clearBuffer( buffer ); // not first overflow? if ( firstBufferOverflowTimeStamp != 0 ) { // if buffer overflows persists, fault if ( calcTimeSince( firstBufferOverflowTimeStamp ) > BUFFER_OVERFLOW_PERSISTENCE_MS ) { - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_COMM_BUFFERS_ADD_TOO_MUCH_DATA, len ) + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_COMM_BUFFERS_ADD_TOO_MUCH_DATA, (U32)buffer ) } } else // first overflow - set time stamp for persistence check { firstBufferOverflowTimeStamp = getMSTimerCount(); } -#ifdef DEBUG_ENABLED - { - // TODO - temporary debug code - remove later - char debugStr[ 256 ]; - sprintf( debugStr, "Comm Buffer Overflow:%5d \n", (U32)buffer ); - sendDebugData( (U08*)debugStr, strlen(debugStr) ); - } -#endif } else { // if good for persistence time period, reset persistence check @@ -199,9 +202,9 @@ * Inputs : commBuffers[], commBufferByteCount[], activeDoubleBuffer[] * Outputs : commBuffers[], commBufferByteCount[], activeDoubleBuffer[], \n * and the given data array is populated with data from the buffer. - * @param buffer : which comm buffer to retrieve data from - * @param data : pointer to byte array to stuff data into - * @param len : # of bytes to retrieve into given data array. + * @param buffer which comm buffer to retrieve data from + * @param data pointer to byte array to stuff data into + * @param len # of bytes to retrieve into given data array. * @return the number of bytes retrieved. *************************************************************************/ U32 getFromCommBuffer( COMM_BUFFER_T buffer, U08* data, U32 len ) @@ -258,9 +261,9 @@ * @details * Inputs : commBuffers[], commBufferByteCount[], activeDoubleBuffer[] * Outputs : given array populated with requested # of bytes from the buffer. - * @param buffer : which comm buffer to retrieve data from - * @param data : pointer to byte array to stuff data into - * @param len : # of bytes to retrieve into given data array. + * @param buffer which comm buffer to retrieve data from + * @param data pointer to byte array to stuff data into + * @param len # of bytes to retrieve into given data array. * @return the number of bytes retrieved. *************************************************************************/ U32 peekFromCommBuffer( COMM_BUFFER_T buffer, U08 *data, U32 len ) @@ -313,7 +316,7 @@ * @details * Inputs : activeDoubleBuffer[], commBufferByteCount[] * Outputs : none - * @param buffer : which comm buffer to get byte count for + * @param buffer which comm buffer to get byte count for * @return the number of bytes in the given comm buffer. *************************************************************************/ U32 numberOfBytesInCommBuffer( COMM_BUFFER_T buffer ) @@ -326,14 +329,7 @@ U32 activeBuffer = activeDoubleBuffer[ buffer ]; U32 inactiveBuffer = GET_TOGGLE( activeBuffer, 0, 1 ); - if ( FALSE == bufferGetLock[ buffer ] ) - { - result = commBufferByteCount[ buffer ][ inactiveBuffer ] + commBufferByteCount[ buffer ][ activeBuffer ]; - } - else - { - result = commBufferByteCount[ buffer ][ inactiveBuffer ]; - } + result = commBufferByteCount[ buffer ][ inactiveBuffer ] + commBufferByteCount[ buffer ][ activeBuffer ]; } else // invalid buffer { @@ -352,7 +348,7 @@ * @details * Inputs : activeDoubleBuffer[] * Outputs : activeDoubleBuffer[], commBufferByteCount[] - * @param buffer : which comm buffer to switch double buffers on + * @param buffer which comm buffer to switch double buffers on * @return the new active buffer for the given buffer. *************************************************************************/ static U32 switchDoubleBuffer( COMM_BUFFER_T buffer ) @@ -377,9 +373,9 @@ * @details * Inputs : commBuffers[], activeDoubleBuffer[], commBufferByteCount[] * Outputs : commBuffers[], activeDoubleBuffer[], commBufferByteCount[] - * @param buffer : which comm buffer get data from - * @param data : pointer to byte array to populate with data - * @param len : # of bytes to get from comm buffer + * @param buffer which comm buffer get data from + * @param data pointer to byte array to populate with data + * @param len # of bytes to get from comm buffer * @return none *************************************************************************/ static void getDataFromInactiveBuffer( COMM_BUFFER_T buffer, U08 *data, U32 len )