2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
50 * This file contains some test scenarios that ensure tasks do not exit queue
\r
51 * send or receive functions prematurely. A description of the tests is
\r
52 * included within the code.
\r
55 /* Kernel includes. */
\r
56 #include "FreeRTOS.h"
\r
60 /* Demo includes. */
\r
61 #include "blocktim.h"
\r
63 /* Task priorities. Allow these to be overridden. */
\r
64 #ifndef bktPRIMARY_PRIORITY
\r
65 #define bktPRIMARY_PRIORITY ( 3 )
\r
68 #ifndef bktSECONDARY_PRIORITY
\r
69 #define bktSECONDARY_PRIORITY ( 2 )
\r
72 /* Task behaviour. */
\r
73 #define bktQUEUE_LENGTH ( 5 )
\r
74 #define bktSHORT_WAIT ( ( ( portTickType ) 20 ) / portTICK_RATE_MS )
\r
75 #define bktPRIMARY_BLOCK_TIME ( 10 )
\r
76 #define bktALLOWABLE_MARGIN ( 15 )
\r
77 #define bktTIME_TO_BLOCK ( 175 )
\r
78 #define bktDONT_BLOCK ( ( portTickType ) 0 )
\r
79 #define bktRUN_INDICATOR ( ( unsigned portBASE_TYPE ) 0x55 )
\r
81 /* The queue on which the tasks block. */
\r
82 static xQueueHandle xTestQueue;
\r
84 /* Handle to the secondary task is required by the primary task for calls
\r
85 to vTaskSuspend/Resume(). */
\r
86 static xTaskHandle xSecondary;
\r
88 /* Used to ensure that tasks are still executing without error. */
\r
89 static volatile portBASE_TYPE xPrimaryCycles = 0, xSecondaryCycles = 0;
\r
90 static volatile portBASE_TYPE xErrorOccurred = pdFALSE;
\r
92 /* Provides a simple mechanism for the primary task to know when the
\r
93 secondary task has executed. */
\r
94 static volatile unsigned portBASE_TYPE xRunIndicator;
\r
96 /* The two test tasks. Their behaviour is commented within the files. */
\r
97 static void vPrimaryBlockTimeTestTask( void *pvParameters );
\r
98 static void vSecondaryBlockTimeTestTask( void *pvParameters );
\r
100 /*-----------------------------------------------------------*/
\r
102 void vCreateBlockTimeTasks( void )
\r
104 /* Create the queue on which the two tasks block. */
\r
105 xTestQueue = xQueueCreate( bktQUEUE_LENGTH, sizeof( portBASE_TYPE ) );
\r
107 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
108 in use. The queue registry is provided as a means for kernel aware
\r
109 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
110 is not being used. The call to vQueueAddToRegistry() will be removed
\r
111 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
112 defined to be less than 1. */
\r
113 vQueueAddToRegistry( xTestQueue, ( signed char * ) "Block_Time_Queue" );
\r
115 /* Create the two test tasks. */
\r
116 xTaskCreate( vPrimaryBlockTimeTestTask, ( signed char * )"BTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL );
\r
117 xTaskCreate( vSecondaryBlockTimeTestTask, ( signed char * )"BTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary );
\r
119 /*-----------------------------------------------------------*/
\r
121 static void vPrimaryBlockTimeTestTask( void *pvParameters )
\r
123 portBASE_TYPE xItem, xData;
\r
124 portTickType xTimeWhenBlocking;
\r
125 portTickType xTimeToBlock, xBlockedTime;
\r
127 ( void ) pvParameters;
\r
131 /*********************************************************************
\r
134 Simple block time wakeup test on queue receives. */
\r
135 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
137 /* The queue is empty. Attempt to read from the queue using a block
\r
138 time. When we wake, ensure the delta in time is as expected. */
\r
139 xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
\r
141 xTimeWhenBlocking = xTaskGetTickCount();
\r
143 /* We should unblock after xTimeToBlock having not received
\r
144 anything on the queue. */
\r
145 if( xQueueReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY )
\r
147 xErrorOccurred = pdTRUE;
\r
150 /* How long were we blocked for? */
\r
151 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
153 if( xBlockedTime < xTimeToBlock )
\r
155 /* Should not have blocked for less than we requested. */
\r
156 xErrorOccurred = pdTRUE;
\r
159 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
\r
161 /* Should not have blocked for longer than we requested,
\r
162 although we would not necessarily run as soon as we were
\r
163 unblocked so a margin is allowed. */
\r
164 xErrorOccurred = pdTRUE;
\r
168 /*********************************************************************
\r
171 Simple block time wakeup test on queue sends.
\r
173 First fill the queue. It should be empty so all sends should pass. */
\r
174 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
176 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
178 xErrorOccurred = pdTRUE;
\r
181 #if configUSE_PREEMPTION == 0
\r
186 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
188 /* The queue is full. Attempt to write to the queue using a block
\r
189 time. When we wake, ensure the delta in time is as expected. */
\r
190 xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
\r
192 xTimeWhenBlocking = xTaskGetTickCount();
\r
194 /* We should unblock after xTimeToBlock having not received
\r
195 anything on the queue. */
\r
196 if( xQueueSend( xTestQueue, &xItem, xTimeToBlock ) != errQUEUE_FULL )
\r
198 xErrorOccurred = pdTRUE;
\r
201 /* How long were we blocked for? */
\r
202 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
204 if( xBlockedTime < xTimeToBlock )
\r
206 /* Should not have blocked for less than we requested. */
\r
207 xErrorOccurred = pdTRUE;
\r
210 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
\r
212 /* Should not have blocked for longer than we requested,
\r
213 although we would not necessarily run as soon as we were
\r
214 unblocked so a margin is allowed. */
\r
215 xErrorOccurred = pdTRUE;
\r
219 /*********************************************************************
\r
222 Wake the other task, it will block attempting to post to the queue.
\r
223 When we read from the queue the other task will wake, but before it
\r
224 can run we will post to the queue again. When the other task runs it
\r
225 will find the queue still full, even though it was woken. It should
\r
226 recognise that its block time has not expired and return to block for
\r
227 the remains of its block time.
\r
229 Wake the other task so it blocks attempting to post to the already
\r
232 vTaskResume( xSecondary );
\r
234 /* We need to wait a little to ensure the other task executes. */
\r
235 while( xRunIndicator != bktRUN_INDICATOR )
\r
237 /* The other task has not yet executed. */
\r
238 vTaskDelay( bktSHORT_WAIT );
\r
240 /* Make sure the other task is blocked on the queue. */
\r
241 vTaskDelay( bktSHORT_WAIT );
\r
244 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
246 /* Now when we make space on the queue the other task should wake
\r
247 but not execute as this task has higher priority. */
\r
248 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
250 xErrorOccurred = pdTRUE;
\r
253 /* Now fill the queue again before the other task gets a chance to
\r
254 execute. If the other task had executed we would find the queue
\r
255 full ourselves, and the other task have set xRunIndicator. */
\r
256 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
258 xErrorOccurred = pdTRUE;
\r
261 if( xRunIndicator == bktRUN_INDICATOR )
\r
263 /* The other task should not have executed. */
\r
264 xErrorOccurred = pdTRUE;
\r
267 /* Raise the priority of the other task so it executes and blocks
\r
268 on the queue again. */
\r
269 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
\r
271 /* The other task should now have re-blocked without exiting the
\r
273 if( xRunIndicator == bktRUN_INDICATOR )
\r
275 /* The other task should not have executed outside of the
\r
277 xErrorOccurred = pdTRUE;
\r
280 /* Set the priority back down. */
\r
281 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
\r
284 /* Let the other task timeout. When it unblockes it will check that it
\r
285 unblocked at the correct time, then suspend itself. */
\r
286 while( xRunIndicator != bktRUN_INDICATOR )
\r
288 vTaskDelay( bktSHORT_WAIT );
\r
290 vTaskDelay( bktSHORT_WAIT );
\r
294 /*********************************************************************
\r
297 As per test 3 - but with the send and receive the other way around.
\r
298 The other task blocks attempting to read from the queue.
\r
300 Empty the queue. We should find that it is full. */
\r
301 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
303 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
305 xErrorOccurred = pdTRUE;
\r
309 /* Wake the other task so it blocks attempting to read from the
\r
310 already empty queue. */
\r
311 vTaskResume( xSecondary );
\r
313 /* We need to wait a little to ensure the other task executes. */
\r
314 while( xRunIndicator != bktRUN_INDICATOR )
\r
316 vTaskDelay( bktSHORT_WAIT );
\r
318 vTaskDelay( bktSHORT_WAIT );
\r
321 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
323 /* Now when we place an item on the queue the other task should
\r
324 wake but not execute as this task has higher priority. */
\r
325 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
327 xErrorOccurred = pdTRUE;
\r
330 /* Now empty the queue again before the other task gets a chance to
\r
331 execute. If the other task had executed we would find the queue
\r
332 empty ourselves, and the other task would be suspended. */
\r
333 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
335 xErrorOccurred = pdTRUE;
\r
338 if( xRunIndicator == bktRUN_INDICATOR )
\r
340 /* The other task should not have executed. */
\r
341 xErrorOccurred = pdTRUE;
\r
344 /* Raise the priority of the other task so it executes and blocks
\r
345 on the queue again. */
\r
346 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
\r
348 /* The other task should now have re-blocked without exiting the
\r
350 if( xRunIndicator == bktRUN_INDICATOR )
\r
352 /* The other task should not have executed outside of the
\r
354 xErrorOccurred = pdTRUE;
\r
356 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
\r
359 /* Let the other task timeout. When it unblockes it will check that it
\r
360 unblocked at the correct time, then suspend itself. */
\r
361 while( xRunIndicator != bktRUN_INDICATOR )
\r
363 vTaskDelay( bktSHORT_WAIT );
\r
365 vTaskDelay( bktSHORT_WAIT );
\r
370 /*-----------------------------------------------------------*/
\r
372 static void vSecondaryBlockTimeTestTask( void *pvParameters )
\r
374 portTickType xTimeWhenBlocking, xBlockedTime;
\r
375 portBASE_TYPE xData;
\r
377 ( void ) pvParameters;
\r
381 /*********************************************************************
\r
384 This task does does not participate in these tests. */
\r
385 vTaskSuspend( NULL );
\r
387 /*********************************************************************
\r
390 The first thing we do is attempt to read from the queue. It should be
\r
391 full so we block. Note the time before we block so we can check the
\r
392 wake time is as per that expected. */
\r
393 xTimeWhenBlocking = xTaskGetTickCount();
\r
395 /* We should unblock after bktTIME_TO_BLOCK having not sent
\r
396 anything to the queue. */
\r
398 xRunIndicator = bktRUN_INDICATOR;
\r
399 if( xQueueSend( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL )
\r
401 xErrorOccurred = pdTRUE;
\r
404 /* How long were we inside the send function? */
\r
405 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
407 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
\r
408 if( xBlockedTime < bktTIME_TO_BLOCK )
\r
410 xErrorOccurred = pdTRUE;
\r
413 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
\r
414 either. A margin is permitted as we would not necessarily run as
\r
415 soon as we unblocked. */
\r
416 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
\r
418 xErrorOccurred = pdTRUE;
\r
421 /* Suspend ready for test 3. */
\r
422 xRunIndicator = bktRUN_INDICATOR;
\r
423 vTaskSuspend( NULL );
\r
425 /*********************************************************************
\r
428 As per test three, but with the send and receive reversed. */
\r
429 xTimeWhenBlocking = xTaskGetTickCount();
\r
431 /* We should unblock after bktTIME_TO_BLOCK having not received
\r
432 anything on the queue. */
\r
433 xRunIndicator = bktRUN_INDICATOR;
\r
434 if( xQueueReceive( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_EMPTY )
\r
436 xErrorOccurred = pdTRUE;
\r
439 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
441 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
\r
442 if( xBlockedTime < bktTIME_TO_BLOCK )
\r
444 xErrorOccurred = pdTRUE;
\r
447 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
\r
448 either. A margin is permitted as we would not necessarily run as soon
\r
449 as we unblocked. */
\r
450 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
\r
452 xErrorOccurred = pdTRUE;
\r
455 xRunIndicator = bktRUN_INDICATOR;
\r
457 xSecondaryCycles++;
\r
460 /*-----------------------------------------------------------*/
\r
462 portBASE_TYPE xAreBlockTimeTestTasksStillRunning( void )
\r
464 static portBASE_TYPE xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0;
\r
465 portBASE_TYPE xReturn = pdPASS;
\r
467 /* Have both tasks performed at least one cycle since this function was
\r
469 if( xPrimaryCycles == xLastPrimaryCycleCount )
\r
474 if( xSecondaryCycles == xLastSecondaryCycleCount )
\r
479 if( xErrorOccurred == pdTRUE )
\r
484 xLastSecondaryCycleCount = xSecondaryCycles;
\r
485 xLastPrimaryCycleCount = xPrimaryCycles;
\r