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