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 #include <stdlib.h>
00066
00067
00068 #include "FreeRTOS.h"
00069 #include "task.h"
00070 #include "queue.h"
00071 #include "semphr.h"
00072
00073
00074 #include "GenQTest.h"
00075
00076 #define genqQUEUE_LENGTH ( 5 )
00077 #define genqNO_BLOCK ( 0 )
00078
00079 #define genqMUTEX_LOW_PRIORITY ( tskIDLE_PRIORITY )
00080 #define genqMUTEX_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
00081 #define genqMUTEX_MEDIUM_PRIORITY ( tskIDLE_PRIORITY + 2 )
00082 #define genqMUTEX_HIGH_PRIORITY ( tskIDLE_PRIORITY + 3 )
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 static void prvSendFrontAndBackTest( void *pvParameters );
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 static void prvLowPriorityMutexTask( void *pvParameters );
00109 static void prvMediumPriorityMutexTask( void *pvParameters );
00110 static void prvHighPriorityMutexTask( void *pvParameters );
00111
00112
00113
00114
00115
00116 static portBASE_TYPE xErrorDetected = pdFALSE;
00117
00118
00119
00120 static volatile unsigned portLONG ulLoopCounter = 0;
00121 static volatile unsigned portLONG ulLoopCounter2 = 0;
00122
00123
00124 static volatile unsigned portLONG ulGuardedVariable = 0;
00125
00126
00127
00128 static xTaskHandle xHighPriorityMutexTask, xMediumPriorityMutexTask;
00129
00130
00131
00132 void vStartGenericQueueTasks( unsigned portBASE_TYPE uxPriority )
00133 {
00134 xQueueHandle xQueue;
00135 xSemaphoreHandle xMutex;
00136
00137
00138
00139 xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( unsigned portLONG ) );
00140
00141
00142
00143
00144
00145
00146
00147 vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "Gen_Queue_Test" );
00148
00149
00150
00151
00152 xTaskCreate( prvSendFrontAndBackTest, ( signed portCHAR * )"GenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );
00153
00154
00155 xMutex = xSemaphoreCreateMutex();
00156
00157
00158
00159
00160
00161
00162
00163 vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Gen_Queue_Mutex" );
00164
00165
00166
00167
00168 xTaskCreate( prvLowPriorityMutexTask, ( signed portCHAR * )"MuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
00169 xTaskCreate( prvMediumPriorityMutexTask, ( signed portCHAR * )"MuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
00170 xTaskCreate( prvHighPriorityMutexTask, ( signed portCHAR * )"MuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );
00171 }
00172
00173
00174 static void prvSendFrontAndBackTest( void *pvParameters )
00175 {
00176 unsigned portLONG ulData, ulData2;
00177 xQueueHandle xQueue;
00178
00179 #ifdef USE_STDIO
00180 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
00181
00182 const portCHAR * const pcTaskStartMsg = "Queue SendToFront/SendToBack/Peek test started.\r\n";
00183
00184
00185 vPrintDisplayMessage( &pcTaskStartMsg );
00186 #endif
00187
00188 xQueue = ( xQueueHandle ) pvParameters;
00189
00190 for( ;; )
00191 {
00192
00193
00194
00195
00196 xQueueSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
00197
00198 if( uxQueueMessagesWaiting( xQueue ) != 1 )
00199 {
00200 xErrorDetected = pdTRUE;
00201 }
00202
00203 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
00204 {
00205 xErrorDetected = pdTRUE;
00206 }
00207
00208
00209
00210 if( ulLoopCounter != ulData )
00211 {
00212 xErrorDetected = pdTRUE;
00213 }
00214
00215
00216
00217 if( uxQueueMessagesWaiting( xQueue ) != 0 )
00218 {
00219 xErrorDetected = pdTRUE;
00220 }
00221
00222 xQueueSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
00223
00224 if( uxQueueMessagesWaiting( xQueue ) != 1 )
00225 {
00226 xErrorDetected = pdTRUE;
00227 }
00228
00229 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
00230 {
00231 xErrorDetected = pdTRUE;
00232 }
00233
00234 if( uxQueueMessagesWaiting( xQueue ) != 0 )
00235 {
00236 xErrorDetected = pdTRUE;
00237 }
00238
00239
00240
00241 if( ulLoopCounter != ulData )
00242 {
00243 xErrorDetected = pdTRUE;
00244 }
00245
00246 #if configUSE_PREEMPTION == 0
00247 taskYIELD();
00248 #endif
00249
00250
00251
00252
00253 for( ulData = 2; ulData < 5; ulData++ )
00254 {
00255 xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
00256 }
00257
00258
00259
00260 if( uxQueueMessagesWaiting( xQueue ) != 3 )
00261 {
00262 xErrorDetected = pdTRUE;
00263 }
00264 ulData = 1;
00265 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
00266 ulData = 0;
00267 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
00268
00269
00270
00271 if( uxQueueMessagesWaiting( xQueue ) != 5 )
00272 {
00273 xErrorDetected = pdTRUE;
00274 }
00275
00276 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
00277 {
00278 xErrorDetected = pdTRUE;
00279 }
00280
00281 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
00282 {
00283 xErrorDetected = pdTRUE;
00284 }
00285
00286 #if configUSE_PREEMPTION == 0
00287 taskYIELD();
00288 #endif
00289
00290
00291 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
00292 {
00293
00294 if( xQueuePeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
00295 {
00296 xErrorDetected = pdTRUE;
00297 }
00298
00299 if( ulData != ulData2 )
00300 {
00301 xErrorDetected = pdTRUE;
00302 }
00303
00304
00305
00306
00307 ulData2 = ~ulData2;
00308 if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
00309 {
00310 xErrorDetected = pdTRUE;
00311 }
00312
00313 if( ulData != ulData2 )
00314 {
00315 xErrorDetected = pdTRUE;
00316 }
00317 }
00318
00319
00320 if( uxQueueMessagesWaiting( xQueue ) != 0 )
00321 {
00322 xErrorDetected = pdTRUE;
00323 }
00324
00325 #if configUSE_PREEMPTION == 0
00326 taskYIELD();
00327 #endif
00328
00329
00330
00331 ulData = 10;
00332 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
00333 {
00334 xErrorDetected = pdTRUE;
00335 }
00336 ulData = 11;
00337 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
00338 {
00339 xErrorDetected = pdTRUE;
00340 }
00341
00342 if( uxQueueMessagesWaiting( xQueue ) != 2 )
00343 {
00344 xErrorDetected = pdTRUE;
00345 }
00346
00347
00348
00349 for( ulData = 9; ulData >= 7; ulData-- )
00350 {
00351 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
00352 {
00353 xErrorDetected = pdTRUE;
00354 }
00355 }
00356
00357
00358
00359 if( uxQueueMessagesWaiting( xQueue ) != 5 )
00360 {
00361 xErrorDetected = pdTRUE;
00362 }
00363
00364 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
00365 {
00366 xErrorDetected = pdTRUE;
00367 }
00368
00369 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
00370 {
00371 xErrorDetected = pdTRUE;
00372 }
00373
00374 #if configUSE_PREEMPTION == 0
00375 taskYIELD();
00376 #endif
00377
00378
00379 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
00380 {
00381 if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
00382 {
00383 xErrorDetected = pdTRUE;
00384 }
00385
00386 if( ulData != ulData2 )
00387 {
00388 xErrorDetected = pdTRUE;
00389 }
00390 }
00391
00392 if( uxQueueMessagesWaiting( xQueue ) != 0 )
00393 {
00394 xErrorDetected = pdTRUE;
00395 }
00396
00397 ulLoopCounter++;
00398 }
00399 }
00400
00401
00402 static void prvLowPriorityMutexTask( void *pvParameters )
00403 {
00404 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
00405
00406 #ifdef USE_STDIO
00407 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
00408
00409 const portCHAR * const pcTaskStartMsg = "Mutex with priority inheritance test started.\r\n";
00410
00411
00412 vPrintDisplayMessage( &pcTaskStartMsg );
00413 #endif
00414
00415 for( ;; )
00416 {
00417
00418 if( xSemaphoreTake( xMutex, genqNO_BLOCK ) != pdPASS )
00419 {
00420 xErrorDetected = pdTRUE;
00421 }
00422
00423
00424 ulGuardedVariable = 0;
00425
00426
00427
00428 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
00429 {
00430 xErrorDetected = pdTRUE;
00431 }
00432
00433
00434
00435 vTaskResume( xHighPriorityMutexTask );
00436
00437
00438
00439 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
00440 {
00441 xErrorDetected = pdTRUE;
00442 }
00443
00444
00445
00446
00447 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
00448 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
00449 {
00450 xErrorDetected = pdTRUE;
00451 }
00452
00453
00454
00455 vTaskResume( xMediumPriorityMutexTask );
00456
00457
00458 if( ulGuardedVariable != 0 )
00459 {
00460 xErrorDetected = pdTRUE;
00461 }
00462
00463
00464
00465
00466
00467
00468 if( xSemaphoreGive( xMutex ) != pdPASS )
00469 {
00470 xErrorDetected = pdTRUE;
00471 }
00472
00473
00474 if( ulGuardedVariable != 1 )
00475 {
00476 xErrorDetected = pdTRUE;
00477 }
00478
00479
00480
00481 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
00482 {
00483 xErrorDetected = pdTRUE;
00484 }
00485
00486
00487
00488 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
00489
00490
00491 ulLoopCounter2++;
00492
00493 #if configUSE_PREEMPTION == 0
00494 taskYIELD();
00495 #endif
00496 }
00497 }
00498
00499
00500 static void prvMediumPriorityMutexTask( void *pvParameters )
00501 {
00502 ( void ) pvParameters;
00503
00504 for( ;; )
00505 {
00506
00507
00508 vTaskSuspend( NULL );
00509
00510
00511
00512
00513 ulGuardedVariable++;
00514 }
00515 }
00516
00517
00518 static void prvHighPriorityMutexTask( void *pvParameters )
00519 {
00520 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
00521
00522 for( ;; )
00523 {
00524
00525
00526 vTaskSuspend( NULL );
00527
00528
00529
00530
00531 if( xSemaphoreTake( xMutex, portMAX_DELAY ) != pdPASS )
00532 {
00533 xErrorDetected = pdTRUE;
00534 }
00535
00536
00537
00538 if( xSemaphoreGive( xMutex ) != pdPASS )
00539 {
00540 xErrorDetected = pdTRUE;
00541 }
00542 }
00543 }
00544
00545
00546
00547 portBASE_TYPE xAreGenericQueueTasksStillRunning( void )
00548 {
00549 static unsigned portLONG ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
00550
00551
00552
00553 if( ulLastLoopCounter == ulLoopCounter )
00554 {
00555 xErrorDetected = pdTRUE;
00556 }
00557
00558 if( ulLastLoopCounter2 == ulLoopCounter2 )
00559 {
00560 xErrorDetected = pdTRUE;
00561 }
00562
00563 ulLastLoopCounter = ulLoopCounter;
00564 ulLastLoopCounter2 = ulLoopCounter2;
00565
00566
00567
00568
00569 return !xErrorDetected;
00570 }
00571
00572