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 file contains some test scenarios that ensure tasks do not exit queue
\r
45 * send or receive functions prematurely. A description of the tests is
\r
46 * included within the code.
\r
49 /* Kernel includes. */
\r
50 #include "FreeRTOS.h"
\r
54 /* Demo includes. */
\r
55 #include "blocktim.h"
\r
57 /* Task priorities. */
\r
58 #define bktPRIMARY_PRIORITY ( 3 )
\r
59 #define bktSECONDARY_PRIORITY ( 2 )
\r
61 /* Task behaviour. */
\r
62 #define bktQUEUE_LENGTH ( 5 )
\r
63 #define bktSHORT_WAIT ( ( ( portTickType ) 20 ) / portTICK_RATE_MS )
\r
64 #define bktPRIMARY_BLOCK_TIME ( 10 )
\r
65 #define bktALLOWABLE_MARGIN ( 12 )
\r
66 #define bktTIME_TO_BLOCK ( 175 )
\r
67 #define bktDONT_BLOCK ( ( portTickType ) 0 )
\r
68 #define bktRUN_INDICATOR ( ( unsigned portBASE_TYPE ) 0x55 )
\r
70 /* The queue on which the tasks block. */
\r
71 static xQueueHandle xTestQueue;
\r
73 /* Handle to the secondary task is required by the primary task for calls
\r
74 to vTaskSuspend/Resume(). */
\r
75 static xTaskHandle xSecondary;
\r
77 /* Used to ensure that tasks are still executing without error. */
\r
78 static portBASE_TYPE xPrimaryCycles = 0, xSecondaryCycles = 0;
\r
79 static portBASE_TYPE xErrorOccurred = pdFALSE;
\r
81 /* Provides a simple mechanism for the primary task to know when the
\r
82 secondary task has executed. */
\r
83 static volatile unsigned portBASE_TYPE xRunIndicator;
\r
85 /* The two test tasks. Their behaviour is commented within the files. */
\r
86 static void vPrimaryBlockTimeTestTask( void *pvParameters );
\r
87 static void vSecondaryBlockTimeTestTask( void *pvParameters );
\r
89 /*-----------------------------------------------------------*/
\r
91 void vCreateBlockTimeTasks( void )
\r
93 /* Create the queue on which the two tasks block. */
\r
94 xTestQueue = xQueueCreate( bktQUEUE_LENGTH, sizeof( portBASE_TYPE ) );
\r
96 /* Create the two test tasks. */
\r
97 xTaskCreate( vPrimaryBlockTimeTestTask, ( signed portCHAR * )"BTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL );
\r
98 xTaskCreate( vSecondaryBlockTimeTestTask, ( signed portCHAR * )"BTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary );
\r
100 /*-----------------------------------------------------------*/
\r
102 static void vPrimaryBlockTimeTestTask( void *pvParameters )
\r
104 portBASE_TYPE xItem, xData;
\r
105 portTickType xTimeWhenBlocking;
\r
106 portTickType xTimeToBlock, xBlockedTime;
\r
108 ( void ) pvParameters;
\r
112 /*********************************************************************
\r
115 Simple block time wakeup test on queue receives. */
\r
116 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
118 /* The queue is empty. Attempt to read from the queue using a block
\r
119 time. When we wake, ensure the delta in time is as expected. */
\r
120 xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
\r
122 /* A critical section is used to minimise the jitter in the time
\r
124 portENTER_CRITICAL();
\r
126 xTimeWhenBlocking = xTaskGetTickCount();
\r
128 /* We should unblock after xTimeToBlock having not received
\r
129 anything on the queue. */
\r
130 if( xQueueReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY )
\r
132 xErrorOccurred = pdTRUE;
\r
135 /* How long were we blocked for? */
\r
136 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
138 portEXIT_CRITICAL();
\r
140 if( xBlockedTime < xTimeToBlock )
\r
142 /* Should not have blocked for less than we requested. */
\r
143 xErrorOccurred = pdTRUE;
\r
146 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
\r
148 /* Should not have blocked for longer than we requested,
\r
149 although we would not necessarily run as soon as we were
\r
150 unblocked so a margin is allowed. */
\r
151 xErrorOccurred = pdTRUE;
\r
155 /*********************************************************************
\r
158 Simple block time wakeup test on queue sends.
\r
160 First fill the queue. It should be empty so all sends should pass. */
\r
161 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
163 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
165 xErrorOccurred = pdTRUE;
\r
168 #if configUSE_PREEMPTION == 0
\r
173 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
175 /* The queue is full. Attempt to write to the queue using a block
\r
176 time. When we wake, ensure the delta in time is as expected. */
\r
177 xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
\r
179 portENTER_CRITICAL();
\r
181 xTimeWhenBlocking = xTaskGetTickCount();
\r
183 /* We should unblock after xTimeToBlock having not received
\r
184 anything on the queue. */
\r
185 if( xQueueSend( xTestQueue, &xItem, xTimeToBlock ) != errQUEUE_FULL )
\r
187 xErrorOccurred = pdTRUE;
\r
190 /* How long were we blocked for? */
\r
191 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
193 portEXIT_CRITICAL();
\r
195 if( xBlockedTime < xTimeToBlock )
\r
197 /* Should not have blocked for less than we requested. */
\r
198 xErrorOccurred = pdTRUE;
\r
201 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
\r
203 /* Should not have blocked for longer than we requested,
\r
204 although we would not necessarily run as soon as we were
\r
205 unblocked so a margin is allowed. */
\r
206 xErrorOccurred = pdTRUE;
\r
210 /*********************************************************************
\r
213 Wake the other task, it will block attempting to post to the queue.
\r
214 When we read from the queue the other task will wake, but before it
\r
215 can run we will post to the queue again. When the other task runs it
\r
216 will find the queue still full, even though it was woken. It should
\r
217 recognise that its block time has not expired and return to block for
\r
218 the remains of its block time.
\r
220 Wake the other task so it blocks attempting to post to the already
\r
223 vTaskResume( xSecondary );
\r
225 /* We need to wait a little to ensure the other task executes. */
\r
226 while( xRunIndicator != bktRUN_INDICATOR )
\r
228 /* The other task has not yet executed. */
\r
229 vTaskDelay( bktSHORT_WAIT );
\r
231 /* Make sure the other task is blocked on the queue. */
\r
232 vTaskDelay( bktSHORT_WAIT );
\r
235 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
237 /* Now when we make space on the queue the other task should wake
\r
238 but not execute as this task has higher priority. */
\r
239 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
241 xErrorOccurred = pdTRUE;
\r
244 /* Now fill the queue again before the other task gets a chance to
\r
245 execute. If the other task had executed we would find the queue
\r
246 full ourselves, and the other task have set xRunIndicator. */
\r
247 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
249 xErrorOccurred = pdTRUE;
\r
252 if( xRunIndicator == bktRUN_INDICATOR )
\r
254 /* The other task should not have executed. */
\r
255 xErrorOccurred = pdTRUE;
\r
258 /* Raise the priority of the other task so it executes and blocks
\r
259 on the queue again. */
\r
260 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
\r
262 /* The other task should now have re-blocked without exiting the
\r
264 if( xRunIndicator == bktRUN_INDICATOR )
\r
266 /* The other task should not have executed outside of the
\r
268 xErrorOccurred = pdTRUE;
\r
271 /* Set the priority back down. */
\r
272 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
\r
275 /* Let the other task timeout. When it unblockes it will check that it
\r
276 unblocked at the correct time, then suspend itself. */
\r
277 while( xRunIndicator != bktRUN_INDICATOR )
\r
279 vTaskDelay( bktSHORT_WAIT );
\r
281 vTaskDelay( bktSHORT_WAIT );
\r
285 /*********************************************************************
\r
288 As per test 3 - but with the send and receive the other way around.
\r
289 The other task blocks attempting to read from the queue.
\r
291 Empty the queue. We should find that it is full. */
\r
292 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
294 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
296 xErrorOccurred = pdTRUE;
\r
300 /* Wake the other task so it blocks attempting to read from the
\r
301 already empty queue. */
\r
302 vTaskResume( xSecondary );
\r
304 /* We need to wait a little to ensure the other task executes. */
\r
305 while( xRunIndicator != bktRUN_INDICATOR )
\r
307 vTaskDelay( bktSHORT_WAIT );
\r
309 vTaskDelay( bktSHORT_WAIT );
\r
312 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
\r
314 /* Now when we place an item on the queue the other task should
\r
315 wake but not execute as this task has higher priority. */
\r
316 if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
\r
318 xErrorOccurred = pdTRUE;
\r
321 /* Now empty the queue again before the other task gets a chance to
\r
322 execute. If the other task had executed we would find the queue
\r
323 empty ourselves, and the other task would be suspended. */
\r
324 if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
\r
326 xErrorOccurred = pdTRUE;
\r
329 if( xRunIndicator == bktRUN_INDICATOR )
\r
331 /* The other task should not have executed. */
\r
332 xErrorOccurred = pdTRUE;
\r
335 /* Raise the priority of the other task so it executes and blocks
\r
336 on the queue again. */
\r
337 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
\r
339 /* The other task should now have re-blocked without exiting the
\r
341 if( xRunIndicator == bktRUN_INDICATOR )
\r
343 /* The other task should not have executed outside of the
\r
345 xErrorOccurred = pdTRUE;
\r
347 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
\r
350 /* Let the other task timeout. When it unblockes it will check that it
\r
351 unblocked at the correct time, then suspend itself. */
\r
352 while( xRunIndicator != bktRUN_INDICATOR )
\r
354 vTaskDelay( bktSHORT_WAIT );
\r
356 vTaskDelay( bktSHORT_WAIT );
\r
361 /*-----------------------------------------------------------*/
\r
363 static void vSecondaryBlockTimeTestTask( void *pvParameters )
\r
365 portTickType xTimeWhenBlocking, xBlockedTime;
\r
366 portBASE_TYPE xData;
\r
368 ( void ) pvParameters;
\r
372 /*********************************************************************
\r
375 This task does does not participate in these tests. */
\r
376 vTaskSuspend( NULL );
\r
378 /*********************************************************************
\r
381 The first thing we do is attempt to read from the queue. It should be
\r
382 full so we block. Note the time before we block so we can check the
\r
383 wake time is as per that expected. */
\r
384 portENTER_CRITICAL();
\r
386 xTimeWhenBlocking = xTaskGetTickCount();
\r
388 /* We should unblock after bktTIME_TO_BLOCK having not received
\r
389 anything on the queue. */
\r
391 xRunIndicator = bktRUN_INDICATOR;
\r
392 if( xQueueSend( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL )
\r
394 xErrorOccurred = pdTRUE;
\r
397 /* How long were we inside the send function? */
\r
398 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
400 portEXIT_CRITICAL();
\r
402 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
\r
403 if( xBlockedTime < bktTIME_TO_BLOCK )
\r
405 xErrorOccurred = pdTRUE;
\r
408 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
\r
409 either. A margin is permitted as we would not necessarily run as
\r
410 soon as we unblocked. */
\r
411 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
\r
413 xErrorOccurred = pdTRUE;
\r
416 /* Suspend ready for test 3. */
\r
417 xRunIndicator = bktRUN_INDICATOR;
\r
418 vTaskSuspend( NULL );
\r
420 /*********************************************************************
\r
423 As per test three, but with the send and receive reversed. */
\r
424 portENTER_CRITICAL();
\r
426 xTimeWhenBlocking = xTaskGetTickCount();
\r
428 /* We should unblock after bktTIME_TO_BLOCK having not received
\r
429 anything on the queue. */
\r
430 xRunIndicator = bktRUN_INDICATOR;
\r
431 if( xQueueReceive( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_EMPTY )
\r
433 xErrorOccurred = pdTRUE;
\r
436 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
\r
438 portEXIT_CRITICAL();
\r
440 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
\r
441 if( xBlockedTime < bktTIME_TO_BLOCK )
\r
443 xErrorOccurred = pdTRUE;
\r
446 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
\r
447 either. A margin is permitted as we would not necessarily run as soon
\r
448 as we unblocked. */
\r
449 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
\r
451 xErrorOccurred = pdTRUE;
\r
454 xRunIndicator = bktRUN_INDICATOR;
\r
456 xSecondaryCycles++;
\r
459 /*-----------------------------------------------------------*/
\r
461 portBASE_TYPE xAreBlockTimeTestTasksStillRunning( void )
\r
463 static portBASE_TYPE xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0;
\r
464 portBASE_TYPE xReturn = pdPASS;
\r
466 /* Have both tasks performed at least one cycle since this function was
\r
468 if( xPrimaryCycles == xLastPrimaryCycleCount )
\r
473 if( xSecondaryCycles == xLastSecondaryCycleCount )
\r
478 if( xErrorOccurred == pdTRUE )
\r
483 xLastSecondaryCycleCount = xSecondaryCycles;
\r
484 xLastPrimaryCycleCount = xPrimaryCycles;
\r