2 FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.
4 This file is part of the FreeRTOS.org distribution.
6 FreeRTOS.org is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 FreeRTOS.org is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with FreeRTOS.org; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 A special exception to the GPL can be applied should you wish to distribute
21 a combined work that includes FreeRTOS.org, without being obliged to provide
22 the source code for any proprietary components. See the licensing section
23 of http://www.FreeRTOS.org for full details of how and when the exception
26 ***************************************************************************
27 ***************************************************************************
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
30 * and even write all or part of your application on your behalf. *
31 * See http://www.OpenRTOS.com for details of the services we provide to *
32 * expedite your project. *
34 ***************************************************************************
35 ***************************************************************************
37 Please ensure to read the configuration and relevant port sections of the
40 http://www.FreeRTOS.org - Documentation, latest information, license and
43 http://www.SafeRTOS.com - A version that is certified for use in safety
46 http://www.OpenRTOS.com - Commercial support, development, porting,
47 licensing and training services.
51 * This file contains some test scenarios that ensure tasks do not exit queue
52 * send or receive functions prematurely. A description of the tests is
53 * included within the code.
56 /* Kernel includes. */
64 /* Task priorities. */
65 #define bktPRIMARY_PRIORITY ( 3 )
66 #define bktSECONDARY_PRIORITY ( 2 )
69 #define bktQUEUE_LENGTH ( 5 )
70 #define bktSHORT_WAIT ( ( ( portTickType ) 20 ) / portTICK_RATE_MS )
71 #define bktPRIMARY_BLOCK_TIME ( 10 )
72 #define bktALLOWABLE_MARGIN ( 15 )
73 #define bktTIME_TO_BLOCK ( 175 )
74 #define bktDONT_BLOCK ( ( portTickType ) 0 )
75 #define bktRUN_INDICATOR ( ( unsigned portBASE_TYPE ) 0x55 )
77 /* The queue on which the tasks block. */
78 static xQueueHandle xTestQueue;
80 /* Handle to the secondary task is required by the primary task for calls
81 to vTaskSuspend/Resume(). */
82 static xTaskHandle xSecondary;
84 /* Used to ensure that tasks are still executing without error. */
85 static volatile portBASE_TYPE xPrimaryCycles = 0, xSecondaryCycles = 0;
86 static volatile portBASE_TYPE xErrorOccurred = pdFALSE;
88 /* Provides a simple mechanism for the primary task to know when the
89 secondary task has executed. */
90 static volatile unsigned portBASE_TYPE xRunIndicator;
92 /* The two test tasks. Their behaviour is commented within the files. */
93 static void vPrimaryBlockTimeTestTask( void *pvParameters );
94 static void vSecondaryBlockTimeTestTask( void *pvParameters );
96 /*-----------------------------------------------------------*/
98 void vCreateBlockTimeTasks( void )
100 /* Create the queue on which the two tasks block. */
101 xTestQueue = xQueueCreate( bktQUEUE_LENGTH, sizeof( portBASE_TYPE ) );
103 /* Create the two test tasks. */
104 xTaskCreate( vPrimaryBlockTimeTestTask, ( signed portCHAR * )"BTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL );
105 xTaskCreate( vSecondaryBlockTimeTestTask, ( signed portCHAR * )"BTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary );
107 /*-----------------------------------------------------------*/
109 static void vPrimaryBlockTimeTestTask( void *pvParameters )
111 portBASE_TYPE xItem, xData;
112 portTickType xTimeWhenBlocking;
113 portTickType xTimeToBlock, xBlockedTime;
115 ( void ) pvParameters;
119 /*********************************************************************
122 Simple block time wakeup test on queue receives. */
123 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
125 /* The queue is empty. Attempt to read from the queue using a block
126 time. When we wake, ensure the delta in time is as expected. */
127 xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
129 xTimeWhenBlocking = xTaskGetTickCount();
131 /* We should unblock after xTimeToBlock having not received
132 anything on the queue. */
133 if( xQueueReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY )
135 xErrorOccurred = pdTRUE;
138 /* How long were we blocked for? */
139 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
141 if( xBlockedTime < xTimeToBlock )
143 /* Should not have blocked for less than we requested. */
144 xErrorOccurred = pdTRUE;
147 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
149 /* Should not have blocked for longer than we requested,
150 although we would not necessarily run as soon as we were
151 unblocked so a margin is allowed. */
152 xErrorOccurred = pdTRUE;
156 /*********************************************************************
159 Simple block time wakeup test on queue sends.
161 First fill the queue. It should be empty so all sends should pass. */
162 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
164 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
166 xErrorOccurred = pdTRUE;
169 #if configUSE_PREEMPTION == 0
174 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
176 /* The queue is full. Attempt to write to the queue using a block
177 time. When we wake, ensure the delta in time is as expected. */
178 xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
180 xTimeWhenBlocking = xTaskGetTickCount();
182 /* We should unblock after xTimeToBlock having not received
183 anything on the queue. */
184 if( xQueueSend( xTestQueue, &xItem, xTimeToBlock ) != errQUEUE_FULL )
186 xErrorOccurred = pdTRUE;
189 /* How long were we blocked for? */
190 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
192 if( xBlockedTime < xTimeToBlock )
194 /* Should not have blocked for less than we requested. */
195 xErrorOccurred = pdTRUE;
198 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
200 /* Should not have blocked for longer than we requested,
201 although we would not necessarily run as soon as we were
202 unblocked so a margin is allowed. */
203 xErrorOccurred = pdTRUE;
207 /*********************************************************************
210 Wake the other task, it will block attempting to post to the queue.
211 When we read from the queue the other task will wake, but before it
212 can run we will post to the queue again. When the other task runs it
213 will find the queue still full, even though it was woken. It should
214 recognise that its block time has not expired and return to block for
215 the remains of its block time.
217 Wake the other task so it blocks attempting to post to the already
220 vTaskResume( xSecondary );
222 /* We need to wait a little to ensure the other task executes. */
223 while( xRunIndicator != bktRUN_INDICATOR )
225 /* The other task has not yet executed. */
226 vTaskDelay( bktSHORT_WAIT );
228 /* Make sure the other task is blocked on the queue. */
229 vTaskDelay( bktSHORT_WAIT );
232 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
234 /* Now when we make space on the queue the other task should wake
235 but not execute as this task has higher priority. */
236 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
238 xErrorOccurred = pdTRUE;
241 /* Now fill the queue again before the other task gets a chance to
242 execute. If the other task had executed we would find the queue
243 full ourselves, and the other task have set xRunIndicator. */
244 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
246 xErrorOccurred = pdTRUE;
249 if( xRunIndicator == bktRUN_INDICATOR )
251 /* The other task should not have executed. */
252 xErrorOccurred = pdTRUE;
255 /* Raise the priority of the other task so it executes and blocks
256 on the queue again. */
257 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
259 /* The other task should now have re-blocked without exiting the
261 if( xRunIndicator == bktRUN_INDICATOR )
263 /* The other task should not have executed outside of the
265 xErrorOccurred = pdTRUE;
268 /* Set the priority back down. */
269 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
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 )
276 vTaskDelay( bktSHORT_WAIT );
278 vTaskDelay( bktSHORT_WAIT );
282 /*********************************************************************
285 As per test 3 - but with the send and receive the other way around.
286 The other task blocks attempting to read from the queue.
288 Empty the queue. We should find that it is full. */
289 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
291 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
293 xErrorOccurred = pdTRUE;
297 /* Wake the other task so it blocks attempting to read from the
298 already empty queue. */
299 vTaskResume( xSecondary );
301 /* We need to wait a little to ensure the other task executes. */
302 while( xRunIndicator != bktRUN_INDICATOR )
304 vTaskDelay( bktSHORT_WAIT );
306 vTaskDelay( bktSHORT_WAIT );
309 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
311 /* Now when we place an item on the queue the other task should
312 wake but not execute as this task has higher priority. */
313 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
315 xErrorOccurred = pdTRUE;
318 /* Now empty the queue again before the other task gets a chance to
319 execute. If the other task had executed we would find the queue
320 empty ourselves, and the other task would be suspended. */
321 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
323 xErrorOccurred = pdTRUE;
326 if( xRunIndicator == bktRUN_INDICATOR )
328 /* The other task should not have executed. */
329 xErrorOccurred = pdTRUE;
332 /* Raise the priority of the other task so it executes and blocks
333 on the queue again. */
334 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
336 /* The other task should now have re-blocked without exiting the
338 if( xRunIndicator == bktRUN_INDICATOR )
340 /* The other task should not have executed outside of the
342 xErrorOccurred = pdTRUE;
344 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
347 /* Let the other task timeout. When it unblockes it will check that it
348 unblocked at the correct time, then suspend itself. */
349 while( xRunIndicator != bktRUN_INDICATOR )
351 vTaskDelay( bktSHORT_WAIT );
353 vTaskDelay( bktSHORT_WAIT );
358 /*-----------------------------------------------------------*/
360 static void vSecondaryBlockTimeTestTask( void *pvParameters )
362 portTickType xTimeWhenBlocking, xBlockedTime;
365 ( void ) pvParameters;
369 /*********************************************************************
372 This task does does not participate in these tests. */
373 vTaskSuspend( NULL );
375 /*********************************************************************
378 The first thing we do is attempt to read from the queue. It should be
379 full so we block. Note the time before we block so we can check the
380 wake time is as per that expected. */
381 xTimeWhenBlocking = xTaskGetTickCount();
383 /* We should unblock after bktTIME_TO_BLOCK having not received
384 anything on the queue. */
386 xRunIndicator = bktRUN_INDICATOR;
387 if( xQueueSend( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL )
389 xErrorOccurred = pdTRUE;
392 /* How long were we inside the send function? */
393 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
395 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
396 if( xBlockedTime < bktTIME_TO_BLOCK )
398 xErrorOccurred = pdTRUE;
401 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
402 either. A margin is permitted as we would not necessarily run as
403 soon as we unblocked. */
404 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
406 xErrorOccurred = pdTRUE;
409 /* Suspend ready for test 3. */
410 xRunIndicator = bktRUN_INDICATOR;
411 vTaskSuspend( NULL );
413 /*********************************************************************
416 As per test three, but with the send and receive reversed. */
417 xTimeWhenBlocking = xTaskGetTickCount();
419 /* We should unblock after bktTIME_TO_BLOCK having not received
420 anything on the queue. */
421 xRunIndicator = bktRUN_INDICATOR;
422 if( xQueueReceive( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_EMPTY )
424 xErrorOccurred = pdTRUE;
427 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
429 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
430 if( xBlockedTime < bktTIME_TO_BLOCK )
432 xErrorOccurred = pdTRUE;
435 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
436 either. A margin is permitted as we would not necessarily run as soon
438 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
440 xErrorOccurred = pdTRUE;
443 xRunIndicator = bktRUN_INDICATOR;
448 /*-----------------------------------------------------------*/
450 portBASE_TYPE xAreBlockTimeTestTasksStillRunning( void )
452 static portBASE_TYPE xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0;
453 portBASE_TYPE xReturn = pdPASS;
455 /* Have both tasks performed at least one cycle since this function was
457 if( xPrimaryCycles == xLastPrimaryCycleCount )
462 if( xSecondaryCycles == xLastSecondaryCycleCount )
467 if( xErrorOccurred == pdTRUE )
472 xLastSecondaryCycleCount = xSecondaryCycles;
473 xLastPrimaryCycleCount = xPrimaryCycles;