]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/QPeek.c
Update version numbers to V7.4.1.
[freertos] / FreeRTOS / Demo / Common / Minimal / QPeek.c
1 /*\r
2     FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 \r
76 /* \r
77  * Tests the behaviour when data is peeked from a queue when there are\r
78  * multiple tasks blocked on the queue.\r
79  */\r
80 \r
81 \r
82 #include <stdlib.h>\r
83 \r
84 /* Scheduler include files. */\r
85 #include "FreeRTOS.h"\r
86 #include "task.h"\r
87 #include "queue.h"\r
88 #include "semphr.h"\r
89 \r
90 /* Demo program include files. */\r
91 #include "QPeek.h"\r
92 \r
93 #define qpeekQUEUE_LENGTH               ( 5 )\r
94 #define qpeekNO_BLOCK                   ( 0 )\r
95 #define qpeekSHORT_DELAY                ( 10 )\r
96 \r
97 #define qpeekLOW_PRIORITY                       ( tskIDLE_PRIORITY + 0 )\r
98 #define qpeekMEDIUM_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
99 #define qpeekHIGH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
100 #define qpeekHIGHEST_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 /*\r
105  * The following three tasks are used to demonstrate the peeking behaviour.\r
106  * Each task is given a different priority to demonstrate the order in which\r
107  * tasks are woken as data is peeked from a queue.\r
108  */\r
109 static void prvLowPriorityPeekTask( void *pvParameters );\r
110 static void prvMediumPriorityPeekTask( void *pvParameters );\r
111 static void prvHighPriorityPeekTask( void *pvParameters );\r
112 static void prvHighestPriorityPeekTask( void *pvParameters );\r
113 \r
114 /*-----------------------------------------------------------*/\r
115 \r
116 /* Flag that will be latched to pdTRUE should any unexpected behaviour be\r
117 detected in any of the tasks. */\r
118 static volatile portBASE_TYPE xErrorDetected = pdFALSE;\r
119 \r
120 /* Counter that is incremented on each cycle of a test.  This is used to\r
121 detect a stalled task - a test that is no longer running. */\r
122 static volatile unsigned portLONG ulLoopCounter = 0;\r
123 \r
124 /* Handles to the test tasks. */\r
125 xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 void vStartQueuePeekTasks( void )\r
129 {\r
130 xQueueHandle xQueue;\r
131 \r
132         /* Create the queue that we are going to use for the test/demo. */\r
133         xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) );\r
134 \r
135         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
136         in use.  The queue registry is provided as a means for kernel aware \r
137         debuggers to locate queues and has no purpose if a kernel aware debugger\r
138         is not being used.  The call to vQueueAddToRegistry() will be removed\r
139         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
140         defined to be less than 1. */\r
141         vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "QPeek_Test_Queue" );\r
142 \r
143         /* Create the demo tasks and pass it the queue just created.  We are\r
144         passing the queue handle by value so it does not matter that it is declared\r
145         on the stack here. */\r
146         xTaskCreate( prvLowPriorityPeekTask, ( signed portCHAR * )"PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );\r
147         xTaskCreate( prvMediumPriorityPeekTask, ( signed portCHAR * )"PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );\r
148         xTaskCreate( prvHighPriorityPeekTask, ( signed portCHAR * )"PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );\r
149         xTaskCreate( prvHighestPriorityPeekTask, ( signed portCHAR * )"PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );\r
150 }\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 static void prvHighestPriorityPeekTask( void *pvParameters )\r
154 {\r
155 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;\r
156 unsigned portLONG ulValue;\r
157 \r
158         #ifdef USE_STDIO\r
159         {\r
160                 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
161         \r
162                 const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n";\r
163 \r
164                 /* Queue a message for printing to say the task has started. */\r
165                 vPrintDisplayMessage( &pcTaskStartMsg );\r
166         }\r
167         #endif\r
168 \r
169         for( ;; )\r
170         {\r
171                 /* Try peeking from the queue.  The queue should be empty so we will\r
172                 block, allowing the high priority task to execute. */\r
173                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
174                 {\r
175                         /* We expected to have received something by the time we unblock. */\r
176                         xErrorDetected = pdTRUE;\r
177                 }\r
178 \r
179                 /* When we reach here the high and medium priority tasks should still\r
180                 be blocked on the queue.  We unblocked because the low priority task\r
181                 wrote a value to the queue, which we should have peeked.  Peeking the\r
182                 data (rather than receiving it) will leave the data on the queue, so\r
183                 the high priority task should then have also been unblocked, but not\r
184                 yet executed. */\r
185                 if( ulValue != 0x11223344 )\r
186                 {\r
187                         /* We did not receive the expected value. */\r
188                         xErrorDetected = pdTRUE;\r
189                 }\r
190 \r
191                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
192                 {\r
193                         /* The message should have been left on the queue. */\r
194                         xErrorDetected = pdTRUE;\r
195                 }\r
196 \r
197                 /* Now we are going to actually receive the data, so when the high\r
198                 priority task runs it will find the queue empty and return to the\r
199                 blocked state. */\r
200                 ulValue = 0;\r
201                 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
202                 {\r
203                         /* We expected to receive the value. */\r
204                         xErrorDetected = pdTRUE;\r
205                 }\r
206 \r
207                 if( ulValue != 0x11223344 )\r
208                 {\r
209                         /* We did not receive the expected value - which should have been\r
210                         the same value as was peeked. */\r
211                         xErrorDetected = pdTRUE;\r
212                 }\r
213 \r
214                 /* Now we will block again as the queue is once more empty.  The low \r
215                 priority task can then execute again. */\r
216                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
217                 {\r
218                         /* We expected to have received something by the time we unblock. */\r
219                         xErrorDetected = pdTRUE;\r
220                 }\r
221 \r
222                 /* When we get here the low priority task should have again written to the\r
223                 queue. */\r
224                 if( ulValue != 0x01234567 )\r
225                 {\r
226                         /* We did not receive the expected value. */\r
227                         xErrorDetected = pdTRUE;\r
228                 }\r
229 \r
230                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
231                 {\r
232                         /* The message should have been left on the queue. */\r
233                         xErrorDetected = pdTRUE;\r
234                 }\r
235 \r
236                 /* We only peeked the data, so suspending ourselves now should enable\r
237                 the high priority task to also peek the data.  The high priority task\r
238                 will have been unblocked when we peeked the data as we left the data\r
239                 in the queue. */\r
240                 vTaskSuspend( NULL );\r
241 \r
242 \r
243 \r
244                 /* This time we are going to do the same as the above test, but the\r
245                 high priority task is going to receive the data, rather than peek it.\r
246                 This means that the medium priority task should never peek the value. */\r
247                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
248                 {\r
249                         xErrorDetected = pdTRUE;\r
250                 }\r
251 \r
252                 if( ulValue != 0xaabbaabb )\r
253                 {\r
254                         xErrorDetected = pdTRUE;\r
255                 }\r
256 \r
257                 vTaskSuspend( NULL );           \r
258         }\r
259 }\r
260 /*-----------------------------------------------------------*/\r
261 \r
262 static void prvHighPriorityPeekTask( void *pvParameters )\r
263 {\r
264 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;\r
265 unsigned portLONG ulValue;\r
266 \r
267         for( ;; )\r
268         {\r
269                 /* Try peeking from the queue.  The queue should be empty so we will\r
270                 block, allowing the medium priority task to execute.  Both the high\r
271                 and highest priority tasks will then be blocked on the queue. */\r
272                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
273                 {\r
274                         /* We expected to have received something by the time we unblock. */\r
275                         xErrorDetected = pdTRUE;\r
276                 }\r
277 \r
278                 /* When we get here the highest priority task should have peeked the data\r
279                 (unblocking this task) then suspended (allowing this task to also peek\r
280                 the data). */\r
281                 if( ulValue != 0x01234567 )\r
282                 {\r
283                         /* We did not receive the expected value. */\r
284                         xErrorDetected = pdTRUE;\r
285                 }\r
286 \r
287                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
288                 {\r
289                         /* The message should have been left on the queue. */\r
290                         xErrorDetected = pdTRUE;\r
291                 }\r
292 \r
293                 /* We only peeked the data, so suspending ourselves now should enable\r
294                 the medium priority task to also peek the data.  The medium priority task\r
295                 will have been unblocked when we peeked the data as we left the data\r
296                 in the queue. */\r
297                 vTaskSuspend( NULL );\r
298 \r
299 \r
300                 /* This time we are going actually receive the value, so the medium\r
301                 priority task will never peek the data - we removed it from the queue. */\r
302                 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
303                 {\r
304                         xErrorDetected = pdTRUE;\r
305                 }\r
306 \r
307                 if( ulValue != 0xaabbaabb )\r
308                 {\r
309                         xErrorDetected = pdTRUE;\r
310                 }\r
311 \r
312                 vTaskSuspend( NULL );                           \r
313         }\r
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 static void prvMediumPriorityPeekTask( void *pvParameters )\r
318 {\r
319 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;\r
320 unsigned portLONG ulValue;\r
321 \r
322         for( ;; )\r
323         {\r
324                 /* Try peeking from the queue.  The queue should be empty so we will\r
325                 block, allowing the low priority task to execute.  The highest, high\r
326                 and medium priority tasks will then all be blocked on the queue. */\r
327                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
328                 {\r
329                         /* We expected to have received something by the time we unblock. */\r
330                         xErrorDetected = pdTRUE;\r
331                 }\r
332 \r
333                 /* When we get here the high priority task should have peeked the data\r
334                 (unblocking this task) then suspended (allowing this task to also peek\r
335                 the data). */\r
336                 if( ulValue != 0x01234567 )\r
337                 {\r
338                         /* We did not receive the expected value. */\r
339                         xErrorDetected = pdTRUE;\r
340                 }\r
341 \r
342                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
343                 {\r
344                         /* The message should have been left on the queue. */\r
345                         xErrorDetected = pdTRUE;\r
346                 }\r
347 \r
348                 /* Just so we know the test is still running. */\r
349                 ulLoopCounter++;\r
350 \r
351                 /* Now we can suspend ourselves so the low priority task can execute\r
352                 again. */\r
353                 vTaskSuspend( NULL );\r
354         }\r
355 }\r
356 /*-----------------------------------------------------------*/\r
357 \r
358 static void prvLowPriorityPeekTask( void *pvParameters )\r
359 {\r
360 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;\r
361 unsigned portLONG ulValue;\r
362 \r
363         for( ;; )\r
364         {\r
365                 /* Write some data to the queue.  This should unblock the highest \r
366                 priority task that is waiting to peek data from the queue. */\r
367                 ulValue = 0x11223344;\r
368                 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
369                 {\r
370                         /* We were expecting the queue to be empty so we should not of\r
371                         had a problem writing to the queue. */\r
372                         xErrorDetected = pdTRUE;\r
373                 }\r
374 \r
375                 /* By the time we get here the data should have been removed from\r
376                 the queue. */\r
377                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
378                 {\r
379                         xErrorDetected = pdTRUE;\r
380                 }\r
381 \r
382                 /* Write another value to the queue, again waking the highest priority\r
383                 task that is blocked on the queue. */\r
384                 ulValue = 0x01234567;\r
385                 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
386                 {\r
387                         /* We were expecting the queue to be empty so we should not of\r
388                         had a problem writing to the queue. */\r
389                         xErrorDetected = pdTRUE;\r
390                 }\r
391 \r
392                 /* All the other tasks should now have successfully peeked the data.\r
393                 The data is still in the queue so we should be able to receive it. */\r
394                 ulValue = 0;\r
395                 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
396                 {\r
397                         /* We expected to receive the data. */\r
398                         xErrorDetected = pdTRUE;\r
399                 }\r
400 \r
401                 if( ulValue != 0x01234567 )\r
402                 {\r
403                         /* We did not receive the expected value. */\r
404                 }\r
405                 \r
406                 /* Lets just delay a while as this is an intensive test as we don't\r
407                 want to starve other tests of processing time. */\r
408                 vTaskDelay( qpeekSHORT_DELAY );\r
409 \r
410                 /* Unsuspend the other tasks so we can repeat the test - this time\r
411                 however not all the other tasks will peek the data as the high\r
412                 priority task is actually going to remove it from the queue.  Send\r
413                 to front is used just to be different.  As the queue is empty it\r
414                 makes no difference to the result. */\r
415                 vTaskResume( xMediumPriorityTask );\r
416                 vTaskResume( xHighPriorityTask );\r
417                 vTaskResume( xHighestPriorityTask );\r
418 \r
419                 ulValue = 0xaabbaabb;\r
420                 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
421                 {\r
422                         /* We were expecting the queue to be empty so we should not of\r
423                         had a problem writing to the queue. */\r
424                         xErrorDetected = pdTRUE;\r
425                 }\r
426 \r
427                 /* This time we should find that the queue is empty.  The high priority\r
428                 task actually removed the data rather than just peeking it. */\r
429                 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )\r
430                 {\r
431                         /* We expected to receive the data. */\r
432                         xErrorDetected = pdTRUE;\r
433                 }\r
434 \r
435                 /* Unsuspend the highest and high priority tasks so we can go back\r
436                 and repeat the whole thing.  The medium priority task should not be\r
437                 suspended as it was not able to peek the data in this last case. */\r
438                 vTaskResume( xHighPriorityTask );\r
439                 vTaskResume( xHighestPriorityTask );            \r
440 \r
441                 /* Lets just delay a while as this is an intensive test as we don't\r
442                 want to starve other tests of processing time. */\r
443                 vTaskDelay( qpeekSHORT_DELAY );\r
444         }\r
445 }\r
446 /*-----------------------------------------------------------*/\r
447 \r
448 /* This is called to check that all the created tasks are still running. */\r
449 portBASE_TYPE xAreQueuePeekTasksStillRunning( void )\r
450 {\r
451 static unsigned portLONG ulLastLoopCounter = 0;\r
452 \r
453         /* If the demo task is still running then we expect the loopcounter to\r
454         have incremented since this function was last called. */\r
455         if( ulLastLoopCounter == ulLoopCounter )\r
456         {\r
457                 xErrorDetected = pdTRUE;\r
458         }\r
459 \r
460         ulLastLoopCounter = ulLoopCounter;\r
461 \r
462         /* Errors detected in the task itself will have latched xErrorDetected\r
463         to true. */\r
464 \r
465         return !xErrorDetected;\r
466 }\r
467 \r