brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd. |
| 3 | |
| 4 | *************************************************************************** |
| 5 | * * |
| 6 | * If you are: * |
| 7 | * * |
| 8 | * + New to FreeRTOS, * |
| 9 | * + Wanting to learn FreeRTOS or multitasking in general quickly * |
| 10 | * + Looking for basic training, * |
| 11 | * + Wanting to improve your FreeRTOS skills and productivity * |
| 12 | * * |
| 13 | * then take a look at the FreeRTOS eBook * |
| 14 | * * |
| 15 | * "Using the FreeRTOS Real Time Kernel - a Practical Guide" * |
| 16 | * http://www.FreeRTOS.org/Documentation * |
| 17 | * * |
| 18 | * A pdf reference manual is also available. Both are usually delivered * |
| 19 | * to your inbox within 20 minutes to two hours when purchased between 8am * |
| 20 | * and 8pm GMT (although please allow up to 24 hours in case of * |
| 21 | * exceptional circumstances). Thank you for your support! * |
| 22 | * * |
| 23 | *************************************************************************** |
| 24 | |
| 25 | This file is part of the FreeRTOS distribution. |
| 26 | |
| 27 | FreeRTOS is free software; you can redistribute it and/or modify it under |
| 28 | the terms of the GNU General Public License (version 2) as published by the |
| 29 | Free Software Foundation AND MODIFIED BY the FreeRTOS exception. |
| 30 | ***NOTE*** The exception to the GPL is included to allow you to distribute |
| 31 | a combined work that includes FreeRTOS without being obliged to provide the |
| 32 | source code for proprietary components outside of the FreeRTOS kernel. |
| 33 | FreeRTOS is distributed in the hope that it will be useful, but WITHOUT |
| 34 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 35 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 36 | more details. You should have received a copy of the GNU General Public |
| 37 | License and the FreeRTOS license exception along with FreeRTOS; if not it |
| 38 | can be viewed here: http://www.freertos.org/a00114.html and also obtained |
| 39 | by writing to Richard Barry, contact details for whom are available on the |
| 40 | FreeRTOS WEB site. |
| 41 | |
| 42 | 1 tab == 4 spaces! |
| 43 | |
| 44 | http://www.FreeRTOS.org - Documentation, latest information, license and |
| 45 | contact details. |
| 46 | |
| 47 | http://www.SafeRTOS.com - A version that is certified for use in safety |
| 48 | critical systems. |
| 49 | |
| 50 | http://www.OpenRTOS.com - Commercial support, development, porting, |
| 51 | licensing and training services. |
| 52 | */ |
| 53 | |
| 54 | /* |
| 55 | * This file contains some test scenarios that ensure tasks do not exit queue |
| 56 | * send or receive functions prematurely. A description of the tests is |
| 57 | * included within the code. |
| 58 | */ |
| 59 | |
| 60 | /* Kernel includes. */ |
| 61 | #include "FreeRTOS.h" |
| 62 | #include "task.h" |
| 63 | #include "queue.h" |
| 64 | |
| 65 | /* Demo includes. */ |
| 66 | #include "blocktim.h" |
| 67 | |
| 68 | /* Task priorities. Allow these to be overridden. */ |
| 69 | #ifndef bktPRIMARY_PRIORITY |
| 70 | #define bktPRIMARY_PRIORITY ( 3 ) |
| 71 | #endif |
| 72 | |
| 73 | #ifndef bktSECONDARY_PRIORITY |
| 74 | #define bktSECONDARY_PRIORITY ( 2 ) |
| 75 | #endif |
| 76 | |
| 77 | /* Task behaviour. */ |
| 78 | #define bktQUEUE_LENGTH ( 5 ) |
| 79 | #define bktSHORT_WAIT ( ( ( portTickType ) 20 ) / portTICK_RATE_MS ) |
| 80 | #define bktPRIMARY_BLOCK_TIME ( 10 ) |
| 81 | #define bktALLOWABLE_MARGIN ( 15 ) |
| 82 | #define bktTIME_TO_BLOCK ( 175 ) |
| 83 | #define bktDONT_BLOCK ( ( portTickType ) 0 ) |
| 84 | #define bktRUN_INDICATOR ( ( unsigned portBASE_TYPE ) 0x55 ) |
| 85 | |
| 86 | /* The queue on which the tasks block. */ |
| 87 | static xQueueHandle xTestQueue; |
| 88 | |
| 89 | /* Handle to the secondary task is required by the primary task for calls |
| 90 | to vTaskSuspend/Resume(). */ |
| 91 | static xTaskHandle xSecondary; |
| 92 | |
| 93 | /* Used to ensure that tasks are still executing without error. */ |
| 94 | static volatile portBASE_TYPE xPrimaryCycles = 0, xSecondaryCycles = 0; |
| 95 | static volatile portBASE_TYPE xErrorOccurred = pdFALSE; |
| 96 | |
| 97 | /* Provides a simple mechanism for the primary task to know when the |
| 98 | secondary task has executed. */ |
| 99 | static volatile unsigned portBASE_TYPE xRunIndicator; |
| 100 | |
| 101 | /* The two test tasks. Their behaviour is commented within the files. */ |
| 102 | static void vPrimaryBlockTimeTestTask(void *pvParameters); |
| 103 | static void vSecondaryBlockTimeTestTask(void *pvParameters); |
| 104 | |
| 105 | /*-----------------------------------------------------------*/ |
| 106 | |
| 107 | void vCreateBlockTimeTasks(void) |
| 108 | { |
| 109 | /* Create the queue on which the two tasks block. */ |
| 110 | xTestQueue = xQueueCreate(bktQUEUE_LENGTH, sizeof(portBASE_TYPE)); |
| 111 | |
| 112 | /* vQueueAddToRegistry() adds the queue to the queue registry, if one is |
| 113 | in use. The queue registry is provided as a means for kernel aware |
| 114 | debuggers to locate queues and has no purpose if a kernel aware debugger |
| 115 | is not being used. The call to vQueueAddToRegistry() will be removed |
| 116 | by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is |
| 117 | defined to be less than 1. */ |
| 118 | vQueueAddToRegistry(xTestQueue, (signed char *) "Block_Time_Queue"); |
| 119 | |
| 120 | /* Create the two test tasks. */ |
| 121 | xTaskCreate(vPrimaryBlockTimeTestTask, (signed char *)"BTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL); |
| 122 | xTaskCreate(vSecondaryBlockTimeTestTask, (signed char *)"BTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary); |
| 123 | } |
| 124 | /*-----------------------------------------------------------*/ |
| 125 | |
| 126 | static void vPrimaryBlockTimeTestTask(void *pvParameters) |
| 127 | { |
| 128 | portBASE_TYPE xItem, xData; |
| 129 | portTickType xTimeWhenBlocking; |
| 130 | portTickType xTimeToBlock, xBlockedTime; |
| 131 | |
| 132 | (void) pvParameters; |
| 133 | |
| 134 | for (;;) { |
| 135 | /********************************************************************* |
| 136 | Test 1 |
| 137 | |
| 138 | Simple block time wakeup test on queue receives. */ |
| 139 | for (xItem = 0; xItem < bktQUEUE_LENGTH; xItem++) { |
| 140 | /* The queue is empty. Attempt to read from the queue using a block |
| 141 | time. When we wake, ensure the delta in time is as expected. */ |
| 142 | xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem; |
| 143 | |
| 144 | xTimeWhenBlocking = xTaskGetTickCount(); |
| 145 | |
| 146 | /* We should unblock after xTimeToBlock having not received |
| 147 | anything on the queue. */ |
| 148 | if (xQueueReceive(xTestQueue, &xData, xTimeToBlock) != errQUEUE_EMPTY) { |
| 149 | xErrorOccurred = pdTRUE; |
| 150 | } |
| 151 | |
| 152 | /* How long were we blocked for? */ |
| 153 | xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking; |
| 154 | |
| 155 | if (xBlockedTime < xTimeToBlock) { |
| 156 | /* Should not have blocked for less than we requested. */ |
| 157 | xErrorOccurred = pdTRUE; |
| 158 | } |
| 159 | |
| 160 | if (xBlockedTime > (xTimeToBlock + bktALLOWABLE_MARGIN)) { |
| 161 | /* Should not have blocked for longer than we requested, |
| 162 | although we would not necessarily run as soon as we were |
| 163 | unblocked so a margin is allowed. */ |
| 164 | xErrorOccurred = pdTRUE; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /********************************************************************* |
| 169 | Test 2 |
| 170 | |
| 171 | Simple block time wakeup test on queue sends. |
| 172 | |
| 173 | First fill the queue. It should be empty so all sends should pass. */ |
| 174 | for (xItem = 0; xItem < bktQUEUE_LENGTH; xItem++) { |
| 175 | if (xQueueSend(xTestQueue, &xItem, bktDONT_BLOCK) != pdPASS) { |
| 176 | xErrorOccurred = pdTRUE; |
| 177 | } |
| 178 | |
| 179 | #if configUSE_PREEMPTION == 0 |
| 180 | taskYIELD(); |
| 181 | #endif |
| 182 | } |
| 183 | |
| 184 | for (xItem = 0; xItem < bktQUEUE_LENGTH; xItem++) { |
| 185 | /* The queue is full. Attempt to write to the queue using a block |
| 186 | time. When we wake, ensure the delta in time is as expected. */ |
| 187 | xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem; |
| 188 | |
| 189 | xTimeWhenBlocking = xTaskGetTickCount(); |
| 190 | |
| 191 | /* We should unblock after xTimeToBlock having not received |
| 192 | anything on the queue. */ |
| 193 | if (xQueueSend(xTestQueue, &xItem, xTimeToBlock) != errQUEUE_FULL) { |
| 194 | xErrorOccurred = pdTRUE; |
| 195 | } |
| 196 | |
| 197 | /* How long were we blocked for? */ |
| 198 | xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking; |
| 199 | |
| 200 | if (xBlockedTime < xTimeToBlock) { |
| 201 | /* Should not have blocked for less than we requested. */ |
| 202 | xErrorOccurred = pdTRUE; |
| 203 | } |
| 204 | |
| 205 | if (xBlockedTime > (xTimeToBlock + bktALLOWABLE_MARGIN)) { |
| 206 | /* Should not have blocked for longer than we requested, |
| 207 | although we would not necessarily run as soon as we were |
| 208 | unblocked so a margin is allowed. */ |
| 209 | xErrorOccurred = pdTRUE; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /********************************************************************* |
| 214 | Test 3 |
| 215 | |
| 216 | Wake the other task, it will block attempting to post to the queue. |
| 217 | When we read from the queue the other task will wake, but before it |
| 218 | can run we will post to the queue again. When the other task runs it |
| 219 | will find the queue still full, even though it was woken. It should |
| 220 | recognise that its block time has not expired and return to block for |
| 221 | the remains of its block time. |
| 222 | |
| 223 | Wake the other task so it blocks attempting to post to the already |
| 224 | full queue. */ |
| 225 | xRunIndicator = 0; |
| 226 | vTaskResume(xSecondary); |
| 227 | |
| 228 | /* We need to wait a little to ensure the other task executes. */ |
| 229 | while (xRunIndicator != bktRUN_INDICATOR) { |
| 230 | /* The other task has not yet executed. */ |
| 231 | vTaskDelay(bktSHORT_WAIT); |
| 232 | } |
| 233 | /* Make sure the other task is blocked on the queue. */ |
| 234 | vTaskDelay(bktSHORT_WAIT); |
| 235 | xRunIndicator = 0; |
| 236 | |
| 237 | for (xItem = 0; xItem < bktQUEUE_LENGTH; xItem++) { |
| 238 | /* Now when we make space on the queue the other task should wake |
| 239 | but not execute as this task has higher priority. */ |
| 240 | if (xQueueReceive(xTestQueue, &xData, bktDONT_BLOCK) != pdPASS) { |
| 241 | xErrorOccurred = pdTRUE; |
| 242 | } |
| 243 | |
| 244 | /* Now fill the queue again before the other task gets a chance to |
| 245 | execute. If the other task had executed we would find the queue |
| 246 | full ourselves, and the other task have set xRunIndicator. */ |
| 247 | if (xQueueSend(xTestQueue, &xItem, bktDONT_BLOCK) != pdPASS) { |
| 248 | xErrorOccurred = pdTRUE; |
| 249 | } |
| 250 | |
| 251 | if (xRunIndicator == bktRUN_INDICATOR) { |
| 252 | /* The other task should not have executed. */ |
| 253 | xErrorOccurred = pdTRUE; |
| 254 | } |
| 255 | |
| 256 | /* Raise the priority of the other task so it executes and blocks |
| 257 | on the queue again. */ |
| 258 | vTaskPrioritySet(xSecondary, bktPRIMARY_PRIORITY + 2); |
| 259 | |
| 260 | /* The other task should now have re-blocked without exiting the |
| 261 | queue function. */ |
| 262 | if (xRunIndicator == bktRUN_INDICATOR) { |
| 263 | /* The other task should not have executed outside of the |
| 264 | queue function. */ |
| 265 | xErrorOccurred = pdTRUE; |
| 266 | } |
| 267 | |
| 268 | /* Set the priority back down. */ |
| 269 | vTaskPrioritySet(xSecondary, bktSECONDARY_PRIORITY); |
| 270 | } |
| 271 | |
| 272 | /* Let the other task timeout. When it unblockes it will check that it |
| 273 | unblocked at the correct time, then suspend itself. */ |
| 274 | while (xRunIndicator != bktRUN_INDICATOR) { |
| 275 | vTaskDelay(bktSHORT_WAIT); |
| 276 | } |
| 277 | vTaskDelay(bktSHORT_WAIT); |
| 278 | xRunIndicator = 0; |
| 279 | |
| 280 | |
| 281 | /********************************************************************* |
| 282 | Test 4 |
| 283 | |
| 284 | As per test 3 - but with the send and receive the other way around. |
| 285 | The other task blocks attempting to read from the queue. |
| 286 | |
| 287 | Empty the queue. We should find that it is full. */ |
| 288 | for (xItem = 0; xItem < bktQUEUE_LENGTH; xItem++) { |
| 289 | if (xQueueReceive(xTestQueue, &xData, bktDONT_BLOCK) != pdPASS) { |
| 290 | xErrorOccurred = pdTRUE; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /* Wake the other task so it blocks attempting to read from the |
| 295 | already empty queue. */ |
| 296 | vTaskResume(xSecondary); |
| 297 | |
| 298 | /* We need to wait a little to ensure the other task executes. */ |
| 299 | while (xRunIndicator != bktRUN_INDICATOR) { |
| 300 | vTaskDelay(bktSHORT_WAIT); |
| 301 | } |
| 302 | vTaskDelay(bktSHORT_WAIT); |
| 303 | xRunIndicator = 0; |
| 304 | |
| 305 | for (xItem = 0; xItem < bktQUEUE_LENGTH; xItem++) { |
| 306 | /* Now when we place an item on the queue the other task should |
| 307 | wake but not execute as this task has higher priority. */ |
| 308 | if (xQueueSend(xTestQueue, &xItem, bktDONT_BLOCK) != pdPASS) { |
| 309 | xErrorOccurred = pdTRUE; |
| 310 | } |
| 311 | |
| 312 | /* Now empty the queue again before the other task gets a chance to |
| 313 | execute. If the other task had executed we would find the queue |
| 314 | empty ourselves, and the other task would be suspended. */ |
| 315 | if (xQueueReceive(xTestQueue, &xData, bktDONT_BLOCK) != pdPASS) { |
| 316 | xErrorOccurred = pdTRUE; |
| 317 | } |
| 318 | |
| 319 | if (xRunIndicator == bktRUN_INDICATOR) { |
| 320 | /* The other task should not have executed. */ |
| 321 | xErrorOccurred = pdTRUE; |
| 322 | } |
| 323 | |
| 324 | /* Raise the priority of the other task so it executes and blocks |
| 325 | on the queue again. */ |
| 326 | vTaskPrioritySet(xSecondary, bktPRIMARY_PRIORITY + 2); |
| 327 | |
| 328 | /* The other task should now have re-blocked without exiting the |
| 329 | queue function. */ |
| 330 | if (xRunIndicator == bktRUN_INDICATOR) { |
| 331 | /* The other task should not have executed outside of the |
| 332 | queue function. */ |
| 333 | xErrorOccurred = pdTRUE; |
| 334 | } |
| 335 | vTaskPrioritySet(xSecondary, bktSECONDARY_PRIORITY); |
| 336 | } |
| 337 | |
| 338 | /* Let the other task timeout. When it unblockes it will check that it |
| 339 | unblocked at the correct time, then suspend itself. */ |
| 340 | while (xRunIndicator != bktRUN_INDICATOR) { |
| 341 | vTaskDelay(bktSHORT_WAIT); |
| 342 | } |
| 343 | vTaskDelay(bktSHORT_WAIT); |
| 344 | |
| 345 | xPrimaryCycles++; |
| 346 | } |
| 347 | } |
| 348 | /*-----------------------------------------------------------*/ |
| 349 | |
| 350 | static void vSecondaryBlockTimeTestTask(void *pvParameters) |
| 351 | { |
| 352 | portTickType xTimeWhenBlocking, xBlockedTime; |
| 353 | portBASE_TYPE xData; |
| 354 | |
| 355 | (void) pvParameters; |
| 356 | |
| 357 | for (;;) { |
| 358 | /********************************************************************* |
| 359 | Test 1 and 2 |
| 360 | |
| 361 | This task does does not participate in these tests. */ |
| 362 | vTaskSuspend(NULL); |
| 363 | |
| 364 | /********************************************************************* |
| 365 | Test 3 |
| 366 | |
| 367 | The first thing we do is attempt to read from the queue. It should be |
| 368 | full so we block. Note the time before we block so we can check the |
| 369 | wake time is as per that expected. */ |
| 370 | xTimeWhenBlocking = xTaskGetTickCount(); |
| 371 | |
| 372 | /* We should unblock after bktTIME_TO_BLOCK having not sent |
| 373 | anything to the queue. */ |
| 374 | xData = 0; |
| 375 | xRunIndicator = bktRUN_INDICATOR; |
| 376 | if (xQueueSend(xTestQueue, &xData, bktTIME_TO_BLOCK) != errQUEUE_FULL) { |
| 377 | xErrorOccurred = pdTRUE; |
| 378 | } |
| 379 | |
| 380 | /* How long were we inside the send function? */ |
| 381 | xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking; |
| 382 | |
| 383 | /* We should not have blocked for less time than bktTIME_TO_BLOCK. */ |
| 384 | if (xBlockedTime < bktTIME_TO_BLOCK) { |
| 385 | xErrorOccurred = pdTRUE; |
| 386 | } |
| 387 | |
| 388 | /* We should of not blocked for much longer than bktALLOWABLE_MARGIN |
| 389 | either. A margin is permitted as we would not necessarily run as |
| 390 | soon as we unblocked. */ |
| 391 | if (xBlockedTime > (bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN)) { |
| 392 | xErrorOccurred = pdTRUE; |
| 393 | } |
| 394 | |
| 395 | /* Suspend ready for test 3. */ |
| 396 | xRunIndicator = bktRUN_INDICATOR; |
| 397 | vTaskSuspend(NULL); |
| 398 | |
| 399 | /********************************************************************* |
| 400 | Test 4 |
| 401 | |
| 402 | As per test three, but with the send and receive reversed. */ |
| 403 | xTimeWhenBlocking = xTaskGetTickCount(); |
| 404 | |
| 405 | /* We should unblock after bktTIME_TO_BLOCK having not received |
| 406 | anything on the queue. */ |
| 407 | xRunIndicator = bktRUN_INDICATOR; |
| 408 | if (xQueueReceive(xTestQueue, &xData, bktTIME_TO_BLOCK) != errQUEUE_EMPTY) { |
| 409 | xErrorOccurred = pdTRUE; |
| 410 | } |
| 411 | |
| 412 | xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking; |
| 413 | |
| 414 | /* We should not have blocked for less time than bktTIME_TO_BLOCK. */ |
| 415 | if (xBlockedTime < bktTIME_TO_BLOCK) { |
| 416 | xErrorOccurred = pdTRUE; |
| 417 | } |
| 418 | |
| 419 | /* We should of not blocked for much longer than bktALLOWABLE_MARGIN |
| 420 | either. A margin is permitted as we would not necessarily run as soon |
| 421 | as we unblocked. */ |
| 422 | if (xBlockedTime > (bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN)) { |
| 423 | xErrorOccurred = pdTRUE; |
| 424 | } |
| 425 | |
| 426 | xRunIndicator = bktRUN_INDICATOR; |
| 427 | |
| 428 | xSecondaryCycles++; |
| 429 | } |
| 430 | } |
| 431 | /*-----------------------------------------------------------*/ |
| 432 | |
| 433 | portBASE_TYPE xAreBlockTimeTestTasksStillRunning(void) |
| 434 | { |
| 435 | static portBASE_TYPE xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0; |
| 436 | portBASE_TYPE xReturn = pdPASS; |
| 437 | |
| 438 | /* Have both tasks performed at least one cycle since this function was |
| 439 | last called? */ |
| 440 | if (xPrimaryCycles == xLastPrimaryCycleCount) { |
| 441 | xReturn = pdFAIL; |
| 442 | } |
| 443 | |
| 444 | if (xSecondaryCycles == xLastSecondaryCycleCount) { |
| 445 | xReturn = pdFAIL; |
| 446 | } |
| 447 | |
| 448 | if (xErrorOccurred == pdTRUE) { |
| 449 | xReturn = pdFAIL; |
| 450 | } |
| 451 | |
| 452 | xLastSecondaryCycleCount = xSecondaryCycles; |
| 453 | xLastPrimaryCycleCount = xPrimaryCycles; |
| 454 | |
| 455 | return xReturn; |
| 456 | } |