2 FreeRTOS V7.0.1 - Copyright (C) 2011 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 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
56 * Tests the behaviour when data is peeked from a queue when there are
\r
57 * multiple tasks blocked on the queue.
\r
63 /* Scheduler include files. */
\r
64 #include "FreeRTOS.h"
\r
69 /* Demo program include files. */
\r
72 #define qpeekQUEUE_LENGTH ( 5 )
\r
73 #define qpeekNO_BLOCK ( 0 )
\r
74 #define qpeekSHORT_DELAY ( 10 )
\r
76 #define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
77 #define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
78 #define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
79 #define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
81 /*-----------------------------------------------------------*/
\r
84 * The following three tasks are used to demonstrate the peeking behaviour.
\r
85 * Each task is given a different priority to demonstrate the order in which
\r
86 * tasks are woken as data is peeked from a queue.
\r
88 static void prvLowPriorityPeekTask( void *pvParameters );
\r
89 static void prvMediumPriorityPeekTask( void *pvParameters );
\r
90 static void prvHighPriorityPeekTask( void *pvParameters );
\r
91 static void prvHighestPriorityPeekTask( void *pvParameters );
\r
93 /*-----------------------------------------------------------*/
\r
95 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
96 detected in any of the tasks. */
\r
97 static volatile portBASE_TYPE xErrorDetected = pdFALSE;
\r
99 /* Counter that is incremented on each cycle of a test. This is used to
\r
100 detect a stalled task - a test that is no longer running. */
\r
101 static volatile unsigned portLONG ulLoopCounter = 0;
\r
103 /* Handles to the test tasks. */
\r
104 xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
\r
105 /*-----------------------------------------------------------*/
\r
107 void vStartQueuePeekTasks( void )
\r
109 xQueueHandle xQueue;
\r
111 /* Create the queue that we are going to use for the test/demo. */
\r
112 xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
114 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
115 in use. The queue registry is provided as a means for kernel aware
\r
116 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
117 is not being used. The call to vQueueAddToRegistry() will be removed
\r
118 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
119 defined to be less than 1. */
\r
120 vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "QPeek_Test_Queue" );
\r
122 /* Create the demo tasks and pass it the queue just created. We are
\r
123 passing the queue handle by value so it does not matter that it is declared
\r
124 on the stack here. */
\r
125 xTaskCreate( prvLowPriorityPeekTask, ( signed portCHAR * )"PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
\r
126 xTaskCreate( prvMediumPriorityPeekTask, ( signed portCHAR * )"PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
\r
127 xTaskCreate( prvHighPriorityPeekTask, ( signed portCHAR * )"PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
\r
128 xTaskCreate( prvHighestPriorityPeekTask, ( signed portCHAR * )"PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
\r
130 /*-----------------------------------------------------------*/
\r
132 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
134 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
135 unsigned portLONG ulValue;
\r
139 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
141 const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
143 /* Queue a message for printing to say the task has started. */
\r
144 vPrintDisplayMessage( &pcTaskStartMsg );
\r
150 /* Try peeking from the queue. The queue should be empty so we will
\r
151 block, allowing the high priority task to execute. */
\r
152 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
154 /* We expected to have received something by the time we unblock. */
\r
155 xErrorDetected = pdTRUE;
\r
158 /* When we reach here the high and medium priority tasks should still
\r
159 be blocked on the queue. We unblocked because the low priority task
\r
160 wrote a value to the queue, which we should have peeked. Peeking the
\r
161 data (rather than receiving it) will leave the data on the queue, so
\r
162 the high priority task should then have also been unblocked, but not
\r
164 if( ulValue != 0x11223344 )
\r
166 /* We did not receive the expected value. */
\r
167 xErrorDetected = pdTRUE;
\r
170 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
172 /* The message should have been left on the queue. */
\r
173 xErrorDetected = pdTRUE;
\r
176 /* Now we are going to actually receive the data, so when the high
\r
177 priority task runs it will find the queue empty and return to the
\r
180 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
182 /* We expected to receive the value. */
\r
183 xErrorDetected = pdTRUE;
\r
186 if( ulValue != 0x11223344 )
\r
188 /* We did not receive the expected value - which should have been
\r
189 the same value as was peeked. */
\r
190 xErrorDetected = pdTRUE;
\r
193 /* Now we will block again as the queue is once more empty. The low
\r
194 priority task can then execute again. */
\r
195 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
197 /* We expected to have received something by the time we unblock. */
\r
198 xErrorDetected = pdTRUE;
\r
201 /* When we get here the low priority task should have again written to the
\r
203 if( ulValue != 0x01234567 )
\r
205 /* We did not receive the expected value. */
\r
206 xErrorDetected = pdTRUE;
\r
209 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
211 /* The message should have been left on the queue. */
\r
212 xErrorDetected = pdTRUE;
\r
215 /* We only peeked the data, so suspending ourselves now should enable
\r
216 the high priority task to also peek the data. The high priority task
\r
217 will have been unblocked when we peeked the data as we left the data
\r
219 vTaskSuspend( NULL );
\r
223 /* This time we are going to do the same as the above test, but the
\r
224 high priority task is going to receive the data, rather than peek it.
\r
225 This means that the medium priority task should never peek the value. */
\r
226 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
228 xErrorDetected = pdTRUE;
\r
231 if( ulValue != 0xaabbaabb )
\r
233 xErrorDetected = pdTRUE;
\r
236 vTaskSuspend( NULL );
\r
239 /*-----------------------------------------------------------*/
\r
241 static void prvHighPriorityPeekTask( void *pvParameters )
\r
243 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
244 unsigned portLONG ulValue;
\r
248 /* Try peeking from the queue. The queue should be empty so we will
\r
249 block, allowing the medium priority task to execute. Both the high
\r
250 and highest priority tasks will then be blocked on the queue. */
\r
251 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
253 /* We expected to have received something by the time we unblock. */
\r
254 xErrorDetected = pdTRUE;
\r
257 /* When we get here the highest priority task should have peeked the data
\r
258 (unblocking this task) then suspended (allowing this task to also peek
\r
260 if( ulValue != 0x01234567 )
\r
262 /* We did not receive the expected value. */
\r
263 xErrorDetected = pdTRUE;
\r
266 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
268 /* The message should have been left on the queue. */
\r
269 xErrorDetected = pdTRUE;
\r
272 /* We only peeked the data, so suspending ourselves now should enable
\r
273 the medium priority task to also peek the data. The medium priority task
\r
274 will have been unblocked when we peeked the data as we left the data
\r
276 vTaskSuspend( NULL );
\r
279 /* This time we are going actually receive the value, so the medium
\r
280 priority task will never peek the data - we removed it from the queue. */
\r
281 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
283 xErrorDetected = pdTRUE;
\r
286 if( ulValue != 0xaabbaabb )
\r
288 xErrorDetected = pdTRUE;
\r
291 vTaskSuspend( NULL );
\r
294 /*-----------------------------------------------------------*/
\r
296 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
298 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
299 unsigned portLONG ulValue;
\r
303 /* Try peeking from the queue. The queue should be empty so we will
\r
304 block, allowing the low priority task to execute. The highest, high
\r
305 and medium priority tasks will then all be blocked on the queue. */
\r
306 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
308 /* We expected to have received something by the time we unblock. */
\r
309 xErrorDetected = pdTRUE;
\r
312 /* When we get here the high priority task should have peeked the data
\r
313 (unblocking this task) then suspended (allowing this task to also peek
\r
315 if( ulValue != 0x01234567 )
\r
317 /* We did not receive the expected value. */
\r
318 xErrorDetected = pdTRUE;
\r
321 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
323 /* The message should have been left on the queue. */
\r
324 xErrorDetected = pdTRUE;
\r
327 /* Just so we know the test is still running. */
\r
330 /* Now we can suspend ourselves so the low priority task can execute
\r
332 vTaskSuspend( NULL );
\r
335 /*-----------------------------------------------------------*/
\r
337 static void prvLowPriorityPeekTask( void *pvParameters )
\r
339 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
340 unsigned portLONG ulValue;
\r
344 /* Write some data to the queue. This should unblock the highest
\r
345 priority task that is waiting to peek data from the queue. */
\r
346 ulValue = 0x11223344;
\r
347 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
349 /* We were expecting the queue to be empty so we should not of
\r
350 had a problem writing to the queue. */
\r
351 xErrorDetected = pdTRUE;
\r
354 /* By the time we get here the data should have been removed from
\r
356 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
358 xErrorDetected = pdTRUE;
\r
361 /* Write another value to the queue, again waking the highest priority
\r
362 task that is blocked on the queue. */
\r
363 ulValue = 0x01234567;
\r
364 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
366 /* We were expecting the queue to be empty so we should not of
\r
367 had a problem writing to the queue. */
\r
368 xErrorDetected = pdTRUE;
\r
371 /* All the other tasks should now have successfully peeked the data.
\r
372 The data is still in the queue so we should be able to receive it. */
\r
374 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
376 /* We expected to receive the data. */
\r
377 xErrorDetected = pdTRUE;
\r
380 if( ulValue != 0x01234567 )
\r
382 /* We did not receive the expected value. */
\r
385 /* Lets just delay a while as this is an intensive test as we don't
\r
386 want to starve other tests of processing time. */
\r
387 vTaskDelay( qpeekSHORT_DELAY );
\r
389 /* Unsuspend the other tasks so we can repeat the test - this time
\r
390 however not all the other tasks will peek the data as the high
\r
391 priority task is actually going to remove it from the queue. Send
\r
392 to front is used just to be different. As the queue is empty it
\r
393 makes no difference to the result. */
\r
394 vTaskResume( xMediumPriorityTask );
\r
395 vTaskResume( xHighPriorityTask );
\r
396 vTaskResume( xHighestPriorityTask );
\r
398 ulValue = 0xaabbaabb;
\r
399 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
401 /* We were expecting the queue to be empty so we should not of
\r
402 had a problem writing to the queue. */
\r
403 xErrorDetected = pdTRUE;
\r
406 /* This time we should find that the queue is empty. The high priority
\r
407 task actually removed the data rather than just peeking it. */
\r
408 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
\r
410 /* We expected to receive the data. */
\r
411 xErrorDetected = pdTRUE;
\r
414 /* Unsuspend the highest and high priority tasks so we can go back
\r
415 and repeat the whole thing. The medium priority task should not be
\r
416 suspended as it was not able to peek the data in this last case. */
\r
417 vTaskResume( xHighPriorityTask );
\r
418 vTaskResume( xHighestPriorityTask );
\r
420 /* Lets just delay a while as this is an intensive test as we don't
\r
421 want to starve other tests of processing time. */
\r
422 vTaskDelay( qpeekSHORT_DELAY );
\r
425 /*-----------------------------------------------------------*/
\r
427 /* This is called to check that all the created tasks are still running. */
\r
428 portBASE_TYPE xAreQueuePeekTasksStillRunning( void )
\r
430 static unsigned portLONG ulLastLoopCounter = 0;
\r
432 /* If the demo task is still running then we expect the loopcounter to
\r
433 have incremented since this function was last called. */
\r
434 if( ulLastLoopCounter == ulLoopCounter )
\r
436 xErrorDetected = pdTRUE;
\r
439 ulLastLoopCounter = ulLoopCounter;
\r
441 /* Errors detected in the task itself will have latched xErrorDetected
\r
444 return !xErrorDetected;
\r