2 FreeRTOS V8.2.1 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
72 * Tests the behaviour when data is peeked from a queue when there are
\r
73 * multiple tasks blocked on the queue.
\r
79 /* Scheduler include files. */
\r
80 #include "FreeRTOS.h"
\r
85 /* Demo program include files. */
\r
88 #define qpeekQUEUE_LENGTH ( 5 )
\r
89 #define qpeekNO_BLOCK ( 0 )
\r
90 #define qpeekSHORT_DELAY ( 10 )
\r
92 #define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
93 #define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
94 #define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
95 #define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
97 /*-----------------------------------------------------------*/
\r
100 * The following three tasks are used to demonstrate the peeking behaviour.
\r
101 * Each task is given a different priority to demonstrate the order in which
\r
102 * tasks are woken as data is peeked from a queue.
\r
104 static void prvLowPriorityPeekTask( void *pvParameters );
\r
105 static void prvMediumPriorityPeekTask( void *pvParameters );
\r
106 static void prvHighPriorityPeekTask( void *pvParameters );
\r
107 static void prvHighestPriorityPeekTask( void *pvParameters );
\r
109 /*-----------------------------------------------------------*/
\r
111 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
112 detected in any of the tasks. */
\r
113 static volatile BaseType_t xErrorDetected = pdFALSE;
\r
115 /* Counter that is incremented on each cycle of a test. This is used to
\r
116 detect a stalled task - a test that is no longer running. */
\r
117 static volatile uint32_t ulLoopCounter = 0;
\r
119 /* Handles to the test tasks. */
\r
120 TaskHandle_t xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
\r
121 /*-----------------------------------------------------------*/
\r
123 void vStartQueuePeekTasks( void )
\r
125 QueueHandle_t xQueue;
\r
127 /* Create the queue that we are going to use for the test/demo. */
\r
128 xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( uint32_t ) );
\r
130 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
131 in use. The queue registry is provided as a means for kernel aware
\r
132 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
133 is not being used. The call to vQueueAddToRegistry() will be removed
\r
134 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
135 defined to be less than 1. */
\r
136 vQueueAddToRegistry( xQueue, "QPeek_Test_Queue" );
\r
138 /* Create the demo tasks and pass it the queue just created. We are
\r
139 passing the queue handle by value so it does not matter that it is declared
\r
140 on the stack here. */
\r
141 xTaskCreate( prvLowPriorityPeekTask, "PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
\r
142 xTaskCreate( prvMediumPriorityPeekTask, "PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
\r
143 xTaskCreate( prvHighPriorityPeekTask, "PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
\r
144 xTaskCreate( prvHighestPriorityPeekTask, "PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
\r
146 /*-----------------------------------------------------------*/
\r
148 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
150 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
155 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
157 const char * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
159 /* Queue a message for printing to say the task has started. */
\r
160 vPrintDisplayMessage( &pcTaskStartMsg );
\r
166 /* Try peeking from the queue. The queue should be empty so we will
\r
167 block, allowing the high priority task to execute. */
\r
168 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
170 /* We expected to have received something by the time we unblock. */
\r
171 xErrorDetected = pdTRUE;
\r
174 /* When we reach here the high and medium priority tasks should still
\r
175 be blocked on the queue. We unblocked because the low priority task
\r
176 wrote a value to the queue, which we should have peeked. Peeking the
\r
177 data (rather than receiving it) will leave the data on the queue, so
\r
178 the high priority task should then have also been unblocked, but not
\r
180 if( ulValue != 0x11223344 )
\r
182 /* We did not receive the expected value. */
\r
183 xErrorDetected = pdTRUE;
\r
186 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
188 /* The message should have been left on the queue. */
\r
189 xErrorDetected = pdTRUE;
\r
192 /* Now we are going to actually receive the data, so when the high
\r
193 priority task runs it will find the queue empty and return to the
\r
196 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
198 /* We expected to receive the value. */
\r
199 xErrorDetected = pdTRUE;
\r
202 if( ulValue != 0x11223344 )
\r
204 /* We did not receive the expected value - which should have been
\r
205 the same value as was peeked. */
\r
206 xErrorDetected = pdTRUE;
\r
209 /* Now we will block again as the queue is once more empty. The low
\r
210 priority task can then execute again. */
\r
211 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
213 /* We expected to have received something by the time we unblock. */
\r
214 xErrorDetected = pdTRUE;
\r
217 /* When we get here the low priority task should have again written to the
\r
219 if( ulValue != 0x01234567 )
\r
221 /* We did not receive the expected value. */
\r
222 xErrorDetected = pdTRUE;
\r
225 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
227 /* The message should have been left on the queue. */
\r
228 xErrorDetected = pdTRUE;
\r
231 /* We only peeked the data, so suspending ourselves now should enable
\r
232 the high priority task to also peek the data. The high priority task
\r
233 will have been unblocked when we peeked the data as we left the data
\r
235 vTaskSuspend( NULL );
\r
239 /* This time we are going to do the same as the above test, but the
\r
240 high priority task is going to receive the data, rather than peek it.
\r
241 This means that the medium priority task should never peek the value. */
\r
242 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
244 xErrorDetected = pdTRUE;
\r
247 if( ulValue != 0xaabbaabb )
\r
249 xErrorDetected = pdTRUE;
\r
252 vTaskSuspend( NULL );
\r
255 /*-----------------------------------------------------------*/
\r
257 static void prvHighPriorityPeekTask( void *pvParameters )
\r
259 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
264 /* Try peeking from the queue. The queue should be empty so we will
\r
265 block, allowing the medium priority task to execute. Both the high
\r
266 and highest priority tasks will then be blocked on the queue. */
\r
267 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
269 /* We expected to have received something by the time we unblock. */
\r
270 xErrorDetected = pdTRUE;
\r
273 /* When we get here the highest priority task should have peeked the data
\r
274 (unblocking this task) then suspended (allowing this task to also peek
\r
276 if( ulValue != 0x01234567 )
\r
278 /* We did not receive the expected value. */
\r
279 xErrorDetected = pdTRUE;
\r
282 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
284 /* The message should have been left on the queue. */
\r
285 xErrorDetected = pdTRUE;
\r
288 /* We only peeked the data, so suspending ourselves now should enable
\r
289 the medium priority task to also peek the data. The medium priority task
\r
290 will have been unblocked when we peeked the data as we left the data
\r
292 vTaskSuspend( NULL );
\r
295 /* This time we are going actually receive the value, so the medium
\r
296 priority task will never peek the data - we removed it from the queue. */
\r
297 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
299 xErrorDetected = pdTRUE;
\r
302 if( ulValue != 0xaabbaabb )
\r
304 xErrorDetected = pdTRUE;
\r
307 vTaskSuspend( NULL );
\r
310 /*-----------------------------------------------------------*/
\r
312 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
314 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
319 /* Try peeking from the queue. The queue should be empty so we will
\r
320 block, allowing the low priority task to execute. The highest, high
\r
321 and medium priority tasks will then all be blocked on the queue. */
\r
322 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
324 /* We expected to have received something by the time we unblock. */
\r
325 xErrorDetected = pdTRUE;
\r
328 /* When we get here the high priority task should have peeked the data
\r
329 (unblocking this task) then suspended (allowing this task to also peek
\r
331 if( ulValue != 0x01234567 )
\r
333 /* We did not receive the expected value. */
\r
334 xErrorDetected = pdTRUE;
\r
337 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
339 /* The message should have been left on the queue. */
\r
340 xErrorDetected = pdTRUE;
\r
343 /* Just so we know the test is still running. */
\r
346 /* Now we can suspend ourselves so the low priority task can execute
\r
348 vTaskSuspend( NULL );
\r
351 /*-----------------------------------------------------------*/
\r
353 static void prvLowPriorityPeekTask( void *pvParameters )
\r
355 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
360 /* Write some data to the queue. This should unblock the highest
\r
361 priority task that is waiting to peek data from the queue. */
\r
362 ulValue = 0x11223344;
\r
363 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
365 /* We were expecting the queue to be empty so we should not of
\r
366 had a problem writing to the queue. */
\r
367 xErrorDetected = pdTRUE;
\r
370 #if configUSE_PREEMPTION == 0
\r
374 /* By the time we get here the data should have been removed from
\r
376 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
378 xErrorDetected = pdTRUE;
\r
381 /* Write another value to the queue, again waking the highest priority
\r
382 task that is blocked on the queue. */
\r
383 ulValue = 0x01234567;
\r
384 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
386 /* We were expecting the queue to be empty so we should not of
\r
387 had a problem writing to the queue. */
\r
388 xErrorDetected = pdTRUE;
\r
391 #if configUSE_PREEMPTION == 0
\r
395 /* All the other tasks should now have successfully peeked the data.
\r
396 The data is still in the queue so we should be able to receive it. */
\r
398 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
400 /* We expected to receive the data. */
\r
401 xErrorDetected = pdTRUE;
\r
404 if( ulValue != 0x01234567 )
\r
406 /* We did not receive the expected value. */
\r
409 /* Lets just delay a while as this is an intensive test as we don't
\r
410 want to starve other tests of processing time. */
\r
411 vTaskDelay( qpeekSHORT_DELAY );
\r
413 /* Unsuspend the other tasks so we can repeat the test - this time
\r
414 however not all the other tasks will peek the data as the high
\r
415 priority task is actually going to remove it from the queue. Send
\r
416 to front is used just to be different. As the queue is empty it
\r
417 makes no difference to the result. */
\r
418 vTaskResume( xMediumPriorityTask );
\r
419 vTaskResume( xHighPriorityTask );
\r
420 vTaskResume( xHighestPriorityTask );
\r
422 #if( configUSE_PREEMPTION == 0 )
\r
426 ulValue = 0xaabbaabb;
\r
427 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
429 /* We were expecting the queue to be empty so we should not of
\r
430 had a problem writing to the queue. */
\r
431 xErrorDetected = pdTRUE;
\r
434 #if configUSE_PREEMPTION == 0
\r
438 /* This time we should find that the queue is empty. The high priority
\r
439 task actually removed the data rather than just peeking it. */
\r
440 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
\r
442 /* We expected to receive the data. */
\r
443 xErrorDetected = pdTRUE;
\r
446 /* Unsuspend the highest and high priority tasks so we can go back
\r
447 and repeat the whole thing. The medium priority task should not be
\r
448 suspended as it was not able to peek the data in this last case. */
\r
449 vTaskResume( xHighPriorityTask );
\r
450 vTaskResume( xHighestPriorityTask );
\r
452 /* Lets just delay a while as this is an intensive test as we don't
\r
453 want to starve other tests of processing time. */
\r
454 vTaskDelay( qpeekSHORT_DELAY );
\r
457 /*-----------------------------------------------------------*/
\r
459 /* This is called to check that all the created tasks are still running. */
\r
460 BaseType_t xAreQueuePeekTasksStillRunning( void )
\r
462 static uint32_t ulLastLoopCounter = 0;
\r
464 /* If the demo task is still running then we expect the loopcounter to
\r
465 have incremented since this function was last called. */
\r
466 if( ulLastLoopCounter == ulLoopCounter )
\r
468 xErrorDetected = pdTRUE;
\r
471 ulLastLoopCounter = ulLoopCounter;
\r
473 /* Errors detected in the task itself will have latched xErrorDetected
\r
476 return ( BaseType_t ) !xErrorDetected;
\r