2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
7 This file is part of the FreeRTOS distribution.
9 FreeRTOS is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License (version 2) as published by the
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
13 ***************************************************************************
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
15 >>! distribute a combined work that includes FreeRTOS without being !<<
16 >>! obliged to provide the source code for proprietary components !<<
17 >>! outside of the FreeRTOS kernel. !<<
18 ***************************************************************************
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
23 link: http://www.freertos.org/a00114.html
25 ***************************************************************************
27 * FreeRTOS provides completely free yet professionally developed, *
28 * robust, strictly quality controlled, supported, and cross *
29 * platform software that is more than just the market leader, it *
30 * is the industry's de facto standard. *
32 * Help yourself get started quickly while simultaneously helping *
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
34 * tutorial book, reference manual, or both: *
35 * http://www.FreeRTOS.org/Documentation *
37 ***************************************************************************
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
40 the FAQ page "My application does not run, what could be wrong?". Have you
41 defined configASSERT()?
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
44 embedded software for free we request you assist our global community by
45 participating in the support forum.
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
48 be as productive as possible as early as possible. Now you can receive
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50 Ltd, and the world's leading authority on the world's leading RTOS.
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
61 licenses offer ticketed support, indemnification and commercial middleware.
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64 engineered and independently SIL3 certified version for use in safety and
65 mission critical applications that require provable dependability.
71 * The first test creates three tasks - two counter tasks (one continuous count
72 * and one limited count) and one controller. A "count" variable is shared
73 * between all three tasks. The two counter tasks should never be in a "ready"
74 * state at the same time. The controller task runs at the same priority as
75 * the continuous count task, and at a lower priority than the limited count
78 * One counter task loops indefinitely, incrementing the shared count variable
79 * on each iteration. To ensure it has exclusive access to the variable it
80 * raises it's priority above that of the controller task before each
81 * increment, lowering it again to it's original priority before starting the
84 * The other counter task increments the shared count variable on each
85 * iteration of it's loop until the count has reached a limit of 0xff - at
86 * which point it suspends itself. It will not start a new loop until the
87 * controller task has made it "ready" again by calling vTaskResume ().
88 * This second counter task operates at a higher priority than controller
89 * task so does not need to worry about mutual exclusion of the counter
92 * The controller task is in two sections. The first section controls and
93 * monitors the continuous count task. When this section is operational the
94 * limited count task is suspended. Likewise, the second section controls
95 * and monitors the limited count task. When this section is operational the
96 * continuous count task is suspended.
98 * In the first section the controller task first takes a copy of the shared
99 * count variable. To ensure mutual exclusion on the count variable it
100 * suspends the continuous count task, resuming it again when the copy has been
101 * taken. The controller task then sleeps for a fixed period - during which
102 * the continuous count task will execute and increment the shared variable.
103 * When the controller task wakes it checks that the continuous count task
104 * has executed by comparing the copy of the shared variable with its current
105 * value. This time, to ensure mutual exclusion, the scheduler itself is
106 * suspended with a call to vTaskSuspendAll (). This is for demonstration
107 * purposes only and is not a recommended technique due to its inefficiency.
109 * After a fixed number of iterations the controller task suspends the
110 * continuous count task, and moves on to its second section.
112 * At the start of the second section the shared variable is cleared to zero.
113 * The limited count task is then woken from it's suspension by a call to
114 * vTaskResume (). As this counter task operates at a higher priority than
115 * the controller task the controller task should not run again until the
116 * shared variable has been counted up to the limited value causing the counter
117 * task to suspend itself. The next line after vTaskResume () is therefore
118 * a check on the shared variable to ensure everything is as expected.
121 * The second test consists of a couple of very simple tasks that post onto a
122 * queue while the scheduler is suspended. This test was added to test parts
123 * of the scheduler not exercised by the first test.
126 * The final set of two tasks implements a third test. This simply raises the
127 * priority of a task while the scheduler is suspended. Again this test was
128 * added to exercise parts of the code not covered by the first test.
130 * \page Priorities dynamic.c
138 + Delay periods are now specified using variables and constants of
139 TickType_t rather than unsigned long.
140 + Added a second, simple test that uses the functions
141 vQueueReceiveWhenSuspendedTask() and vQueueSendWhenSuspendedTask().
145 + Added a third simple test that uses the vTaskPrioritySet() function
146 while the scheduler is suspended.
147 + Modified the controller task slightly to test the calling of
148 vTaskResumeAll() while the scheduler is suspended.
153 /* Scheduler include files. */
154 #include "FreeRTOS.h"
158 /* Demo app include files. */
162 /* Function that implements the "limited count" task as described above. */
163 static void vLimitedIncrementTask( void * pvParameters );
165 /* Function that implements the "continuous count" task as described above. */
166 static void vContinuousIncrementTask( void * pvParameters );
168 /* Function that implements the controller task as described above. */
169 static void vCounterControlTask( void * pvParameters );
171 /* The simple test functions that check sending and receiving while the
172 scheduler is suspended. */
173 static void vQueueReceiveWhenSuspendedTask( void *pvParameters );
174 static void vQueueSendWhenSuspendedTask( void *pvParameters );
176 /* The simple test functions that check raising and lowering of task priorities
177 while the scheduler is suspended. */
178 static void prvChangePriorityWhenSuspendedTask( void *pvParameters );
179 static void prvChangePriorityHelperTask( void *pvParameters );
182 /* Demo task specific constants. */
183 #define priSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
184 #define priSLEEP_TIME ( ( TickType_t ) 50 )
185 #define priLOOPS ( 5 )
186 #define priMAX_COUNT ( ( unsigned long ) 0xff )
187 #define priNO_BLOCK ( ( TickType_t ) 0 )
188 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
190 /*-----------------------------------------------------------*/
192 /* Handles to the two counter tasks. These could be passed in as parameters
193 to the controller task to prevent them having to be file scope. */
194 static TaskHandle_t xContinuousIncrementHandle, xLimitedIncrementHandle, xChangePriorityWhenSuspendedHandle;
196 /* The shared counter variable. This is passed in as a parameter to the two
197 counter variables for demonstration purposes. */
198 static unsigned long ulCounter;
200 /* Variable used in a similar way by the test that checks the raising and
201 lowering of task priorities while the scheduler is suspended. */
202 static unsigned long ulPrioritySetCounter;
204 /* Variables used to check that the tasks are still operating without error.
205 Each complete iteration of the controller task increments this variable
206 provided no errors have been found. The variable maintaining the same value
207 is therefore indication of an error. */
208 static unsigned short usCheckVariable = ( unsigned short ) 0;
209 static portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
210 static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
211 static portBASE_TYPE xPriorityRaiseWhenSuspendedError = pdFALSE;
213 /* Queue used by the second test. */
214 QueueHandle_t xSuspendedTestQueue;
216 /*-----------------------------------------------------------*/
218 * Start the seven tasks as described at the top of the file.
219 * Note that the limited count task is given a higher priority.
221 void vStartDynamicPriorityTasks( void )
223 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );
224 xTaskCreate( vContinuousIncrementTask, "CONT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
225 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
226 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
227 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_SEND", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
228 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RECV", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
229 xTaskCreate( prvChangePriorityWhenSuspendedTask, "1st_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
230 xTaskCreate( prvChangePriorityHelperTask, "2nd_P_CHANGE", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xChangePriorityWhenSuspendedHandle );
232 /*-----------------------------------------------------------*/
235 * Just loops around incrementing the shared variable until the limit has been
236 * reached. Once the limit has been reached it suspends itself.
238 static void vLimitedIncrementTask( void * pvParameters )
240 unsigned long *pulCounter;
242 /* Take a pointer to the shared variable from the parameters passed into
244 pulCounter = ( unsigned long * ) pvParameters;
246 /* This will run before the control task, so the first thing it does is
247 suspend - the control task will resume it when ready. */
248 vTaskSuspend( NULL );
252 /* Just count up to a value then suspend. */
255 if( *pulCounter >= priMAX_COUNT )
257 vTaskSuspend( NULL );
261 /*-----------------------------------------------------------*/
264 * Just keep counting the shared variable up. The control task will suspend
265 * this task when it wants.
267 static void vContinuousIncrementTask( void * pvParameters )
269 unsigned long *pulCounter;
270 unsigned portBASE_TYPE uxOurPriority;
272 /* Take a pointer to the shared variable from the parameters passed into
274 pulCounter = ( unsigned long * ) pvParameters;
276 /* Query our priority so we can raise it when exclusive access to the
277 shared variable is required. */
278 uxOurPriority = uxTaskPriorityGet( NULL );
282 /* Raise our priority above the controller task to ensure a context
283 switch does not occur while we are accessing this variable. */
284 vTaskPrioritySet( NULL, uxOurPriority + 1 );
286 vTaskPrioritySet( NULL, uxOurPriority );
288 #if configUSE_PREEMPTION == 0
293 /*-----------------------------------------------------------*/
296 * Controller task as described above.
298 static void vCounterControlTask( void * pvParameters )
300 unsigned long ulLastCounter;
302 short sError = pdFALSE;
303 const char * const pcTaskStartMsg = "Priority manipulation tasks started.\r\n";
304 const char * const pcTaskFailMsg = "Priority manipulation Task Failed\r\n";
306 /* Just to stop warning messages. */
307 ( void ) pvParameters;
309 /* Queue a message for printing to say the task has started. */
310 vPrintDisplayMessage( &pcTaskStartMsg );
314 /* Start with the counter at zero. */
315 ulCounter = ( unsigned long ) 0;
317 /* First section : */
319 /* Check the continuous count task is running. */
320 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
322 /* Suspend the continuous count task so we can take a mirror of the
323 shared variable without risk of corruption. */
324 vTaskSuspend( xContinuousIncrementHandle );
325 ulLastCounter = ulCounter;
326 vTaskResume( xContinuousIncrementHandle );
328 /* Now delay to ensure the other task has processor time. */
329 vTaskDelay( priSLEEP_TIME );
331 /* Check the shared variable again. This time to ensure mutual
332 exclusion the whole scheduler will be locked. This is just for
336 if( ulLastCounter == ulCounter )
338 /* The shared variable has not changed. There is a problem
339 with the continuous count task so flag an error. */
342 vPrintDisplayMessage( &pcTaskFailMsg );
350 /* Second section: */
352 /* Suspend the continuous counter task so it stops accessing the shared variable. */
353 vTaskSuspend( xContinuousIncrementHandle );
355 /* Reset the variable. */
356 ulCounter = ( unsigned long ) 0;
358 /* Resume the limited count task which has a higher priority than us.
359 We should therefore not return from this call until the limited count
360 task has suspended itself with a known value in the counter variable.
361 The scheduler suspension is not necessary but is included for test
364 vTaskResume( xLimitedIncrementHandle );
367 /* Does the counter variable have the expected value? */
368 if( ulCounter != priMAX_COUNT )
371 vPrintDisplayMessage( &pcTaskFailMsg );
374 if( sError == pdFALSE )
376 /* If no errors have occurred then increment the check variable. */
377 portENTER_CRITICAL();
382 #if configUSE_PREEMPTION == 0
386 /* Resume the continuous count task and do it all again. */
387 vTaskResume( xContinuousIncrementHandle );
390 /*-----------------------------------------------------------*/
392 static void vQueueSendWhenSuspendedTask( void *pvParameters )
394 static unsigned long ulValueToSend = ( unsigned long ) 0;
395 const char * const pcTaskStartMsg = "Queue send while suspended task started.\r\n";
396 const char * const pcTaskFailMsg = "Queue send while suspended failed.\r\n";
398 /* Just to stop warning messages. */
399 ( void ) pvParameters;
401 /* Queue a message for printing to say the task has started. */
402 vPrintDisplayMessage( &pcTaskStartMsg );
408 /* We must not block while the scheduler is suspended! */
409 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
411 if( xSuspendedQueueSendError == pdFALSE )
414 vPrintDisplayMessage( &pcTaskFailMsg );
418 xSuspendedQueueSendError = pdTRUE;
423 vTaskDelay( priSLEEP_TIME );
428 /*-----------------------------------------------------------*/
430 static void vQueueReceiveWhenSuspendedTask( void *pvParameters )
432 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;
433 const char * const pcTaskStartMsg = "Queue receive while suspended task started.\r\n";
434 const char * const pcTaskFailMsg = "Queue receive while suspended failed.\r\n";
435 portBASE_TYPE xGotValue;
437 /* Just to stop warning messages. */
438 ( void ) pvParameters;
440 /* Queue a message for printing to say the task has started. */
441 vPrintDisplayMessage( &pcTaskStartMsg );
447 /* Suspending the scheduler here is fairly pointless and
448 undesirable for a normal application. It is done here purely
449 to test the scheduler. The inner xTaskResumeAll() should
450 never return pdTRUE as the scheduler is still locked by the
456 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
458 if( xTaskResumeAll() )
460 xSuspendedQueueReceiveError = pdTRUE;
465 #if configUSE_PREEMPTION == 0
469 } while( xGotValue == pdFALSE );
471 if( ulReceivedValue != ulExpectedValue )
473 if( xSuspendedQueueReceiveError == pdFALSE )
475 vPrintDisplayMessage( &pcTaskFailMsg );
477 xSuspendedQueueReceiveError = pdTRUE;
483 /*-----------------------------------------------------------*/
485 static void prvChangePriorityWhenSuspendedTask( void *pvParameters )
487 const char * const pcTaskStartMsg = "Priority change when suspended task started.\r\n";
488 const char * const pcTaskFailMsg = "Priority change when suspended task failed.\r\n";
490 /* Just to stop warning messages. */
491 ( void ) pvParameters;
493 /* Queue a message for printing to say the task has started. */
494 vPrintDisplayMessage( &pcTaskStartMsg );
498 /* Start with the counter at 0 so we know what the counter should be
499 when we check it next. */
500 ulPrioritySetCounter = ( unsigned long ) 0;
502 /* Resume the helper task. At this time it has a priority lower than
503 ours so no context switch should occur. */
504 vTaskResume( xChangePriorityWhenSuspendedHandle );
506 /* Check to ensure the task just resumed has not executed. */
507 portENTER_CRITICAL();
509 if( ulPrioritySetCounter != ( unsigned long ) 0 )
511 xPriorityRaiseWhenSuspendedError = pdTRUE;
512 vPrintDisplayMessage( &pcTaskFailMsg );
517 /* Now try raising the priority while the scheduler is suspended. */
520 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, ( configMAX_PRIORITIES - 1 ) );
522 /* Again, even though the helper task has a priority greater than
523 ours, it should not have executed yet because the scheduler is
525 portENTER_CRITICAL();
527 if( ulPrioritySetCounter != ( unsigned long ) 0 )
529 xPriorityRaiseWhenSuspendedError = pdTRUE;
530 vPrintDisplayMessage( &pcTaskFailMsg );
537 /* Now the scheduler has been resumed the helper task should
538 immediately preempt us and execute. When it executes it will increment
539 the ulPrioritySetCounter exactly once before suspending itself.
541 We should now always find the counter set to 1. */
542 portENTER_CRITICAL();
544 if( ulPrioritySetCounter != ( unsigned long ) 1 )
546 xPriorityRaiseWhenSuspendedError = pdTRUE;
547 vPrintDisplayMessage( &pcTaskFailMsg );
552 /* Delay until we try this again. */
553 vTaskDelay( priSLEEP_TIME * 2 );
555 /* Set the priority of the helper task back ready for the next
556 execution of this task. */
558 vTaskPrioritySet( xChangePriorityWhenSuspendedHandle, tskIDLE_PRIORITY );
562 /*-----------------------------------------------------------*/
564 static void prvChangePriorityHelperTask( void *pvParameters )
566 /* Just to stop warning messages. */
567 ( void ) pvParameters;
571 /* This is the helper task for prvChangePriorityWhenSuspendedTask().
572 It has it's priority raised and lowered. When it runs it simply
573 increments the counter then suspends itself again. This allows
574 prvChangePriorityWhenSuspendedTask() to know how many times it has
576 ulPrioritySetCounter++;
577 vTaskSuspend( NULL );
580 /*-----------------------------------------------------------*/
582 /* Called to check that all the created tasks are still running without error. */
583 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
585 /* Keep a history of the check variables so we know if it has been incremented
586 since the last call. */
587 static unsigned short usLastTaskCheck = ( unsigned short ) 0;
588 portBASE_TYPE xReturn = pdTRUE;
590 /* Check the tasks are still running by ensuring the check variable
591 is still incrementing. */
593 if( usCheckVariable == usLastTaskCheck )
595 /* The check has not incremented so an error exists. */
599 if( xSuspendedQueueSendError == pdTRUE )
604 if( xSuspendedQueueReceiveError == pdTRUE )
609 if( xPriorityRaiseWhenSuspendedError == pdTRUE )
614 usLastTaskCheck = usCheckVariable;