]> git.sur5r.net Git - freertos/blob - Demo/Common/Full/events.c
Update version numbers to V4.8.0
[freertos] / Demo / Common / Full / events.c
1 /*\r
2         FreeRTOS.org V4.8.0 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         ***************************************************************************\r
28         *                                                                                                                                                 *\r
29         * SAVE TIME AND MONEY!  Why not get us to quote to get FreeRTOS.org               *\r
30         * running on your hardware - or even write all or part of your application*\r
31         * for you?  See http://www.OpenRTOS.com for details.                                      *\r
32         *                                                                                                                                                 *\r
33         ***************************************************************************\r
34         ***************************************************************************\r
35 \r
36         Please ensure to read the configuration and relevant port sections of the\r
37         online documentation.\r
38 \r
39         http://www.FreeRTOS.org - Documentation, latest information, license and \r
40         contact details.\r
41 \r
42         http://www.SafeRTOS.com - A version that is certified for use in safety \r
43         critical systems.\r
44 \r
45         http://www.OpenRTOS.com - Commercial support, development, porting, \r
46         licensing and training services.\r
47 */\r
48 \r
49 /**\r
50  * This file exercises the event mechanism whereby more than one task is\r
51  * blocked waiting for the same event.\r
52  *\r
53  * The demo creates five tasks - four 'event' tasks, and a controlling task.\r
54  * The event tasks have various different priorities and all block on reading\r
55  * the same queue.  The controlling task writes data to the queue, then checks\r
56  * to see which of the event tasks read the data from the queue.  The\r
57  * controlling task has the lowest priority of all the tasks so is guaranteed\r
58  * to always get preempted immediately upon writing to the queue.\r
59  *\r
60  * By selectively suspending and resuming the event tasks the controlling task\r
61  * can check that the highest priority task that is blocked on the queue is the\r
62  * task that reads the posted data from the queue.\r
63  *\r
64  * Two of the event tasks share the same priority.  When neither of these tasks\r
65  * are suspended they should alternate - one reading one message from the queue,\r
66  * the other the next message, etc.\r
67  */\r
68 \r
69 /* Standard includes. */\r
70 #include <stdlib.h>\r
71 #include <stdio.h>\r
72 #include <string.h>\r
73 \r
74 /* Scheduler include files. */\r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 #include "queue.h"\r
78 \r
79 /* Demo program include files. */\r
80 #include "mevents.h"\r
81 #include "print.h"\r
82 \r
83 /* Demo specific constants. */\r
84 #define evtSTACK_SIZE           ( ( unsigned portBASE_TYPE ) configMINIMAL_STACK_SIZE )\r
85 #define evtNUM_TASKS            ( 4 )\r
86 #define evtQUEUE_LENGTH         ( ( unsigned portBASE_TYPE ) 3 )\r
87 #define evtNO_DELAY                                             0\r
88 \r
89 /* Just indexes used to uniquely identify the tasks.  Note that two tasks are\r
90 'highest' priority. */\r
91 #define evtHIGHEST_PRIORITY_INDEX_2             3\r
92 #define evtHIGHEST_PRIORITY_INDEX_1             2\r
93 #define evtMEDIUM_PRIORITY_INDEX                1\r
94 #define evtLOWEST_PRIORITY_INDEX                0\r
95 \r
96 /* Each event task increments one of these counters each time it reads data\r
97 from the queue. */\r
98 static volatile portBASE_TYPE xTaskCounters[ evtNUM_TASKS ] = { 0, 0, 0, 0 };\r
99 \r
100 /* Each time the controlling task posts onto the queue it increments the \r
101 expected count of the task that it expected to read the data from the queue \r
102 (i.e. the task with the highest priority that should be blocked on the queue).  \r
103 \r
104 xExpectedTaskCounters are incremented from the controlling task, and \r
105 xTaskCounters are incremented from the individual event tasks - therefore\r
106 comparing xTaskCounters to xExpectedTaskCounters shows whether or not the \r
107 correct task was unblocked by the post. */\r
108 static portBASE_TYPE xExpectedTaskCounters[ evtNUM_TASKS ] = { 0, 0, 0, 0 };\r
109 \r
110 /* Handles to the four event tasks.  These are required to suspend and resume\r
111 the tasks. */\r
112 static xTaskHandle xCreatedTasks[ evtNUM_TASKS ];\r
113 \r
114 /* The single queue onto which the controlling task posts, and the four event\r
115 tasks block. */\r
116 static xQueueHandle xQueue;\r
117 \r
118 /* Flag used to indicate whether or not an error has occurred at any time.\r
119 An error is either the queue being full when not expected, or an unexpected\r
120 task reading data from the queue. */\r
121 static portBASE_TYPE xHealthStatus = pdPASS;\r
122 \r
123 /*-----------------------------------------------------------*/\r
124 \r
125 /* Function that implements the event task.  This is created four times. */\r
126 static void prvMultiEventTask( void *pvParameters );\r
127 \r
128 /* Function that implements the controlling task. */\r
129 static void prvEventControllerTask( void *pvParameters );\r
130 \r
131 /* This is a utility function that posts data to the queue, then compares \r
132 xExpectedTaskCounters with xTaskCounters to ensure everything worked as \r
133 expected.\r
134 \r
135 The event tasks all have higher priorities the controlling task.  Therefore\r
136 the controlling task will always get preempted between writhing to the queue\r
137 and checking the task counters. \r
138 \r
139 @param xExpectedTask  The index to the task that the controlling task thinks\r
140                       should be the highest priority task waiting for data, and\r
141                                           therefore the task that will unblock.\r
142                                           \r
143 @param  xIncrement    The number of items that should be written to the queue.\r
144 */\r
145 static void prvCheckTaskCounters( portBASE_TYPE xExpectedTask, portBASE_TYPE xIncrement );\r
146 \r
147 /* This is just incremented each cycle of the controlling tasks function so\r
148 the main application can ensure the test is still running. */\r
149 static portBASE_TYPE xCheckVariable = 0;\r
150 \r
151 /*-----------------------------------------------------------*/\r
152 \r
153 void vStartMultiEventTasks( void )\r
154 {\r
155         /* Create the queue to be used for all the communications. */\r
156         xQueue = xQueueCreate( evtQUEUE_LENGTH, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );\r
157 \r
158         /* Start the controlling task.  This has the idle priority to ensure it is\r
159         always preempted by the event tasks. */\r
160         xTaskCreate( prvEventControllerTask, "EvntCTRL", evtSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
161 \r
162         /* Start the four event tasks.  Note that two have priority 3, one \r
163         priority 2 and the other priority 1. */\r
164         xTaskCreate( prvMultiEventTask, "Event0", evtSTACK_SIZE, ( void * ) &( xTaskCounters[ 0 ] ), 1, &( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] ) );\r
165         xTaskCreate( prvMultiEventTask, "Event1", evtSTACK_SIZE, ( void * ) &( xTaskCounters[ 1 ] ), 2, &( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] ) );\r
166         xTaskCreate( prvMultiEventTask, "Event2", evtSTACK_SIZE, ( void * ) &( xTaskCounters[ 2 ] ), 3, &( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] ) );\r
167         xTaskCreate( prvMultiEventTask, "Event3", evtSTACK_SIZE, ( void * ) &( xTaskCounters[ 3 ] ), 3, &( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_2 ] ) );\r
168 }\r
169 /*-----------------------------------------------------------*/\r
170 \r
171 static void prvMultiEventTask( void *pvParameters )\r
172 {\r
173 portBASE_TYPE *pxCounter;\r
174 unsigned portBASE_TYPE uxDummy;\r
175 const portCHAR * const pcTaskStartMsg = "Multi event task started.\r\n";\r
176 \r
177         /* The variable this task will increment is passed in as a parameter. */\r
178         pxCounter = ( portBASE_TYPE * ) pvParameters;\r
179 \r
180         vPrintDisplayMessage( &pcTaskStartMsg );\r
181 \r
182         for( ;; )\r
183         {\r
184                 /* Block on the queue. */\r
185                 if( xQueueReceive( xQueue, &uxDummy, portMAX_DELAY ) )\r
186                 {\r
187                         /* We unblocked by reading the queue - so simply increment\r
188                         the counter specific to this task instance. */\r
189                         ( *pxCounter )++;\r
190                 }\r
191                 else\r
192                 {\r
193                         xHealthStatus = pdFAIL;\r
194                 }\r
195         }\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 static void prvEventControllerTask( void *pvParameters )\r
200 {\r
201 const portCHAR * const pcTaskStartMsg = "Multi event controller task started.\r\n";\r
202 portBASE_TYPE xDummy = 0;\r
203 \r
204         /* Just to stop warnings. */\r
205         ( void ) pvParameters;\r
206 \r
207         vPrintDisplayMessage( &pcTaskStartMsg );\r
208 \r
209         for( ;; )\r
210         {\r
211                 /* All tasks are blocked on the queue.  When a message is posted one of\r
212                 the two tasks that share the highest priority should unblock to read\r
213                 the queue.  The next message written should unblock the other task with\r
214                 the same high priority, and so on in order.   No other task should \r
215                 unblock to read data as they have lower priorities. */\r
216 \r
217                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );\r
218                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_2, 1 );\r
219                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );\r
220                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_2, 1 );\r
221                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );\r
222 \r
223                 /* For the rest of these tests we don't need the second 'highest' \r
224                 priority task - so it is suspended. */\r
225                 vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_2 ] );\r
226 \r
227 \r
228 \r
229                 /* Now suspend the other highest priority task.  The medium priority \r
230                 task will then be the task with the highest priority that remains \r
231                 blocked on the queue. */\r
232                 vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
233                 \r
234                 /* This time, when we post onto the queue we will expect the medium\r
235                 priority task to unblock and preempt us. */\r
236                 prvCheckTaskCounters( evtMEDIUM_PRIORITY_INDEX, 1 );\r
237 \r
238                 /* Now try resuming the highest priority task while the scheduler is\r
239                 suspended.  The task should start executing as soon as the scheduler\r
240                 is resumed - therefore when we post to the queue again, the highest\r
241                 priority task should again preempt us. */\r
242                 vTaskSuspendAll();\r
243                         vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
244                 xTaskResumeAll();\r
245                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );\r
246                 \r
247                 /* Now we are going to suspend the high and medium priority tasks.  The\r
248                 low priority task should then preempt us.  Again the task suspension is \r
249                 done with the whole scheduler suspended just for test purposes. */\r
250                 vTaskSuspendAll();\r
251                         vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
252                         vTaskSuspend( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] );\r
253                 xTaskResumeAll();\r
254                 prvCheckTaskCounters( evtLOWEST_PRIORITY_INDEX, 1 );\r
255                 \r
256                 /* Do the same basic test another few times - selectively suspending\r
257                 and resuming tasks and each time calling prvCheckTaskCounters() passing\r
258                 to the function the number of the task we expected to be unblocked by \r
259                 the     post. */\r
260 \r
261                 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
262                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );\r
263                 \r
264                 vTaskSuspendAll(); /* Just for test. */\r
265                         vTaskSuspendAll(); /* Just for test. */\r
266                                 vTaskSuspendAll(); /* Just for even more test. */\r
267                                         vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
268                                 xTaskResumeAll();\r
269                         xTaskResumeAll();\r
270                 xTaskResumeAll();\r
271                 prvCheckTaskCounters( evtLOWEST_PRIORITY_INDEX, 1 );\r
272                 \r
273                 vTaskResume( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] );\r
274                 prvCheckTaskCounters( evtMEDIUM_PRIORITY_INDEX, 1 );\r
275                 \r
276                 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
277                 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );\r
278 \r
279                 /* Now a slight change, first suspend all tasks. */\r
280                 vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
281                 vTaskSuspend( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] );\r
282                 vTaskSuspend( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );\r
283                 \r
284                 /* Now when we resume the low priority task and write to the queue 3 \r
285                 times.  We expect the low priority task to service the queue three\r
286                 times. */\r
287                 vTaskResume( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );\r
288                 prvCheckTaskCounters( evtLOWEST_PRIORITY_INDEX, evtQUEUE_LENGTH );\r
289                 \r
290                 /* Again suspend all tasks (only the low priority task is not suspended\r
291                 already). */\r
292                 vTaskSuspend( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );\r
293                 \r
294                 /* This time we are going to suspend the scheduler, resume the low\r
295                 priority task, then resume the high priority task.  In this state we\r
296                 will write to the queue three times.  When the scheduler is resumed\r
297                 we expect the high priority task to service all three messages. */\r
298                 vTaskSuspendAll();\r
299                 {\r
300                         vTaskResume( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );\r
301                         vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );\r
302                         \r
303                         for( xDummy = 0; xDummy < evtQUEUE_LENGTH; xDummy++ )\r
304                         {\r
305                                 if( xQueueSend( xQueue, &xDummy, evtNO_DELAY ) != pdTRUE )\r
306                                 {\r
307                                         xHealthStatus = pdFAIL;\r
308                                 }\r
309                         }                       \r
310                         \r
311                         /* The queue should not have been serviced yet!.  The scheduler\r
312                         is still suspended. */\r
313                         if( memcmp( ( void * ) xExpectedTaskCounters, ( void * ) xTaskCounters, sizeof( xExpectedTaskCounters ) ) )\r
314                         {\r
315                                 xHealthStatus = pdFAIL;\r
316                         }\r
317                 }\r
318                 xTaskResumeAll();\r
319 \r
320                 /* We should have been preempted by resuming the scheduler - so by the\r
321                 time we are running again we expect the high priority task to have \r
322                 removed three items from the queue. */\r
323                 xExpectedTaskCounters[ evtHIGHEST_PRIORITY_INDEX_1 ] += evtQUEUE_LENGTH;\r
324                 if( memcmp( ( void * ) xExpectedTaskCounters, ( void * ) xTaskCounters, sizeof( xExpectedTaskCounters ) ) )\r
325                 {\r
326                         xHealthStatus = pdFAIL;\r
327                 }\r
328                 \r
329                 /* The medium priority and second high priority tasks are still \r
330                 suspended.  Make sure to resume them before starting again. */\r
331                 vTaskResume( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] );\r
332                 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_2 ] );\r
333 \r
334                 /* Just keep incrementing to show the task is still executing. */\r
335                 xCheckVariable++;\r
336         }\r
337 }\r
338 /*-----------------------------------------------------------*/\r
339 \r
340 static void prvCheckTaskCounters( portBASE_TYPE xExpectedTask, portBASE_TYPE xIncrement )\r
341 {\r
342 portBASE_TYPE xDummy = 0;\r
343 \r
344         /* Write to the queue the requested number of times.  The data written is\r
345         not important. */\r
346         for( xDummy = 0; xDummy < xIncrement; xDummy++ )\r
347         {\r
348                 if( xQueueSend( xQueue, &xDummy, evtNO_DELAY ) != pdTRUE )\r
349                 {\r
350                         /* Did not expect to ever find the queue full. */\r
351                         xHealthStatus = pdFAIL;\r
352                 }\r
353         }\r
354 \r
355         /* All the tasks blocked on the queue have a priority higher than the \r
356         controlling task.  Writing to the queue will therefore have caused this\r
357         task to be preempted.  By the time this line executes the event task will\r
358         have executed and incremented its counter.  Increment the expected counter\r
359         to the same value. */\r
360         ( xExpectedTaskCounters[ xExpectedTask ] ) += xIncrement;\r
361 \r
362         /* Check the actual counts and expected counts really are the same. */\r
363         if( memcmp( ( void * ) xExpectedTaskCounters, ( void * ) xTaskCounters, sizeof( xExpectedTaskCounters ) ) )\r
364         {\r
365                 /* The counters were not the same.  This means a task we did not expect\r
366                 to unblock actually did unblock. */\r
367                 xHealthStatus = pdFAIL;\r
368         }\r
369 }\r
370 /*-----------------------------------------------------------*/\r
371 \r
372 portBASE_TYPE xAreMultiEventTasksStillRunning( void )\r
373 {\r
374 static portBASE_TYPE xPreviousCheckVariable = 0;\r
375 \r
376         /* Called externally to periodically check that this test is still\r
377         operational. */\r
378 \r
379         if( xPreviousCheckVariable == xCheckVariable )\r
380         {\r
381                 xHealthStatus = pdFAIL;\r
382         }\r
383         \r
384         xPreviousCheckVariable = xCheckVariable;\r
385         \r
386         return xHealthStatus;   \r
387 }\r
388 \r
389 \r