2 FreeRTOS V7.5.3 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
68 * Tests the behaviour when data is peeked from a queue when there are
\r
69 * multiple tasks blocked on the queue.
\r
75 /* Scheduler include files. */
\r
76 #include "FreeRTOS.h"
\r
81 /* Demo program include files. */
\r
84 #define qpeekQUEUE_LENGTH ( 5 )
\r
85 #define qpeekNO_BLOCK ( 0 )
\r
86 #define qpeekSHORT_DELAY ( 10 )
\r
88 #define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
89 #define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
90 #define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
91 #define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
93 /*-----------------------------------------------------------*/
\r
96 * The following three tasks are used to demonstrate the peeking behaviour.
\r
97 * Each task is given a different priority to demonstrate the order in which
\r
98 * tasks are woken as data is peeked from a queue.
\r
100 static void prvLowPriorityPeekTask( void *pvParameters );
\r
101 static void prvMediumPriorityPeekTask( void *pvParameters );
\r
102 static void prvHighPriorityPeekTask( void *pvParameters );
\r
103 static void prvHighestPriorityPeekTask( void *pvParameters );
\r
105 /*-----------------------------------------------------------*/
\r
107 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
108 detected in any of the tasks. */
\r
109 static volatile portBASE_TYPE xErrorDetected = pdFALSE;
\r
111 /* Counter that is incremented on each cycle of a test. This is used to
\r
112 detect a stalled task - a test that is no longer running. */
\r
113 static volatile unsigned portLONG ulLoopCounter = 0;
\r
115 /* Handles to the test tasks. */
\r
116 xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
\r
117 /*-----------------------------------------------------------*/
\r
119 void vStartQueuePeekTasks( void )
\r
121 xQueueHandle xQueue;
\r
123 /* Create the queue that we are going to use for the test/demo. */
\r
124 xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
126 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
127 in use. The queue registry is provided as a means for kernel aware
\r
128 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
129 is not being used. The call to vQueueAddToRegistry() will be removed
\r
130 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
131 defined to be less than 1. */
\r
132 vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "QPeek_Test_Queue" );
\r
134 /* Create the demo tasks and pass it the queue just created. We are
\r
135 passing the queue handle by value so it does not matter that it is declared
\r
136 on the stack here. */
\r
137 xTaskCreate( prvLowPriorityPeekTask, ( signed portCHAR * )"PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
\r
138 xTaskCreate( prvMediumPriorityPeekTask, ( signed portCHAR * )"PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
\r
139 xTaskCreate( prvHighPriorityPeekTask, ( signed portCHAR * )"PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
\r
140 xTaskCreate( prvHighestPriorityPeekTask, ( signed portCHAR * )"PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
\r
142 /*-----------------------------------------------------------*/
\r
144 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
146 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
147 unsigned portLONG ulValue;
\r
151 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
153 const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
155 /* Queue a message for printing to say the task has started. */
\r
156 vPrintDisplayMessage( &pcTaskStartMsg );
\r
162 /* Try peeking from the queue. The queue should be empty so we will
\r
163 block, allowing the high priority task to execute. */
\r
164 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
166 /* We expected to have received something by the time we unblock. */
\r
167 xErrorDetected = pdTRUE;
\r
170 /* When we reach here the high and medium priority tasks should still
\r
171 be blocked on the queue. We unblocked because the low priority task
\r
172 wrote a value to the queue, which we should have peeked. Peeking the
\r
173 data (rather than receiving it) will leave the data on the queue, so
\r
174 the high priority task should then have also been unblocked, but not
\r
176 if( ulValue != 0x11223344 )
\r
178 /* We did not receive the expected value. */
\r
179 xErrorDetected = pdTRUE;
\r
182 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
184 /* The message should have been left on the queue. */
\r
185 xErrorDetected = pdTRUE;
\r
188 /* Now we are going to actually receive the data, so when the high
\r
189 priority task runs it will find the queue empty and return to the
\r
192 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
194 /* We expected to receive the value. */
\r
195 xErrorDetected = pdTRUE;
\r
198 if( ulValue != 0x11223344 )
\r
200 /* We did not receive the expected value - which should have been
\r
201 the same value as was peeked. */
\r
202 xErrorDetected = pdTRUE;
\r
205 /* Now we will block again as the queue is once more empty. The low
\r
206 priority task can then execute again. */
\r
207 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
209 /* We expected to have received something by the time we unblock. */
\r
210 xErrorDetected = pdTRUE;
\r
213 /* When we get here the low priority task should have again written to the
\r
215 if( ulValue != 0x01234567 )
\r
217 /* We did not receive the expected value. */
\r
218 xErrorDetected = pdTRUE;
\r
221 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
223 /* The message should have been left on the queue. */
\r
224 xErrorDetected = pdTRUE;
\r
227 /* We only peeked the data, so suspending ourselves now should enable
\r
228 the high priority task to also peek the data. The high priority task
\r
229 will have been unblocked when we peeked the data as we left the data
\r
231 vTaskSuspend( NULL );
\r
235 /* This time we are going to do the same as the above test, but the
\r
236 high priority task is going to receive the data, rather than peek it.
\r
237 This means that the medium priority task should never peek the value. */
\r
238 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
240 xErrorDetected = pdTRUE;
\r
243 if( ulValue != 0xaabbaabb )
\r
245 xErrorDetected = pdTRUE;
\r
248 vTaskSuspend( NULL );
\r
251 /*-----------------------------------------------------------*/
\r
253 static void prvHighPriorityPeekTask( void *pvParameters )
\r
255 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
256 unsigned portLONG ulValue;
\r
260 /* Try peeking from the queue. The queue should be empty so we will
\r
261 block, allowing the medium priority task to execute. Both the high
\r
262 and highest priority tasks will then be blocked on the queue. */
\r
263 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
265 /* We expected to have received something by the time we unblock. */
\r
266 xErrorDetected = pdTRUE;
\r
269 /* When we get here the highest priority task should have peeked the data
\r
270 (unblocking this task) then suspended (allowing this task to also peek
\r
272 if( ulValue != 0x01234567 )
\r
274 /* We did not receive the expected value. */
\r
275 xErrorDetected = pdTRUE;
\r
278 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
280 /* The message should have been left on the queue. */
\r
281 xErrorDetected = pdTRUE;
\r
284 /* We only peeked the data, so suspending ourselves now should enable
\r
285 the medium priority task to also peek the data. The medium priority task
\r
286 will have been unblocked when we peeked the data as we left the data
\r
288 vTaskSuspend( NULL );
\r
291 /* This time we are going actually receive the value, so the medium
\r
292 priority task will never peek the data - we removed it from the queue. */
\r
293 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
295 xErrorDetected = pdTRUE;
\r
298 if( ulValue != 0xaabbaabb )
\r
300 xErrorDetected = pdTRUE;
\r
303 vTaskSuspend( NULL );
\r
306 /*-----------------------------------------------------------*/
\r
308 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
310 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
311 unsigned portLONG ulValue;
\r
315 /* Try peeking from the queue. The queue should be empty so we will
\r
316 block, allowing the low priority task to execute. The highest, high
\r
317 and medium priority tasks will then all be blocked on the queue. */
\r
318 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
320 /* We expected to have received something by the time we unblock. */
\r
321 xErrorDetected = pdTRUE;
\r
324 /* When we get here the high priority task should have peeked the data
\r
325 (unblocking this task) then suspended (allowing this task to also peek
\r
327 if( ulValue != 0x01234567 )
\r
329 /* We did not receive the expected value. */
\r
330 xErrorDetected = pdTRUE;
\r
333 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
335 /* The message should have been left on the queue. */
\r
336 xErrorDetected = pdTRUE;
\r
339 /* Just so we know the test is still running. */
\r
342 /* Now we can suspend ourselves so the low priority task can execute
\r
344 vTaskSuspend( NULL );
\r
347 /*-----------------------------------------------------------*/
\r
349 static void prvLowPriorityPeekTask( void *pvParameters )
\r
351 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
352 unsigned portLONG ulValue;
\r
356 /* Write some data to the queue. This should unblock the highest
\r
357 priority task that is waiting to peek data from the queue. */
\r
358 ulValue = 0x11223344;
\r
359 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
361 /* We were expecting the queue to be empty so we should not of
\r
362 had a problem writing to the queue. */
\r
363 xErrorDetected = pdTRUE;
\r
366 /* By the time we get here the data should have been removed from
\r
368 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
370 xErrorDetected = pdTRUE;
\r
373 /* Write another value to the queue, again waking the highest priority
\r
374 task that is blocked on the queue. */
\r
375 ulValue = 0x01234567;
\r
376 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
378 /* We were expecting the queue to be empty so we should not of
\r
379 had a problem writing to the queue. */
\r
380 xErrorDetected = pdTRUE;
\r
383 /* All the other tasks should now have successfully peeked the data.
\r
384 The data is still in the queue so we should be able to receive it. */
\r
386 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
388 /* We expected to receive the data. */
\r
389 xErrorDetected = pdTRUE;
\r
392 if( ulValue != 0x01234567 )
\r
394 /* We did not receive the expected value. */
\r
397 /* Lets just delay a while as this is an intensive test as we don't
\r
398 want to starve other tests of processing time. */
\r
399 vTaskDelay( qpeekSHORT_DELAY );
\r
401 /* Unsuspend the other tasks so we can repeat the test - this time
\r
402 however not all the other tasks will peek the data as the high
\r
403 priority task is actually going to remove it from the queue. Send
\r
404 to front is used just to be different. As the queue is empty it
\r
405 makes no difference to the result. */
\r
406 vTaskResume( xMediumPriorityTask );
\r
407 vTaskResume( xHighPriorityTask );
\r
408 vTaskResume( xHighestPriorityTask );
\r
410 ulValue = 0xaabbaabb;
\r
411 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
413 /* We were expecting the queue to be empty so we should not of
\r
414 had a problem writing to the queue. */
\r
415 xErrorDetected = pdTRUE;
\r
418 /* This time we should find that the queue is empty. The high priority
\r
419 task actually removed the data rather than just peeking it. */
\r
420 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
\r
422 /* We expected to receive the data. */
\r
423 xErrorDetected = pdTRUE;
\r
426 /* Unsuspend the highest and high priority tasks so we can go back
\r
427 and repeat the whole thing. The medium priority task should not be
\r
428 suspended as it was not able to peek the data in this last case. */
\r
429 vTaskResume( xHighPriorityTask );
\r
430 vTaskResume( xHighestPriorityTask );
\r
432 /* Lets just delay a while as this is an intensive test as we don't
\r
433 want to starve other tests of processing time. */
\r
434 vTaskDelay( qpeekSHORT_DELAY );
\r
437 /*-----------------------------------------------------------*/
\r
439 /* This is called to check that all the created tasks are still running. */
\r
440 portBASE_TYPE xAreQueuePeekTasksStillRunning( void )
\r
442 static unsigned portLONG ulLastLoopCounter = 0;
\r
444 /* If the demo task is still running then we expect the loopcounter to
\r
445 have incremented since this function was last called. */
\r
446 if( ulLastLoopCounter == ulLoopCounter )
\r
448 xErrorDetected = pdTRUE;
\r
451 ulLastLoopCounter = ulLoopCounter;
\r
453 /* Errors detected in the task itself will have latched xErrorDetected
\r
456 return !xErrorDetected;
\r