2 FreeRTOS V9.0.0rc1 - 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 * 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 its priority above that of the controller task before each
\r
81 * increment, lowering it again to its original priority before starting the
\r
84 * The other counter task increments the shared count variable on each
\r
85 * iteration of its 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 its 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
127 #include <stdlib.h>
\r
129 /* Scheduler include files. */
\r
130 #include "FreeRTOS.h"
\r
132 #include "semphr.h"
\r
134 /* Demo app include files. */
\r
135 #include "dynamic.h"
\r
137 /* Function that implements the "limited count" task as described above. */
\r
138 static portTASK_FUNCTION_PROTO( vLimitedIncrementTask, pvParameters );
\r
140 /* Function that implements the "continuous count" task as described above. */
\r
141 static portTASK_FUNCTION_PROTO( vContinuousIncrementTask, pvParameters );
\r
143 /* Function that implements the controller task as described above. */
\r
144 static portTASK_FUNCTION_PROTO( vCounterControlTask, pvParameters );
\r
146 static portTASK_FUNCTION_PROTO( vQueueReceiveWhenSuspendedTask, pvParameters );
\r
147 static portTASK_FUNCTION_PROTO( vQueueSendWhenSuspendedTask, pvParameters );
\r
149 /* Demo task specific constants. */
\r
150 #define priSTACK_SIZE ( configMINIMAL_STACK_SIZE )
\r
151 #define priSLEEP_TIME pdMS_TO_TICKS( 128 )
\r
152 #define priLOOPS ( 5 )
\r
153 #define priMAX_COUNT ( ( uint32_t ) 0xff )
\r
154 #define priNO_BLOCK ( ( TickType_t ) 0 )
\r
155 #define priSUSPENDED_QUEUE_LENGTH ( 1 )
\r
157 /*-----------------------------------------------------------*/
\r
159 /* Handles to the two counter tasks. These could be passed in as parameters
\r
160 to the controller task to prevent them having to be file scope. */
\r
161 static TaskHandle_t xContinuousIncrementHandle, xLimitedIncrementHandle;
\r
163 /* The shared counter variable. This is passed in as a parameter to the two
\r
164 counter variables for demonstration purposes. */
\r
165 static volatile uint32_t ulCounter;
\r
167 /* Variables used to check that the tasks are still operating without error.
\r
168 Each complete iteration of the controller task increments this variable
\r
169 provided no errors have been found. The variable maintaining the same value
\r
170 is therefore indication of an error. */
\r
171 static volatile uint16_t usCheckVariable = ( uint16_t ) 0;
\r
172 static volatile BaseType_t xSuspendedQueueSendError = pdFALSE;
\r
173 static volatile BaseType_t xSuspendedQueueReceiveError = pdFALSE;
\r
175 /* Queue used by the second test. */
\r
176 QueueHandle_t xSuspendedTestQueue;
\r
178 /* The value the queue receive task expects to receive next. This is file
\r
179 scope so xAreDynamicPriorityTasksStillRunning() can ensure it is still
\r
181 static uint32_t ulExpectedValue = ( uint32_t ) 0;
\r
183 /*-----------------------------------------------------------*/
\r
185 * Start the three tasks as described at the top of the file.
\r
186 * Note that the limited count task is given a higher priority.
\r
188 void vStartDynamicPriorityTasks( void )
\r
190 xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( uint32_t ) );
\r
192 if( xSuspendedTestQueue != NULL )
\r
194 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
195 in use. The queue registry is provided as a means for kernel aware
\r
196 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
197 is not being used. The call to vQueueAddToRegistry() will be removed
\r
198 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
199 defined to be less than 1. */
\r
200 vQueueAddToRegistry( xSuspendedTestQueue, "Suspended_Test_Queue" );
\r
202 xTaskCreate( vContinuousIncrementTask, "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
203 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
204 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
205 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
206 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
209 /*-----------------------------------------------------------*/
\r
212 * Just loops around incrementing the shared variable until the limit has been
\r
213 * reached. Once the limit has been reached it suspends itself.
\r
215 static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )
\r
217 uint32_t *pulCounter;
\r
219 /* Take a pointer to the shared variable from the parameters passed into
\r
221 pulCounter = ( uint32_t * ) pvParameters;
\r
223 /* This will run before the control task, so the first thing it does is
\r
224 suspend - the control task will resume it when ready. */
\r
225 vTaskSuspend( NULL );
\r
229 /* Just count up to a value then suspend. */
\r
232 if( *pulCounter >= priMAX_COUNT )
\r
234 vTaskSuspend( NULL );
\r
238 /*-----------------------------------------------------------*/
\r
241 * Just keep counting the shared variable up. The control task will suspend
\r
242 * this task when it wants.
\r
244 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )
\r
246 volatile uint32_t *pulCounter;
\r
247 UBaseType_t uxOurPriority;
\r
249 /* Take a pointer to the shared variable from the parameters passed into
\r
251 pulCounter = ( uint32_t * ) pvParameters;
\r
253 /* Query our priority so we can raise it when exclusive access to the
\r
254 shared variable is required. */
\r
255 uxOurPriority = uxTaskPriorityGet( NULL );
\r
259 /* Raise the priority above the controller task to ensure a context
\r
260 switch does not occur while the variable is being accessed. */
\r
261 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
263 configASSERT( ( uxTaskPriorityGet( NULL ) == ( uxOurPriority + 1 ) ) );
\r
266 vTaskPrioritySet( NULL, uxOurPriority );
\r
268 #if( configUSE_PREEMPTION == 0 )
\r
272 configASSERT( ( uxTaskPriorityGet( NULL ) == uxOurPriority ) );
\r
275 /*-----------------------------------------------------------*/
\r
278 * Controller task as described above.
\r
280 static portTASK_FUNCTION( vCounterControlTask, pvParameters )
\r
282 uint32_t ulLastCounter;
\r
284 short sError = pdFALSE;
\r
286 /* Just to stop warning messages. */
\r
287 ( void ) pvParameters;
\r
291 /* Start with the counter at zero. */
\r
292 ulCounter = ( uint32_t ) 0;
\r
294 /* First section : */
\r
296 /* Check the continuous count task is running. */
\r
297 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
299 /* Suspend the continuous count task so we can take a mirror of the
\r
300 shared variable without risk of corruption. This is not really
\r
301 needed as the other task raises its priority above this task's
\r
303 vTaskSuspend( xContinuousIncrementHandle );
\r
305 #if( INCLUDE_eTaskGetState == 1 )
\r
307 configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eSuspended );
\r
309 #endif /* INCLUDE_eTaskGetState */
\r
311 ulLastCounter = ulCounter;
\r
313 vTaskResume( xContinuousIncrementHandle );
\r
315 #if( configUSE_PREEMPTION == 0 )
\r
319 #if( INCLUDE_eTaskGetState == 1 )
\r
321 configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eReady );
\r
323 #endif /* INCLUDE_eTaskGetState */
\r
325 /* Now delay to ensure the other task has processor time. */
\r
326 vTaskDelay( priSLEEP_TIME );
\r
328 /* Check the shared variable again. This time to ensure mutual
\r
329 exclusion the whole scheduler will be locked. This is just for
\r
333 if( ulLastCounter == ulCounter )
\r
335 /* The shared variable has not changed. There is a problem
\r
336 with the continuous count task so flag an error. */
\r
343 /* Second section: */
\r
345 /* Suspend the continuous counter task so it stops accessing the shared
\r
347 vTaskSuspend( xContinuousIncrementHandle );
\r
349 /* Reset the variable. */
\r
350 ulCounter = ( uint32_t ) 0;
\r
352 #if( INCLUDE_eTaskGetState == 1 )
\r
354 configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );
\r
356 #endif /* INCLUDE_eTaskGetState */
\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 vTaskResume( xLimitedIncrementHandle );
\r
363 #if( configUSE_PREEMPTION == 0 )
\r
367 /* This task should not run again until xLimitedIncrementHandle has
\r
368 suspended itself. */
\r
369 #if( INCLUDE_eTaskGetState == 1 )
\r
371 configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );
\r
373 #endif /* INCLUDE_eTaskGetState */
\r
375 /* Does the counter variable have the expected value? */
\r
376 if( ulCounter != priMAX_COUNT )
\r
381 if( sError == pdFALSE )
\r
383 /* If no errors have occurred then increment the check variable. */
\r
384 portENTER_CRITICAL();
\r
386 portEXIT_CRITICAL();
\r
389 /* Resume the continuous count task and do it all again. */
\r
390 vTaskResume( xContinuousIncrementHandle );
\r
392 #if( configUSE_PREEMPTION == 0 )
\r
397 /*-----------------------------------------------------------*/
\r
399 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )
\r
401 static uint32_t ulValueToSend = ( uint32_t ) 0;
\r
403 /* Just to stop warning messages. */
\r
404 ( void ) pvParameters;
\r
410 /* We must not block while the scheduler is suspended! */
\r
411 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
413 xSuspendedQueueSendError = pdTRUE;
\r
418 vTaskDelay( priSLEEP_TIME );
\r
423 /*-----------------------------------------------------------*/
\r
425 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )
\r
427 uint32_t ulReceivedValue;
\r
428 BaseType_t xGotValue;
\r
430 /* Just to stop warning messages. */
\r
431 ( void ) pvParameters;
\r
437 /* Suspending the scheduler here is fairly pointless and
\r
438 undesirable for a normal application. It is done here purely
\r
439 to test the scheduler. The inner xTaskResumeAll() should
\r
440 never return pdTRUE as the scheduler is still locked by the
\r
446 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
448 if( xTaskResumeAll() != pdFALSE )
\r
450 xSuspendedQueueReceiveError = pdTRUE;
\r
455 #if configUSE_PREEMPTION == 0
\r
461 } while( xGotValue == pdFALSE );
\r
463 if( ulReceivedValue != ulExpectedValue )
\r
465 xSuspendedQueueReceiveError = pdTRUE;
\r
468 if( xSuspendedQueueReceiveError != pdTRUE )
\r
470 /* Only increment the variable if an error has not occurred. This
\r
471 allows xAreDynamicPriorityTasksStillRunning() to check for stalled
\r
472 tasks as well as explicit errors. */
\r
477 /*-----------------------------------------------------------*/
\r
479 /* Called to check that all the created tasks are still running without error. */
\r
480 BaseType_t xAreDynamicPriorityTasksStillRunning( void )
\r
482 /* Keep a history of the check variables so we know if it has been incremented
\r
483 since the last call. */
\r
484 static uint16_t usLastTaskCheck = ( uint16_t ) 0;
\r
485 static uint32_t ulLastExpectedValue = ( uint32_t ) 0U;
\r
486 BaseType_t xReturn = pdTRUE;
\r
488 /* Check the tasks are still running by ensuring the check variable
\r
489 is still incrementing. */
\r
491 if( usCheckVariable == usLastTaskCheck )
\r
493 /* The check has not incremented so an error exists. */
\r
497 if( ulExpectedValue == ulLastExpectedValue )
\r
499 /* The value being received by the queue receive task has not
\r
500 incremented so an error exists. */
\r
504 if( xSuspendedQueueSendError == pdTRUE )
\r
509 if( xSuspendedQueueReceiveError == pdTRUE )
\r
514 usLastTaskCheck = usCheckVariable;
\r
515 ulLastExpectedValue = ulExpectedValue;
\r