2 FreeRTOS.org V4.7.1 - 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
28 Please ensure to read the configuration and relevant port sections of the
\r
29 online documentation.
\r
31 +++ http://www.FreeRTOS.org +++
\r
32 Documentation, latest information, license and contact details.
\r
34 +++ http://www.SafeRTOS.com +++
\r
35 A version that is certified for use in safety critical systems.
\r
37 +++ http://www.OpenRTOS.com +++
\r
38 Commercial support, development, porting, licensing and training services.
\r
40 ***************************************************************************
\r
44 * This is a version of BlockTim.c that uses the light weight API.
\r
46 * This file contains some test scenarios that ensure tasks do not exit queue
\r
47 * send or receive functions prematurely. A description of the tests is
\r
48 * included within the code.
\r
51 /* Kernel includes. */
\r
52 #include "FreeRTOS.h"
\r
56 /* Demo includes. */
\r
57 #include "AltBlock.h"
\r
59 /* Task priorities. */
\r
60 #define bktPRIMARY_PRIORITY ( 3 )
\r
61 #define bktSECONDARY_PRIORITY ( 2 )
\r
63 /* Task behaviour. */
\r
64 #define bktQUEUE_LENGTH ( 5 )
\r
65 #define bktSHORT_WAIT ( ( ( portTickType ) 20 ) / portTICK_RATE_MS )
\r
66 #define bktPRIMARY_BLOCK_TIME ( 10 )
\r
67 #define bktALLOWABLE_MARGIN ( 12 )
\r
68 #define bktTIME_TO_BLOCK ( 175 )
\r
69 #define bktDONT_BLOCK ( ( portTickType ) 0 )
\r
70 #define bktRUN_INDICATOR ( ( unsigned portBASE_TYPE ) 0x55 )
\r
72 /* The queue on which the tasks block. */
\r
73 static xQueueHandle xTestQueue;
\r
75 /* Handle to the secondary task is required by the primary task for calls
\r
76 to vTaskSuspend/Resume(). */
\r
77 static xTaskHandle xSecondary;
\r
79 /* Used to ensure that tasks are still executing without error. */
\r
80 static portBASE_TYPE xPrimaryCycles = 0, xSecondaryCycles = 0;
\r
81 static portBASE_TYPE xErrorOccurred = pdFALSE;
\r
83 /* Provides a simple mechanism for the primary task to know when the
\r
84 secondary task has executed. */
\r
85 static volatile unsigned portBASE_TYPE xRunIndicator;
\r
87 /* The two test tasks. Their behaviour is commented within the files. */
\r
88 static void vPrimaryBlockTimeTestTask( void *pvParameters );
\r
89 static void vSecondaryBlockTimeTestTask( void *pvParameters );
\r
91 /*-----------------------------------------------------------*/
\r
93 void vCreateAltBlockTimeTasks( void )
\r
95 /* Create the queue on which the two tasks block. */
\r
96 xTestQueue = xQueueCreate( bktQUEUE_LENGTH, sizeof( portBASE_TYPE ) );
\r
98 /* Create the two test tasks. */
\r
99 xTaskCreate( vPrimaryBlockTimeTestTask, ( signed portCHAR * )"FBTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL );
\r
100 xTaskCreate( vSecondaryBlockTimeTestTask, ( signed portCHAR * )"FBTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary );
\r
102 /*-----------------------------------------------------------*/
\r
104 static void vPrimaryBlockTimeTestTask( void *pvParameters )
\r
106 portBASE_TYPE xItem, xData;
\r
107 portTickType xTimeWhenBlocking;
\r
108 portTickType xTimeToBlock, xBlockedTime;
\r
111 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
113 const portCHAR * const pcTaskStartMsg = "Alt primary block time test started.\r\n";
\r
115 /* Queue a message for printing to say the task has started. */
\r
116 vPrintDisplayMessage( &pcTaskStartMsg );
\r
119 ( void ) pvParameters;
\r
123 /*********************************************************************
\r
126 Simple block time wakeup test on queue receives. */
\r
127 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
129 /* The queue is empty. Attempt to read from the queue using a block
\r
130 time. When we wake, ensure the delta in time is as expected. */
\r
131 xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
\r
133 /* A critical section is used to minimise the jitter in the time
\r
135 portENTER_CRITICAL();
\r
137 xTimeWhenBlocking = xTaskGetTickCount();
\r
139 /* We should unblock after xTimeToBlock having not received
\r
140 anything on the queue. */
\r
141 if( xQueueAltReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY )
\r
143 xErrorOccurred = pdTRUE;
\r
146 /* How long were we blocked for? */
\r
147 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
149 portEXIT_CRITICAL();
\r
151 if( xBlockedTime < xTimeToBlock )
\r
153 /* Should not have blocked for less than we requested. */
\r
154 xErrorOccurred = pdTRUE;
\r
157 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
\r
159 /* Should not have blocked for longer than we requested,
\r
160 although we would not necessarily run as soon as we were
\r
161 unblocked so a margin is allowed. */
\r
162 xErrorOccurred = pdTRUE;
\r
167 #if configUSE_PREEMPTION == 0
\r
172 /*********************************************************************
\r
175 Simple block time wakeup test on queue sends.
\r
177 First fill the queue. It should be empty so all sends should pass. */
\r
178 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
180 if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
182 xErrorOccurred = pdTRUE;
\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 portENTER_CRITICAL();
\r
194 xTimeWhenBlocking = xTaskGetTickCount();
\r
196 /* We should unblock after xTimeToBlock having not received
\r
197 anything on the queue. */
\r
198 if( xQueueAltSendToBack( xTestQueue, &xItem, xTimeToBlock ) != errQUEUE_FULL )
\r
200 xErrorOccurred = pdTRUE;
\r
203 /* How long were we blocked for? */
\r
204 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
206 portEXIT_CRITICAL();
\r
208 if( xBlockedTime < xTimeToBlock )
\r
210 /* Should not have blocked for less than we requested. */
\r
211 xErrorOccurred = pdTRUE;
\r
214 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
\r
216 /* Should not have blocked for longer than we requested,
\r
217 although we would not necessarily run as soon as we were
\r
218 unblocked so a margin is allowed. */
\r
219 xErrorOccurred = pdTRUE;
\r
223 #if configUSE_PREEMPTION == 0
\r
228 /*********************************************************************
\r
231 Wake the other task, it will block attempting to post to the queue.
\r
232 When we read from the queue the other task will wake, but before it
\r
233 can run we will post to the queue again. When the other task runs it
\r
234 will find the queue still full, even though it was woken. It should
\r
235 recognise that its block time has not expired and return to block for
\r
236 the remains of its block time.
\r
238 Wake the other task so it blocks attempting to post to the already
\r
241 vTaskResume( xSecondary );
\r
243 /* We need to wait a little to ensure the other task executes. */
\r
244 while( xRunIndicator != bktRUN_INDICATOR )
\r
246 /* The other task has not yet executed. */
\r
247 vTaskDelay( bktSHORT_WAIT );
\r
249 /* Make sure the other task is blocked on the queue. */
\r
250 vTaskDelay( bktSHORT_WAIT );
\r
253 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
255 /* Now when we make space on the queue the other task should wake
\r
256 but not execute as this task has higher priority. */
\r
257 if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
259 xErrorOccurred = pdTRUE;
\r
262 /* Now fill the queue again before the other task gets a chance to
\r
263 execute. If the other task had executed we would find the queue
\r
264 full ourselves, and the other task have set xRunIndicator. */
\r
265 if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
267 xErrorOccurred = pdTRUE;
\r
270 if( xRunIndicator == bktRUN_INDICATOR )
\r
272 /* The other task should not have executed. */
\r
273 xErrorOccurred = pdTRUE;
\r
276 /* Raise the priority of the other task so it executes and blocks
\r
277 on the queue again. */
\r
278 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
\r
280 /* The other task should now have re-blocked without exiting the
\r
282 if( xRunIndicator == bktRUN_INDICATOR )
\r
284 /* The other task should not have executed outside of the
\r
286 xErrorOccurred = pdTRUE;
\r
289 /* Set the priority back down. */
\r
290 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
\r
293 /* Let the other task timeout. When it unblockes it will check that it
\r
294 unblocked at the correct time, then suspend itself. */
\r
295 while( xRunIndicator != bktRUN_INDICATOR )
\r
297 vTaskDelay( bktSHORT_WAIT );
\r
299 vTaskDelay( bktSHORT_WAIT );
\r
302 #if configUSE_PREEMPTION == 0
\r
306 /*********************************************************************
\r
309 As per test 3 - but with the send and receive the other way around.
\r
310 The other task blocks attempting to read from the queue.
\r
312 Empty the queue. We should find that it is full. */
\r
313 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
315 if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
317 xErrorOccurred = pdTRUE;
\r
321 /* Wake the other task so it blocks attempting to read from the
\r
322 already empty queue. */
\r
323 vTaskResume( xSecondary );
\r
325 /* We need to wait a little to ensure the other task executes. */
\r
326 while( xRunIndicator != bktRUN_INDICATOR )
\r
328 vTaskDelay( bktSHORT_WAIT );
\r
330 vTaskDelay( bktSHORT_WAIT );
\r
333 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
335 /* Now when we place an item on the queue the other task should
\r
336 wake but not execute as this task has higher priority. */
\r
337 if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
339 xErrorOccurred = pdTRUE;
\r
342 /* Now empty the queue again before the other task gets a chance to
\r
343 execute. If the other task had executed we would find the queue
\r
344 empty ourselves, and the other task would be suspended. */
\r
345 if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
347 xErrorOccurred = pdTRUE;
\r
350 if( xRunIndicator == bktRUN_INDICATOR )
\r
352 /* The other task should not have executed. */
\r
353 xErrorOccurred = pdTRUE;
\r
356 /* Raise the priority of the other task so it executes and blocks
\r
357 on the queue again. */
\r
358 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
\r
360 /* The other task should now have re-blocked without exiting the
\r
362 if( xRunIndicator == bktRUN_INDICATOR )
\r
364 /* The other task should not have executed outside of the
\r
366 xErrorOccurred = pdTRUE;
\r
368 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
\r
371 /* Let the other task timeout. When it unblockes it will check that it
\r
372 unblocked at the correct time, then suspend itself. */
\r
373 while( xRunIndicator != bktRUN_INDICATOR )
\r
375 vTaskDelay( bktSHORT_WAIT );
\r
377 vTaskDelay( bktSHORT_WAIT );
\r
382 /*-----------------------------------------------------------*/
\r
384 static void vSecondaryBlockTimeTestTask( void *pvParameters )
\r
386 portTickType xTimeWhenBlocking, xBlockedTime;
\r
387 portBASE_TYPE xData;
\r
390 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
392 const portCHAR * const pcTaskStartMsg = "Alt secondary block time test started.\r\n";
\r
394 /* Queue a message for printing to say the task has started. */
\r
395 vPrintDisplayMessage( &pcTaskStartMsg );
\r
398 ( void ) pvParameters;
\r
402 /*********************************************************************
\r
405 This task does does not participate in these tests. */
\r
406 vTaskSuspend( NULL );
\r
408 /*********************************************************************
\r
411 The first thing we do is attempt to read from the queue. It should be
\r
412 full so we block. Note the time before we block so we can check the
\r
413 wake time is as per that expected. */
\r
414 portENTER_CRITICAL();
\r
416 xTimeWhenBlocking = xTaskGetTickCount();
\r
418 /* We should unblock after bktTIME_TO_BLOCK having not received
\r
419 anything on the queue. */
\r
421 xRunIndicator = bktRUN_INDICATOR;
\r
422 if( xQueueAltSendToBack( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL )
\r
424 xErrorOccurred = pdTRUE;
\r
427 /* How long were we inside the send function? */
\r
428 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
430 portEXIT_CRITICAL();
\r
432 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
\r
433 if( xBlockedTime < bktTIME_TO_BLOCK )
\r
435 xErrorOccurred = pdTRUE;
\r
438 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
\r
439 either. A margin is permitted as we would not necessarily run as
\r
440 soon as we unblocked. */
\r
441 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
\r
443 xErrorOccurred = pdTRUE;
\r
446 /* Suspend ready for test 3. */
\r
447 xRunIndicator = bktRUN_INDICATOR;
\r
448 vTaskSuspend( NULL );
\r
450 /*********************************************************************
\r
453 As per test three, but with the send and receive reversed. */
\r
454 portENTER_CRITICAL();
\r
456 xTimeWhenBlocking = xTaskGetTickCount();
\r
458 /* We should unblock after bktTIME_TO_BLOCK having not received
\r
459 anything on the queue. */
\r
460 xRunIndicator = bktRUN_INDICATOR;
\r
461 if( xQueueAltReceive( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_EMPTY )
\r
463 xErrorOccurred = pdTRUE;
\r
466 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
468 portEXIT_CRITICAL();
\r
470 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
\r
471 if( xBlockedTime < bktTIME_TO_BLOCK )
\r
473 xErrorOccurred = pdTRUE;
\r
476 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
\r
477 either. A margin is permitted as we would not necessarily run as soon
\r
478 as we unblocked. */
\r
479 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
\r
481 xErrorOccurred = pdTRUE;
\r
484 xRunIndicator = bktRUN_INDICATOR;
\r
486 xSecondaryCycles++;
\r
489 /*-----------------------------------------------------------*/
\r
491 portBASE_TYPE xAreAltBlockTimeTestTasksStillRunning( void )
\r
493 static portBASE_TYPE xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0;
\r
494 portBASE_TYPE xReturn = pdPASS;
\r
496 /* Have both tasks performed at least one cycle since this function was
\r
498 if( xPrimaryCycles == xLastPrimaryCycleCount )
\r
503 if( xSecondaryCycles == xLastSecondaryCycleCount )
\r
508 if( xErrorOccurred == pdTRUE )
\r
513 xLastSecondaryCycleCount = xSecondaryCycles;
\r
514 xLastPrimaryCycleCount = xPrimaryCycles;
\r