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