Index: App/Services/CommBuffers.c =================================================================== diff -u -r24f2fc89f0bfb62edcae316511e3fb4d7a336d01 -rc91e9da338d92432930d3589a4055ebbb404c6cb --- App/Services/CommBuffers.c (.../CommBuffers.c) (revision 24f2fc89f0bfb62edcae316511e3fb4d7a336d01) +++ App/Services/CommBuffers.c (.../CommBuffers.c) (revision c91e9da338d92432930d3589a4055ebbb404c6cb) @@ -13,8 +13,7 @@ * @brief CommBuffers service module. Provides data buffering functionality. \n * Data coming in via CAN/UART Rx interrupts can be placed in a buffer to \n * be processed at a later time. Data is double buffered to prevent \n - * contention between Rx interrupt and the thread that is consuming the data \n - * at a later time. \n + * contention between Rx/Tx interrupts and the processing thread(s). \n * Data to be transmitted via CAN/UART can be placed in a buffer to be sent \n * when the transmitter becomes available. \n * If a buffer becomes too full to service more data, a s/w fault is triggered. @@ -26,6 +25,8 @@ // ********** private definitions ********** +#define COMM_BUFFER_LENGTH 128 // bytes + // ********** private data ********** static U32 commBufferByteCount[NUM_OF_COMM_BUFFERS][2]; // for each buffer, how many bytes does it contain? (also index to next available) @@ -70,9 +71,8 @@ * @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. It is \n - * assumed that this thread will be higher priority than the thread calling \n - * the getFromCommBuffer() function so no thread protection is given. + * 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. * @details * Inputs : commBufferByteCount[], activeDoubleBuffers[] * Outputs : commBuffers[], commBufferByteCount[] @@ -127,34 +127,40 @@ /************************************************************************* * @brief getFromCommBuffer - * The getFromCommBuffer function fills a given byte array with data from \n - * a given buffer and returns the number of bytes retrieved from the buffer. \n - * Only one function in one thread should be calling this function. + * The getFromCommBuffer function fills a given byte array with a given \n + * number of bytes from a given buffer and returns the number of bytes \n + * retrieved from the buffer. This function will always draw from the \n + * inactive double buffer. If some of the requested data is in the active \n + * double buffer, the double buffers will be switched when the inactive \n + * double buffer is emptied and the retrieve will continue to the requested \n + * number of bytes. + * Only one function in one thread should be calling this function for a given \n + * buffer. * @details * Inputs : commBuffers[], commBufferByteCount[] * Outputs : commBufferByteCount[], activeDoubleBuffers[] * @param buffer : which comm buffer to retrieve data from * @param data : pointer to byte array to stuff data into - * @param maxLen : maximum # of bytes to retrieve into given data array. + * @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 maxLen ) +U32 getFromCommBuffer( COMM_BUFFER_T buffer, U08* data, U32 len ) { U32 result = 0; // verify given buffer if ( buffer < NUM_OF_COMM_BUFFERS ) { - // check to see if anything in buffer - if ( FALSE == isCommBufferEmpty( buffer ) ) + // check to see if requested # of bytes is in given buffer + if ( getNumberOfBytesInCommBuffer( buffer ) >= len ) { // switch double buffer (now we'll read from what was active buffer and Rx interrupts will feed new active buffer) U32 activeBuffer = switchDoubleBuffer( buffer ); U32 inactiveBuffer = ( activeBuffer == 0 ? 1 : 0 ); U32 bytesRetrieved = commBufferByteCount[buffer][inactiveBuffer]; // ensure caller's data buffer can hold the data we have to retrieve - if ( bytesRetrieved <= maxLen ) + if ( bytesRetrieved <= 1000 ) // was maxLen { U08 *buffPtr; // buffer source for retrieved data U08 *dstPtr; // buffer destination for retrieved data @@ -179,7 +185,7 @@ // TODO - s/w fault? } } - else // buffer is empty + else // buffer contains insufficient data to satisfy the request { // result already set to zero } @@ -193,6 +199,34 @@ } /************************************************************************* + * @brief getNumberOfBytesInCommBuffer + * The getNumberOfBytesInCommBuffer function determines how many bytes \n + * are currently contained in a given comm buffer. Both double buffers \n + * are considered for this. + * @details + * Inputs : activeDoubleBuffers[], commBufferByteCount[] + * Outputs : none + * @param buffer : which comm buffer to get byte count for + * @return the number of bytes in the given comm buffer. + *************************************************************************/ +U32 getNumberOfBytesInCommBuffer( COMM_BUFFER_T buffer ) +{ + U32 result = 0; + + // verify given buffer + if ( buffer < NUM_OF_COMM_BUFFERS ) + { + result = commBufferByteCount[buffer][0] + commBufferByteCount[buffer][1]; + } + else // invalid buffer + { + // TODO - s/w fault. + } + + return result; +} + +/************************************************************************* * @brief switchDoubleBuffer * The switchDoubleBuffer function switches the active and inactive buffers \n * for the given buffer.