Index: firmware/App/Services/CommBuffers.c =================================================================== diff -u -rc67def50892f9a7c2f1f22985b5351465a8f6773 -r30f049651877229042e3f8700c8596e5b9a1e0f4 --- firmware/App/Services/CommBuffers.c (.../CommBuffers.c) (revision c67def50892f9a7c2f1f22985b5351465a8f6773) +++ firmware/App/Services/CommBuffers.c (.../CommBuffers.c) (revision 30f049651877229042e3f8700c8596e5b9a1e0f4) @@ -16,7 +16,7 @@ ***************************************************************************/ #include -#include // for memcpy() +#include // For memcpy() #include "CommBuffers.h" #include "SystemComm.h" @@ -30,16 +30,16 @@ // ********** private definitions ********** -#define COMM_BUFFER_LENGTH 512 ///< max bytes in each comm buffer (each side of double buffer is this size) -#define DOUBLE_BUFFERS 2 ///< need 2 buffers for double buffering -#define BUFFER_OVERFLOW_PERSISTENCE_MS 5000 ///< how many ms buffer overflows must persist before fault +#define COMM_BUFFER_LENGTH 512 ///< Max bytes in each comm buffer (each side of double buffer is this size) +#define DOUBLE_BUFFERS 2 ///< Need 2 buffers for double buffering +#define BUFFER_OVERFLOW_PERSISTENCE_MS 5000 ///< How many ms buffer overflows must persist before fault // ********** private data ********** -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 U32 firstBufferOverflowTimeStamp = 0; ///< time stamp of a prior overflow event - allows for an overflow persistence check +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 U32 firstBufferOverflowTimeStamp = 0; ///< Time stamp of a prior overflow event - allows for an overflow persistence check // ********** private function prototypes ********** @@ -57,7 +57,7 @@ { S32 b; - // reset and zero out all buffers + // Reset and zero out all buffers for ( b = 0; b < NUM_OF_COMM_BUFFERS; b++ ) { clearBuffer( (COMM_BUFFER_T)b ); @@ -80,7 +80,7 @@ { S32 d; - // thread protection for queue operations + // Thread protection for queue operations _disable_IRQ(); activeDoubleBuffer[ buffer ] = 0; @@ -90,7 +90,7 @@ memset( &commBuffers[ buffer ][ d ][ 0 ], 0, COMM_BUFFER_LENGTH ); } - // release thread protection + // Release thread protection _enable_IRQ(); } } @@ -113,44 +113,44 @@ { BOOL result = FALSE; - // verify given buffer + // Verify given buffer if ( buffer < NUM_OF_COMM_BUFFERS ) { 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) if ( ( FALSE == isHDOnlyCANNode() ) || ( FALSE == isCANBoxForXmit( (CAN_MESSAGE_BOX_T)buffer ) ) ) { - // thread protection for queue operations + // Thread protection for queue operations _disable_IRQ(); activeBuffer = activeDoubleBuffer[ buffer ]; currentActiveBufCount = commBufferByteCount[ buffer ][ activeBuffer ]; - // check to make sure buffer is not too full to service this add + // 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 + U08 *buffPtr; // Buffer destination for added data - // set destination pointer to end of active buffer data + // Set destination pointer to end of active buffer data buffPtr = &commBuffers[ buffer ][ activeBuffer ][ currentActiveBufCount ]; - // copy source data to destination buffer + // 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) + // 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 + // Data successfully added to buffer result = TRUE; } - else // buffer too full to add this much data + else // Buffer too full to add this much data { bufferFull = TRUE; } - // release thread protection + // Release thread protection _enable_IRQ(); } - // if buffer was full, check persistence - trigger s/w fault if persists + // If buffer was full, check persistence - trigger s/w fault if persists if ( TRUE == bufferFull ) { #ifdef DEBUG_ENABLED @@ -160,29 +160,29 @@ sendDebugDataToUI( (U08*)debugStr ); #endif clearBuffer( buffer ); - // not first overflow? + // Not first overflow? if ( firstBufferOverflowTimeStamp != 0 ) { - // if buffer overflows persists, fault + // If buffer overflows persists, fault if ( calcTimeSince( firstBufferOverflowTimeStamp ) > BUFFER_OVERFLOW_PERSISTENCE_MS ) { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_COMM_BUFFERS_ADD_TOO_MUCH_DATA, (U32)buffer ) } } - else // first overflow - set time stamp for persistence check + else // First overflow - set time stamp for persistence check { firstBufferOverflowTimeStamp = getMSTimerCount(); } } else - { // if good for persistence time period, reset persistence check + { // If good for persistence time period, reset persistence check if ( ( firstBufferOverflowTimeStamp != 0 ) && ( calcTimeSince( firstBufferOverflowTimeStamp ) > BUFFER_OVERFLOW_PERSISTENCE_MS ) ) { firstBufferOverflowTimeStamp = 0; } } } - else // invalid buffer given + else // Invalid buffer given { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_COMM_BUFFERS_ADD_INVALID_BUFFER, buffer ) } @@ -211,38 +211,38 @@ { U32 result = 0; - // verify given buffer + // Verify given buffer if ( buffer < NUM_OF_COMM_BUFFERS ) { - // thread protection for queue operations + // Thread protection for queue operations _disable_IRQ(); - // verify requested # of bytes to get are in the buffer + // Verify requested # of bytes to get are in the buffer if ( ( len <= ( COMM_BUFFER_LENGTH * DOUBLE_BUFFERS ) ) && ( len <= numberOfBytesInCommBuffer( buffer ) ) ) { U32 activeBuffer = activeDoubleBuffer[ buffer ]; U32 inactiveBuffer = ( activeBuffer == 0 ? 1 : 0 ); U32 bytesInInactiveBuffer = commBufferByteCount[ buffer ][ inactiveBuffer ]; U32 sizeOfFirstConsumption = MIN( len, bytesInInactiveBuffer ); - // see what we can get from inactive buffer - getDataFromInactiveBuffer( buffer, data, sizeOfFirstConsumption ); // will switch double buffers if we empty inactive buffer - // will return # of bytes consumed + // See what we can get from inactive buffer + getDataFromInactiveBuffer( buffer, data, sizeOfFirstConsumption ); // Will switch double buffers if we empty inactive buffer + // Will return # of bytes consumed result = sizeOfFirstConsumption; - // do we need more from active buffer? + // Do we need more from active buffer? if ( len > bytesInInactiveBuffer ) { U32 remNumOfBytes = len - sizeOfFirstConsumption; U08 *remPtr = data + sizeOfFirstConsumption; getDataFromInactiveBuffer( buffer, remPtr, remNumOfBytes ); - // will return # of bytes consumed + // Will return # of bytes consumed result += remNumOfBytes; } } - // release thread protection + // Release thread protection _enable_IRQ(); } - else // invalid buffer given + else // Invalid buffer given { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_COMM_BUFFERS_GET_INVALID_BUFFER, buffer ) } @@ -269,12 +269,12 @@ { U32 numOfBytesPeeked = 0; - // verify given buffer + // Verify given buffer if ( buffer < NUM_OF_COMM_BUFFERS ) { - // thread protection for queue operations + // Thread protection for queue operations _disable_IRQ(); - // verify requested # of bytes to peek are in the buffer + // Verify requested # of bytes to peek are in the buffer if ( ( len <= ( COMM_BUFFER_LENGTH * DOUBLE_BUFFERS ) ) && ( len <= numberOfBytesInCommBuffer( buffer ) ) ) { U32 activeBuffer = activeDoubleBuffer[ buffer ]; @@ -286,7 +286,7 @@ memcpy( data, &commBuffers[ buffer ][ inactiveBuffer ][ 0 ], len ); numOfBytesPeeked = len; } - else // will need to get the rest from active buffer + else // Will need to get the rest from active buffer { U32 remNumOfBytes = len - bytesInInactiveBuffer; U08 *remPtr = data + bytesInInactiveBuffer; @@ -296,10 +296,10 @@ numOfBytesPeeked = bytesInInactiveBuffer + remNumOfBytes; } } - // release thread protection + // Release thread protection _enable_IRQ(); } - else // invalid buffer given + else // Invalid buffer given { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_COMM_BUFFERS_PEEK_INVALID_BUFFER, buffer ) } @@ -321,15 +321,15 @@ { U32 result = 0; - // verify given buffer + // Verify given buffer if ( buffer < NUM_OF_COMM_BUFFERS ) { U32 activeBuffer = activeDoubleBuffer[ buffer ]; U32 inactiveBuffer = GET_TOGGLE( activeBuffer, 0, 1 ); result = commBufferByteCount[ buffer ][ inactiveBuffer ] + commBufferByteCount[ buffer ][ activeBuffer ]; } - else // invalid buffer + else // Invalid buffer { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_COMM_BUFFERS_COUNT_INVALID_BUFFER, buffer ) } @@ -353,12 +353,12 @@ U32 activeBuffer = activeDoubleBuffer[ buffer ]; U32 inactiveBuffer = ( activeBuffer == 0 ? 1 : 0 ); - // ensure inactive buffer is reset before making active + // Ensure inactive buffer is reset before making active commBufferByteCount[ buffer ][ inactiveBuffer ] = 0; - // switch buffers + // Switch buffers activeDoubleBuffer[ buffer ] = inactiveBuffer; - // return the new active buffer (was just inactive) + // Return the new active buffer (was just inactive) return inactiveBuffer; } @@ -380,22 +380,22 @@ U32 inactiveBuffer = ( activeBuffer == 0 ? 1 : 0 ); U32 bytesInInactiveBuffer = commBufferByteCount[ buffer ][ inactiveBuffer ]; - // get the requested data from inactive buffer + // Get the requested data from inactive buffer memcpy( data, &commBuffers[ buffer ][ inactiveBuffer ][ 0 ], len ); if ( len < bytesInInactiveBuffer ) { U08 *endPtr = (&commBuffers[ buffer ][ inactiveBuffer ][ 0 ] + len); - // move un-consumed data in inactive buffer to start of inactive buffer + // Move un-consumed data in inactive buffer to start of inactive buffer memcpy( &commBuffers[ buffer ][ inactiveBuffer ][ 0 ], endPtr, ( bytesInInactiveBuffer - len ) ); - // reduce byte count for inactive buffer by # of bytes consumed + // Reduce byte count for inactive buffer by # of bytes consumed commBufferByteCount[ buffer ][ inactiveBuffer ] -= len; } else { - // inactive buffer has been emptied so switch double buffers - switchDoubleBuffer( buffer ); // switch will zero count off inactive buffer + // Inactive buffer has been emptied so switch double buffers + switchDoubleBuffer( buffer ); // Switch will zero count off inactive buffer } }