00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 #include <stdlib.h>
00088
00089
00090 #include "FreeRTOS.h"
00091 #include "task.h"
00092 #include "queue.h"
00093
00094
00095 #include "BlockQ.h"
00096
00097 #define blckqSTACK_SIZE configMINIMAL_STACK_SIZE
00098 #define blckqNUM_TASK_SETS ( 3 )
00099
00100
00101 typedef struct BLOCKING_QUEUE_PARAMETERS
00102 {
00103 xQueueHandle xQueue;
00104 portTickType xBlockTime;
00105 volatile short *psCheckVariable;
00106 } xBlockingQueueParameters;
00107
00108
00109 static portTASK_FUNCTION_PROTO( vBlockingQueueProducer, pvParameters );
00110
00111
00112
00113 static portTASK_FUNCTION_PROTO( vBlockingQueueConsumer, pvParameters );
00114
00115
00116
00117
00118 static volatile short sBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( unsigned short ) 0, ( unsigned short ) 0, ( unsigned short ) 0 };
00119
00120
00121
00122 static volatile short sBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( unsigned short ) 0, ( unsigned short ) 0, ( unsigned short ) 0 };
00123
00124
00125
00126 void vStartBlockingQueueTasks( unsigned portBASE_TYPE uxPriority )
00127 {
00128 xBlockingQueueParameters *pxQueueParameters1, *pxQueueParameters2;
00129 xBlockingQueueParameters *pxQueueParameters3, *pxQueueParameters4;
00130 xBlockingQueueParameters *pxQueueParameters5, *pxQueueParameters6;
00131 const unsigned portBASE_TYPE uxQueueSize1 = 1, uxQueueSize5 = 5;
00132 const portTickType xBlockTime = ( portTickType ) 1000 / portTICK_RATE_MS;
00133 const portTickType xDontBlock = ( portTickType ) 0;
00134
00135
00136
00137
00138 pxQueueParameters1 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
00139
00140
00141
00142 pxQueueParameters1->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
00143
00144
00145 pxQueueParameters1->xBlockTime = xBlockTime;
00146
00147
00148
00149 pxQueueParameters1->psCheckVariable = &( sBlockingConsumerCount[ 0 ] );
00150
00151
00152 pxQueueParameters2 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
00153
00154
00155 pxQueueParameters2->xQueue = pxQueueParameters1->xQueue;
00156
00157
00158
00159 pxQueueParameters2->xBlockTime = xDontBlock;
00160
00161
00162
00163 pxQueueParameters2->psCheckVariable = &( sBlockingProducerCount[ 0 ] );
00164
00165
00166
00167
00168 xTaskCreate( vBlockingQueueConsumer, ( signed char * ) "QConsB1", blckqSTACK_SIZE, ( void * ) pxQueueParameters1, uxPriority, NULL );
00169 xTaskCreate( vBlockingQueueProducer, ( signed char * ) "QProdB2", blckqSTACK_SIZE, ( void * ) pxQueueParameters2, tskIDLE_PRIORITY, NULL );
00170
00171
00172
00173
00174
00175
00176 pxQueueParameters3 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
00177 pxQueueParameters3->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
00178 pxQueueParameters3->xBlockTime = xDontBlock;
00179 pxQueueParameters3->psCheckVariable = &( sBlockingProducerCount[ 1 ] );
00180
00181 pxQueueParameters4 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
00182 pxQueueParameters4->xQueue = pxQueueParameters3->xQueue;
00183 pxQueueParameters4->xBlockTime = xBlockTime;
00184 pxQueueParameters4->psCheckVariable = &( sBlockingConsumerCount[ 1 ] );
00185
00186 xTaskCreate( vBlockingQueueConsumer, ( signed char * ) "QProdB3", blckqSTACK_SIZE, ( void * ) pxQueueParameters3, tskIDLE_PRIORITY, NULL );
00187 xTaskCreate( vBlockingQueueProducer, ( signed char * ) "QConsB4", blckqSTACK_SIZE, ( void * ) pxQueueParameters4, uxPriority, NULL );
00188
00189
00190
00191
00192
00193 pxQueueParameters5 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
00194 pxQueueParameters5->xQueue = xQueueCreate( uxQueueSize5, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
00195 pxQueueParameters5->xBlockTime = xBlockTime;
00196 pxQueueParameters5->psCheckVariable = &( sBlockingProducerCount[ 2 ] );
00197
00198 pxQueueParameters6 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
00199 pxQueueParameters6->xQueue = pxQueueParameters5->xQueue;
00200 pxQueueParameters6->xBlockTime = xBlockTime;
00201 pxQueueParameters6->psCheckVariable = &( sBlockingConsumerCount[ 2 ] );
00202
00203 xTaskCreate( vBlockingQueueProducer, ( signed char * ) "QProdB5", blckqSTACK_SIZE, ( void * ) pxQueueParameters5, tskIDLE_PRIORITY, NULL );
00204 xTaskCreate( vBlockingQueueConsumer, ( signed char * ) "QConsB6", blckqSTACK_SIZE, ( void * ) pxQueueParameters6, tskIDLE_PRIORITY, NULL );
00205 }
00206
00207
00208 static portTASK_FUNCTION( vBlockingQueueProducer, pvParameters )
00209 {
00210 unsigned short usValue = 0;
00211 xBlockingQueueParameters *pxQueueParameters;
00212 short sErrorEverOccurred = pdFALSE;
00213
00214 pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;
00215
00216 for( ;; )
00217 {
00218 if( xQueueSend( pxQueueParameters->xQueue, ( void * ) &usValue, pxQueueParameters->xBlockTime ) != pdPASS )
00219 {
00220 sErrorEverOccurred = pdTRUE;
00221 }
00222 else
00223 {
00224
00225
00226 if( sErrorEverOccurred == pdFALSE )
00227 {
00228 ( *pxQueueParameters->psCheckVariable )++;
00229 }
00230
00231
00232
00233 ++usValue;
00234 }
00235 }
00236 }
00237
00238
00239 static portTASK_FUNCTION( vBlockingQueueConsumer, pvParameters )
00240 {
00241 unsigned short usData, usExpectedValue = 0;
00242 xBlockingQueueParameters *pxQueueParameters;
00243 short sErrorEverOccurred = pdFALSE;
00244
00245 pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;
00246
00247 for( ;; )
00248 {
00249 if( xQueueReceive( pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime ) == pdPASS )
00250 {
00251 if( usData != usExpectedValue )
00252 {
00253
00254 usExpectedValue = usData;
00255
00256 sErrorEverOccurred = pdTRUE;
00257 }
00258 else
00259 {
00260
00261
00262 if( sErrorEverOccurred == pdFALSE )
00263 {
00264 ( *pxQueueParameters->psCheckVariable )++;
00265 }
00266
00267
00268
00269 ++usExpectedValue;
00270 }
00271 }
00272 }
00273 }
00274
00275
00276
00277 portBASE_TYPE xAreBlockingQueuesStillRunning( void )
00278 {
00279 static short sLastBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( unsigned short ) 0, ( unsigned short ) 0, ( unsigned short ) 0 };
00280 static short sLastBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( unsigned short ) 0, ( unsigned short ) 0, ( unsigned short ) 0 };
00281 portBASE_TYPE xReturn = pdPASS, xTasks;
00282
00283
00284
00285
00286
00287
00288
00289
00290 for( xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++ )
00291 {
00292 if( sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ] )
00293 {
00294 xReturn = pdFALSE;
00295 }
00296 sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ];
00297
00298
00299 if( sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ] )
00300 {
00301 xReturn = pdFALSE;
00302 }
00303 sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ];
00304 }
00305
00306 return xReturn;
00307 }
00308