2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 * Tests the behaviour when data is peeked from a queue when there are
\r
70 * multiple tasks blocked on the queue.
\r
76 /* Scheduler include files. */
\r
77 #include "FreeRTOS.h"
\r
82 /* Demo program include files. */
\r
85 #define qpeekQUEUE_LENGTH ( 5 )
\r
86 #define qpeekNO_BLOCK ( 0 )
\r
87 #define qpeekSHORT_DELAY ( 10 )
\r
89 #define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
90 #define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
91 #define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
92 #define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
94 /*-----------------------------------------------------------*/
\r
97 * The following three tasks are used to demonstrate the peeking behaviour.
\r
98 * Each task is given a different priority to demonstrate the order in which
\r
99 * tasks are woken as data is peeked from a queue.
\r
101 static void prvLowPriorityPeekTask( void *pvParameters );
\r
102 static void prvMediumPriorityPeekTask( void *pvParameters );
\r
103 static void prvHighPriorityPeekTask( void *pvParameters );
\r
104 static void prvHighestPriorityPeekTask( void *pvParameters );
\r
106 /*-----------------------------------------------------------*/
\r
108 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
109 detected in any of the tasks. */
\r
110 static volatile portBASE_TYPE xErrorDetected = pdFALSE;
\r
112 /* Counter that is incremented on each cycle of a test. This is used to
\r
113 detect a stalled task - a test that is no longer running. */
\r
114 static volatile unsigned portLONG ulLoopCounter = 0;
\r
116 /* Handles to the test tasks. */
\r
117 xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
\r
118 /*-----------------------------------------------------------*/
\r
120 void vStartQueuePeekTasks( void )
\r
122 xQueueHandle xQueue;
\r
124 /* Create the queue that we are going to use for the test/demo. */
\r
125 xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
127 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
128 in use. The queue registry is provided as a means for kernel aware
\r
129 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
130 is not being used. The call to vQueueAddToRegistry() will be removed
\r
131 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
132 defined to be less than 1. */
\r
133 vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "QPeek_Test_Queue" );
\r
135 /* Create the demo tasks and pass it the queue just created. We are
\r
136 passing the queue handle by value so it does not matter that it is declared
\r
137 on the stack here. */
\r
138 xTaskCreate( prvLowPriorityPeekTask, ( signed portCHAR * )"PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
\r
139 xTaskCreate( prvMediumPriorityPeekTask, ( signed portCHAR * )"PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
\r
140 xTaskCreate( prvHighPriorityPeekTask, ( signed portCHAR * )"PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
\r
141 xTaskCreate( prvHighestPriorityPeekTask, ( signed portCHAR * )"PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
\r
143 /*-----------------------------------------------------------*/
\r
145 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
147 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
148 unsigned portLONG ulValue;
\r
152 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
154 const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
156 /* Queue a message for printing to say the task has started. */
\r
157 vPrintDisplayMessage( &pcTaskStartMsg );
\r
163 /* Try peeking from the queue. The queue should be empty so we will
\r
164 block, allowing the high priority task to execute. */
\r
165 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
167 /* We expected to have received something by the time we unblock. */
\r
168 xErrorDetected = pdTRUE;
\r
171 /* When we reach here the high and medium priority tasks should still
\r
172 be blocked on the queue. We unblocked because the low priority task
\r
173 wrote a value to the queue, which we should have peeked. Peeking the
\r
174 data (rather than receiving it) will leave the data on the queue, so
\r
175 the high priority task should then have also been unblocked, but not
\r
177 if( ulValue != 0x11223344 )
\r
179 /* We did not receive the expected value. */
\r
180 xErrorDetected = pdTRUE;
\r
183 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
185 /* The message should have been left on the queue. */
\r
186 xErrorDetected = pdTRUE;
\r
189 /* Now we are going to actually receive the data, so when the high
\r
190 priority task runs it will find the queue empty and return to the
\r
193 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
195 /* We expected to receive the value. */
\r
196 xErrorDetected = pdTRUE;
\r
199 if( ulValue != 0x11223344 )
\r
201 /* We did not receive the expected value - which should have been
\r
202 the same value as was peeked. */
\r
203 xErrorDetected = pdTRUE;
\r
206 /* Now we will block again as the queue is once more empty. The low
\r
207 priority task can then execute again. */
\r
208 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
210 /* We expected to have received something by the time we unblock. */
\r
211 xErrorDetected = pdTRUE;
\r
214 /* When we get here the low priority task should have again written to the
\r
216 if( ulValue != 0x01234567 )
\r
218 /* We did not receive the expected value. */
\r
219 xErrorDetected = pdTRUE;
\r
222 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
224 /* The message should have been left on the queue. */
\r
225 xErrorDetected = pdTRUE;
\r
228 /* We only peeked the data, so suspending ourselves now should enable
\r
229 the high priority task to also peek the data. The high priority task
\r
230 will have been unblocked when we peeked the data as we left the data
\r
232 vTaskSuspend( NULL );
\r
236 /* This time we are going to do the same as the above test, but the
\r
237 high priority task is going to receive the data, rather than peek it.
\r
238 This means that the medium priority task should never peek the value. */
\r
239 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
241 xErrorDetected = pdTRUE;
\r
244 if( ulValue != 0xaabbaabb )
\r
246 xErrorDetected = pdTRUE;
\r
249 vTaskSuspend( NULL );
\r
252 /*-----------------------------------------------------------*/
\r
254 static void prvHighPriorityPeekTask( void *pvParameters )
\r
256 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
257 unsigned portLONG ulValue;
\r
261 /* Try peeking from the queue. The queue should be empty so we will
\r
262 block, allowing the medium priority task to execute. Both the high
\r
263 and highest priority tasks will then be blocked on the queue. */
\r
264 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
266 /* We expected to have received something by the time we unblock. */
\r
267 xErrorDetected = pdTRUE;
\r
270 /* When we get here the highest priority task should have peeked the data
\r
271 (unblocking this task) then suspended (allowing this task to also peek
\r
273 if( ulValue != 0x01234567 )
\r
275 /* We did not receive the expected value. */
\r
276 xErrorDetected = pdTRUE;
\r
279 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
281 /* The message should have been left on the queue. */
\r
282 xErrorDetected = pdTRUE;
\r
285 /* We only peeked the data, so suspending ourselves now should enable
\r
286 the medium priority task to also peek the data. The medium priority task
\r
287 will have been unblocked when we peeked the data as we left the data
\r
289 vTaskSuspend( NULL );
\r
292 /* This time we are going actually receive the value, so the medium
\r
293 priority task will never peek the data - we removed it from the queue. */
\r
294 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
296 xErrorDetected = pdTRUE;
\r
299 if( ulValue != 0xaabbaabb )
\r
301 xErrorDetected = pdTRUE;
\r
304 vTaskSuspend( NULL );
\r
307 /*-----------------------------------------------------------*/
\r
309 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
311 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
312 unsigned portLONG ulValue;
\r
316 /* Try peeking from the queue. The queue should be empty so we will
\r
317 block, allowing the low priority task to execute. The highest, high
\r
318 and medium priority tasks will then all be blocked on the queue. */
\r
319 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
321 /* We expected to have received something by the time we unblock. */
\r
322 xErrorDetected = pdTRUE;
\r
325 /* When we get here the high priority task should have peeked the data
\r
326 (unblocking this task) then suspended (allowing this task to also peek
\r
328 if( ulValue != 0x01234567 )
\r
330 /* We did not receive the expected value. */
\r
331 xErrorDetected = pdTRUE;
\r
334 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
336 /* The message should have been left on the queue. */
\r
337 xErrorDetected = pdTRUE;
\r
340 /* Just so we know the test is still running. */
\r
343 /* Now we can suspend ourselves so the low priority task can execute
\r
345 vTaskSuspend( NULL );
\r
348 /*-----------------------------------------------------------*/
\r
350 static void prvLowPriorityPeekTask( void *pvParameters )
\r
352 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
353 unsigned portLONG ulValue;
\r
357 /* Write some data to the queue. This should unblock the highest
\r
358 priority task that is waiting to peek data from the queue. */
\r
359 ulValue = 0x11223344;
\r
360 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
362 /* We were expecting the queue to be empty so we should not of
\r
363 had a problem writing to the queue. */
\r
364 xErrorDetected = pdTRUE;
\r
367 /* By the time we get here the data should have been removed from
\r
369 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
371 xErrorDetected = pdTRUE;
\r
374 /* Write another value to the queue, again waking the highest priority
\r
375 task that is blocked on the queue. */
\r
376 ulValue = 0x01234567;
\r
377 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
379 /* We were expecting the queue to be empty so we should not of
\r
380 had a problem writing to the queue. */
\r
381 xErrorDetected = pdTRUE;
\r
384 /* All the other tasks should now have successfully peeked the data.
\r
385 The data is still in the queue so we should be able to receive it. */
\r
387 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
389 /* We expected to receive the data. */
\r
390 xErrorDetected = pdTRUE;
\r
393 if( ulValue != 0x01234567 )
\r
395 /* We did not receive the expected value. */
\r
398 /* Lets just delay a while as this is an intensive test as we don't
\r
399 want to starve other tests of processing time. */
\r
400 vTaskDelay( qpeekSHORT_DELAY );
\r
402 /* Unsuspend the other tasks so we can repeat the test - this time
\r
403 however not all the other tasks will peek the data as the high
\r
404 priority task is actually going to remove it from the queue. Send
\r
405 to front is used just to be different. As the queue is empty it
\r
406 makes no difference to the result. */
\r
407 vTaskResume( xMediumPriorityTask );
\r
408 vTaskResume( xHighPriorityTask );
\r
409 vTaskResume( xHighestPriorityTask );
\r
411 ulValue = 0xaabbaabb;
\r
412 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
414 /* We were expecting the queue to be empty so we should not of
\r
415 had a problem writing to the queue. */
\r
416 xErrorDetected = pdTRUE;
\r
419 /* This time we should find that the queue is empty. The high priority
\r
420 task actually removed the data rather than just peeking it. */
\r
421 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
\r
423 /* We expected to receive the data. */
\r
424 xErrorDetected = pdTRUE;
\r
427 /* Unsuspend the highest and high priority tasks so we can go back
\r
428 and repeat the whole thing. The medium priority task should not be
\r
429 suspended as it was not able to peek the data in this last case. */
\r
430 vTaskResume( xHighPriorityTask );
\r
431 vTaskResume( xHighestPriorityTask );
\r
433 /* Lets just delay a while as this is an intensive test as we don't
\r
434 want to starve other tests of processing time. */
\r
435 vTaskDelay( qpeekSHORT_DELAY );
\r
438 /*-----------------------------------------------------------*/
\r
440 /* This is called to check that all the created tasks are still running. */
\r
441 portBASE_TYPE xAreQueuePeekTasksStillRunning( void )
\r
443 static unsigned portLONG ulLastLoopCounter = 0;
\r
445 /* If the demo task is still running then we expect the loopcounter to
\r
446 have incremented since this function was last called. */
\r
447 if( ulLastLoopCounter == ulLoopCounter )
\r
449 xErrorDetected = pdTRUE;
\r
452 ulLastLoopCounter = ulLoopCounter;
\r
454 /* Errors detected in the task itself will have latched xErrorDetected
\r
457 return !xErrorDetected;
\r