]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/QPeek.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / Common / Minimal / QPeek.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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     >>!   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
17 \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
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\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
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\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
40      *                                                                       *\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
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\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
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\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
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \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
81 \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
84 \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
88 \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
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 \r
97 /*\r
98  * Tests the behaviour when data is peeked from a queue when there are\r
99  * multiple tasks blocked on the queue.\r
100  */\r
101 \r
102 \r
103 #include <stdlib.h>\r
104 \r
105 /* Scheduler include files. */\r
106 #include "FreeRTOS.h"\r
107 #include "task.h"\r
108 #include "queue.h"\r
109 #include "semphr.h"\r
110 \r
111 /* Demo program include files. */\r
112 #include "QPeek.h"\r
113 \r
114 #define qpeekQUEUE_LENGTH               ( 5 )\r
115 #define qpeekNO_BLOCK                   ( 0 )\r
116 #define qpeekSHORT_DELAY                ( 10 )\r
117 \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
122 \r
123 /*-----------------------------------------------------------*/\r
124 \r
125 /*\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
129  */\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
134 \r
135 /*-----------------------------------------------------------*/\r
136 \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
140 \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
144 \r
145 /* Handles to the test tasks. */\r
146 TaskHandle_t xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;\r
147 /*-----------------------------------------------------------*/\r
148 \r
149 void vStartQueuePeekTasks( void )\r
150 {\r
151 QueueHandle_t xQueue;\r
152 \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
155 \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
163 \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
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 static void prvHighestPriorityPeekTask( void *pvParameters )\r
175 {\r
176 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
177 uint32_t ulValue;\r
178 \r
179         #ifdef USE_STDIO\r
180         {\r
181                 void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
182 \r
183                 const char * const pcTaskStartMsg = "Queue peek test started.\r\n";\r
184 \r
185                 /* Queue a message for printing to say the task has started. */\r
186                 vPrintDisplayMessage( &pcTaskStartMsg );\r
187         }\r
188         #endif\r
189 \r
190         for( ;; )\r
191         {\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
195                 {\r
196                         /* We expected to have received something by the time we unblock. */\r
197                         xErrorDetected = pdTRUE;\r
198                 }\r
199 \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
205                 yet executed. */\r
206                 if( ulValue != 0x11223344 )\r
207                 {\r
208                         /* We did not receive the expected value. */\r
209                         xErrorDetected = pdTRUE;\r
210                 }\r
211 \r
212                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
213                 {\r
214                         /* The message should have been left on the queue. */\r
215                         xErrorDetected = pdTRUE;\r
216                 }\r
217 \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
220                 blocked state. */\r
221                 ulValue = 0;\r
222                 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
223                 {\r
224                         /* We expected to receive the value. */\r
225                         xErrorDetected = pdTRUE;\r
226                 }\r
227 \r
228                 if( ulValue != 0x11223344 )\r
229                 {\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
233                 }\r
234 \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
238                 {\r
239                         /* We expected to have received something by the time we unblock. */\r
240                         xErrorDetected = pdTRUE;\r
241                 }\r
242 \r
243                 /* When we get here the low priority task should have again written to the\r
244                 queue. */\r
245                 if( ulValue != 0x01234567 )\r
246                 {\r
247                         /* We did not receive the expected value. */\r
248                         xErrorDetected = pdTRUE;\r
249                 }\r
250 \r
251                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
252                 {\r
253                         /* The message should have been left on the queue. */\r
254                         xErrorDetected = pdTRUE;\r
255                 }\r
256 \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
260                 in the queue. */\r
261                 vTaskSuspend( NULL );\r
262 \r
263 \r
264 \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
269                 {\r
270                         xErrorDetected = pdTRUE;\r
271                 }\r
272 \r
273                 if( ulValue != 0xaabbaabb )\r
274                 {\r
275                         xErrorDetected = pdTRUE;\r
276                 }\r
277 \r
278                 vTaskSuspend( NULL );\r
279         }\r
280 }\r
281 /*-----------------------------------------------------------*/\r
282 \r
283 static void prvHighPriorityPeekTask( void *pvParameters )\r
284 {\r
285 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
286 uint32_t ulValue;\r
287 \r
288         for( ;; )\r
289         {\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
294                 {\r
295                         /* We expected to have received something by the time we unblock. */\r
296                         xErrorDetected = pdTRUE;\r
297                 }\r
298 \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
301                 the data). */\r
302                 if( ulValue != 0x01234567 )\r
303                 {\r
304                         /* We did not receive the expected value. */\r
305                         xErrorDetected = pdTRUE;\r
306                 }\r
307 \r
308                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
309                 {\r
310                         /* The message should have been left on the queue. */\r
311                         xErrorDetected = pdTRUE;\r
312                 }\r
313 \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
317                 in the queue. */\r
318                 vTaskSuspend( NULL );\r
319 \r
320 \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
324                 {\r
325                         xErrorDetected = pdTRUE;\r
326                 }\r
327 \r
328                 if( ulValue != 0xaabbaabb )\r
329                 {\r
330                         xErrorDetected = pdTRUE;\r
331                 }\r
332 \r
333                 vTaskSuspend( NULL );\r
334         }\r
335 }\r
336 /*-----------------------------------------------------------*/\r
337 \r
338 static void prvMediumPriorityPeekTask( void *pvParameters )\r
339 {\r
340 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
341 uint32_t ulValue;\r
342 \r
343         for( ;; )\r
344         {\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
349                 {\r
350                         /* We expected to have received something by the time we unblock. */\r
351                         xErrorDetected = pdTRUE;\r
352                 }\r
353 \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
356                 the data). */\r
357                 if( ulValue != 0x01234567 )\r
358                 {\r
359                         /* We did not receive the expected value. */\r
360                         xErrorDetected = pdTRUE;\r
361                 }\r
362 \r
363                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
364                 {\r
365                         /* The message should have been left on the queue. */\r
366                         xErrorDetected = pdTRUE;\r
367                 }\r
368 \r
369                 /* Just so we know the test is still running. */\r
370                 ulLoopCounter++;\r
371 \r
372                 /* Now we can suspend ourselves so the low priority task can execute\r
373                 again. */\r
374                 vTaskSuspend( NULL );\r
375         }\r
376 }\r
377 /*-----------------------------------------------------------*/\r
378 \r
379 static void prvLowPriorityPeekTask( void *pvParameters )\r
380 {\r
381 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
382 uint32_t ulValue;\r
383 \r
384         for( ;; )\r
385         {\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
390                 {\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
394                 }\r
395 \r
396                 #if configUSE_PREEMPTION == 0\r
397                         taskYIELD();\r
398                 #endif\r
399 \r
400                 /* By the time we get here the data should have been removed from\r
401                 the queue. */\r
402                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
403                 {\r
404                         xErrorDetected = pdTRUE;\r
405                 }\r
406 \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
411                 {\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
415                 }\r
416 \r
417                 #if configUSE_PREEMPTION == 0\r
418                         taskYIELD();\r
419                 #endif\r
420 \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
423                 ulValue = 0;\r
424                 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
425                 {\r
426                         /* We expected to receive the data. */\r
427                         xErrorDetected = pdTRUE;\r
428                 }\r
429 \r
430                 if( ulValue != 0x01234567 )\r
431                 {\r
432                         /* We did not receive the expected value. */\r
433                 }\r
434 \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
438 \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
447 \r
448                 #if( configUSE_PREEMPTION == 0 )\r
449                         taskYIELD();\r
450                 #endif\r
451 \r
452                 ulValue = 0xaabbaabb;\r
453                 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
454                 {\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
458                 }\r
459 \r
460                 #if configUSE_PREEMPTION == 0\r
461                         taskYIELD();\r
462                 #endif\r
463 \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
467                 {\r
468                         /* We expected to receive the data. */\r
469                         xErrorDetected = pdTRUE;\r
470                 }\r
471 \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
477 \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
481         }\r
482 }\r
483 /*-----------------------------------------------------------*/\r
484 \r
485 /* This is called to check that all the created tasks are still running. */\r
486 BaseType_t xAreQueuePeekTasksStillRunning( void )\r
487 {\r
488 static uint32_t ulLastLoopCounter = 0;\r
489 \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
493         {\r
494                 xErrorDetected = pdTRUE;\r
495         }\r
496 \r
497         ulLastLoopCounter = ulLoopCounter;\r
498 \r
499         /* Errors detected in the task itself will have latched xErrorDetected\r
500         to true. */\r
501 \r
502         return ( BaseType_t ) !xErrorDetected;\r
503 }\r
504 \r