2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
71 * Tests the behaviour when data is peeked from a queue when there are
\r
72 * multiple tasks blocked on the queue.
\r
78 /* Scheduler include files. */
\r
79 #include "FreeRTOS.h"
\r
84 /* Demo program include files. */
\r
87 #define qpeekQUEUE_LENGTH ( 5 )
\r
88 #define qpeekNO_BLOCK ( 0 )
\r
89 #define qpeekSHORT_DELAY ( 10 )
\r
91 #define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
92 #define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
93 #define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
94 #define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
96 /*-----------------------------------------------------------*/
\r
99 * The following three tasks are used to demonstrate the peeking behaviour.
\r
100 * Each task is given a different priority to demonstrate the order in which
\r
101 * tasks are woken as data is peeked from a queue.
\r
103 static void prvLowPriorityPeekTask( void *pvParameters );
\r
104 static void prvMediumPriorityPeekTask( void *pvParameters );
\r
105 static void prvHighPriorityPeekTask( void *pvParameters );
\r
106 static void prvHighestPriorityPeekTask( void *pvParameters );
\r
108 /*-----------------------------------------------------------*/
\r
110 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
111 detected in any of the tasks. */
\r
112 static volatile portBASE_TYPE xErrorDetected = pdFALSE;
\r
114 /* Counter that is incremented on each cycle of a test. This is used to
\r
115 detect a stalled task - a test that is no longer running. */
\r
116 static volatile unsigned portLONG ulLoopCounter = 0;
\r
118 /* Handles to the test tasks. */
\r
119 xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
\r
120 /*-----------------------------------------------------------*/
\r
122 void vStartQueuePeekTasks( void )
\r
124 xQueueHandle xQueue;
\r
126 /* Create the queue that we are going to use for the test/demo. */
\r
127 xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
129 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
130 in use. The queue registry is provided as a means for kernel aware
\r
131 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
132 is not being used. The call to vQueueAddToRegistry() will be removed
\r
133 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
134 defined to be less than 1. */
\r
135 vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "QPeek_Test_Queue" );
\r
137 /* Create the demo tasks and pass it the queue just created. We are
\r
138 passing the queue handle by value so it does not matter that it is declared
\r
139 on the stack here. */
\r
140 xTaskCreate( prvLowPriorityPeekTask, ( signed portCHAR * )"PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
\r
141 xTaskCreate( prvMediumPriorityPeekTask, ( signed portCHAR * )"PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
\r
142 xTaskCreate( prvHighPriorityPeekTask, ( signed portCHAR * )"PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
\r
143 xTaskCreate( prvHighestPriorityPeekTask, ( signed portCHAR * )"PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
\r
145 /*-----------------------------------------------------------*/
\r
147 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
149 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
150 unsigned portLONG ulValue;
\r
154 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
156 const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
158 /* Queue a message for printing to say the task has started. */
\r
159 vPrintDisplayMessage( &pcTaskStartMsg );
\r
165 /* Try peeking from the queue. The queue should be empty so we will
\r
166 block, allowing the high priority task to execute. */
\r
167 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
169 /* We expected to have received something by the time we unblock. */
\r
170 xErrorDetected = pdTRUE;
\r
173 /* When we reach here the high and medium priority tasks should still
\r
174 be blocked on the queue. We unblocked because the low priority task
\r
175 wrote a value to the queue, which we should have peeked. Peeking the
\r
176 data (rather than receiving it) will leave the data on the queue, so
\r
177 the high priority task should then have also been unblocked, but not
\r
179 if( ulValue != 0x11223344 )
\r
181 /* We did not receive the expected value. */
\r
182 xErrorDetected = pdTRUE;
\r
185 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
187 /* The message should have been left on the queue. */
\r
188 xErrorDetected = pdTRUE;
\r
191 /* Now we are going to actually receive the data, so when the high
\r
192 priority task runs it will find the queue empty and return to the
\r
195 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
197 /* We expected to receive the value. */
\r
198 xErrorDetected = pdTRUE;
\r
201 if( ulValue != 0x11223344 )
\r
203 /* We did not receive the expected value - which should have been
\r
204 the same value as was peeked. */
\r
205 xErrorDetected = pdTRUE;
\r
208 /* Now we will block again as the queue is once more empty. The low
\r
209 priority task can then execute again. */
\r
210 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
212 /* We expected to have received something by the time we unblock. */
\r
213 xErrorDetected = pdTRUE;
\r
216 /* When we get here the low priority task should have again written to the
\r
218 if( ulValue != 0x01234567 )
\r
220 /* We did not receive the expected value. */
\r
221 xErrorDetected = pdTRUE;
\r
224 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
226 /* The message should have been left on the queue. */
\r
227 xErrorDetected = pdTRUE;
\r
230 /* We only peeked the data, so suspending ourselves now should enable
\r
231 the high priority task to also peek the data. The high priority task
\r
232 will have been unblocked when we peeked the data as we left the data
\r
234 vTaskSuspend( NULL );
\r
238 /* This time we are going to do the same as the above test, but the
\r
239 high priority task is going to receive the data, rather than peek it.
\r
240 This means that the medium priority task should never peek the value. */
\r
241 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
243 xErrorDetected = pdTRUE;
\r
246 if( ulValue != 0xaabbaabb )
\r
248 xErrorDetected = pdTRUE;
\r
251 vTaskSuspend( NULL );
\r
254 /*-----------------------------------------------------------*/
\r
256 static void prvHighPriorityPeekTask( void *pvParameters )
\r
258 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
259 unsigned portLONG ulValue;
\r
263 /* Try peeking from the queue. The queue should be empty so we will
\r
264 block, allowing the medium priority task to execute. Both the high
\r
265 and highest priority tasks will then be blocked on the queue. */
\r
266 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
268 /* We expected to have received something by the time we unblock. */
\r
269 xErrorDetected = pdTRUE;
\r
272 /* When we get here the highest priority task should have peeked the data
\r
273 (unblocking this task) then suspended (allowing this task to also peek
\r
275 if( ulValue != 0x01234567 )
\r
277 /* We did not receive the expected value. */
\r
278 xErrorDetected = pdTRUE;
\r
281 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
283 /* The message should have been left on the queue. */
\r
284 xErrorDetected = pdTRUE;
\r
287 /* We only peeked the data, so suspending ourselves now should enable
\r
288 the medium priority task to also peek the data. The medium priority task
\r
289 will have been unblocked when we peeked the data as we left the data
\r
291 vTaskSuspend( NULL );
\r
294 /* This time we are going actually receive the value, so the medium
\r
295 priority task will never peek the data - we removed it from the queue. */
\r
296 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
298 xErrorDetected = pdTRUE;
\r
301 if( ulValue != 0xaabbaabb )
\r
303 xErrorDetected = pdTRUE;
\r
306 vTaskSuspend( NULL );
\r
309 /*-----------------------------------------------------------*/
\r
311 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
313 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
314 unsigned portLONG ulValue;
\r
318 /* Try peeking from the queue. The queue should be empty so we will
\r
319 block, allowing the low priority task to execute. The highest, high
\r
320 and medium priority tasks will then all be blocked on the queue. */
\r
321 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
323 /* We expected to have received something by the time we unblock. */
\r
324 xErrorDetected = pdTRUE;
\r
327 /* When we get here the high priority task should have peeked the data
\r
328 (unblocking this task) then suspended (allowing this task to also peek
\r
330 if( ulValue != 0x01234567 )
\r
332 /* We did not receive the expected value. */
\r
333 xErrorDetected = pdTRUE;
\r
336 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
338 /* The message should have been left on the queue. */
\r
339 xErrorDetected = pdTRUE;
\r
342 /* Just so we know the test is still running. */
\r
345 /* Now we can suspend ourselves so the low priority task can execute
\r
347 vTaskSuspend( NULL );
\r
350 /*-----------------------------------------------------------*/
\r
352 static void prvLowPriorityPeekTask( void *pvParameters )
\r
354 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
355 unsigned portLONG ulValue;
\r
359 /* Write some data to the queue. This should unblock the highest
\r
360 priority task that is waiting to peek data from the queue. */
\r
361 ulValue = 0x11223344;
\r
362 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
364 /* We were expecting the queue to be empty so we should not of
\r
365 had a problem writing to the queue. */
\r
366 xErrorDetected = pdTRUE;
\r
369 /* By the time we get here the data should have been removed from
\r
371 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
373 xErrorDetected = pdTRUE;
\r
376 /* Write another value to the queue, again waking the highest priority
\r
377 task that is blocked on the queue. */
\r
378 ulValue = 0x01234567;
\r
379 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
381 /* We were expecting the queue to be empty so we should not of
\r
382 had a problem writing to the queue. */
\r
383 xErrorDetected = pdTRUE;
\r
386 /* All the other tasks should now have successfully peeked the data.
\r
387 The data is still in the queue so we should be able to receive it. */
\r
389 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
391 /* We expected to receive the data. */
\r
392 xErrorDetected = pdTRUE;
\r
395 if( ulValue != 0x01234567 )
\r
397 /* We did not receive the expected value. */
\r
400 /* Lets just delay a while as this is an intensive test as we don't
\r
401 want to starve other tests of processing time. */
\r
402 vTaskDelay( qpeekSHORT_DELAY );
\r
404 /* Unsuspend the other tasks so we can repeat the test - this time
\r
405 however not all the other tasks will peek the data as the high
\r
406 priority task is actually going to remove it from the queue. Send
\r
407 to front is used just to be different. As the queue is empty it
\r
408 makes no difference to the result. */
\r
409 vTaskResume( xMediumPriorityTask );
\r
410 vTaskResume( xHighPriorityTask );
\r
411 vTaskResume( xHighestPriorityTask );
\r
413 ulValue = 0xaabbaabb;
\r
414 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
416 /* We were expecting the queue to be empty so we should not of
\r
417 had a problem writing to the queue. */
\r
418 xErrorDetected = pdTRUE;
\r
421 /* This time we should find that the queue is empty. The high priority
\r
422 task actually removed the data rather than just peeking it. */
\r
423 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
\r
425 /* We expected to receive the data. */
\r
426 xErrorDetected = pdTRUE;
\r
429 /* Unsuspend the highest and high priority tasks so we can go back
\r
430 and repeat the whole thing. The medium priority task should not be
\r
431 suspended as it was not able to peek the data in this last case. */
\r
432 vTaskResume( xHighPriorityTask );
\r
433 vTaskResume( xHighestPriorityTask );
\r
435 /* Lets just delay a while as this is an intensive test as we don't
\r
436 want to starve other tests of processing time. */
\r
437 vTaskDelay( qpeekSHORT_DELAY );
\r
440 /*-----------------------------------------------------------*/
\r
442 /* This is called to check that all the created tasks are still running. */
\r
443 portBASE_TYPE xAreQueuePeekTasksStillRunning( void )
\r
445 static unsigned portLONG ulLastLoopCounter = 0;
\r
447 /* If the demo task is still running then we expect the loopcounter to
\r
448 have incremented since this function was last called. */
\r
449 if( ulLastLoopCounter == ulLoopCounter )
\r
451 xErrorDetected = pdTRUE;
\r
454 ulLastLoopCounter = ulLoopCounter;
\r
456 /* Errors detected in the task itself will have latched xErrorDetected
\r
459 return !xErrorDetected;
\r