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 #include "FreeRTOS.h"
00062 #include "task.h"
00063 #include "queue.h"
00064
00065
00066 #include "blocktim.h"
00067
00068
00069 #ifndef bktPRIMARY_PRIORITY
00070 #define bktPRIMARY_PRIORITY ( 3 )
00071 #endif
00072
00073 #ifndef bktSECONDARY_PRIORITY
00074 #define bktSECONDARY_PRIORITY ( 2 )
00075 #endif
00076
00077
00078 #define bktQUEUE_LENGTH ( 5 )
00079 #define bktSHORT_WAIT ( ( ( portTickType ) 20 ) / portTICK_RATE_MS )
00080 #define bktPRIMARY_BLOCK_TIME ( 10 )
00081 #define bktALLOWABLE_MARGIN ( 15 )
00082 #define bktTIME_TO_BLOCK ( 175 )
00083 #define bktDONT_BLOCK ( ( portTickType ) 0 )
00084 #define bktRUN_INDICATOR ( ( unsigned portBASE_TYPE ) 0x55 )
00085
00086
00087 static xQueueHandle xTestQueue;
00088
00089
00090
00091 static xTaskHandle xSecondary;
00092
00093
00094 static volatile portBASE_TYPE xPrimaryCycles = 0, xSecondaryCycles = 0;
00095 static volatile portBASE_TYPE xErrorOccurred = pdFALSE;
00096
00097
00098
00099 static volatile unsigned portBASE_TYPE xRunIndicator;
00100
00101
00102 static void vPrimaryBlockTimeTestTask( void *pvParameters );
00103 static void vSecondaryBlockTimeTestTask( void *pvParameters );
00104
00105
00106
00107 void vCreateBlockTimeTasks( void )
00108 {
00109
00110 xTestQueue = xQueueCreate( bktQUEUE_LENGTH, sizeof( portBASE_TYPE ) );
00111
00112
00113
00114
00115
00116
00117
00118 vQueueAddToRegistry( xTestQueue, ( signed char * ) "Block_Time_Queue" );
00119
00120
00121 xTaskCreate( vPrimaryBlockTimeTestTask, ( signed char * )"BTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL );
00122 xTaskCreate( vSecondaryBlockTimeTestTask, ( signed char * )"BTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary );
00123 }
00124
00125
00126 static void vPrimaryBlockTimeTestTask( void *pvParameters )
00127 {
00128 portBASE_TYPE xItem, xData;
00129 portTickType xTimeWhenBlocking;
00130 portTickType xTimeToBlock, xBlockedTime;
00131
00132 ( void ) pvParameters;
00133
00134 for( ;; )
00135 {
00136
00137
00138
00139
00140 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
00141 {
00142
00143
00144 xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
00145
00146 xTimeWhenBlocking = xTaskGetTickCount();
00147
00148
00149
00150 if( xQueueReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY )
00151 {
00152 xErrorOccurred = pdTRUE;
00153 }
00154
00155
00156 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
00157
00158 if( xBlockedTime < xTimeToBlock )
00159 {
00160
00161 xErrorOccurred = pdTRUE;
00162 }
00163
00164 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
00165 {
00166
00167
00168
00169 xErrorOccurred = pdTRUE;
00170 }
00171 }
00172
00173
00174
00175
00176
00177
00178
00179 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
00180 {
00181 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
00182 {
00183 xErrorOccurred = pdTRUE;
00184 }
00185
00186 #if configUSE_PREEMPTION == 0
00187 taskYIELD();
00188 #endif
00189 }
00190
00191 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
00192 {
00193
00194
00195 xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
00196
00197 xTimeWhenBlocking = xTaskGetTickCount();
00198
00199
00200
00201 if( xQueueSend( xTestQueue, &xItem, xTimeToBlock ) != errQUEUE_FULL )
00202 {
00203 xErrorOccurred = pdTRUE;
00204 }
00205
00206
00207 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
00208
00209 if( xBlockedTime < xTimeToBlock )
00210 {
00211
00212 xErrorOccurred = pdTRUE;
00213 }
00214
00215 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
00216 {
00217
00218
00219
00220 xErrorOccurred = pdTRUE;
00221 }
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 xRunIndicator = 0;
00237 vTaskResume( xSecondary );
00238
00239
00240 while( xRunIndicator != bktRUN_INDICATOR )
00241 {
00242
00243 vTaskDelay( bktSHORT_WAIT );
00244 }
00245
00246 vTaskDelay( bktSHORT_WAIT );
00247 xRunIndicator = 0;
00248
00249 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
00250 {
00251
00252
00253 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
00254 {
00255 xErrorOccurred = pdTRUE;
00256 }
00257
00258
00259
00260
00261 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
00262 {
00263 xErrorOccurred = pdTRUE;
00264 }
00265
00266 if( xRunIndicator == bktRUN_INDICATOR )
00267 {
00268
00269 xErrorOccurred = pdTRUE;
00270 }
00271
00272
00273
00274 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
00275
00276
00277
00278 if( xRunIndicator == bktRUN_INDICATOR )
00279 {
00280
00281
00282 xErrorOccurred = pdTRUE;
00283 }
00284
00285
00286 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
00287 }
00288
00289
00290
00291 while( xRunIndicator != bktRUN_INDICATOR )
00292 {
00293 vTaskDelay( bktSHORT_WAIT );
00294 }
00295 vTaskDelay( bktSHORT_WAIT );
00296 xRunIndicator = 0;
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
00307 {
00308 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
00309 {
00310 xErrorOccurred = pdTRUE;
00311 }
00312 }
00313
00314
00315
00316 vTaskResume( xSecondary );
00317
00318
00319 while( xRunIndicator != bktRUN_INDICATOR )
00320 {
00321 vTaskDelay( bktSHORT_WAIT );
00322 }
00323 vTaskDelay( bktSHORT_WAIT );
00324 xRunIndicator = 0;
00325
00326 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
00327 {
00328
00329
00330 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
00331 {
00332 xErrorOccurred = pdTRUE;
00333 }
00334
00335
00336
00337
00338 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
00339 {
00340 xErrorOccurred = pdTRUE;
00341 }
00342
00343 if( xRunIndicator == bktRUN_INDICATOR )
00344 {
00345
00346 xErrorOccurred = pdTRUE;
00347 }
00348
00349
00350
00351 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
00352
00353
00354
00355 if( xRunIndicator == bktRUN_INDICATOR )
00356 {
00357
00358
00359 xErrorOccurred = pdTRUE;
00360 }
00361 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
00362 }
00363
00364
00365
00366 while( xRunIndicator != bktRUN_INDICATOR )
00367 {
00368 vTaskDelay( bktSHORT_WAIT );
00369 }
00370 vTaskDelay( bktSHORT_WAIT );
00371
00372 xPrimaryCycles++;
00373 }
00374 }
00375
00376
00377 static void vSecondaryBlockTimeTestTask( void *pvParameters )
00378 {
00379 portTickType xTimeWhenBlocking, xBlockedTime;
00380 portBASE_TYPE xData;
00381
00382 ( void ) pvParameters;
00383
00384 for( ;; )
00385 {
00386
00387
00388
00389
00390 vTaskSuspend( NULL );
00391
00392
00393
00394
00395
00396
00397
00398 xTimeWhenBlocking = xTaskGetTickCount();
00399
00400
00401
00402 xData = 0;
00403 xRunIndicator = bktRUN_INDICATOR;
00404 if( xQueueSend( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL )
00405 {
00406 xErrorOccurred = pdTRUE;
00407 }
00408
00409
00410 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
00411
00412
00413 if( xBlockedTime < bktTIME_TO_BLOCK )
00414 {
00415 xErrorOccurred = pdTRUE;
00416 }
00417
00418
00419
00420
00421 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
00422 {
00423 xErrorOccurred = pdTRUE;
00424 }
00425
00426
00427 xRunIndicator = bktRUN_INDICATOR;
00428 vTaskSuspend( NULL );
00429
00430
00431
00432
00433
00434 xTimeWhenBlocking = xTaskGetTickCount();
00435
00436
00437
00438 xRunIndicator = bktRUN_INDICATOR;
00439 if( xQueueReceive( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_EMPTY )
00440 {
00441 xErrorOccurred = pdTRUE;
00442 }
00443
00444 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
00445
00446
00447 if( xBlockedTime < bktTIME_TO_BLOCK )
00448 {
00449 xErrorOccurred = pdTRUE;
00450 }
00451
00452
00453
00454
00455 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
00456 {
00457 xErrorOccurred = pdTRUE;
00458 }
00459
00460 xRunIndicator = bktRUN_INDICATOR;
00461
00462 xSecondaryCycles++;
00463 }
00464 }
00465
00466
00467 portBASE_TYPE xAreBlockTimeTestTasksStillRunning( void )
00468 {
00469 static portBASE_TYPE xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0;
00470 portBASE_TYPE xReturn = pdPASS;
00471
00472
00473
00474 if( xPrimaryCycles == xLastPrimaryCycleCount )
00475 {
00476 xReturn = pdFAIL;
00477 }
00478
00479 if( xSecondaryCycles == xLastSecondaryCycleCount )
00480 {
00481 xReturn = pdFAIL;
00482 }
00483
00484 if( xErrorOccurred == pdTRUE )
00485 {
00486 xReturn = pdFAIL;
00487 }
00488
00489 xLastSecondaryCycleCount = xSecondaryCycles;
00490 xLastPrimaryCycleCount = xPrimaryCycles;
00491
00492 return xReturn;
00493 }