Index: firmware/App/Services/CommBuffers.c =================================================================== diff -u -ra303cd4258157a8fbcbd8af4dd2bbaadec1a736c -rb64c49fdcf2b6d95e61e63f8e258c4e600935bbd --- firmware/App/Services/CommBuffers.c (.../CommBuffers.c) (revision a303cd4258157a8fbcbd8af4dd2bbaadec1a736c) +++ firmware/App/Services/CommBuffers.c (.../CommBuffers.c) (revision b64c49fdcf2b6d95e61e63f8e258c4e600935bbd) @@ -22,7 +22,6 @@ #include // for memcpy() -#include "Common.h" #include "CommBuffers.h" // ********** private definitions ********** @@ -32,9 +31,10 @@ // ********** private data ********** -static U32 commBufferByteCount[NUM_OF_COMM_BUFFERS][DOUBLE_BUFFERS]; // for each buffer, how many bytes does it contain? (also index to next available) -static 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 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 // ********** private function prototypes ********** @@ -57,13 +57,13 @@ // reset and zero out all buffers for ( b = 0; b < NUM_OF_COMM_BUFFERS; b++ ) { - activeDoubleBuffer[b] = 0; + activeDoubleBuffer[ b ] = 0; for ( d = 0; d < DOUBLE_BUFFERS; d++ ) { - commBufferByteCount[b][d] = 0; + commBufferByteCount[ b ][ d ] = 0; for ( i = 0; i < COMM_BUFFER_LENGTH; i++ ) { - commBuffers[b][d][i] = 0; + commBuffers[ b ][ d ][ i ] = 0; } } } @@ -87,44 +87,48 @@ BOOL addToCommBuffer( COMM_BUFFER_T buffer, U08* data, U32 len ) { BOOL result = FALSE; - U32 activeBuffer = activeDoubleBuffer[buffer]; // verify given buffer if ( buffer < NUM_OF_COMM_BUFFERS ) { + U32 activeBuffer; 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(); + bufferGetLock[ buffer ] = TRUE; - 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 - // 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]; + 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; + // release thread protection + bufferGetLock[ buffer ] = FALSE; + _enable_IRQ(); // data successfully added to buffer result = TRUE; } else // buffer too full to add this much data { // release thread protection + bufferGetLock[ buffer ] = FALSE; _enable_IRQ(); - // TODO - s/w fault? + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_SOFTWARE_FAULT, SW_FAULT_ID_COMM_BUFFERS_ADD_TOO_MUCH_DATA, len ) } } else // invalid buffer given { - // TODO - s/w fault + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_SOFTWARE_FAULT, SW_FAULT_ID_COMM_BUFFERS_ADD_INVALID_BUFFER, buffer ) } return result; @@ -155,12 +159,12 @@ // verify given buffer if ( buffer < NUM_OF_COMM_BUFFERS ) { - // verify size of get + // 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 activeBuffer = activeDoubleBuffer[ buffer ]; U32 inactiveBuffer = ( activeBuffer == 0 ? 1 : 0 ); - U32 bytesInInactiveBuffer = commBufferByteCount[buffer][inactiveBuffer]; + U32 bytesInInactiveBuffer = commBufferByteCount[ buffer ][ inactiveBuffer ]; U32 sizeOfFirstConsumption = MIN( len, bytesInInactiveBuffer ); // see what we can get from inactive buffer @@ -178,14 +182,10 @@ result += remNumOfBytes; } } - else // invalid peek size given - { - // TODO - s/w fault - } } else // invalid buffer given { - // TODO - s/w fault + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_SOFTWARE_FAULT, SW_FAULT_ID_COMM_BUFFERS_GET_INVALID_BUFFER, buffer ) } return result; @@ -214,36 +214,32 @@ // verify given buffer if ( buffer < NUM_OF_COMM_BUFFERS ) { - // verify size of peek + // verify requested # of bytes to peek are in the buffer if ( ( len <= ( COMM_BUFFER_LENGTH * DOUBLE_BUFFERS ) ) && ( len <= numberOfBytesInCommBuffer( buffer ) ) ) { - U32 activeBuffer = activeDoubleBuffer[buffer]; + U32 activeBuffer = activeDoubleBuffer[ buffer ]; U32 inactiveBuffer = ( activeBuffer == 0 ? 1 : 0 ); - U32 bytesInInactiveBuffer = commBufferByteCount[buffer][inactiveBuffer]; + U32 bytesInInactiveBuffer = commBufferByteCount[ buffer ][ inactiveBuffer ]; if ( len <= bytesInInactiveBuffer ) { - memcpy( data, &commBuffers[buffer][inactiveBuffer][0], len ); + memcpy( data, &commBuffers[ buffer ][ inactiveBuffer ][ 0 ], len ); numOfBytesPeeked = len; } else // will need to get the rest from active buffer { U32 remNumOfBytes = len - bytesInInactiveBuffer; U08 *remPtr = data + bytesInInactiveBuffer; - memcpy( data, &commBuffers[buffer][inactiveBuffer][0], bytesInInactiveBuffer ); - memcpy( remPtr, &commBuffers[buffer][activeBuffer][0], remNumOfBytes ); + memcpy( data, &commBuffers[ buffer ][ inactiveBuffer ][ 0 ], bytesInInactiveBuffer ); + memcpy( remPtr, &commBuffers[ buffer ][ activeBuffer ][ 0 ], remNumOfBytes ); numOfBytesPeeked = bytesInInactiveBuffer + remNumOfBytes; } } - else // invalid peek size given - { - // TODO - s/w fault - } } else // invalid buffer given { - // TODO - s/w fault + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_SOFTWARE_FAULT, SW_FAULT_ID_COMM_BUFFERS_PEEK_INVALID_BUFFER, buffer ) } return numOfBytesPeeked; @@ -267,11 +263,21 @@ // verify given buffer if ( buffer < NUM_OF_COMM_BUFFERS ) { - result = commBufferByteCount[buffer][0] + commBufferByteCount[buffer][1]; + 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 ]; + } } else // invalid buffer { - // TODO - s/w fault. + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_SOFTWARE_FAULT, SW_FAULT_ID_COMM_BUFFERS_COUNT_INVALID_BUFFER, buffer ) } return result; @@ -291,13 +297,13 @@ *************************************************************************/ static U32 switchDoubleBuffer( COMM_BUFFER_T buffer ) { - U32 activeBuffer = activeDoubleBuffer[buffer]; + U32 activeBuffer = activeDoubleBuffer[ buffer ]; U32 inactiveBuffer = ( activeBuffer == 0 ? 1 : 0 ); // ensure inactive buffer is reset before making active - commBufferByteCount[buffer][inactiveBuffer] = 0; + commBufferByteCount[ buffer ][ inactiveBuffer ] = 0; // switch buffers - activeDoubleBuffer[buffer] = inactiveBuffer; + activeDoubleBuffer[ buffer ] = inactiveBuffer; // return the new active buffer (was just inactive) return inactiveBuffer; @@ -318,21 +324,21 @@ *************************************************************************/ static void getDataFromInactiveBuffer( COMM_BUFFER_T buffer, U08 *data, U32 len ) { - U32 activeBuffer = activeDoubleBuffer[buffer]; + U32 activeBuffer = activeDoubleBuffer[ buffer ]; U32 inactiveBuffer = ( activeBuffer == 0 ? 1 : 0 ); - U32 bytesInInactiveBuffer = commBufferByteCount[buffer][inactiveBuffer]; + U32 bytesInInactiveBuffer = commBufferByteCount[ buffer ][ inactiveBuffer ]; // get the requested data from inactive buffer - memcpy( data, &commBuffers[buffer][inactiveBuffer][0], len ); + memcpy( data, &commBuffers[ buffer ][ inactiveBuffer ][ 0 ], len ); if ( len < bytesInInactiveBuffer ) { - U08 *endPtr = (&commBuffers[buffer][inactiveBuffer][0] + len); + U08 *endPtr = (&commBuffers[ buffer ][ inactiveBuffer ][ 0 ] + len); // move un-consumed data in inactive buffer to start of inactive buffer - memcpy( &commBuffers[buffer][inactiveBuffer][0], endPtr, (bytesInInactiveBuffer - len) ); + memcpy( &commBuffers[ buffer ][ inactiveBuffer ][ 0 ], endPtr, ( bytesInInactiveBuffer - len ) ); // reduce byte count for inactive buffer by # of bytes consumed - commBufferByteCount[buffer][inactiveBuffer] -= len; + commBufferByteCount[ buffer ][ inactiveBuffer ] -= len; } else { @@ -341,4 +347,3 @@ } } -