2 FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 * This file exercises the event mechanism whereby more than one task is
\r
77 * blocked waiting for the same event.
\r
79 * The demo creates five tasks - four 'event' tasks, and a controlling task.
\r
80 * The event tasks have various different priorities and all block on reading
\r
81 * the same queue. The controlling task writes data to the queue, then checks
\r
82 * to see which of the event tasks read the data from the queue. The
\r
83 * controlling task has the lowest priority of all the tasks so is guaranteed
\r
84 * to always get preempted immediately upon writing to the queue.
\r
86 * By selectively suspending and resuming the event tasks the controlling task
\r
87 * can check that the highest priority task that is blocked on the queue is the
\r
88 * task that reads the posted data from the queue.
\r
90 * Two of the event tasks share the same priority. When neither of these tasks
\r
91 * are suspended they should alternate - one reading one message from the queue,
\r
92 * the other the next message, etc.
\r
95 /* Standard includes. */
\r
100 /* Scheduler include files. */
\r
101 #include "FreeRTOS.h"
\r
105 /* Demo program include files. */
\r
106 #include "mevents.h"
\r
109 /* Demo specific constants. */
\r
110 #define evtSTACK_SIZE ( ( unsigned portBASE_TYPE ) configMINIMAL_STACK_SIZE )
\r
111 #define evtNUM_TASKS ( 4 )
\r
112 #define evtQUEUE_LENGTH ( ( unsigned portBASE_TYPE ) 3 )
\r
113 #define evtNO_DELAY 0
\r
115 /* Just indexes used to uniquely identify the tasks. Note that two tasks are
\r
116 'highest' priority. */
\r
117 #define evtHIGHEST_PRIORITY_INDEX_2 3
\r
118 #define evtHIGHEST_PRIORITY_INDEX_1 2
\r
119 #define evtMEDIUM_PRIORITY_INDEX 1
\r
120 #define evtLOWEST_PRIORITY_INDEX 0
\r
122 /* Each event task increments one of these counters each time it reads data
\r
124 static volatile portBASE_TYPE xTaskCounters[ evtNUM_TASKS ] = { 0, 0, 0, 0 };
\r
126 /* Each time the controlling task posts onto the queue it increments the
\r
127 expected count of the task that it expected to read the data from the queue
\r
128 (i.e. the task with the highest priority that should be blocked on the queue).
\r
130 xExpectedTaskCounters are incremented from the controlling task, and
\r
131 xTaskCounters are incremented from the individual event tasks - therefore
\r
132 comparing xTaskCounters to xExpectedTaskCounters shows whether or not the
\r
133 correct task was unblocked by the post. */
\r
134 static portBASE_TYPE xExpectedTaskCounters[ evtNUM_TASKS ] = { 0, 0, 0, 0 };
\r
136 /* Handles to the four event tasks. These are required to suspend and resume
\r
138 static xTaskHandle xCreatedTasks[ evtNUM_TASKS ];
\r
140 /* The single queue onto which the controlling task posts, and the four event
\r
142 static xQueueHandle xQueue;
\r
144 /* Flag used to indicate whether or not an error has occurred at any time.
\r
145 An error is either the queue being full when not expected, or an unexpected
\r
146 task reading data from the queue. */
\r
147 static portBASE_TYPE xHealthStatus = pdPASS;
\r
149 /*-----------------------------------------------------------*/
\r
151 /* Function that implements the event task. This is created four times. */
\r
152 static void prvMultiEventTask( void *pvParameters );
\r
154 /* Function that implements the controlling task. */
\r
155 static void prvEventControllerTask( void *pvParameters );
\r
157 /* This is a utility function that posts data to the queue, then compares
\r
158 xExpectedTaskCounters with xTaskCounters to ensure everything worked as
\r
161 The event tasks all have higher priorities the controlling task. Therefore
\r
162 the controlling task will always get preempted between writhing to the queue
\r
163 and checking the task counters.
\r
165 @param xExpectedTask The index to the task that the controlling task thinks
\r
166 should be the highest priority task waiting for data, and
\r
167 therefore the task that will unblock.
\r
169 @param xIncrement The number of items that should be written to the queue.
\r
171 static void prvCheckTaskCounters( portBASE_TYPE xExpectedTask, portBASE_TYPE xIncrement );
\r
173 /* This is just incremented each cycle of the controlling tasks function so
\r
174 the main application can ensure the test is still running. */
\r
175 static portBASE_TYPE xCheckVariable = 0;
\r
177 /*-----------------------------------------------------------*/
\r
179 void vStartMultiEventTasks( void )
\r
181 /* Create the queue to be used for all the communications. */
\r
182 xQueue = xQueueCreate( evtQUEUE_LENGTH, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );
\r
184 /* Start the controlling task. This has the idle priority to ensure it is
\r
185 always preempted by the event tasks. */
\r
186 xTaskCreate( prvEventControllerTask, "EvntCTRL", evtSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
188 /* Start the four event tasks. Note that two have priority 3, one
\r
189 priority 2 and the other priority 1. */
\r
190 xTaskCreate( prvMultiEventTask, "Event0", evtSTACK_SIZE, ( void * ) &( xTaskCounters[ 0 ] ), 1, &( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] ) );
\r
191 xTaskCreate( prvMultiEventTask, "Event1", evtSTACK_SIZE, ( void * ) &( xTaskCounters[ 1 ] ), 2, &( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] ) );
\r
192 xTaskCreate( prvMultiEventTask, "Event2", evtSTACK_SIZE, ( void * ) &( xTaskCounters[ 2 ] ), 3, &( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] ) );
\r
193 xTaskCreate( prvMultiEventTask, "Event3", evtSTACK_SIZE, ( void * ) &( xTaskCounters[ 3 ] ), 3, &( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_2 ] ) );
\r
195 /*-----------------------------------------------------------*/
\r
197 static void prvMultiEventTask( void *pvParameters )
\r
199 portBASE_TYPE *pxCounter;
\r
200 unsigned portBASE_TYPE uxDummy;
\r
201 const char * const pcTaskStartMsg = "Multi event task started.\r\n";
\r
203 /* The variable this task will increment is passed in as a parameter. */
\r
204 pxCounter = ( portBASE_TYPE * ) pvParameters;
\r
206 vPrintDisplayMessage( &pcTaskStartMsg );
\r
210 /* Block on the queue. */
\r
211 if( xQueueReceive( xQueue, &uxDummy, portMAX_DELAY ) )
\r
213 /* We unblocked by reading the queue - so simply increment
\r
214 the counter specific to this task instance. */
\r
219 xHealthStatus = pdFAIL;
\r
223 /*-----------------------------------------------------------*/
\r
225 static void prvEventControllerTask( void *pvParameters )
\r
227 const char * const pcTaskStartMsg = "Multi event controller task started.\r\n";
\r
228 portBASE_TYPE xDummy = 0;
\r
230 /* Just to stop warnings. */
\r
231 ( void ) pvParameters;
\r
233 vPrintDisplayMessage( &pcTaskStartMsg );
\r
237 /* All tasks are blocked on the queue. When a message is posted one of
\r
238 the two tasks that share the highest priority should unblock to read
\r
239 the queue. The next message written should unblock the other task with
\r
240 the same high priority, and so on in order. No other task should
\r
241 unblock to read data as they have lower priorities. */
\r
243 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );
\r
244 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_2, 1 );
\r
245 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );
\r
246 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_2, 1 );
\r
247 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );
\r
249 /* For the rest of these tests we don't need the second 'highest'
\r
250 priority task - so it is suspended. */
\r
251 vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_2 ] );
\r
255 /* Now suspend the other highest priority task. The medium priority
\r
256 task will then be the task with the highest priority that remains
\r
257 blocked on the queue. */
\r
258 vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );
\r
260 /* This time, when we post onto the queue we will expect the medium
\r
261 priority task to unblock and preempt us. */
\r
262 prvCheckTaskCounters( evtMEDIUM_PRIORITY_INDEX, 1 );
\r
264 /* Now try resuming the highest priority task while the scheduler is
\r
265 suspended. The task should start executing as soon as the scheduler
\r
266 is resumed - therefore when we post to the queue again, the highest
\r
267 priority task should again preempt us. */
\r
269 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );
\r
271 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );
\r
273 /* Now we are going to suspend the high and medium priority tasks. The
\r
274 low priority task should then preempt us. Again the task suspension is
\r
275 done with the whole scheduler suspended just for test purposes. */
\r
277 vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );
\r
278 vTaskSuspend( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] );
\r
280 prvCheckTaskCounters( evtLOWEST_PRIORITY_INDEX, 1 );
\r
282 /* Do the same basic test another few times - selectively suspending
\r
283 and resuming tasks and each time calling prvCheckTaskCounters() passing
\r
284 to the function the number of the task we expected to be unblocked by
\r
287 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );
\r
288 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );
\r
290 vTaskSuspendAll(); /* Just for test. */
\r
291 vTaskSuspendAll(); /* Just for test. */
\r
292 vTaskSuspendAll(); /* Just for even more test. */
\r
293 vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );
\r
297 prvCheckTaskCounters( evtLOWEST_PRIORITY_INDEX, 1 );
\r
299 vTaskResume( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] );
\r
300 prvCheckTaskCounters( evtMEDIUM_PRIORITY_INDEX, 1 );
\r
302 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );
\r
303 prvCheckTaskCounters( evtHIGHEST_PRIORITY_INDEX_1, 1 );
\r
305 /* Now a slight change, first suspend all tasks. */
\r
306 vTaskSuspend( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );
\r
307 vTaskSuspend( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] );
\r
308 vTaskSuspend( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );
\r
310 /* Now when we resume the low priority task and write to the queue 3
\r
311 times. We expect the low priority task to service the queue three
\r
313 vTaskResume( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );
\r
314 prvCheckTaskCounters( evtLOWEST_PRIORITY_INDEX, evtQUEUE_LENGTH );
\r
316 /* Again suspend all tasks (only the low priority task is not suspended
\r
318 vTaskSuspend( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );
\r
320 /* This time we are going to suspend the scheduler, resume the low
\r
321 priority task, then resume the high priority task. In this state we
\r
322 will write to the queue three times. When the scheduler is resumed
\r
323 we expect the high priority task to service all three messages. */
\r
326 vTaskResume( xCreatedTasks[ evtLOWEST_PRIORITY_INDEX ] );
\r
327 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_1 ] );
\r
329 for( xDummy = 0; xDummy < evtQUEUE_LENGTH; xDummy++ )
\r
331 if( xQueueSend( xQueue, &xDummy, evtNO_DELAY ) != pdTRUE )
\r
333 xHealthStatus = pdFAIL;
\r
337 /* The queue should not have been serviced yet!. The scheduler
\r
338 is still suspended. */
\r
339 if( memcmp( ( void * ) xExpectedTaskCounters, ( void * ) xTaskCounters, sizeof( xExpectedTaskCounters ) ) )
\r
341 xHealthStatus = pdFAIL;
\r
346 /* We should have been preempted by resuming the scheduler - so by the
\r
347 time we are running again we expect the high priority task to have
\r
348 removed three items from the queue. */
\r
349 xExpectedTaskCounters[ evtHIGHEST_PRIORITY_INDEX_1 ] += evtQUEUE_LENGTH;
\r
350 if( memcmp( ( void * ) xExpectedTaskCounters, ( void * ) xTaskCounters, sizeof( xExpectedTaskCounters ) ) )
\r
352 xHealthStatus = pdFAIL;
\r
355 /* The medium priority and second high priority tasks are still
\r
356 suspended. Make sure to resume them before starting again. */
\r
357 vTaskResume( xCreatedTasks[ evtMEDIUM_PRIORITY_INDEX ] );
\r
358 vTaskResume( xCreatedTasks[ evtHIGHEST_PRIORITY_INDEX_2 ] );
\r
360 /* Just keep incrementing to show the task is still executing. */
\r
364 /*-----------------------------------------------------------*/
\r
366 static void prvCheckTaskCounters( portBASE_TYPE xExpectedTask, portBASE_TYPE xIncrement )
\r
368 portBASE_TYPE xDummy = 0;
\r
370 /* Write to the queue the requested number of times. The data written is
\r
372 for( xDummy = 0; xDummy < xIncrement; xDummy++ )
\r
374 if( xQueueSend( xQueue, &xDummy, evtNO_DELAY ) != pdTRUE )
\r
376 /* Did not expect to ever find the queue full. */
\r
377 xHealthStatus = pdFAIL;
\r
381 /* All the tasks blocked on the queue have a priority higher than the
\r
382 controlling task. Writing to the queue will therefore have caused this
\r
383 task to be preempted. By the time this line executes the event task will
\r
384 have executed and incremented its counter. Increment the expected counter
\r
385 to the same value. */
\r
386 ( xExpectedTaskCounters[ xExpectedTask ] ) += xIncrement;
\r
388 /* Check the actual counts and expected counts really are the same. */
\r
389 if( memcmp( ( void * ) xExpectedTaskCounters, ( void * ) xTaskCounters, sizeof( xExpectedTaskCounters ) ) )
\r
391 /* The counters were not the same. This means a task we did not expect
\r
392 to unblock actually did unblock. */
\r
393 xHealthStatus = pdFAIL;
\r
396 /*-----------------------------------------------------------*/
\r
398 portBASE_TYPE xAreMultiEventTasksStillRunning( void )
\r
400 static portBASE_TYPE xPreviousCheckVariable = 0;
\r
402 /* Called externally to periodically check that this test is still
\r
405 if( xPreviousCheckVariable == xCheckVariable )
\r
407 xHealthStatus = pdFAIL;
\r
410 xPreviousCheckVariable = xCheckVariable;
\r
412 return xHealthStatus;
\r