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