2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
98 * Tests the behaviour when data is peeked from a queue when there are
\r
99 * multiple tasks blocked on the queue.
\r
103 #include <stdlib.h>
\r
105 /* Scheduler include files. */
\r
106 #include "FreeRTOS.h"
\r
109 #include "semphr.h"
\r
111 /* Demo program include files. */
\r
114 #define qpeekQUEUE_LENGTH ( 5 )
\r
115 #define qpeekNO_BLOCK ( 0 )
\r
116 #define qpeekSHORT_DELAY ( 10 )
\r
118 #define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
119 #define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
120 #define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
121 #define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
123 /*-----------------------------------------------------------*/
\r
126 * The following three tasks are used to demonstrate the peeking behaviour.
\r
127 * Each task is given a different priority to demonstrate the order in which
\r
128 * tasks are woken as data is peeked from a queue.
\r
130 static void prvLowPriorityPeekTask( void *pvParameters );
\r
131 static void prvMediumPriorityPeekTask( void *pvParameters );
\r
132 static void prvHighPriorityPeekTask( void *pvParameters );
\r
133 static void prvHighestPriorityPeekTask( void *pvParameters );
\r
135 /*-----------------------------------------------------------*/
\r
137 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
138 detected in any of the tasks. */
\r
139 static volatile BaseType_t xErrorDetected = pdFALSE;
\r
141 /* Counter that is incremented on each cycle of a test. This is used to
\r
142 detect a stalled task - a test that is no longer running. */
\r
143 static volatile uint32_t ulLoopCounter = 0;
\r
145 /* Handles to the test tasks. */
\r
146 TaskHandle_t xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
\r
147 /*-----------------------------------------------------------*/
\r
149 void vStartQueuePeekTasks( void )
\r
151 QueueHandle_t xQueue;
\r
153 /* Create the queue that we are going to use for the test/demo. */
\r
154 xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( uint32_t ) );
\r
156 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
157 in use. The queue registry is provided as a means for kernel aware
\r
158 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
159 is not being used. The call to vQueueAddToRegistry() will be removed
\r
160 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
161 defined to be less than 1. */
\r
162 vQueueAddToRegistry( xQueue, "QPeek_Test_Queue" );
\r
164 /* Create the demo tasks and pass it the queue just created. We are
\r
165 passing the queue handle by value so it does not matter that it is declared
\r
166 on the stack here. */
\r
167 xTaskCreate( prvLowPriorityPeekTask, "PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
\r
168 xTaskCreate( prvMediumPriorityPeekTask, "PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
\r
169 xTaskCreate( prvHighPriorityPeekTask, "PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
\r
170 xTaskCreate( prvHighestPriorityPeekTask, "PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
\r
172 /*-----------------------------------------------------------*/
\r
174 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
176 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
181 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
183 const char * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
185 /* Queue a message for printing to say the task has started. */
\r
186 vPrintDisplayMessage( &pcTaskStartMsg );
\r
192 /* Try peeking from the queue. The queue should be empty so we will
\r
193 block, allowing the high priority task to execute. */
\r
194 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
196 /* We expected to have received something by the time we unblock. */
\r
197 xErrorDetected = pdTRUE;
\r
200 /* When we reach here the high and medium priority tasks should still
\r
201 be blocked on the queue. We unblocked because the low priority task
\r
202 wrote a value to the queue, which we should have peeked. Peeking the
\r
203 data (rather than receiving it) will leave the data on the queue, so
\r
204 the high priority task should then have also been unblocked, but not
\r
206 if( ulValue != 0x11223344 )
\r
208 /* We did not receive the expected value. */
\r
209 xErrorDetected = pdTRUE;
\r
212 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
214 /* The message should have been left on the queue. */
\r
215 xErrorDetected = pdTRUE;
\r
218 /* Now we are going to actually receive the data, so when the high
\r
219 priority task runs it will find the queue empty and return to the
\r
222 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
224 /* We expected to receive the value. */
\r
225 xErrorDetected = pdTRUE;
\r
228 if( ulValue != 0x11223344 )
\r
230 /* We did not receive the expected value - which should have been
\r
231 the same value as was peeked. */
\r
232 xErrorDetected = pdTRUE;
\r
235 /* Now we will block again as the queue is once more empty. The low
\r
236 priority task can then execute again. */
\r
237 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
239 /* We expected to have received something by the time we unblock. */
\r
240 xErrorDetected = pdTRUE;
\r
243 /* When we get here the low priority task should have again written to the
\r
245 if( ulValue != 0x01234567 )
\r
247 /* We did not receive the expected value. */
\r
248 xErrorDetected = pdTRUE;
\r
251 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
253 /* The message should have been left on the queue. */
\r
254 xErrorDetected = pdTRUE;
\r
257 /* We only peeked the data, so suspending ourselves now should enable
\r
258 the high priority task to also peek the data. The high priority task
\r
259 will have been unblocked when we peeked the data as we left the data
\r
261 vTaskSuspend( NULL );
\r
265 /* This time we are going to do the same as the above test, but the
\r
266 high priority task is going to receive the data, rather than peek it.
\r
267 This means that the medium priority task should never peek the value. */
\r
268 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
270 xErrorDetected = pdTRUE;
\r
273 if( ulValue != 0xaabbaabb )
\r
275 xErrorDetected = pdTRUE;
\r
278 vTaskSuspend( NULL );
\r
281 /*-----------------------------------------------------------*/
\r
283 static void prvHighPriorityPeekTask( void *pvParameters )
\r
285 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
290 /* Try peeking from the queue. The queue should be empty so we will
\r
291 block, allowing the medium priority task to execute. Both the high
\r
292 and highest priority tasks will then be blocked on the queue. */
\r
293 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
295 /* We expected to have received something by the time we unblock. */
\r
296 xErrorDetected = pdTRUE;
\r
299 /* When we get here the highest priority task should have peeked the data
\r
300 (unblocking this task) then suspended (allowing this task to also peek
\r
302 if( ulValue != 0x01234567 )
\r
304 /* We did not receive the expected value. */
\r
305 xErrorDetected = pdTRUE;
\r
308 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
310 /* The message should have been left on the queue. */
\r
311 xErrorDetected = pdTRUE;
\r
314 /* We only peeked the data, so suspending ourselves now should enable
\r
315 the medium priority task to also peek the data. The medium priority task
\r
316 will have been unblocked when we peeked the data as we left the data
\r
318 vTaskSuspend( NULL );
\r
321 /* This time we are going actually receive the value, so the medium
\r
322 priority task will never peek the data - we removed it from the queue. */
\r
323 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
325 xErrorDetected = pdTRUE;
\r
328 if( ulValue != 0xaabbaabb )
\r
330 xErrorDetected = pdTRUE;
\r
333 vTaskSuspend( NULL );
\r
336 /*-----------------------------------------------------------*/
\r
338 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
340 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
345 /* Try peeking from the queue. The queue should be empty so we will
\r
346 block, allowing the low priority task to execute. The highest, high
\r
347 and medium priority tasks will then all be blocked on the queue. */
\r
348 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
350 /* We expected to have received something by the time we unblock. */
\r
351 xErrorDetected = pdTRUE;
\r
354 /* When we get here the high priority task should have peeked the data
\r
355 (unblocking this task) then suspended (allowing this task to also peek
\r
357 if( ulValue != 0x01234567 )
\r
359 /* We did not receive the expected value. */
\r
360 xErrorDetected = pdTRUE;
\r
363 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
365 /* The message should have been left on the queue. */
\r
366 xErrorDetected = pdTRUE;
\r
369 /* Just so we know the test is still running. */
\r
372 /* Now we can suspend ourselves so the low priority task can execute
\r
374 vTaskSuspend( NULL );
\r
377 /*-----------------------------------------------------------*/
\r
379 static void prvLowPriorityPeekTask( void *pvParameters )
\r
381 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
386 /* Write some data to the queue. This should unblock the highest
\r
387 priority task that is waiting to peek data from the queue. */
\r
388 ulValue = 0x11223344;
\r
389 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
391 /* We were expecting the queue to be empty so we should not of
\r
392 had a problem writing to the queue. */
\r
393 xErrorDetected = pdTRUE;
\r
396 #if configUSE_PREEMPTION == 0
\r
400 /* By the time we get here the data should have been removed from
\r
402 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
404 xErrorDetected = pdTRUE;
\r
407 /* Write another value to the queue, again waking the highest priority
\r
408 task that is blocked on the queue. */
\r
409 ulValue = 0x01234567;
\r
410 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
412 /* We were expecting the queue to be empty so we should not of
\r
413 had a problem writing to the queue. */
\r
414 xErrorDetected = pdTRUE;
\r
417 #if configUSE_PREEMPTION == 0
\r
421 /* All the other tasks should now have successfully peeked the data.
\r
422 The data is still in the queue so we should be able to receive it. */
\r
424 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
426 /* We expected to receive the data. */
\r
427 xErrorDetected = pdTRUE;
\r
430 if( ulValue != 0x01234567 )
\r
432 /* We did not receive the expected value. */
\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
439 /* Unsuspend the other tasks so we can repeat the test - this time
\r
440 however not all the other tasks will peek the data as the high
\r
441 priority task is actually going to remove it from the queue. Send
\r
442 to front is used just to be different. As the queue is empty it
\r
443 makes no difference to the result. */
\r
444 vTaskResume( xMediumPriorityTask );
\r
445 vTaskResume( xHighPriorityTask );
\r
446 vTaskResume( xHighestPriorityTask );
\r
448 #if( configUSE_PREEMPTION == 0 )
\r
452 ulValue = 0xaabbaabb;
\r
453 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
455 /* We were expecting the queue to be empty so we should not of
\r
456 had a problem writing to the queue. */
\r
457 xErrorDetected = pdTRUE;
\r
460 #if configUSE_PREEMPTION == 0
\r
464 /* This time we should find that the queue is empty. The high priority
\r
465 task actually removed the data rather than just peeking it. */
\r
466 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
\r
468 /* We expected to receive the data. */
\r
469 xErrorDetected = pdTRUE;
\r
472 /* Unsuspend the highest and high priority tasks so we can go back
\r
473 and repeat the whole thing. The medium priority task should not be
\r
474 suspended as it was not able to peek the data in this last case. */
\r
475 vTaskResume( xHighPriorityTask );
\r
476 vTaskResume( xHighestPriorityTask );
\r
478 /* Lets just delay a while as this is an intensive test as we don't
\r
479 want to starve other tests of processing time. */
\r
480 vTaskDelay( qpeekSHORT_DELAY );
\r
483 /*-----------------------------------------------------------*/
\r
485 /* This is called to check that all the created tasks are still running. */
\r
486 BaseType_t xAreQueuePeekTasksStillRunning( void )
\r
488 static uint32_t ulLastLoopCounter = 0;
\r
490 /* If the demo task is still running then we expect the loopcounter to
\r
491 have incremented since this function was last called. */
\r
492 if( ulLastLoopCounter == ulLoopCounter )
\r
494 xErrorDetected = pdTRUE;
\r
497 ulLastLoopCounter = ulLoopCounter;
\r
499 /* Errors detected in the task itself will have latched xErrorDetected
\r
502 return ( BaseType_t ) !xErrorDetected;
\r