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