]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/events.c
Prepare for V7.2.0 release.
[freertos] / FreeRTOS / Demo / Common / Full / events.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  * This file exercises the event mechanism whereby more than one task is\r
69  * blocked waiting for the same event.\r
70  *\r
71  * The demo creates five tasks - four 'event' tasks, and a controlling task.\r
72  * The event tasks have various different priorities and all block on reading\r
73  * the same queue.  The controlling task writes data to the queue, then checks\r
74  * to see which of the event tasks read the data from the queue.  The\r
75  * controlling task has the lowest priority of all the tasks so is guaranteed\r
76  * to always get preempted immediately upon writing to the queue.\r
77  *\r
78  * By selectively suspending and resuming the event tasks the controlling task\r
79  * can check that the highest priority task that is blocked on the queue is the\r
80  * task that reads the posted data from the queue.\r
81  *\r
82  * Two of the event tasks share the same priority.  When neither of these tasks\r
83  * are suspended they should alternate - one reading one message from the queue,\r
84  * the other the next message, etc.\r
85  */\r
86 \r
87 /* Standard includes. */\r
88 #include <stdlib.h>\r
89 #include <stdio.h>\r
90 #include <string.h>\r
91 \r
92 /* Scheduler include files. */\r
93 #include "FreeRTOS.h"\r
94 #include "task.h"\r
95 #include "queue.h"\r
96 \r
97 /* Demo program include files. */\r
98 #include "mevents.h"\r
99 #include "print.h"\r
100 \r
101 /* Demo specific constants. */\r
102 #define evtSTACK_SIZE           ( ( unsigned portBASE_TYPE ) configMINIMAL_STACK_SIZE )\r
103 #define evtNUM_TASKS            ( 4 )\r
104 #define evtQUEUE_LENGTH         ( ( unsigned portBASE_TYPE ) 3 )\r
105 #define evtNO_DELAY                                             0\r
106 \r
107 /* Just indexes used to uniquely identify the tasks.  Note that two tasks are\r
108 'highest' priority. */\r
109 #define evtHIGHEST_PRIORITY_INDEX_2             3\r
110 #define evtHIGHEST_PRIORITY_INDEX_1             2\r
111 #define evtMEDIUM_PRIORITY_INDEX                1\r
112 #define evtLOWEST_PRIORITY_INDEX                0\r
113 \r
114 /* Each event task increments one of these counters each time it reads data\r
115 from the queue. */\r
116 static volatile portBASE_TYPE xTaskCounters[ evtNUM_TASKS ] = { 0, 0, 0, 0 };\r
117 \r
118 /* Each time the controlling task posts onto the queue it increments the \r
119 expected count of the task that it expected to read the data from the queue \r
120 (i.e. the task with the highest priority that should be blocked on the queue).  \r
121 \r
122 xExpectedTaskCounters are incremented from the controlling task, and \r
123 xTaskCounters are incremented from the individual event tasks - therefore\r
124 comparing xTaskCounters to xExpectedTaskCounters shows whether or not the \r
125 correct task was unblocked by the post. */\r
126 static portBASE_TYPE xExpectedTaskCounters[ evtNUM_TASKS ] = { 0, 0, 0, 0 };\r
127 \r
128 /* Handles to the four event tasks.  These are required to suspend and resume\r
129 the tasks. */\r
130 static xTaskHandle xCreatedTasks[ evtNUM_TASKS ];\r
131 \r
132 /* The single queue onto which the controlling task posts, and the four event\r
133 tasks block. */\r
134 static xQueueHandle xQueue;\r
135 \r
136 /* Flag used to indicate whether or not an error has occurred at any time.\r
137 An error is either the queue being full when not expected, or an unexpected\r
138 task reading data from the queue. */\r
139 static portBASE_TYPE xHealthStatus = pdPASS;\r
140 \r
141 /*-----------------------------------------------------------*/\r
142 \r
143 /* Function that implements the event task.  This is created four times. */\r
144 static void prvMultiEventTask( void *pvParameters );\r
145 \r
146 /* Function that implements the controlling task. */\r
147 static void prvEventControllerTask( void *pvParameters );\r
148 \r
149 /* This is a utility function that posts data to the queue, then compares \r
150 xExpectedTaskCounters with xTaskCounters to ensure everything worked as \r
151 expected.\r
152 \r
153 The event tasks all have higher priorities the controlling task.  Therefore\r
154 the controlling task will always get preempted between writhing to the queue\r
155 and checking the task counters. \r
156 \r
157 @param xExpectedTask  The index to the task that the controlling task thinks\r
158                       should be the highest priority task waiting for data, and\r
159                                           therefore the task that will unblock.\r
160                                           \r
161 @param  xIncrement    The number of items that should be written to the queue.\r
162 */\r
163 static void prvCheckTaskCounters( portBASE_TYPE xExpectedTask, portBASE_TYPE xIncrement );\r
164 \r
165 /* This is just incremented each cycle of the controlling tasks function so\r
166 the main application can ensure the test is still running. */\r
167 static portBASE_TYPE xCheckVariable = 0;\r
168 \r
169 /*-----------------------------------------------------------*/\r
170 \r
171 void vStartMultiEventTasks( void )\r
172 {\r
173         /* Create the queue to be used for all the communications. */\r
174         xQueue = xQueueCreate( evtQUEUE_LENGTH, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );\r
175 \r
176         /* Start the controlling task.  This has the idle priority to ensure it is\r
177         always preempted by the event tasks. */\r
178         xTaskCreate( prvEventControllerTask, "EvntCTRL", evtSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
179 \r
180         /* Start the four event tasks.  Note that two have priority 3, one \r
181         priority 2 and the other priority 1. */\r
182         xTaskCreate( prvMultiEventTask, "Event0", evtSTACK_SIZE, ( void * ) &( xTaskCounters[ 0 ] ), 1, &( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] ) );\r
183         xTaskCreate( prvMultiEventTask, "Event1", evtSTACK_SIZE, ( void * ) &( xTaskCounters[ 1 ] ), 2, &( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] ) );\r
184         xTaskCreate( prvMultiEventTask, "Event2", evtSTACK_SIZE, ( void * ) &( xTaskCounters[ 2 ] ), 3, &( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] ) );\r
185         xTaskCreate( prvMultiEventTask, "Event3", evtSTACK_SIZE, ( void * ) &( xTaskCounters[ 3 ] ), 3, &( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_2 ] ) );\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 static void prvMultiEventTask( void *pvParameters )\r
190 {\r
191 portBASE_TYPE *pxCounter;\r
192 unsigned portBASE_TYPE uxDummy;\r
193 const char * const pcTaskStartMsg = "Multi event task started.\r\n";\r
194 \r
195         /* The variable this task will increment is passed in as a parameter. */\r
196         pxCounter = ( portBASE_TYPE * ) pvParameters;\r
197 \r
198         vPrintDisplayMessage( &pcTaskStartMsg );\r
199 \r
200         for( ;; )\r
201         {\r
202                 /* Block on the queue. */\r
203                 if( xQueueReceive( xQueue, &uxDummy, portMAX_DELAY ) )\r
204                 {\r
205                         /* We unblocked by reading the queue - so simply increment\r
206                         the counter specific to this task instance. */\r
207                         ( *pxCounter )++;\r
208                 }\r
209                 else\r
210                 {\r
211                         xHealthStatus = pdFAIL;\r
212                 }\r
213         }\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 static void prvEventControllerTask( void *pvParameters )\r
218 {\r
219 const char * const pcTaskStartMsg = "Multi event controller task started.\r\n";\r
220 portBASE_TYPE xDummy = 0;\r
221 \r
222         /* Just to stop warnings. */\r
223         ( void ) pvParameters;\r
224 \r
225         vPrintDisplayMessage( &pcTaskStartMsg );\r
226 \r
227         for( ;; )\r
228         {\r
229                 /* All tasks are blocked on the queue.  When a message is posted one of\r
230                 the two tasks that share the highest priority should unblock to read\r
231                 the queue.  The next message written should unblock the other task with\r
232                 the same high priority, and so on in order.   No other task should \r
233                 unblock to read data as they have lower priorities. */\r
234 \r
235                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );\r
236                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_2, 1 );\r
237                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );\r
238                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_2, 1 );\r
239                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );\r
240 \r
241                 /* For the rest of these tests we don't need the second 'highest' \r
242                 priority task - so it is suspended. */\r
243                 vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_2 ] );\r
244 \r
245 \r
246 \r
247                 /* Now suspend the other highest priority task.  The medium priority \r
248                 task will then be the task with the highest priority that remains \r
249                 blocked on the queue. */\r
250                 vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
251                 \r
252                 /* This time, when we post onto the queue we will expect the medium\r
253                 priority task to unblock and preempt us. */\r
254                 prvCheckTaskCounters( evtMEDIUM_PRIORITY_INDEX, 1 );\r
255 \r
256                 /* Now try resuming the highest priority task while the scheduler is\r
257                 suspended.  The task should start executing as soon as the scheduler\r
258                 is resumed - therefore when we post to the queue again, the highest\r
259                 priority task should again preempt us. */\r
260                 vTaskSuspendAll();\r
261                         vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
262                 xTaskResumeAll();\r
263                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );\r
264                 \r
265                 /* Now we are going to suspend the high and medium priority tasks.  The\r
266                 low priority task should then preempt us.  Again the task suspension is \r
267                 done with the whole scheduler suspended just for test purposes. */\r
268                 vTaskSuspendAll();\r
269                         vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
270                         vTaskSuspend( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] );\r
271                 xTaskResumeAll();\r
272                 prvCheckTaskCounters( evtLOWEST_PRIORITY_INDEX, 1 );\r
273                 \r
274                 /* Do the same basic test another few times - selectively suspending\r
275                 and resuming tasks and each time calling prvCheckTaskCounters() passing\r
276                 to the function the number of the task we expected to be unblocked by \r
277                 the     post. */\r
278 \r
279                 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
280                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );\r
281                 \r
282                 vTaskSuspendAll(); /* Just for test. */\r
283                         vTaskSuspendAll(); /* Just for test. */\r
284                                 vTaskSuspendAll(); /* Just for even more test. */\r
285                                         vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
286                                 xTaskResumeAll();\r
287                         xTaskResumeAll();\r
288                 xTaskResumeAll();\r
289                 prvCheckTaskCounters( evtLOWEST_PRIORITY_INDEX, 1 );\r
290                 \r
291                 vTaskResume( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] );\r
292                 prvCheckTaskCounters( evtMEDIUM_PRIORITY_INDEX, 1 );\r
293                 \r
294                 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
295                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );\r
296 \r
297                 /* Now a slight change, first suspend all tasks. */\r
298                 vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
299                 vTaskSuspend( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] );\r
300                 vTaskSuspend( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );\r
301                 \r
302                 /* Now when we resume the low priority task and write to the queue 3 \r
303                 times.  We expect the low priority task to service the queue three\r
304                 times. */\r
305                 vTaskResume( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );\r
306                 prvCheckTaskCounters( evtLOWEST_PRIORITY_INDEX, evtQUEUE_LENGTH );\r
307                 \r
308                 /* Again suspend all tasks (only the low priority task is not suspended\r
309                 already). */\r
310                 vTaskSuspend( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );\r
311                 \r
312                 /* This time we are going to suspend the scheduler, resume the low\r
313                 priority task, then resume the high priority task.  In this state we\r
314                 will write to the queue three times.  When the scheduler is resumed\r
315                 we expect the high priority task to service all three messages. */\r
316                 vTaskSuspendAll();\r
317                 {\r
318                         vTaskResume( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );\r
319                         vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
320                         \r
321                         for( xDummy = 0; xDummy < evtQUEUE_LENGTH; xDummy++ )\r
322                         {\r
323                                 if( xQueueSend( xQueue, &xDummy, evtNO_DELAY ) != pdTRUE )\r
324                                 {\r
325                                         xHealthStatus = pdFAIL;\r
326                                 }\r
327                         }                       \r
328                         \r
329                         /* The queue should not have been serviced yet!.  The scheduler\r
330                         is still suspended. */\r
331                         if( memcmp( ( void * ) xExpectedTaskCounters, ( void * ) xTaskCounters, sizeof( xExpectedTaskCounters ) ) )\r
332                         {\r
333                                 xHealthStatus = pdFAIL;\r
334                         }\r
335                 }\r
336                 xTaskResumeAll();\r
337 \r
338                 /* We should have been preempted by resuming the scheduler - so by the\r
339                 time we are running again we expect the high priority task to have \r
340                 removed three items from the queue. */\r
341                 xExpectedTaskCounters[ evtHIGHEST_PRIORITY_INDEX_1 ] += evtQUEUE_LENGTH;\r
342                 if( memcmp( ( void * ) xExpectedTaskCounters, ( void * ) xTaskCounters, sizeof( xExpectedTaskCounters ) ) )\r
343                 {\r
344                         xHealthStatus = pdFAIL;\r
345                 }\r
346                 \r
347                 /* The medium priority and second high priority tasks are still \r
348                 suspended.  Make sure to resume them before starting again. */\r
349                 vTaskResume( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] );\r
350                 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_2 ] );\r
351 \r
352                 /* Just keep incrementing to show the task is still executing. */\r
353                 xCheckVariable++;\r
354         }\r
355 }\r
356 /*-----------------------------------------------------------*/\r
357 \r
358 static void prvCheckTaskCounters( portBASE_TYPE xExpectedTask, portBASE_TYPE xIncrement )\r
359 {\r
360 portBASE_TYPE xDummy = 0;\r
361 \r
362         /* Write to the queue the requested number of times.  The data written is\r
363         not important. */\r
364         for( xDummy = 0; xDummy < xIncrement; xDummy++ )\r
365         {\r
366                 if( xQueueSend( xQueue, &xDummy, evtNO_DELAY ) != pdTRUE )\r
367                 {\r
368                         /* Did not expect to ever find the queue full. */\r
369                         xHealthStatus = pdFAIL;\r
370                 }\r
371         }\r
372 \r
373         /* All the tasks blocked on the queue have a priority higher than the \r
374         controlling task.  Writing to the queue will therefore have caused this\r
375         task to be preempted.  By the time this line executes the event task will\r
376         have executed and incremented its counter.  Increment the expected counter\r
377         to the same value. */\r
378         ( xExpectedTaskCounters[ xExpectedTask ] ) += xIncrement;\r
379 \r
380         /* Check the actual counts and expected counts really are the same. */\r
381         if( memcmp( ( void * ) xExpectedTaskCounters, ( void * ) xTaskCounters, sizeof( xExpectedTaskCounters ) ) )\r
382         {\r
383                 /* The counters were not the same.  This means a task we did not expect\r
384                 to unblock actually did unblock. */\r
385                 xHealthStatus = pdFAIL;\r
386         }\r
387 }\r
388 /*-----------------------------------------------------------*/\r
389 \r
390 portBASE_TYPE xAreMultiEventTasksStillRunning( void )\r
391 {\r
392 static portBASE_TYPE xPreviousCheckVariable = 0;\r
393 \r
394         /* Called externally to periodically check that this test is still\r
395         operational. */\r
396 \r
397         if( xPreviousCheckVariable == xCheckVariable )\r
398         {\r
399                 xHealthStatus = pdFAIL;\r
400         }\r
401         \r
402         xPreviousCheckVariable = xCheckVariable;\r
403         \r
404         return xHealthStatus;   \r
405 }\r
406 \r
407 \r