]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/QPeek.c
5c4751741463b1b3345d49fdfe9270b5b1e47d2d
[freertos] / FreeRTOS / Demo / Common / Minimal / QPeek.c
1 /*\r
2     FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \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
12 \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
19 \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
24 \r
25     ***************************************************************************\r
26      *                                                                       *\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
31      *                                                                       *\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
36      *                                                                       *\r
37     ***************************************************************************\r
38 \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
42 \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
46 \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
51 \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
55 \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
58 \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
62 \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
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /*\r
72  * Tests the behaviour when data is peeked from a queue when there are\r
73  * multiple tasks blocked on the queue.\r
74  */\r
75 \r
76 \r
77 #include <stdlib.h>\r
78 \r
79 /* Scheduler include files. */\r
80 #include "FreeRTOS.h"\r
81 #include "task.h"\r
82 #include "queue.h"\r
83 #include "semphr.h"\r
84 \r
85 /* Demo program include files. */\r
86 #include "QPeek.h"\r
87 \r
88 #define qpeekQUEUE_LENGTH               ( 5 )\r
89 #define qpeekNO_BLOCK                   ( 0 )\r
90 #define qpeekSHORT_DELAY                ( 10 )\r
91 \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
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /*\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
103  */\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
108 \r
109 /*-----------------------------------------------------------*/\r
110 \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
114 \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
118 \r
119 /* Handles to the test tasks. */\r
120 TaskHandle_t xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;\r
121 /*-----------------------------------------------------------*/\r
122 \r
123 void vStartQueuePeekTasks( void )\r
124 {\r
125 QueueHandle_t xQueue;\r
126 \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
129 \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
137 \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
145 }\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 static void prvHighestPriorityPeekTask( void *pvParameters )\r
149 {\r
150 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
151 uint32_t ulValue;\r
152 \r
153         #ifdef USE_STDIO\r
154         {\r
155                 void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
156 \r
157                 const char * const pcTaskStartMsg = "Queue peek test started.\r\n";\r
158 \r
159                 /* Queue a message for printing to say the task has started. */\r
160                 vPrintDisplayMessage( &pcTaskStartMsg );\r
161         }\r
162         #endif\r
163 \r
164         for( ;; )\r
165         {\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
169                 {\r
170                         /* We expected to have received something by the time we unblock. */\r
171                         xErrorDetected = pdTRUE;\r
172                 }\r
173 \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
179                 yet executed. */\r
180                 if( ulValue != 0x11223344 )\r
181                 {\r
182                         /* We did not receive the expected value. */\r
183                         xErrorDetected = pdTRUE;\r
184                 }\r
185 \r
186                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
187                 {\r
188                         /* The message should have been left on the queue. */\r
189                         xErrorDetected = pdTRUE;\r
190                 }\r
191 \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
194                 blocked state. */\r
195                 ulValue = 0;\r
196                 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
197                 {\r
198                         /* We expected to receive the value. */\r
199                         xErrorDetected = pdTRUE;\r
200                 }\r
201 \r
202                 if( ulValue != 0x11223344 )\r
203                 {\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
207                 }\r
208 \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
212                 {\r
213                         /* We expected to have received something by the time we unblock. */\r
214                         xErrorDetected = pdTRUE;\r
215                 }\r
216 \r
217                 /* When we get here the low priority task should have again written to the\r
218                 queue. */\r
219                 if( ulValue != 0x01234567 )\r
220                 {\r
221                         /* We did not receive the expected value. */\r
222                         xErrorDetected = pdTRUE;\r
223                 }\r
224 \r
225                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
226                 {\r
227                         /* The message should have been left on the queue. */\r
228                         xErrorDetected = pdTRUE;\r
229                 }\r
230 \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
234                 in the queue. */\r
235                 vTaskSuspend( NULL );\r
236 \r
237 \r
238 \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
243                 {\r
244                         xErrorDetected = pdTRUE;\r
245                 }\r
246 \r
247                 if( ulValue != 0xaabbaabb )\r
248                 {\r
249                         xErrorDetected = pdTRUE;\r
250                 }\r
251 \r
252                 vTaskSuspend( NULL );\r
253         }\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 static void prvHighPriorityPeekTask( void *pvParameters )\r
258 {\r
259 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
260 uint32_t ulValue;\r
261 \r
262         for( ;; )\r
263         {\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
268                 {\r
269                         /* We expected to have received something by the time we unblock. */\r
270                         xErrorDetected = pdTRUE;\r
271                 }\r
272 \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
275                 the data). */\r
276                 if( ulValue != 0x01234567 )\r
277                 {\r
278                         /* We did not receive the expected value. */\r
279                         xErrorDetected = pdTRUE;\r
280                 }\r
281 \r
282                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
283                 {\r
284                         /* The message should have been left on the queue. */\r
285                         xErrorDetected = pdTRUE;\r
286                 }\r
287 \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
291                 in the queue. */\r
292                 vTaskSuspend( NULL );\r
293 \r
294 \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
298                 {\r
299                         xErrorDetected = pdTRUE;\r
300                 }\r
301 \r
302                 if( ulValue != 0xaabbaabb )\r
303                 {\r
304                         xErrorDetected = pdTRUE;\r
305                 }\r
306 \r
307                 vTaskSuspend( NULL );\r
308         }\r
309 }\r
310 /*-----------------------------------------------------------*/\r
311 \r
312 static void prvMediumPriorityPeekTask( void *pvParameters )\r
313 {\r
314 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
315 uint32_t ulValue;\r
316 \r
317         for( ;; )\r
318         {\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
323                 {\r
324                         /* We expected to have received something by the time we unblock. */\r
325                         xErrorDetected = pdTRUE;\r
326                 }\r
327 \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
330                 the data). */\r
331                 if( ulValue != 0x01234567 )\r
332                 {\r
333                         /* We did not receive the expected value. */\r
334                         xErrorDetected = pdTRUE;\r
335                 }\r
336 \r
337                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
338                 {\r
339                         /* The message should have been left on the queue. */\r
340                         xErrorDetected = pdTRUE;\r
341                 }\r
342 \r
343                 /* Just so we know the test is still running. */\r
344                 ulLoopCounter++;\r
345 \r
346                 /* Now we can suspend ourselves so the low priority task can execute\r
347                 again. */\r
348                 vTaskSuspend( NULL );\r
349         }\r
350 }\r
351 /*-----------------------------------------------------------*/\r
352 \r
353 static void prvLowPriorityPeekTask( void *pvParameters )\r
354 {\r
355 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
356 uint32_t ulValue;\r
357 \r
358         for( ;; )\r
359         {\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
364                 {\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
368                 }\r
369 \r
370                 #if configUSE_PREEMPTION == 0\r
371                         taskYIELD();\r
372                 #endif\r
373 \r
374                 /* By the time we get here the data should have been removed from\r
375                 the queue. */\r
376                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
377                 {\r
378                         xErrorDetected = pdTRUE;\r
379                 }\r
380 \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
385                 {\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
389                 }\r
390 \r
391                 #if configUSE_PREEMPTION == 0\r
392                         taskYIELD();\r
393                 #endif\r
394 \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
397                 ulValue = 0;\r
398                 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
399                 {\r
400                         /* We expected to receive the data. */\r
401                         xErrorDetected = pdTRUE;\r
402                 }\r
403 \r
404                 if( ulValue != 0x01234567 )\r
405                 {\r
406                         /* We did not receive the expected value. */\r
407                 }\r
408 \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
412 \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
421 \r
422                 #if( configUSE_PREEMPTION == 0 )\r
423                         taskYIELD();\r
424                 #endif\r
425 \r
426                 ulValue = 0xaabbaabb;\r
427                 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
428                 {\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
432                 }\r
433 \r
434                 #if configUSE_PREEMPTION == 0\r
435                         taskYIELD();\r
436                 #endif\r
437 \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
441                 {\r
442                         /* We expected to receive the data. */\r
443                         xErrorDetected = pdTRUE;\r
444                 }\r
445 \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
451 \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
455         }\r
456 }\r
457 /*-----------------------------------------------------------*/\r
458 \r
459 /* This is called to check that all the created tasks are still running. */\r
460 BaseType_t xAreQueuePeekTasksStillRunning( void )\r
461 {\r
462 static uint32_t ulLastLoopCounter = 0;\r
463 \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
467         {\r
468                 xErrorDetected = pdTRUE;\r
469         }\r
470 \r
471         ulLastLoopCounter = ulLoopCounter;\r
472 \r
473         /* Errors detected in the task itself will have latched xErrorDetected\r
474         to true. */\r
475 \r
476         return ( BaseType_t ) !xErrorDetected;\r
477 }\r
478 \r