2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
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
37 ***************************************************************************
\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
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
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
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
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
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
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
71 * This file exercises the event mechanism whereby more than one task is
\r
72 * blocked waiting for the same event.
\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
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
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
90 /* Standard includes. */
\r
95 /* Scheduler include files. */
\r
96 #include "FreeRTOS.h"
\r
100 /* Demo program include files. */
\r
101 #include "mevents.h"
\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
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
117 /* Each event task increments one of these counters each time it reads data
\r
119 static volatile portBASE_TYPE xTaskCounters[ evtNUM_TASKS ] = { 0, 0, 0, 0 };
\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
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
131 /* Handles to the four event tasks. These are required to suspend and resume
\r
133 static TaskHandle_t xCreatedTasks[ evtNUM_TASKS ];
\r
135 /* The single queue onto which the controlling task posts, and the four event
\r
137 static QueueHandle_t xQueue;
\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
144 /*-----------------------------------------------------------*/
\r
146 /* Function that implements the event task. This is created four times. */
\r
147 static void prvMultiEventTask( void *pvParameters );
\r
149 /* Function that implements the controlling task. */
\r
150 static void prvEventControllerTask( void *pvParameters );
\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
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
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
164 @param xIncrement The number of items that should be written to the queue.
\r
166 static void prvCheckTaskCounters( portBASE_TYPE xExpectedTask, portBASE_TYPE xIncrement );
\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
172 /*-----------------------------------------------------------*/
\r
174 void vStartMultiEventTasks( void )
\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
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
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
190 /*-----------------------------------------------------------*/
\r
192 static void prvMultiEventTask( void *pvParameters )
\r
194 portBASE_TYPE *pxCounter;
\r
195 unsigned portBASE_TYPE uxDummy;
\r
196 const char * const pcTaskStartMsg = "Multi event task started.\r\n";
\r
198 /* The variable this task will increment is passed in as a parameter. */
\r
199 pxCounter = ( portBASE_TYPE * ) pvParameters;
\r
201 vPrintDisplayMessage( &pcTaskStartMsg );
\r
205 /* Block on the queue. */
\r
206 if( xQueueReceive( xQueue, &uxDummy, portMAX_DELAY ) )
\r
208 /* We unblocked by reading the queue - so simply increment
\r
209 the counter specific to this task instance. */
\r
214 xHealthStatus = pdFAIL;
\r
218 /*-----------------------------------------------------------*/
\r
220 static void prvEventControllerTask( void *pvParameters )
\r
222 const char * const pcTaskStartMsg = "Multi event controller task started.\r\n";
\r
223 portBASE_TYPE xDummy = 0;
\r
225 /* Just to stop warnings. */
\r
226 ( void ) pvParameters;
\r
228 vPrintDisplayMessage( &pcTaskStartMsg );
\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
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
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
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
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
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
264 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );
\r
266 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );
\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
272 vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );
\r
273 vTaskSuspend( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] );
\r
275 prvCheckTaskCounters( evtLOWEST_PRIORITY_INDEX, 1 );
\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
282 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );
\r
283 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );
\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
292 prvCheckTaskCounters( evtLOWEST_PRIORITY_INDEX, 1 );
\r
294 vTaskResume( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] );
\r
295 prvCheckTaskCounters( evtMEDIUM_PRIORITY_INDEX, 1 );
\r
297 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );
\r
298 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );
\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
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
308 vTaskResume( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );
\r
309 prvCheckTaskCounters( evtLOWEST_PRIORITY_INDEX, evtQUEUE_LENGTH );
\r
311 /* Again suspend all tasks (only the low priority task is not suspended
\r
313 vTaskSuspend( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );
\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
321 vTaskResume( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );
\r
322 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );
\r
324 for( xDummy = 0; xDummy < evtQUEUE_LENGTH; xDummy++ )
\r
326 if( xQueueSend( xQueue, &xDummy, evtNO_DELAY ) != pdTRUE )
\r
328 xHealthStatus = pdFAIL;
\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
336 xHealthStatus = pdFAIL;
\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
347 xHealthStatus = pdFAIL;
\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
355 /* Just keep incrementing to show the task is still executing. */
\r
359 /*-----------------------------------------------------------*/
\r
361 static void prvCheckTaskCounters( portBASE_TYPE xExpectedTask, portBASE_TYPE xIncrement )
\r
363 portBASE_TYPE xDummy = 0;
\r
365 /* Write to the queue the requested number of times. The data written is
\r
367 for( xDummy = 0; xDummy < xIncrement; xDummy++ )
\r
369 if( xQueueSend( xQueue, &xDummy, evtNO_DELAY ) != pdTRUE )
\r
371 /* Did not expect to ever find the queue full. */
\r
372 xHealthStatus = pdFAIL;
\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
383 /* Check the actual counts and expected counts really are the same. */
\r
384 if( memcmp( ( void * ) xExpectedTaskCounters, ( void * ) xTaskCounters, sizeof( xExpectedTaskCounters ) ) )
\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
391 /*-----------------------------------------------------------*/
\r
393 portBASE_TYPE xAreMultiEventTasksStillRunning( void )
\r
395 static portBASE_TYPE xPreviousCheckVariable = 0;
\r
397 /* Called externally to periodically check that this test is still
\r
400 if( xPreviousCheckVariable == xCheckVariable )
\r
402 xHealthStatus = pdFAIL;
\r
405 xPreviousCheckVariable = xCheckVariable;
\r
407 return xHealthStatus;
\r