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