2 FreeRTOS V8.2.2 - Copyright (C) 2015 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 * The first test creates three tasks - two counter tasks (one continuous count
\r
72 * and one limited count) and one controller. A "count" variable is shared
\r
73 * between all three tasks. The two counter tasks should never be in a "ready"
\r
74 * state at the same time. The controller task runs at the same priority as
\r
75 * the continuous count task, and at a lower priority than the limited count
\r
78 * One counter task loops indefinitely, incrementing the shared count variable
\r
79 * on each iteration. To ensure it has exclusive access to the variable it
\r
80 * raises it's priority above that of the controller task before each
\r
81 * increment, lowering it again to it's original priority before starting the
\r
84 * The other counter task increments the shared count variable on each
\r
85 * iteration of it's loop until the count has reached a limit of 0xff - at
\r
86 * which point it suspends itself. It will not start a new loop until the
\r
87 * controller task has made it "ready" again by calling vTaskResume ().
\r
88 * This second counter task operates at a higher priority than controller
\r
89 * task so does not need to worry about mutual exclusion of the counter
\r
92 * The controller task is in two sections. The first section controls and
\r
93 * monitors the continuous count task. When this section is operational the
\r
94 * limited count task is suspended. Likewise, the second section controls
\r
95 * and monitors the limited count task. When this section is operational the
\r
96 * continuous count task is suspended.
\r
98 * In the first section the controller task first takes a copy of the shared
\r
99 * count variable. To ensure mutual exclusion on the count variable it
\r
100 * suspends the continuous count task, resuming it again when the copy has been
\r
101 * taken. The controller task then sleeps for a fixed period - during which
\r
102 * the continuous count task will execute and increment the shared variable.
\r
103 * When the controller task wakes it checks that the continuous count task
\r
104 * has executed by comparing the copy of the shared variable with its current
\r
105 * value. This time, to ensure mutual exclusion, the scheduler itself is
\r
106 * suspended with a call to vTaskSuspendAll (). This is for demonstration
\r
107 * purposes only and is not a recommended technique due to its inefficiency.
\r
109 * After a fixed number of iterations the controller task suspends the
\r
110 * continuous count task, and moves on to its second section.
\r
112 * At the start of the second section the shared variable is cleared to zero.
\r
113 * The limited count task is then woken from it's suspension by a call to
\r
114 * vTaskResume (). As this counter task operates at a higher priority than
\r
115 * the controller task the controller task should not run again until the
\r
116 * shared variable has been counted up to the limited value causing the counter
\r
117 * task to suspend itself. The next line after vTaskResume () is therefore
\r
118 * a check on the shared variable to ensure everything is as expected.
\r
121 * The second test consists of a couple of very simple tasks that post onto a
\r
122 * queue while the scheduler is suspended. This test was added to test parts
\r
123 * of the scheduler not exercised by the first test.
\r
126 * The final set of two tasks implements a third test. This simply raises the
\r
127 * priority of a task while the scheduler is suspended. Again this test was
\r
128 * added to exercise parts of the code not covered by the first test.
\r
130 * \page Priorities dynamic.c
\r
131 * \ingroup DemoFiles
\r
136 Changes from V2.0.0
\r
138 + Delay periods are now specified using variables and constants of
\r
139 TickType_t rather than unsigned long.
\r
140 + Added a second, simple test that uses the functions
\r
141 vQueueReceiveWhenSuspendedTask() and vQueueSendWhenSuspendedTask().
\r
143 Changes from V3.1.1
\r
145 + Added a third simple test that uses the vTaskPrioritySet() function
\r
146 while the scheduler is suspended.
\r
147 + Modified the controller task slightly to test the calling of
\r
148 vTaskResumeAll() while the scheduler is suspended.
\r
151 #include <stdlib.h>
\r
153 /* Scheduler include files. */
\r
154 #include "FreeRTOS.h"
\r
156 #include "semphr.h"
\r
158 /* Demo app include files. */
\r
159 #include "dynamic.h"
\r
162 /* Function that implements the "limited count" task as described above. */
\r
163 static void vLimitedIncrementTask( void * pvParameters );
\r
165 /* Function that implements the "continuous count" task as described above. */
\r
166 static void vContinuousIncrementTask( void * pvParameters );
\r
168 /* Function that implements the controller task as described above. */
\r
169 static void vCounterControlTask( void * pvParameters );
\r
171 /* The simple test functions that check sending and receiving while the
\r
172 scheduler is suspended. */
\r
173 static void vQueueReceiveWhenSuspendedTask( void *pvParameters );
\r
174 static void vQueueSendWhenSuspendedTask( void *pvParameters );
\r
176 /* The simple test functions that check raising and lowering of task priorities
\r
177 while the scheduler is suspended. */
\r
178 static void prvChangePriorityWhenSuspendedTask( void *pvParameters );
\r
179 static void prvChangePriorityHelperTask( void *pvParameters );
\r
182 /* Demo task specific constants. */
\r
183 #define priSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
184 #define priSLEEP_TIME ( ( TickType_t ) 50 )
\r
185 #define priLOOPS ( 5 )
\r
186 #define priMAX_COUNT ( ( unsigned long ) 0xff )
\r
187 #define priNO_BLOCK ( ( TickType_t ) 0 )
\r
188 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
190 /*-----------------------------------------------------------*/
\r
192 /* Handles to the two counter tasks. These could be passed in as parameters
\r
193 to the controller task to prevent them having to be file scope. */
\r
194 static TaskHandle_t xContinuousIncrementHandle, xLimitedIncrementHandle, xChangePriorityWhenSuspendedHandle;
\r
196 /* The shared counter variable. This is passed in as a parameter to the two
\r
197 counter variables for demonstration purposes. */
\r
198 static unsigned long ulCounter;
\r
200 /* Variable used in a similar way by the test that checks the raising and
\r
201 lowering of task priorities while the scheduler is suspended. */
\r
202 static unsigned long ulPrioritySetCounter;
\r
204 /* Variables used to check that the tasks are still operating without error.
\r
205 Each complete iteration of the controller task increments this variable
\r
206 provided no errors have been found. The variable maintaining the same value
\r
207 is therefore indication of an error. */
\r
208 static unsigned short usCheckVariable = ( unsigned short ) 0;
\r
209 static portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
\r
210 static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
\r
211 static portBASE_TYPE xPriorityRaiseWhenSuspendedError = pdFALSE;
\r
213 /* Queue used by the second test. */
\r
214 QueueHandle_t xSuspendedTestQueue;
\r
216 /*-----------------------------------------------------------*/
\r
218 * Start the seven tasks as described at the top of the file.
\r
219 * Note that the limited count task is given a higher priority.
\r
221 void vStartDynamicPriorityTasks( void )
\r
223 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
224 xTaskCreate( vContinuousIncrementTask, "CONT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
225 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
226 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
227 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_SEND", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
228 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RECV", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
229 xTaskCreate( prvChangePriorityWhenSuspendedTask, "1st_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
230 xTaskCreate( prvChangePriorityHelperTask, "2nd_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xChangePriorityWhenSuspendedHandle );
\r
232 /*-----------------------------------------------------------*/
\r
235 * Just loops around incrementing the shared variable until the limit has been
\r
236 * reached. Once the limit has been reached it suspends itself.
\r
238 static void vLimitedIncrementTask( void * pvParameters )
\r
240 unsigned long *pulCounter;
\r
242 /* Take a pointer to the shared variable from the parameters passed into
\r
244 pulCounter = ( unsigned long * ) pvParameters;
\r
246 /* This will run before the control task, so the first thing it does is
\r
247 suspend - the control task will resume it when ready. */
\r
248 vTaskSuspend( NULL );
\r
252 /* Just count up to a value then suspend. */
\r
253 ( *pulCounter )++;
\r
255 if( *pulCounter >= priMAX_COUNT )
\r
257 vTaskSuspend( NULL );
\r
261 /*-----------------------------------------------------------*/
\r
264 * Just keep counting the shared variable up. The control task will suspend
\r
265 * this task when it wants.
\r
267 static void vContinuousIncrementTask( void * pvParameters )
\r
269 unsigned long *pulCounter;
\r
270 unsigned portBASE_TYPE uxOurPriority;
\r
272 /* Take a pointer to the shared variable from the parameters passed into
\r
274 pulCounter = ( unsigned long * ) pvParameters;
\r
276 /* Query our priority so we can raise it when exclusive access to the
\r
277 shared variable is required. */
\r
278 uxOurPriority = uxTaskPriorityGet( NULL );
\r
282 /* Raise our priority above the controller task to ensure a context
\r
283 switch does not occur while we are accessing this variable. */
\r
284 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
285 ( *pulCounter )++;
\r
286 vTaskPrioritySet( NULL, uxOurPriority );
\r
288 #if configUSE_PREEMPTION == 0
\r
293 /*-----------------------------------------------------------*/
\r
296 * Controller task as described above.
\r
298 static void vCounterControlTask( void * pvParameters )
\r
300 unsigned long ulLastCounter;
\r
302 short sError = pdFALSE;
\r
303 const char * const pcTaskStartMsg = "Priority manipulation tasks started.\r\n";
\r
304 const char * const pcTaskFailMsg = "Priority manipulation Task Failed\r\n";
\r
306 /* Just to stop warning messages. */
\r
307 ( void ) pvParameters;
\r
309 /* Queue a message for printing to say the task has started. */
\r
310 vPrintDisplayMessage( &pcTaskStartMsg );
\r
314 /* Start with the counter at zero. */
\r
315 ulCounter = ( unsigned long ) 0;
\r
317 /* First section : */
\r
319 /* Check the continuous count task is running. */
\r
320 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
322 /* Suspend the continuous count task so we can take a mirror of the
\r
323 shared variable without risk of corruption. */
\r
324 vTaskSuspend( xContinuousIncrementHandle );
\r
325 ulLastCounter = ulCounter;
\r
326 vTaskResume( xContinuousIncrementHandle );
\r
328 /* Now delay to ensure the other task has processor time. */
\r
329 vTaskDelay( priSLEEP_TIME );
\r
331 /* Check the shared variable again. This time to ensure mutual
\r
332 exclusion the whole scheduler will be locked. This is just for
\r
336 if( ulLastCounter == ulCounter )
\r
338 /* The shared variable has not changed. There is a problem
\r
339 with the continuous count task so flag an error. */
\r
342 vPrintDisplayMessage( &pcTaskFailMsg );
\r
350 /* Second section: */
\r
352 /* Suspend the continuous counter task so it stops accessing the shared variable. */
\r
353 vTaskSuspend( xContinuousIncrementHandle );
\r
355 /* Reset the variable. */
\r
356 ulCounter = ( unsigned long ) 0;
\r
358 /* Resume the limited count task which has a higher priority than us.
\r
359 We should therefore not return from this call until the limited count
\r
360 task has suspended itself with a known value in the counter variable.
\r
361 The scheduler suspension is not necessary but is included for test
\r
364 vTaskResume( xLimitedIncrementHandle );
\r
367 /* Does the counter variable have the expected value? */
\r
368 if( ulCounter != priMAX_COUNT )
\r
371 vPrintDisplayMessage( &pcTaskFailMsg );
\r
374 if( sError == pdFALSE )
\r
376 /* If no errors have occurred then increment the check variable. */
\r
377 portENTER_CRITICAL();
\r
379 portEXIT_CRITICAL();
\r
382 #if configUSE_PREEMPTION == 0
\r
386 /* Resume the continuous count task and do it all again. */
\r
387 vTaskResume( xContinuousIncrementHandle );
\r
390 /*-----------------------------------------------------------*/
\r
392 static void vQueueSendWhenSuspendedTask( void *pvParameters )
\r
394 static unsigned long ulValueToSend = ( unsigned long ) 0;
\r
395 const char * const pcTaskStartMsg = "Queue send while suspended task started.\r\n";
\r
396 const char * const pcTaskFailMsg = "Queue send while suspended failed.\r\n";
\r
398 /* Just to stop warning messages. */
\r
399 ( void ) pvParameters;
\r
401 /* Queue a message for printing to say the task has started. */
\r
402 vPrintDisplayMessage( &pcTaskStartMsg );
\r
408 /* We must not block while the scheduler is suspended! */
\r
409 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
411 if( xSuspendedQueueSendError == pdFALSE )
\r
414 vPrintDisplayMessage( &pcTaskFailMsg );
\r
418 xSuspendedQueueSendError = pdTRUE;
\r
423 vTaskDelay( priSLEEP_TIME );
\r
428 /*-----------------------------------------------------------*/
\r
430 static void vQueueReceiveWhenSuspendedTask( void *pvParameters )
\r
432 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;
\r
433 const char * const pcTaskStartMsg = "Queue receive while suspended task started.\r\n";
\r
434 const char * const pcTaskFailMsg = "Queue receive while suspended failed.\r\n";
\r
435 portBASE_TYPE xGotValue;
\r
437 /* Just to stop warning messages. */
\r
438 ( void ) pvParameters;
\r
440 /* Queue a message for printing to say the task has started. */
\r
441 vPrintDisplayMessage( &pcTaskStartMsg );
\r
447 /* Suspending the scheduler here is fairly pointless and
\r
448 undesirable for a normal application. It is done here purely
\r
449 to test the scheduler. The inner xTaskResumeAll() should
\r
450 never return pdTRUE as the scheduler is still locked by the
\r
456 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
458 if( xTaskResumeAll() )
\r
460 xSuspendedQueueReceiveError = pdTRUE;
\r
465 #if configUSE_PREEMPTION == 0
\r
469 } while( xGotValue == pdFALSE );
\r
471 if( ulReceivedValue != ulExpectedValue )
\r
473 if( xSuspendedQueueReceiveError == pdFALSE )
\r
475 vPrintDisplayMessage( &pcTaskFailMsg );
\r
477 xSuspendedQueueReceiveError = pdTRUE;
\r
483 /*-----------------------------------------------------------*/
\r
485 static void prvChangePriorityWhenSuspendedTask( void *pvParameters )
\r
487 const char * const pcTaskStartMsg = "Priority change when suspended task started.\r\n";
\r
488 const char * const pcTaskFailMsg = "Priority change when suspended task failed.\r\n";
\r
490 /* Just to stop warning messages. */
\r
491 ( void ) pvParameters;
\r
493 /* Queue a message for printing to say the task has started. */
\r
494 vPrintDisplayMessage( &pcTaskStartMsg );
\r
498 /* Start with the counter at 0 so we know what the counter should be
\r
499 when we check it next. */
\r
500 ulPrioritySetCounter = ( unsigned long ) 0;
\r
502 /* Resume the helper task. At this time it has a priority lower than
\r
503 ours so no context switch should occur. */
\r
504 vTaskResume( xChangePriorityWhenSuspendedHandle );
\r
506 /* Check to ensure the task just resumed has not executed. */
\r
507 portENTER_CRITICAL();
\r
509 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
511 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
512 vPrintDisplayMessage( &pcTaskFailMsg );
\r
515 portEXIT_CRITICAL();
\r
517 /* Now try raising the priority while the scheduler is suspended. */
\r
520 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, ( configMAX_PRIORITIES - 1 ) );
\r
522 /* Again, even though the helper task has a priority greater than
\r
523 ours, it should not have executed yet because the scheduler is
\r
525 portENTER_CRITICAL();
\r
527 if( ulPrioritySetCounter != ( unsigned long ) 0 )
\r
529 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
530 vPrintDisplayMessage( &pcTaskFailMsg );
\r
533 portEXIT_CRITICAL();
\r
537 /* Now the scheduler has been resumed the helper task should
\r
538 immediately preempt us and execute. When it executes it will increment
\r
539 the ulPrioritySetCounter exactly once before suspending itself.
\r
541 We should now always find the counter set to 1. */
\r
542 portENTER_CRITICAL();
\r
544 if( ulPrioritySetCounter != ( unsigned long ) 1 )
\r
546 xPriorityRaiseWhenSuspendedError = pdTRUE;
\r
547 vPrintDisplayMessage( &pcTaskFailMsg );
\r
550 portEXIT_CRITICAL();
\r
552 /* Delay until we try this again. */
\r
553 vTaskDelay( priSLEEP_TIME * 2 );
\r
555 /* Set the priority of the helper task back ready for the next
\r
556 execution of this task. */
\r
558 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, tskIDLE_PRIORITY );
\r
562 /*-----------------------------------------------------------*/
\r
564 static void prvChangePriorityHelperTask( void *pvParameters )
\r
566 /* Just to stop warning messages. */
\r
567 ( void ) pvParameters;
\r
571 /* This is the helper task for prvChangePriorityWhenSuspendedTask().
\r
572 It has it's priority raised and lowered. When it runs it simply
\r
573 increments the counter then suspends itself again. This allows
\r
574 prvChangePriorityWhenSuspendedTask() to know how many times it has
\r
576 ulPrioritySetCounter++;
\r
577 vTaskSuspend( NULL );
\r
580 /*-----------------------------------------------------------*/
\r
582 /* Called to check that all the created tasks are still running without error. */
\r
583 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
\r
585 /* Keep a history of the check variables so we know if it has been incremented
\r
586 since the last call. */
\r
587 static unsigned short usLastTaskCheck = ( unsigned short ) 0;
\r
588 portBASE_TYPE xReturn = pdTRUE;
\r
590 /* Check the tasks are still running by ensuring the check variable
\r
591 is still incrementing. */
\r
593 if( usCheckVariable == usLastTaskCheck )
\r
595 /* The check has not incremented so an error exists. */
\r
599 if( xSuspendedQueueSendError == pdTRUE )
\r
604 if( xSuspendedQueueReceiveError == pdTRUE )
\r
609 if( xPriorityRaiseWhenSuspendedError == pdTRUE )
\r
614 usLastTaskCheck = usCheckVariable;
\r