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