2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * The first test creates three tasks - two counter tasks (one continuous count
\r
67 * and one limited count) and one controller. A "count" variable is shared
\r
68 * between all three tasks. The two counter tasks should never be in a "ready"
\r
69 * state at the same time. The controller task runs at the same priority as
\r
70 * the continuous count task, and at a lower priority than the limited count
\r
73 * One counter task loops indefinitely, incrementing the shared count variable
\r
74 * on each iteration. To ensure it has exclusive access to the variable it
\r
75 * raises it's priority above that of the controller task before each
\r
76 * increment, lowering it again to it's original priority before starting the
\r
79 * The other counter task increments the shared count variable on each
\r
80 * iteration of it's loop until the count has reached a limit of 0xff - at
\r
81 * which point it suspends itself. It will not start a new loop until the
\r
82 * controller task has made it "ready" again by calling vTaskResume ().
\r
83 * This second counter task operates at a higher priority than controller
\r
84 * task so does not need to worry about mutual exclusion of the counter
\r
87 * The controller task is in two sections. The first section controls and
\r
88 * monitors the continuous count task. When this section is operational the
\r
89 * limited count task is suspended. Likewise, the second section controls
\r
90 * and monitors the limited count task. When this section is operational the
\r
91 * continuous count task is suspended.
\r
93 * In the first section the controller task first takes a copy of the shared
\r
94 * count variable. To ensure mutual exclusion on the count variable it
\r
95 * suspends the continuous count task, resuming it again when the copy has been
\r
96 * taken. The controller task then sleeps for a fixed period - during which
\r
97 * the continuous count task will execute and increment the shared variable.
\r
98 * When the controller task wakes it checks that the continuous count task
\r
99 * has executed by comparing the copy of the shared variable with its current
\r
100 * value. This time, to ensure mutual exclusion, the scheduler itself is
\r
101 * suspended with a call to vTaskSuspendAll (). This is for demonstration
\r
102 * purposes only and is not a recommended technique due to its inefficiency.
\r
104 * After a fixed number of iterations the controller task suspends the
\r
105 * continuous count task, and moves on to its second section.
\r
107 * At the start of the second section the shared variable is cleared to zero.
\r
108 * The limited count task is then woken from it's suspension by a call to
\r
109 * vTaskResume (). As this counter task operates at a higher priority than
\r
110 * the controller task the controller task should not run again until the
\r
111 * shared variable has been counted up to the limited value causing the counter
\r
112 * task to suspend itself. The next line after vTaskResume () is therefore
\r
113 * a check on the shared variable to ensure everything is as expected.
\r
116 * The second test consists of a couple of very simple tasks that post onto a
\r
117 * queue while the scheduler is suspended. This test was added to test parts
\r
118 * of the scheduler not exercised by the first test.
\r
121 * The final set of two tasks implements a third test. This simply raises the
\r
122 * priority of a task while the scheduler is suspended. Again this test was
\r
123 * added to exercise parts of the code not covered by the first test.
\r
125 * \page Priorities dynamic.c
\r
126 * \ingroup DemoFiles
\r
131 Changes from V2.0.0
\r
133 + Delay periods are now specified using variables and constants of
\r
134 portTickType rather than unsigned long.
\r
135 + Added a second, simple test that uses the functions
\r
136 vQueueReceiveWhenSuspendedTask() and vQueueSendWhenSuspendedTask().
\r
138 Changes from V3.1.1
\r
140 + Added a third simple test that uses the vTaskPrioritySet() function
\r
141 while the scheduler is suspended.
\r
142 + Modified the controller task slightly to test the calling of
\r
143 vTaskResumeAll() while the scheduler is suspended.
\r
146 #include <stdlib.h>
\r
148 /* Scheduler include files. */
\r
149 #include "FreeRTOS.h"
\r
151 #include "semphr.h"
\r
153 /* Demo app include files. */
\r
154 #include "dynamic.h"
\r
157 /* Function that implements the "limited count" task as described above. */
\r
158 static void vLimitedIncrementTask( void * pvParameters );
\r
160 /* Function that implements the "continuous count" task as described above. */
\r
161 static void vContinuousIncrementTask( void * pvParameters );
\r
163 /* Function that implements the controller task as described above. */
\r
164 static void vCounterControlTask( void * pvParameters );
\r
166 /* The simple test functions that check sending and receiving while the
\r
167 scheduler is suspended. */
\r
168 static void vQueueReceiveWhenSuspendedTask( void *pvParameters );
\r
169 static void vQueueSendWhenSuspendedTask( void *pvParameters );
\r
171 /* The simple test functions that check raising and lowering of task priorities
\r
172 while the scheduler is suspended. */
\r
173 static void prvChangePriorityWhenSuspendedTask( void *pvParameters );
\r
174 static void prvChangePriorityHelperTask( void *pvParameters );
\r
177 /* Demo task specific constants. */
\r
178 #define priSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
179 #define priSLEEP_TIME ( ( portTickType ) 50 )
\r
180 #define priLOOPS ( 5 )
\r
181 #define priMAX_COUNT ( ( unsigned long ) 0xff )
\r
182 #define priNO_BLOCK ( ( portTickType ) 0 )
\r
183 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
185 /*-----------------------------------------------------------*/
\r
187 /* Handles to the two counter tasks. These could be passed in as parameters
\r
188 to the controller task to prevent them having to be file scope. */
\r
189 static xTaskHandle xContinuousIncrementHandle, xLimitedIncrementHandle, xChangePriorityWhenSuspendedHandle;
\r
191 /* The shared counter variable. This is passed in as a parameter to the two
\r
192 counter variables for demonstration purposes. */
\r
193 static unsigned long ulCounter;
\r
195 /* Variable used in a similar way by the test that checks the raising and
\r
196 lowering of task priorities while the scheduler is suspended. */
\r
197 static unsigned long ulPrioritySetCounter;
\r
199 /* Variables used to check that the tasks are still operating without error.
\r
200 Each complete iteration of the controller task increments this variable
\r
201 provided no errors have been found. The variable maintaining the same value
\r
202 is therefore indication of an error. */
\r
203 static unsigned short usCheckVariable = ( unsigned short ) 0;
\r
204 static portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
205 static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
206 static portBASE_TYPE xPriorityRaiseWhenSuspendedError = pdFALSE;
\r
208 /* Queue used by the second test. */
\r
209 xQueueHandle xSuspendedTestQueue;
\r
211 /*-----------------------------------------------------------*/
\r
213 * Start the seven tasks as described at the top of the file.
\r
214 * Note that the limited count task is given a higher priority.
\r
216 void vStartDynamicPriorityTasks( void )
\r
218 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
219 xTaskCreate( vContinuousIncrementTask, "CONT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
220 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
221 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
222 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_SEND", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
223 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RECV", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
224 xTaskCreate( prvChangePriorityWhenSuspendedTask, "1st_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
225 xTaskCreate( prvChangePriorityHelperTask, "2nd_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xChangePriorityWhenSuspendedHandle );
\r
227 /*-----------------------------------------------------------*/
\r
230 * Just loops around incrementing the shared variable until the limit has been
\r
231 * reached. Once the limit has been reached it suspends itself.
\r
233 static void vLimitedIncrementTask( void * pvParameters )
\r
235 unsigned long *pulCounter;
\r
237 /* Take a pointer to the shared variable from the parameters passed into
\r
239 pulCounter = ( unsigned long * ) pvParameters;
\r
241 /* This will run before the control task, so the first thing it does is
\r
242 suspend - the control task will resume it when ready. */
\r
243 vTaskSuspend( NULL );
\r
247 /* Just count up to a value then suspend. */
\r
248 ( *pulCounter )++;
\r
250 if( *pulCounter >= priMAX_COUNT )
\r
252 vTaskSuspend( NULL );
\r
256 /*-----------------------------------------------------------*/
\r
259 * Just keep counting the shared variable up. The control task will suspend
\r
260 * this task when it wants.
\r
262 static void vContinuousIncrementTask( void * pvParameters )
\r
264 unsigned long *pulCounter;
\r
265 unsigned portBASE_TYPE uxOurPriority;
\r
267 /* Take a pointer to the shared variable from the parameters passed into
\r
269 pulCounter = ( unsigned long * ) pvParameters;
\r
271 /* Query our priority so we can raise it when exclusive access to the
\r
272 shared variable is required. */
\r
273 uxOurPriority = uxTaskPriorityGet( NULL );
\r
277 /* Raise our priority above the controller task to ensure a context
\r
278 switch does not occur while we are accessing this variable. */
\r
279 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
280 ( *pulCounter )++;
\r
281 vTaskPrioritySet( NULL, uxOurPriority );
\r
283 #if configUSE_PREEMPTION == 0
\r
288 /*-----------------------------------------------------------*/
\r
291 * Controller task as described above.
\r
293 static void vCounterControlTask( void * pvParameters )
\r
295 unsigned long ulLastCounter;
\r
297 short sError = pdFALSE;
\r
298 const char * const pcTaskStartMsg = "Priority manipulation tasks started.\r\n";
\r
299 const char * const pcTaskFailMsg = "Priority manipulation Task Failed\r\n";
\r
301 /* Just to stop warning messages. */
\r
302 ( void ) pvParameters;
\r
304 /* Queue a message for printing to say the task has started. */
\r
305 vPrintDisplayMessage( &pcTaskStartMsg );
\r
309 /* Start with the counter at zero. */
\r
310 ulCounter = ( unsigned long ) 0;
\r
312 /* First section : */
\r
314 /* Check the continuous count task is running. */
\r
315 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
317 /* Suspend the continuous count task so we can take a mirror of the
\r
318 shared variable without risk of corruption. */
\r
319 vTaskSuspend( xContinuousIncrementHandle );
\r
320 ulLastCounter = ulCounter;
\r
321 vTaskResume( xContinuousIncrementHandle );
\r
323 /* Now delay to ensure the other task has processor time. */
\r
324 vTaskDelay( priSLEEP_TIME );
\r
326 /* Check the shared variable again. This time to ensure mutual
\r
327 exclusion the whole scheduler will be locked. This is just for
\r
331 if( ulLastCounter == ulCounter )
\r
333 /* The shared variable has not changed. There is a problem
\r
334 with the continuous count task so flag an error. */
\r
337 vPrintDisplayMessage( &pcTaskFailMsg );
\r
345 /* Second section: */
\r
347 /* Suspend the continuous counter task so it stops accessing the shared variable. */
\r
348 vTaskSuspend( xContinuousIncrementHandle );
\r
350 /* Reset the variable. */
\r
351 ulCounter = ( unsigned long ) 0;
\r
353 /* Resume the limited count task which has a higher priority than us.
\r
354 We should therefore not return from this call until the limited count
\r
355 task has suspended itself with a known value in the counter variable.
\r
356 The scheduler suspension is not necessary but is included for test
\r
359 vTaskResume( xLimitedIncrementHandle );
\r
362 /* Does the counter variable have the expected value? */
\r
363 if( ulCounter != priMAX_COUNT )
\r
366 vPrintDisplayMessage( &pcTaskFailMsg );
\r
369 if( sError == pdFALSE )
\r
371 /* If no errors have occurred then increment the check variable. */
\r
372 portENTER_CRITICAL();
\r
374 portEXIT_CRITICAL();
\r
377 #if configUSE_PREEMPTION == 0
\r
381 /* Resume the continuous count task and do it all again. */
\r
382 vTaskResume( xContinuousIncrementHandle );
\r
385 /*-----------------------------------------------------------*/
\r
387 static void vQueueSendWhenSuspendedTask( void *pvParameters )
\r
389 static unsigned long ulValueToSend = ( unsigned long ) 0;
\r
390 const char * const pcTaskStartMsg = "Queue send while suspended task started.\r\n";
\r
391 const char * const pcTaskFailMsg = "Queue send while suspended failed.\r\n";
\r
393 /* Just to stop warning messages. */
\r
394 ( void ) pvParameters;
\r
396 /* Queue a message for printing to say the task has started. */
\r
397 vPrintDisplayMessage( &pcTaskStartMsg );
\r
403 /* We must not block while the scheduler is suspended! */
\r
404 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
406 if( xSuspendedQueueSendError == pdFALSE )
\r
409 vPrintDisplayMessage( &pcTaskFailMsg );
\r
413 xSuspendedQueueSendError = pdTRUE;
\r
418 vTaskDelay( priSLEEP_TIME );
\r
423 /*-----------------------------------------------------------*/
\r
425 static void vQueueReceiveWhenSuspendedTask( void *pvParameters )
\r
427 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;
\r
428 const char * const pcTaskStartMsg = "Queue receive while suspended task started.\r\n";
\r
429 const char * const pcTaskFailMsg = "Queue receive while suspended failed.\r\n";
\r
430 portBASE_TYPE xGotValue;
\r
432 /* Just to stop warning messages. */
\r
433 ( void ) pvParameters;
\r
435 /* Queue a message for printing to say the task has started. */
\r
436 vPrintDisplayMessage( &pcTaskStartMsg );
\r
442 /* Suspending the scheduler here is fairly pointless and
\r
443 undesirable for a normal application. It is done here purely
\r
444 to test the scheduler. The inner xTaskResumeAll() should
\r
445 never return pdTRUE as the scheduler is still locked by the
\r
451 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
453 if( xTaskResumeAll() )
\r
455 xSuspendedQueueReceiveError = pdTRUE;
\r
460 #if configUSE_PREEMPTION == 0
\r
464 } while( xGotValue == pdFALSE );
\r
466 if( ulReceivedValue != ulExpectedValue )
\r
468 if( xSuspendedQueueReceiveError == pdFALSE )
\r
470 vPrintDisplayMessage( &pcTaskFailMsg );
\r
472 xSuspendedQueueReceiveError = pdTRUE;
\r
478 /*-----------------------------------------------------------*/
\r
480 static void prvChangePriorityWhenSuspendedTask( void *pvParameters )
\r
482 const char * const pcTaskStartMsg = "Priority change when suspended task started.\r\n";
\r
483 const char * const pcTaskFailMsg = "Priority change when suspended task failed.\r\n";
\r
485 /* Just to stop warning messages. */
\r
486 ( void ) pvParameters;
\r
488 /* Queue a message for printing to say the task has started. */
\r
489 vPrintDisplayMessage( &pcTaskStartMsg );
\r
493 /* Start with the counter at 0 so we know what the counter should be
\r
494 when we check it next. */
\r
495 ulPrioritySetCounter = ( unsigned long ) 0;
\r
497 /* Resume the helper task. At this time it has a priority lower than
\r
498 ours so no context switch should occur. */
\r
499 vTaskResume( xChangePriorityWhenSuspendedHandle );
\r
501 /* Check to ensure the task just resumed has not executed. */
\r
502 portENTER_CRITICAL();
\r
504 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
506 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
507 vPrintDisplayMessage( &pcTaskFailMsg );
\r
510 portEXIT_CRITICAL();
\r
512 /* Now try raising the priority while the scheduler is suspended. */
\r
515 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, ( configMAX_PRIORITIES - 1 ) );
\r
517 /* Again, even though the helper task has a priority greater than
\r
518 ours, it should not have executed yet because the scheduler is
\r
520 portENTER_CRITICAL();
\r
522 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
524 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
525 vPrintDisplayMessage( &pcTaskFailMsg );
\r
528 portEXIT_CRITICAL();
\r
532 /* Now the scheduler has been resumed the helper task should
\r
533 immediately preempt us and execute. When it executes it will increment
\r
534 the ulPrioritySetCounter exactly once before suspending itself.
\r
536 We should now always find the counter set to 1. */
\r
537 portENTER_CRITICAL();
\r
539 if( ulPrioritySetCounter != ( unsigned long ) 1 )
\r
541 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
542 vPrintDisplayMessage( &pcTaskFailMsg );
\r
545 portEXIT_CRITICAL();
\r
547 /* Delay until we try this again. */
\r
548 vTaskDelay( priSLEEP_TIME * 2 );
\r
550 /* Set the priority of the helper task back ready for the next
\r
551 execution of this task. */
\r
553 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, tskIDLE_PRIORITY );
\r
557 /*-----------------------------------------------------------*/
\r
559 static void prvChangePriorityHelperTask( void *pvParameters )
\r
561 /* Just to stop warning messages. */
\r
562 ( void ) pvParameters;
\r
566 /* This is the helper task for prvChangePriorityWhenSuspendedTask().
\r
567 It has it's priority raised and lowered. When it runs it simply
\r
568 increments the counter then suspends itself again. This allows
\r
569 prvChangePriorityWhenSuspendedTask() to know how many times it has
\r
571 ulPrioritySetCounter++;
\r
572 vTaskSuspend( NULL );
\r
575 /*-----------------------------------------------------------*/
\r
577 /* Called to check that all the created tasks are still running without error. */
\r
578 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
580 /* Keep a history of the check variables so we know if it has been incremented
\r
581 since the last call. */
\r
582 static unsigned short usLastTaskCheck = ( unsigned short ) 0;
\r
583 portBASE_TYPE xReturn = pdTRUE;
\r
585 /* Check the tasks are still running by ensuring the check variable
\r
586 is still incrementing. */
\r
588 if( usCheckVariable == usLastTaskCheck )
\r
590 /* The check has not incremented so an error exists. */
\r
594 if( xSuspendedQueueSendError == pdTRUE )
\r
599 if( xSuspendedQueueReceiveError == pdTRUE )
\r
604 if( xPriorityRaiseWhenSuspendedError == pdTRUE )
\r
609 usLastTaskCheck = usCheckVariable;
\r