2 FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 * This file contains some test scenarios that ensure tasks do not exit queue
\r
72 * send or receive functions prematurely. A description of the tests is
\r
73 * included within the code.
\r
76 /* Kernel includes. */
\r
77 #include "FreeRTOS.h"
\r
81 /* Demo includes. */
\r
82 #include "blocktim.h"
\r
84 /* Task priorities. Allow these to be overridden. */
\r
85 #ifndef bktPRIMARY_PRIORITY
\r
86 #define bktPRIMARY_PRIORITY ( configMAX_PRIORITIES - 3 )
\r
89 #ifndef bktSECONDARY_PRIORITY
\r
90 #define bktSECONDARY_PRIORITY ( configMAX_PRIORITIES - 4 )
\r
93 /* Task behaviour. */
\r
94 #define bktQUEUE_LENGTH ( 5 )
\r
95 #define bktSHORT_WAIT pdMS_TO_TICKS( ( TickType_t ) 20 )
\r
96 #define bktPRIMARY_BLOCK_TIME ( 10 )
\r
97 #define bktALLOWABLE_MARGIN ( 15 )
\r
98 #define bktTIME_TO_BLOCK ( 175 )
\r
99 #define bktDONT_BLOCK ( ( TickType_t ) 0 )
\r
100 #define bktRUN_INDICATOR ( ( UBaseType_t ) 0x55 )
\r
102 /*-----------------------------------------------------------*/
\r
105 * The two test tasks. Their behaviour is commented within the functions.
\r
107 static void vPrimaryBlockTimeTestTask( void *pvParameters );
\r
108 static void vSecondaryBlockTimeTestTask( void *pvParameters );
\r
111 * Very basic tests to verify the block times are as expected.
\r
113 static void prvBasicDelayTests( void );
\r
115 /*-----------------------------------------------------------*/
\r
117 /* The queue on which the tasks block. */
\r
118 static QueueHandle_t xTestQueue;
\r
120 /* Handle to the secondary task is required by the primary task for calls
\r
121 to vTaskSuspend/Resume(). */
\r
122 static TaskHandle_t xSecondary;
\r
124 /* Used to ensure that tasks are still executing without error. */
\r
125 static volatile BaseType_t xPrimaryCycles = 0, xSecondaryCycles = 0;
\r
126 static volatile BaseType_t xErrorOccurred = pdFALSE;
\r
128 /* Provides a simple mechanism for the primary task to know when the
\r
129 secondary task has executed. */
\r
130 static volatile UBaseType_t xRunIndicator;
\r
132 /*-----------------------------------------------------------*/
\r
134 void vCreateBlockTimeTasks( void )
\r
136 /* Create the queue on which the two tasks block. */
\r
137 xTestQueue = xQueueCreate( bktQUEUE_LENGTH, sizeof( BaseType_t ) );
\r
139 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
140 in use. The queue registry is provided as a means for kernel aware
\r
141 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
142 is not being used. The call to vQueueAddToRegistry() will be removed
\r
143 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
144 defined to be less than 1. */
\r
145 vQueueAddToRegistry( xTestQueue, "Block_Time_Queue" );
\r
147 /* Create the two test tasks. */
\r
148 xTaskCreate( vPrimaryBlockTimeTestTask, "BTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL );
\r
149 xTaskCreate( vSecondaryBlockTimeTestTask, "BTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary );
\r
151 /*-----------------------------------------------------------*/
\r
153 static void vPrimaryBlockTimeTestTask( void *pvParameters )
\r
155 BaseType_t xItem, xData;
\r
156 TickType_t xTimeWhenBlocking;
\r
157 TickType_t xTimeToBlock, xBlockedTime;
\r
159 ( void ) pvParameters;
\r
163 /*********************************************************************
\r
166 Basic vTaskDelay() and vTaskDelayUntil() tests. */
\r
167 prvBasicDelayTests();
\r
170 /*********************************************************************
\r
173 Simple block time wakeup test on queue receives. */
\r
174 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
176 /* The queue is empty. Attempt to read from the queue using a block
\r
177 time. When we wake, ensure the delta in time is as expected. */
\r
178 xTimeToBlock = ( TickType_t ) ( bktPRIMARY_BLOCK_TIME << xItem );
\r
180 xTimeWhenBlocking = xTaskGetTickCount();
\r
182 /* We should unblock after xTimeToBlock having not received
\r
183 anything on the queue. */
\r
184 if( xQueueReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY )
\r
186 xErrorOccurred = pdTRUE;
\r
189 /* How long were we blocked for? */
\r
190 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
192 if( xBlockedTime < xTimeToBlock )
\r
194 /* Should not have blocked for less than we requested. */
\r
195 xErrorOccurred = pdTRUE;
\r
198 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
\r
200 /* Should not have blocked for longer than we requested,
\r
201 although we would not necessarily run as soon as we were
\r
202 unblocked so a margin is allowed. */
\r
203 xErrorOccurred = pdTRUE;
\r
207 /*********************************************************************
\r
210 Simple block time wakeup test on queue sends.
\r
212 First fill the queue. It should be empty so all sends should pass. */
\r
213 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
215 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
217 xErrorOccurred = pdTRUE;
\r
220 #if configUSE_PREEMPTION == 0
\r
225 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
227 /* The queue is full. Attempt to write to the queue using a block
\r
228 time. When we wake, ensure the delta in time is as expected. */
\r
229 xTimeToBlock = ( TickType_t ) ( bktPRIMARY_BLOCK_TIME << xItem );
\r
231 xTimeWhenBlocking = xTaskGetTickCount();
\r
233 /* We should unblock after xTimeToBlock having not received
\r
234 anything on the queue. */
\r
235 if( xQueueSend( xTestQueue, &xItem, xTimeToBlock ) != errQUEUE_FULL )
\r
237 xErrorOccurred = pdTRUE;
\r
240 /* How long were we blocked for? */
\r
241 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
243 if( xBlockedTime < xTimeToBlock )
\r
245 /* Should not have blocked for less than we requested. */
\r
246 xErrorOccurred = pdTRUE;
\r
249 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
\r
251 /* Should not have blocked for longer than we requested,
\r
252 although we would not necessarily run as soon as we were
\r
253 unblocked so a margin is allowed. */
\r
254 xErrorOccurred = pdTRUE;
\r
258 /*********************************************************************
\r
261 Wake the other task, it will block attempting to post to the queue.
\r
262 When we read from the queue the other task will wake, but before it
\r
263 can run we will post to the queue again. When the other task runs it
\r
264 will find the queue still full, even though it was woken. It should
\r
265 recognise that its block time has not expired and return to block for
\r
266 the remains of its block time.
\r
268 Wake the other task so it blocks attempting to post to the already
\r
271 vTaskResume( xSecondary );
\r
273 /* We need to wait a little to ensure the other task executes. */
\r
274 while( xRunIndicator != bktRUN_INDICATOR )
\r
276 /* The other task has not yet executed. */
\r
277 vTaskDelay( bktSHORT_WAIT );
\r
279 /* Make sure the other task is blocked on the queue. */
\r
280 vTaskDelay( bktSHORT_WAIT );
\r
283 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
285 /* Now when we make space on the queue the other task should wake
\r
286 but not execute as this task has higher priority. */
\r
287 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
289 xErrorOccurred = pdTRUE;
\r
292 /* Now fill the queue again before the other task gets a chance to
\r
293 execute. If the other task had executed we would find the queue
\r
294 full ourselves, and the other task have set xRunIndicator. */
\r
295 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
297 xErrorOccurred = pdTRUE;
\r
300 if( xRunIndicator == bktRUN_INDICATOR )
\r
302 /* The other task should not have executed. */
\r
303 xErrorOccurred = pdTRUE;
\r
306 /* Raise the priority of the other task so it executes and blocks
\r
307 on the queue again. */
\r
308 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
\r
310 /* The other task should now have re-blocked without exiting the
\r
312 if( xRunIndicator == bktRUN_INDICATOR )
\r
314 /* The other task should not have executed outside of the
\r
316 xErrorOccurred = pdTRUE;
\r
319 /* Set the priority back down. */
\r
320 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
\r
323 /* Let the other task timeout. When it unblockes it will check that it
\r
324 unblocked at the correct time, then suspend itself. */
\r
325 while( xRunIndicator != bktRUN_INDICATOR )
\r
327 vTaskDelay( bktSHORT_WAIT );
\r
329 vTaskDelay( bktSHORT_WAIT );
\r
333 /*********************************************************************
\r
336 As per test 3 - but with the send and receive the other way around.
\r
337 The other task blocks attempting to read from the queue.
\r
339 Empty the queue. We should find that it is full. */
\r
340 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
342 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
344 xErrorOccurred = pdTRUE;
\r
348 /* Wake the other task so it blocks attempting to read from the
\r
349 already empty queue. */
\r
350 vTaskResume( xSecondary );
\r
352 /* We need to wait a little to ensure the other task executes. */
\r
353 while( xRunIndicator != bktRUN_INDICATOR )
\r
355 vTaskDelay( bktSHORT_WAIT );
\r
357 vTaskDelay( bktSHORT_WAIT );
\r
360 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
362 /* Now when we place an item on the queue the other task should
\r
363 wake but not execute as this task has higher priority. */
\r
364 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
366 xErrorOccurred = pdTRUE;
\r
369 /* Now empty the queue again before the other task gets a chance to
\r
370 execute. If the other task had executed we would find the queue
\r
371 empty ourselves, and the other task would be suspended. */
\r
372 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
374 xErrorOccurred = pdTRUE;
\r
377 if( xRunIndicator == bktRUN_INDICATOR )
\r
379 /* The other task should not have executed. */
\r
380 xErrorOccurred = pdTRUE;
\r
383 /* Raise the priority of the other task so it executes and blocks
\r
384 on the queue again. */
\r
385 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
\r
387 /* The other task should now have re-blocked without exiting the
\r
389 if( xRunIndicator == bktRUN_INDICATOR )
\r
391 /* The other task should not have executed outside of the
\r
393 xErrorOccurred = pdTRUE;
\r
395 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
\r
398 /* Let the other task timeout. When it unblockes it will check that it
\r
399 unblocked at the correct time, then suspend itself. */
\r
400 while( xRunIndicator != bktRUN_INDICATOR )
\r
402 vTaskDelay( bktSHORT_WAIT );
\r
404 vTaskDelay( bktSHORT_WAIT );
\r
409 /*-----------------------------------------------------------*/
\r
411 static void vSecondaryBlockTimeTestTask( void *pvParameters )
\r
413 TickType_t xTimeWhenBlocking, xBlockedTime;
\r
416 ( void ) pvParameters;
\r
420 /*********************************************************************
\r
423 This task does not participate in these tests. */
\r
424 vTaskSuspend( NULL );
\r
426 /*********************************************************************
\r
429 The first thing we do is attempt to read from the queue. It should be
\r
430 full so we block. Note the time before we block so we can check the
\r
431 wake time is as per that expected. */
\r
432 xTimeWhenBlocking = xTaskGetTickCount();
\r
434 /* We should unblock after bktTIME_TO_BLOCK having not sent anything to
\r
437 xRunIndicator = bktRUN_INDICATOR;
\r
438 if( xQueueSend( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL )
\r
440 xErrorOccurred = pdTRUE;
\r
443 /* How long were we inside the send function? */
\r
444 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
446 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
\r
447 if( xBlockedTime < bktTIME_TO_BLOCK )
\r
449 xErrorOccurred = pdTRUE;
\r
452 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
\r
453 either. A margin is permitted as we would not necessarily run as
\r
454 soon as we unblocked. */
\r
455 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
\r
457 xErrorOccurred = pdTRUE;
\r
460 /* Suspend ready for test 3. */
\r
461 xRunIndicator = bktRUN_INDICATOR;
\r
462 vTaskSuspend( NULL );
\r
464 /*********************************************************************
\r
467 As per test three, but with the send and receive reversed. */
\r
468 xTimeWhenBlocking = xTaskGetTickCount();
\r
470 /* We should unblock after bktTIME_TO_BLOCK having not received
\r
471 anything on the queue. */
\r
472 xRunIndicator = bktRUN_INDICATOR;
\r
473 if( xQueueReceive( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_EMPTY )
\r
475 xErrorOccurred = pdTRUE;
\r
478 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
480 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
\r
481 if( xBlockedTime < bktTIME_TO_BLOCK )
\r
483 xErrorOccurred = pdTRUE;
\r
486 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
\r
487 either. A margin is permitted as we would not necessarily run as soon
\r
488 as we unblocked. */
\r
489 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
\r
491 xErrorOccurred = pdTRUE;
\r
494 xRunIndicator = bktRUN_INDICATOR;
\r
496 xSecondaryCycles++;
\r
499 /*-----------------------------------------------------------*/
\r
501 static void prvBasicDelayTests( void )
\r
503 TickType_t xPreTime, xPostTime, x, xLastUnblockTime, xExpectedUnblockTime;
\r
504 const TickType_t xPeriod = 75, xCycles = 5, xAllowableMargin = ( bktALLOWABLE_MARGIN >> 1 );
\r
506 /* Temporarily increase priority so the timing is more accurate, but not so
\r
507 high as to disrupt the timer tests. */
\r
508 vTaskPrioritySet( NULL, configTIMER_TASK_PRIORITY - 1 );
\r
510 /* Crude check to too that vTaskDelay() blocks for the expected period. */
\r
511 xPreTime = xTaskGetTickCount();
\r
512 vTaskDelay( bktTIME_TO_BLOCK );
\r
513 xPostTime = xTaskGetTickCount();
\r
515 /* The priority is higher, so the allowable margin is halved when compared
\r
516 to the other tests in this file. */
\r
517 if( ( xPostTime - xPreTime ) > ( bktTIME_TO_BLOCK + xAllowableMargin ) )
\r
519 xErrorOccurred = pdTRUE;
\r
522 /* Now crude tests to check the vTaskDelayUntil() functionality. */
\r
523 xPostTime = xTaskGetTickCount();
\r
524 xLastUnblockTime = xPostTime;
\r
526 for( x = 0; x < xCycles; x++ )
\r
528 /* Calculate the next expected unblock time from the time taken before
\r
529 this loop was entered. */
\r
530 xExpectedUnblockTime = xPostTime + ( x * xPeriod );
\r
532 vTaskDelayUntil( &xLastUnblockTime, xPeriod );
\r
534 if( ( xTaskGetTickCount() - xExpectedUnblockTime ) > ( bktTIME_TO_BLOCK + xAllowableMargin ) )
\r
536 xErrorOccurred = pdTRUE;
\r
542 /* Reset to the original task priority ready for the other tests. */
\r
543 vTaskPrioritySet( NULL, bktPRIMARY_PRIORITY );
\r
545 /*-----------------------------------------------------------*/
\r
547 BaseType_t xAreBlockTimeTestTasksStillRunning( void )
\r
549 static BaseType_t xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0;
\r
550 BaseType_t xReturn = pdPASS;
\r
552 /* Have both tasks performed at least one cycle since this function was
\r
554 if( xPrimaryCycles == xLastPrimaryCycleCount )
\r
559 if( xSecondaryCycles == xLastSecondaryCycleCount )
\r
564 if( xErrorOccurred == pdTRUE )
\r
569 xLastSecondaryCycleCount = xSecondaryCycles;
\r
570 xLastPrimaryCycleCount = xPrimaryCycles;
\r