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