2 FreeRTOS V7.4.1 - Copyright (C) 2013 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
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
77 * Tests the behaviour when data is peeked from a queue when there are
\r
78 * multiple tasks blocked on the queue.
\r
84 /* Scheduler include files. */
\r
85 #include "FreeRTOS.h"
\r
90 /* Demo program include files. */
\r
93 #define qpeekQUEUE_LENGTH ( 5 )
\r
94 #define qpeekNO_BLOCK ( 0 )
\r
95 #define qpeekSHORT_DELAY ( 10 )
\r
97 #define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
98 #define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
99 #define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
100 #define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
102 /*-----------------------------------------------------------*/
\r
105 * The following three tasks are used to demonstrate the peeking behaviour.
\r
106 * Each task is given a different priority to demonstrate the order in which
\r
107 * tasks are woken as data is peeked from a queue.
\r
109 static void prvLowPriorityPeekTask( void *pvParameters );
\r
110 static void prvMediumPriorityPeekTask( void *pvParameters );
\r
111 static void prvHighPriorityPeekTask( void *pvParameters );
\r
112 static void prvHighestPriorityPeekTask( void *pvParameters );
\r
114 /*-----------------------------------------------------------*/
\r
116 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
117 detected in any of the tasks. */
\r
118 static volatile portBASE_TYPE xErrorDetected = pdFALSE;
\r
120 /* Counter that is incremented on each cycle of a test. This is used to
\r
121 detect a stalled task - a test that is no longer running. */
\r
122 static volatile unsigned portLONG ulLoopCounter = 0;
\r
124 /* Handles to the test tasks. */
\r
125 xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
\r
126 /*-----------------------------------------------------------*/
\r
128 void vStartQueuePeekTasks( void )
\r
130 xQueueHandle xQueue;
\r
132 /* Create the queue that we are going to use for the test/demo. */
\r
133 xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
135 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
136 in use. The queue registry is provided as a means for kernel aware
\r
137 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
138 is not being used. The call to vQueueAddToRegistry() will be removed
\r
139 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
140 defined to be less than 1. */
\r
141 vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "QPeek_Test_Queue" );
\r
143 /* Create the demo tasks and pass it the queue just created. We are
\r
144 passing the queue handle by value so it does not matter that it is declared
\r
145 on the stack here. */
\r
146 xTaskCreate( prvLowPriorityPeekTask, ( signed portCHAR * )"PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
\r
147 xTaskCreate( prvMediumPriorityPeekTask, ( signed portCHAR * )"PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
\r
148 xTaskCreate( prvHighPriorityPeekTask, ( signed portCHAR * )"PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
\r
149 xTaskCreate( prvHighestPriorityPeekTask, ( signed portCHAR * )"PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
\r
151 /*-----------------------------------------------------------*/
\r
153 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
155 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
156 unsigned portLONG ulValue;
\r
160 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
162 const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
164 /* Queue a message for printing to say the task has started. */
\r
165 vPrintDisplayMessage( &pcTaskStartMsg );
\r
171 /* Try peeking from the queue. The queue should be empty so we will
\r
172 block, allowing the high priority task to execute. */
\r
173 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
175 /* We expected to have received something by the time we unblock. */
\r
176 xErrorDetected = pdTRUE;
\r
179 /* When we reach here the high and medium priority tasks should still
\r
180 be blocked on the queue. We unblocked because the low priority task
\r
181 wrote a value to the queue, which we should have peeked. Peeking the
\r
182 data (rather than receiving it) will leave the data on the queue, so
\r
183 the high priority task should then have also been unblocked, but not
\r
185 if( ulValue != 0x11223344 )
\r
187 /* We did not receive the expected value. */
\r
188 xErrorDetected = pdTRUE;
\r
191 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
193 /* The message should have been left on the queue. */
\r
194 xErrorDetected = pdTRUE;
\r
197 /* Now we are going to actually receive the data, so when the high
\r
198 priority task runs it will find the queue empty and return to the
\r
201 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
203 /* We expected to receive the value. */
\r
204 xErrorDetected = pdTRUE;
\r
207 if( ulValue != 0x11223344 )
\r
209 /* We did not receive the expected value - which should have been
\r
210 the same value as was peeked. */
\r
211 xErrorDetected = pdTRUE;
\r
214 /* Now we will block again as the queue is once more empty. The low
\r
215 priority task can then execute again. */
\r
216 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
218 /* We expected to have received something by the time we unblock. */
\r
219 xErrorDetected = pdTRUE;
\r
222 /* When we get here the low priority task should have again written to the
\r
224 if( ulValue != 0x01234567 )
\r
226 /* We did not receive the expected value. */
\r
227 xErrorDetected = pdTRUE;
\r
230 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
232 /* The message should have been left on the queue. */
\r
233 xErrorDetected = pdTRUE;
\r
236 /* We only peeked the data, so suspending ourselves now should enable
\r
237 the high priority task to also peek the data. The high priority task
\r
238 will have been unblocked when we peeked the data as we left the data
\r
240 vTaskSuspend( NULL );
\r
244 /* This time we are going to do the same as the above test, but the
\r
245 high priority task is going to receive the data, rather than peek it.
\r
246 This means that the medium priority task should never peek the value. */
\r
247 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
249 xErrorDetected = pdTRUE;
\r
252 if( ulValue != 0xaabbaabb )
\r
254 xErrorDetected = pdTRUE;
\r
257 vTaskSuspend( NULL );
\r
260 /*-----------------------------------------------------------*/
\r
262 static void prvHighPriorityPeekTask( void *pvParameters )
\r
264 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
265 unsigned portLONG ulValue;
\r
269 /* Try peeking from the queue. The queue should be empty so we will
\r
270 block, allowing the medium priority task to execute. Both the high
\r
271 and highest priority tasks will then be blocked on the queue. */
\r
272 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
274 /* We expected to have received something by the time we unblock. */
\r
275 xErrorDetected = pdTRUE;
\r
278 /* When we get here the highest priority task should have peeked the data
\r
279 (unblocking this task) then suspended (allowing this task to also peek
\r
281 if( ulValue != 0x01234567 )
\r
283 /* We did not receive the expected value. */
\r
284 xErrorDetected = pdTRUE;
\r
287 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
289 /* The message should have been left on the queue. */
\r
290 xErrorDetected = pdTRUE;
\r
293 /* We only peeked the data, so suspending ourselves now should enable
\r
294 the medium priority task to also peek the data. The medium priority task
\r
295 will have been unblocked when we peeked the data as we left the data
\r
297 vTaskSuspend( NULL );
\r
300 /* This time we are going actually receive the value, so the medium
\r
301 priority task will never peek the data - we removed it from the queue. */
\r
302 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
304 xErrorDetected = pdTRUE;
\r
307 if( ulValue != 0xaabbaabb )
\r
309 xErrorDetected = pdTRUE;
\r
312 vTaskSuspend( NULL );
\r
315 /*-----------------------------------------------------------*/
\r
317 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
319 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
320 unsigned portLONG ulValue;
\r
324 /* Try peeking from the queue. The queue should be empty so we will
\r
325 block, allowing the low priority task to execute. The highest, high
\r
326 and medium priority tasks will then all be blocked on the queue. */
\r
327 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
329 /* We expected to have received something by the time we unblock. */
\r
330 xErrorDetected = pdTRUE;
\r
333 /* When we get here the high priority task should have peeked the data
\r
334 (unblocking this task) then suspended (allowing this task to also peek
\r
336 if( ulValue != 0x01234567 )
\r
338 /* We did not receive the expected value. */
\r
339 xErrorDetected = pdTRUE;
\r
342 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
344 /* The message should have been left on the queue. */
\r
345 xErrorDetected = pdTRUE;
\r
348 /* Just so we know the test is still running. */
\r
351 /* Now we can suspend ourselves so the low priority task can execute
\r
353 vTaskSuspend( NULL );
\r
356 /*-----------------------------------------------------------*/
\r
358 static void prvLowPriorityPeekTask( void *pvParameters )
\r
360 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
361 unsigned portLONG ulValue;
\r
365 /* Write some data to the queue. This should unblock the highest
\r
366 priority task that is waiting to peek data from the queue. */
\r
367 ulValue = 0x11223344;
\r
368 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
370 /* We were expecting the queue to be empty so we should not of
\r
371 had a problem writing to the queue. */
\r
372 xErrorDetected = pdTRUE;
\r
375 /* By the time we get here the data should have been removed from
\r
377 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
379 xErrorDetected = pdTRUE;
\r
382 /* Write another value to the queue, again waking the highest priority
\r
383 task that is blocked on the queue. */
\r
384 ulValue = 0x01234567;
\r
385 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
387 /* We were expecting the queue to be empty so we should not of
\r
388 had a problem writing to the queue. */
\r
389 xErrorDetected = pdTRUE;
\r
392 /* All the other tasks should now have successfully peeked the data.
\r
393 The data is still in the queue so we should be able to receive it. */
\r
395 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
397 /* We expected to receive the data. */
\r
398 xErrorDetected = pdTRUE;
\r
401 if( ulValue != 0x01234567 )
\r
403 /* We did not receive the expected value. */
\r
406 /* Lets just delay a while as this is an intensive test as we don't
\r
407 want to starve other tests of processing time. */
\r
408 vTaskDelay( qpeekSHORT_DELAY );
\r
410 /* Unsuspend the other tasks so we can repeat the test - this time
\r
411 however not all the other tasks will peek the data as the high
\r
412 priority task is actually going to remove it from the queue. Send
\r
413 to front is used just to be different. As the queue is empty it
\r
414 makes no difference to the result. */
\r
415 vTaskResume( xMediumPriorityTask );
\r
416 vTaskResume( xHighPriorityTask );
\r
417 vTaskResume( xHighestPriorityTask );
\r
419 ulValue = 0xaabbaabb;
\r
420 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
422 /* We were expecting the queue to be empty so we should not of
\r
423 had a problem writing to the queue. */
\r
424 xErrorDetected = pdTRUE;
\r
427 /* This time we should find that the queue is empty. The high priority
\r
428 task actually removed the data rather than just peeking it. */
\r
429 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
\r
431 /* We expected to receive the data. */
\r
432 xErrorDetected = pdTRUE;
\r
435 /* Unsuspend the highest and high priority tasks so we can go back
\r
436 and repeat the whole thing. The medium priority task should not be
\r
437 suspended as it was not able to peek the data in this last case. */
\r
438 vTaskResume( xHighPriorityTask );
\r
439 vTaskResume( xHighestPriorityTask );
\r
441 /* Lets just delay a while as this is an intensive test as we don't
\r
442 want to starve other tests of processing time. */
\r
443 vTaskDelay( qpeekSHORT_DELAY );
\r
446 /*-----------------------------------------------------------*/
\r
448 /* This is called to check that all the created tasks are still running. */
\r
449 portBASE_TYPE xAreQueuePeekTasksStillRunning( void )
\r
451 static unsigned portLONG ulLastLoopCounter = 0;
\r
453 /* If the demo task is still running then we expect the loopcounter to
\r
454 have incremented since this function was last called. */
\r
455 if( ulLastLoopCounter == ulLoopCounter )
\r
457 xErrorDetected = pdTRUE;
\r
460 ulLastLoopCounter = ulLoopCounter;
\r
462 /* Errors detected in the task itself will have latched xErrorDetected
\r
465 return !xErrorDetected;
\r