Index: App/Services/CommBuffers.c =================================================================== diff -u -r6dde2145782d8f68eb66e053f1db8f8847b15980 -ra87b6b9e253c6c0fcc84bca6f5de71959ce18bcc --- App/Services/CommBuffers.c (.../CommBuffers.c) (revision 6dde2145782d8f68eb66e053f1db8f8847b15980) +++ App/Services/CommBuffers.c (.../CommBuffers.c) (revision a87b6b9e253c6c0fcc84bca6f5de71959ce18bcc) @@ -38,7 +38,6 @@ static U32 switchDoubleBuffer( COMM_BUFFER_T buffer ); static void getDataFromInactiveBuffer( COMM_BUFFER_T buffer, U08 *data, U32 len ); -static U32 getRemainingByteCapacity( COMM_BUFFER_T buffer ); /************************************************************************* * @brief initCommBuffers @@ -72,8 +71,7 @@ * @brief addToCommBuffer * The addToCommBuffer function adds data of specified length to a specified \n * communication buffer. S/W fault if buffer too full to add data. \n - * Only one function in one thread should be calling this function for a \n - * given buffer. This function will always add to the active double buffer. + * This function will always add to the active double buffer. \n * @details * Inputs : commBufferByteCount[], activeDoubleBuffer[] * Outputs : commBuffers[], commBufferByteCount[] @@ -90,31 +88,38 @@ // verify given buffer if ( buffer < NUM_OF_COMM_BUFFERS ) { + U32 currentActiveBufCount; // where to start adding new data to buffer (after existing data) + + // add requires brief thread protection because there may be multiple sources for transmits trying to add data to a buffer. + _disable_IRQ(); + + currentActiveBufCount = commBufferByteCount[buffer][activeBuffer]; + // check to make sure buffer is not too full to service this add - if ( len <= getRemainingByteCapacity( buffer ) ) + if ( len <= ( COMM_BUFFER_LENGTH - currentActiveBufCount ) ) { U08 *buffPtr; // buffer destination for added data U08 *srcPtr; // data source - U32 dstIndex; // where to start adding bytes in destination buffer - U32 i; - // get destination pointer to first available byte in buffer - dstIndex = commBufferByteCount[buffer][activeBuffer]; - buffPtr = &commBuffers[buffer][activeBuffer][dstIndex]; + // adjust buffer count per this data add (also reserves space to add data before releasing thread protection) + commBufferByteCount[buffer][activeBuffer] += len; + // release thread protection + _enable_IRQ(); + // set destination pointer to end of active buffer data + buffPtr = &commBuffers[buffer][activeBuffer][currentActiveBufCount]; // get source pointer to start of given data array srcPtr = data; // copy source data to destination buffer - for ( i = 0; i < len; i++ ) - { - *buffPtr++ = *srcPtr++; - } + memcpy( buffPtr, srcPtr, len ); // adjust buffer count per this data add commBufferByteCount[buffer][activeBuffer] += len; // data successfully added to buffer result = TRUE; } else // buffer too full to add this much data { + // release thread protection + _enable_IRQ(); // TODO - s/w fault? } } @@ -132,7 +137,7 @@ * number of bytes from a given buffer and returns the number of bytes \n * retrieved from the buffer. This function will draw from the inactive \n * double buffer first and, if needed, switch double buffers to draw the \n - * reset of the requested data. + * rest of the requested data. * Only one function in one thread should be calling this function for a given \n * buffer. * @details @@ -245,7 +250,6 @@ return numOfBytesPeeked; } - /************************************************************************* * @brief numberOfBytesInCommBuffer * The numberOfBytesInCommBuffer function determines how many bytes \n @@ -336,23 +340,4 @@ } } -/************************************************************************* - * @brief getRemainingByteCapacity - * The getRemainingByteCapacity function determines the remaining capacity \n - * of the given buffer. - * @details - * Inputs : commBufferByteCount[], activeDoubleBuffer[] - * Outputs : none - * @param buffer : which comm buffer to check the capacity of - * @return the number of bytes of capacity remaining in the buffer. - *************************************************************************/ -static U32 getRemainingByteCapacity( COMM_BUFFER_T buffer ) -{ - U32 result; - U32 activeBuffer = activeDoubleBuffer[buffer]; - result = COMM_BUFFER_LENGTH - commBufferByteCount[buffer][activeBuffer]; - - return result; -} -