2 FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
10 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
17 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
19 A special exception to the GPL is included to allow you to distribute a
\r
20 combined work that includes FreeRTOS.org without being obliged to provide
\r
21 the source code for any proprietary components. See the licensing section
\r
22 of http://www.FreeRTOS.org for full details.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 * This is a version of BlockTim.c that uses the light weight API.
\r
55 * This file contains some test scenarios that ensure tasks do not exit queue
\r
56 * send or receive functions prematurely. A description of the tests is
\r
57 * included within the code.
\r
60 /* Kernel includes. */
\r
61 #include "FreeRTOS.h"
\r
65 /* Demo includes. */
\r
66 #include "AltBlock.h"
\r
68 /* Task priorities. */
\r
69 #define bktPRIMARY_PRIORITY ( 3 )
\r
70 #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 ( 12 )
\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 portBASE_TYPE xPrimaryCycles = 0, xSecondaryCycles = 0;
\r
90 static 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 vCreateAltBlockTimeTasks( 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 portCHAR * ) "AltBlockQueue" );
\r
116 /* Create the two test tasks. */
\r
117 xTaskCreate( vPrimaryBlockTimeTestTask, ( signed portCHAR * )"FBTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL );
\r
118 xTaskCreate( vSecondaryBlockTimeTestTask, ( signed portCHAR * )"FBTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary );
\r
120 /*-----------------------------------------------------------*/
\r
122 static void vPrimaryBlockTimeTestTask( void *pvParameters )
\r
124 portBASE_TYPE xItem, xData;
\r
125 portTickType xTimeWhenBlocking;
\r
126 portTickType xTimeToBlock, xBlockedTime;
\r
129 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
131 const portCHAR * const pcTaskStartMsg = "Alt primary block time test started.\r\n";
\r
133 /* Queue a message for printing to say the task has started. */
\r
134 vPrintDisplayMessage( &pcTaskStartMsg );
\r
137 ( void ) pvParameters;
\r
141 /*********************************************************************
\r
144 Simple block time wakeup test on queue receives. */
\r
145 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
147 /* The queue is empty. Attempt to read from the queue using a block
\r
148 time. When we wake, ensure the delta in time is as expected. */
\r
149 xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
\r
151 /* A critical section is used to minimise the jitter in the time
\r
153 portENTER_CRITICAL();
\r
155 xTimeWhenBlocking = xTaskGetTickCount();
\r
157 /* We should unblock after xTimeToBlock having not received
\r
158 anything on the queue. */
\r
159 if( xQueueAltReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY )
\r
161 xErrorOccurred = pdTRUE;
\r
164 /* How long were we blocked for? */
\r
165 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
167 portEXIT_CRITICAL();
\r
169 if( xBlockedTime < xTimeToBlock )
\r
171 /* Should not have blocked for less than we requested. */
\r
172 xErrorOccurred = pdTRUE;
\r
175 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
\r
177 /* Should not have blocked for longer than we requested,
\r
178 although we would not necessarily run as soon as we were
\r
179 unblocked so a margin is allowed. */
\r
180 xErrorOccurred = pdTRUE;
\r
185 #if configUSE_PREEMPTION == 0
\r
190 /*********************************************************************
\r
193 Simple block time wakeup test on queue sends.
\r
195 First fill the queue. It should be empty so all sends should pass. */
\r
196 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
198 if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
200 xErrorOccurred = pdTRUE;
\r
204 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
206 /* The queue is full. Attempt to write to the queue using a block
\r
207 time. When we wake, ensure the delta in time is as expected. */
\r
208 xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
\r
210 portENTER_CRITICAL();
\r
212 xTimeWhenBlocking = xTaskGetTickCount();
\r
214 /* We should unblock after xTimeToBlock having not received
\r
215 anything on the queue. */
\r
216 if( xQueueAltSendToBack( xTestQueue, &xItem, xTimeToBlock ) != errQUEUE_FULL )
\r
218 xErrorOccurred = pdTRUE;
\r
221 /* How long were we blocked for? */
\r
222 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
224 portEXIT_CRITICAL();
\r
226 if( xBlockedTime < xTimeToBlock )
\r
228 /* Should not have blocked for less than we requested. */
\r
229 xErrorOccurred = pdTRUE;
\r
232 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
\r
234 /* Should not have blocked for longer than we requested,
\r
235 although we would not necessarily run as soon as we were
\r
236 unblocked so a margin is allowed. */
\r
237 xErrorOccurred = pdTRUE;
\r
241 #if configUSE_PREEMPTION == 0
\r
246 /*********************************************************************
\r
249 Wake the other task, it will block attempting to post to the queue.
\r
250 When we read from the queue the other task will wake, but before it
\r
251 can run we will post to the queue again. When the other task runs it
\r
252 will find the queue still full, even though it was woken. It should
\r
253 recognise that its block time has not expired and return to block for
\r
254 the remains of its block time.
\r
256 Wake the other task so it blocks attempting to post to the already
\r
259 vTaskResume( xSecondary );
\r
261 /* We need to wait a little to ensure the other task executes. */
\r
262 while( xRunIndicator != bktRUN_INDICATOR )
\r
264 /* The other task has not yet executed. */
\r
265 vTaskDelay( bktSHORT_WAIT );
\r
267 /* Make sure the other task is blocked on the queue. */
\r
268 vTaskDelay( bktSHORT_WAIT );
\r
271 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
273 /* Now when we make space on the queue the other task should wake
\r
274 but not execute as this task has higher priority. */
\r
275 if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
277 xErrorOccurred = pdTRUE;
\r
280 /* Now fill the queue again before the other task gets a chance to
\r
281 execute. If the other task had executed we would find the queue
\r
282 full ourselves, and the other task have set xRunIndicator. */
\r
283 if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
285 xErrorOccurred = pdTRUE;
\r
288 if( xRunIndicator == bktRUN_INDICATOR )
\r
290 /* The other task should not have executed. */
\r
291 xErrorOccurred = pdTRUE;
\r
294 /* Raise the priority of the other task so it executes and blocks
\r
295 on the queue again. */
\r
296 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
\r
298 /* The other task should now have re-blocked without exiting the
\r
300 if( xRunIndicator == bktRUN_INDICATOR )
\r
302 /* The other task should not have executed outside of the
\r
304 xErrorOccurred = pdTRUE;
\r
307 /* Set the priority back down. */
\r
308 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
\r
311 /* Let the other task timeout. When it unblockes it will check that it
\r
312 unblocked at the correct time, then suspend itself. */
\r
313 while( xRunIndicator != bktRUN_INDICATOR )
\r
315 vTaskDelay( bktSHORT_WAIT );
\r
317 vTaskDelay( bktSHORT_WAIT );
\r
320 #if configUSE_PREEMPTION == 0
\r
324 /*********************************************************************
\r
327 As per test 3 - but with the send and receive the other way around.
\r
328 The other task blocks attempting to read from the queue.
\r
330 Empty the queue. We should find that it is full. */
\r
331 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
333 if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
335 xErrorOccurred = pdTRUE;
\r
339 /* Wake the other task so it blocks attempting to read from the
\r
340 already empty queue. */
\r
341 vTaskResume( xSecondary );
\r
343 /* We need to wait a little to ensure the other task executes. */
\r
344 while( xRunIndicator != bktRUN_INDICATOR )
\r
346 vTaskDelay( bktSHORT_WAIT );
\r
348 vTaskDelay( bktSHORT_WAIT );
\r
351 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
353 /* Now when we place an item on the queue the other task should
\r
354 wake but not execute as this task has higher priority. */
\r
355 if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
357 xErrorOccurred = pdTRUE;
\r
360 /* Now empty the queue again before the other task gets a chance to
\r
361 execute. If the other task had executed we would find the queue
\r
362 empty ourselves, and the other task would be suspended. */
\r
363 if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
365 xErrorOccurred = pdTRUE;
\r
368 if( xRunIndicator == bktRUN_INDICATOR )
\r
370 /* The other task should not have executed. */
\r
371 xErrorOccurred = pdTRUE;
\r
374 /* Raise the priority of the other task so it executes and blocks
\r
375 on the queue again. */
\r
376 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
\r
378 /* The other task should now have re-blocked without exiting the
\r
380 if( xRunIndicator == bktRUN_INDICATOR )
\r
382 /* The other task should not have executed outside of the
\r
384 xErrorOccurred = pdTRUE;
\r
386 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
\r
389 /* Let the other task timeout. When it unblockes it will check that it
\r
390 unblocked at the correct time, then suspend itself. */
\r
391 while( xRunIndicator != bktRUN_INDICATOR )
\r
393 vTaskDelay( bktSHORT_WAIT );
\r
395 vTaskDelay( bktSHORT_WAIT );
\r
400 /*-----------------------------------------------------------*/
\r
402 static void vSecondaryBlockTimeTestTask( void *pvParameters )
\r
404 portTickType xTimeWhenBlocking, xBlockedTime;
\r
405 portBASE_TYPE xData;
\r
408 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
410 const portCHAR * const pcTaskStartMsg = "Alt secondary block time test started.\r\n";
\r
412 /* Queue a message for printing to say the task has started. */
\r
413 vPrintDisplayMessage( &pcTaskStartMsg );
\r
416 ( void ) pvParameters;
\r
420 /*********************************************************************
\r
423 This task does 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 portENTER_CRITICAL();
\r
434 xTimeWhenBlocking = xTaskGetTickCount();
\r
436 /* We should unblock after bktTIME_TO_BLOCK having not received
\r
437 anything on the queue. */
\r
439 xRunIndicator = bktRUN_INDICATOR;
\r
440 if( xQueueAltSendToBack( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL )
\r
442 xErrorOccurred = pdTRUE;
\r
445 /* How long were we inside the send function? */
\r
446 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
448 portEXIT_CRITICAL();
\r
450 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
\r
451 if( xBlockedTime < bktTIME_TO_BLOCK )
\r
453 xErrorOccurred = pdTRUE;
\r
456 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
\r
457 either. A margin is permitted as we would not necessarily run as
\r
458 soon as we unblocked. */
\r
459 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
\r
461 xErrorOccurred = pdTRUE;
\r
464 /* Suspend ready for test 3. */
\r
465 xRunIndicator = bktRUN_INDICATOR;
\r
466 vTaskSuspend( NULL );
\r
468 /*********************************************************************
\r
471 As per test three, but with the send and receive reversed. */
\r
472 portENTER_CRITICAL();
\r
474 xTimeWhenBlocking = xTaskGetTickCount();
\r
476 /* We should unblock after bktTIME_TO_BLOCK having not received
\r
477 anything on the queue. */
\r
478 xRunIndicator = bktRUN_INDICATOR;
\r
479 if( xQueueAltReceive( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_EMPTY )
\r
481 xErrorOccurred = pdTRUE;
\r
484 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
486 portEXIT_CRITICAL();
\r
488 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
\r
489 if( xBlockedTime < bktTIME_TO_BLOCK )
\r
491 xErrorOccurred = pdTRUE;
\r
494 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
\r
495 either. A margin is permitted as we would not necessarily run as soon
\r
496 as we unblocked. */
\r
497 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
\r
499 xErrorOccurred = pdTRUE;
\r
502 xRunIndicator = bktRUN_INDICATOR;
\r
504 xSecondaryCycles++;
\r
507 /*-----------------------------------------------------------*/
\r
509 portBASE_TYPE xAreAltBlockTimeTestTasksStillRunning( void )
\r
511 static portBASE_TYPE xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0;
\r
512 portBASE_TYPE xReturn = pdPASS;
\r
514 /* Have both tasks performed at least one cycle since this function was
\r
516 if( xPrimaryCycles == xLastPrimaryCycleCount )
\r
521 if( xSecondaryCycles == xLastSecondaryCycleCount )
\r
526 if( xErrorOccurred == pdTRUE )
\r
531 xLastSecondaryCycleCount = xSecondaryCycles;
\r
532 xLastPrimaryCycleCount = xPrimaryCycles;
\r