2 FreeRTOS V9.0.0rc1 - Copyright (C) 2016 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 if( xQueue != NULL )
\r
132 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
133 in use. The queue registry is provided as a means for kernel aware
\r
134 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
135 is not being used. The call to vQueueAddToRegistry() will be removed
\r
136 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
137 defined to be less than 1. */
\r
138 vQueueAddToRegistry( xQueue, "QPeek_Test_Queue" );
\r
140 /* Create the demo tasks and pass it the queue just created. We are
\r
141 passing the queue handle by value so it does not matter that it is declared
\r
142 on the stack here. */
\r
143 xTaskCreate( prvLowPriorityPeekTask, "PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
\r
144 xTaskCreate( prvMediumPriorityPeekTask, "PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
\r
145 xTaskCreate( prvHighPriorityPeekTask, "PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
\r
146 xTaskCreate( prvHighestPriorityPeekTask, "PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
\r
149 /*-----------------------------------------------------------*/
\r
151 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
153 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
158 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
160 const char * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
162 /* Queue a message for printing to say the task has started. */
\r
163 vPrintDisplayMessage( &pcTaskStartMsg );
\r
169 /* Try peeking from the queue. The queue should be empty so we will
\r
170 block, allowing the high priority task to execute. */
\r
171 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
173 /* We expected to have received something by the time we unblock. */
\r
174 xErrorDetected = pdTRUE;
\r
177 /* When we reach here the high and medium priority tasks should still
\r
178 be blocked on the queue. We unblocked because the low priority task
\r
179 wrote a value to the queue, which we should have peeked. Peeking the
\r
180 data (rather than receiving it) will leave the data on the queue, so
\r
181 the high priority task should then have also been unblocked, but not
\r
183 if( ulValue != 0x11223344 )
\r
185 /* We did not receive the expected value. */
\r
186 xErrorDetected = pdTRUE;
\r
189 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
191 /* The message should have been left on the queue. */
\r
192 xErrorDetected = pdTRUE;
\r
195 /* Now we are going to actually receive the data, so when the high
\r
196 priority task runs it will find the queue empty and return to the
\r
199 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
201 /* We expected to receive the value. */
\r
202 xErrorDetected = pdTRUE;
\r
205 if( ulValue != 0x11223344 )
\r
207 /* We did not receive the expected value - which should have been
\r
208 the same value as was peeked. */
\r
209 xErrorDetected = pdTRUE;
\r
212 /* Now we will block again as the queue is once more empty. The low
\r
213 priority task can then execute again. */
\r
214 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
216 /* We expected to have received something by the time we unblock. */
\r
217 xErrorDetected = pdTRUE;
\r
220 /* When we get here the low priority task should have again written to the
\r
222 if( ulValue != 0x01234567 )
\r
224 /* We did not receive the expected value. */
\r
225 xErrorDetected = pdTRUE;
\r
228 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
230 /* The message should have been left on the queue. */
\r
231 xErrorDetected = pdTRUE;
\r
234 /* We only peeked the data, so suspending ourselves now should enable
\r
235 the high priority task to also peek the data. The high priority task
\r
236 will have been unblocked when we peeked the data as we left the data
\r
238 vTaskSuspend( NULL );
\r
242 /* This time we are going to do the same as the above test, but the
\r
243 high priority task is going to receive the data, rather than peek it.
\r
244 This means that the medium priority task should never peek the value. */
\r
245 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
247 xErrorDetected = pdTRUE;
\r
250 if( ulValue != 0xaabbaabb )
\r
252 xErrorDetected = pdTRUE;
\r
255 vTaskSuspend( NULL );
\r
258 /*-----------------------------------------------------------*/
\r
260 static void prvHighPriorityPeekTask( void *pvParameters )
\r
262 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
267 /* Try peeking from the queue. The queue should be empty so we will
\r
268 block, allowing the medium priority task to execute. Both the high
\r
269 and highest priority tasks will then be blocked on the queue. */
\r
270 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
272 /* We expected to have received something by the time we unblock. */
\r
273 xErrorDetected = pdTRUE;
\r
276 /* When we get here the highest priority task should have peeked the data
\r
277 (unblocking this task) then suspended (allowing this task to also peek
\r
279 if( ulValue != 0x01234567 )
\r
281 /* We did not receive the expected value. */
\r
282 xErrorDetected = pdTRUE;
\r
285 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
287 /* The message should have been left on the queue. */
\r
288 xErrorDetected = pdTRUE;
\r
291 /* We only peeked the data, so suspending ourselves now should enable
\r
292 the medium priority task to also peek the data. The medium priority task
\r
293 will have been unblocked when we peeked the data as we left the data
\r
295 vTaskSuspend( NULL );
\r
298 /* This time we are going actually receive the value, so the medium
\r
299 priority task will never peek the data - we removed it from the queue. */
\r
300 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
302 xErrorDetected = pdTRUE;
\r
305 if( ulValue != 0xaabbaabb )
\r
307 xErrorDetected = pdTRUE;
\r
310 vTaskSuspend( NULL );
\r
313 /*-----------------------------------------------------------*/
\r
315 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
317 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
322 /* Try peeking from the queue. The queue should be empty so we will
\r
323 block, allowing the low priority task to execute. The highest, high
\r
324 and medium priority tasks will then all be blocked on the queue. */
\r
325 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
327 /* We expected to have received something by the time we unblock. */
\r
328 xErrorDetected = pdTRUE;
\r
331 /* When we get here the high priority task should have peeked the data
\r
332 (unblocking this task) then suspended (allowing this task to also peek
\r
334 if( ulValue != 0x01234567 )
\r
336 /* We did not receive the expected value. */
\r
337 xErrorDetected = pdTRUE;
\r
340 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
342 /* The message should have been left on the queue. */
\r
343 xErrorDetected = pdTRUE;
\r
346 /* Just so we know the test is still running. */
\r
349 /* Now we can suspend ourselves so the low priority task can execute
\r
351 vTaskSuspend( NULL );
\r
354 /*-----------------------------------------------------------*/
\r
356 static void prvLowPriorityPeekTask( void *pvParameters )
\r
358 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
363 /* Write some data to the queue. This should unblock the highest
\r
364 priority task that is waiting to peek data from the queue. */
\r
365 ulValue = 0x11223344;
\r
366 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
368 /* We were expecting the queue to be empty so we should not of
\r
369 had a problem writing to the queue. */
\r
370 xErrorDetected = pdTRUE;
\r
373 #if configUSE_PREEMPTION == 0
\r
377 /* By the time we get here the data should have been removed from
\r
379 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
381 xErrorDetected = pdTRUE;
\r
384 /* Write another value to the queue, again waking the highest priority
\r
385 task that is blocked on the queue. */
\r
386 ulValue = 0x01234567;
\r
387 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
389 /* We were expecting the queue to be empty so we should not of
\r
390 had a problem writing to the queue. */
\r
391 xErrorDetected = pdTRUE;
\r
394 #if configUSE_PREEMPTION == 0
\r
398 /* All the other tasks should now have successfully peeked the data.
\r
399 The data is still in the queue so we should be able to receive it. */
\r
401 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
403 /* We expected to receive the data. */
\r
404 xErrorDetected = pdTRUE;
\r
407 if( ulValue != 0x01234567 )
\r
409 /* We did not receive the expected value. */
\r
412 /* Lets just delay a while as this is an intensive test as we don't
\r
413 want to starve other tests of processing time. */
\r
414 vTaskDelay( qpeekSHORT_DELAY );
\r
416 /* Unsuspend the other tasks so we can repeat the test - this time
\r
417 however not all the other tasks will peek the data as the high
\r
418 priority task is actually going to remove it from the queue. Send
\r
419 to front is used just to be different. As the queue is empty it
\r
420 makes no difference to the result. */
\r
421 vTaskResume( xMediumPriorityTask );
\r
422 vTaskResume( xHighPriorityTask );
\r
423 vTaskResume( xHighestPriorityTask );
\r
425 #if( configUSE_PREEMPTION == 0 )
\r
429 ulValue = 0xaabbaabb;
\r
430 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
432 /* We were expecting the queue to be empty so we should not of
\r
433 had a problem writing to the queue. */
\r
434 xErrorDetected = pdTRUE;
\r
437 #if configUSE_PREEMPTION == 0
\r
441 /* This time we should find that the queue is empty. The high priority
\r
442 task actually removed the data rather than just peeking it. */
\r
443 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
\r
445 /* We expected to receive the data. */
\r
446 xErrorDetected = pdTRUE;
\r
449 /* Unsuspend the highest and high priority tasks so we can go back
\r
450 and repeat the whole thing. The medium priority task should not be
\r
451 suspended as it was not able to peek the data in this last case. */
\r
452 vTaskResume( xHighPriorityTask );
\r
453 vTaskResume( xHighestPriorityTask );
\r
455 /* Lets just delay a while as this is an intensive test as we don't
\r
456 want to starve other tests of processing time. */
\r
457 vTaskDelay( qpeekSHORT_DELAY );
\r
460 /*-----------------------------------------------------------*/
\r
462 /* This is called to check that all the created tasks are still running. */
\r
463 BaseType_t xAreQueuePeekTasksStillRunning( void )
\r
465 static uint32_t ulLastLoopCounter = 0;
\r
467 /* If the demo task is still running then we expect the loopcounter to
\r
468 have incremented since this function was last called. */
\r
469 if( ulLastLoopCounter == ulLoopCounter )
\r
471 xErrorDetected = pdTRUE;
\r
474 ulLastLoopCounter = ulLoopCounter;
\r
476 /* Errors detected in the task itself will have latched xErrorDetected
\r
479 return ( BaseType_t ) !xErrorDetected;
\r