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