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 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 ( ( TickType_t ) 128 / portTICK_PERIOD_MS )
\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 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
193 in use. The queue registry is provided as a means for kernel aware
\r
194 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
195 is not being used. The call to vQueueAddToRegistry() will be removed
\r
196 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
197 defined to be less than 1. */
\r
198 vQueueAddToRegistry( xSuspendedTestQueue, "Suspended_Test_Queue" );
\r
200 xTaskCreate( vContinuousIncrementTask, "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );
\r
201 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
\r
202 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
203 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
204 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
206 /*-----------------------------------------------------------*/
\r
209 * Just loops around incrementing the shared variable until the limit has been
\r
210 * reached. Once the limit has been reached it suspends itself.
\r
212 static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )
\r
214 uint32_t *pulCounter;
\r
216 /* Take a pointer to the shared variable from the parameters passed into
\r
218 pulCounter = ( uint32_t * ) pvParameters;
\r
220 /* This will run before the control task, so the first thing it does is
\r
221 suspend - the control task will resume it when ready. */
\r
222 vTaskSuspend( NULL );
\r
226 /* Just count up to a value then suspend. */
\r
229 if( *pulCounter >= priMAX_COUNT )
\r
231 vTaskSuspend( NULL );
\r
235 /*-----------------------------------------------------------*/
\r
238 * Just keep counting the shared variable up. The control task will suspend
\r
239 * this task when it wants.
\r
241 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )
\r
243 volatile uint32_t *pulCounter;
\r
244 UBaseType_t uxOurPriority;
\r
246 /* Take a pointer to the shared variable from the parameters passed into
\r
248 pulCounter = ( uint32_t * ) pvParameters;
\r
250 /* Query our priority so we can raise it when exclusive access to the
\r
251 shared variable is required. */
\r
252 uxOurPriority = uxTaskPriorityGet( NULL );
\r
256 /* Raise the priority above the controller task to ensure a context
\r
257 switch does not occur while the variable is being accessed. */
\r
258 vTaskPrioritySet( NULL, uxOurPriority + 1 );
\r
260 configASSERT( ( uxTaskPriorityGet( NULL ) == ( uxOurPriority + 1 ) ) );
\r
263 vTaskPrioritySet( NULL, uxOurPriority );
\r
265 #if( configUSE_PREEMPTION == 0 )
\r
269 configASSERT( ( uxTaskPriorityGet( NULL ) == uxOurPriority ) );
\r
272 /*-----------------------------------------------------------*/
\r
275 * Controller task as described above.
\r
277 static portTASK_FUNCTION( vCounterControlTask, pvParameters )
\r
279 uint32_t ulLastCounter;
\r
281 short sError = pdFALSE;
\r
283 /* Just to stop warning messages. */
\r
284 ( void ) pvParameters;
\r
288 /* Start with the counter at zero. */
\r
289 ulCounter = ( uint32_t ) 0;
\r
291 /* First section : */
\r
293 /* Check the continuous count task is running. */
\r
294 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
\r
296 /* Suspend the continuous count task so we can take a mirror of the
\r
297 shared variable without risk of corruption. This is not really
\r
298 needed as the other task raises its priority above this task's
\r
300 vTaskSuspend( xContinuousIncrementHandle );
\r
302 #if( INCLUDE_eTaskGetState == 1 )
\r
304 configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eSuspended );
\r
306 #endif /* INCLUDE_eTaskGetState */
\r
308 ulLastCounter = ulCounter;
\r
310 vTaskResume( xContinuousIncrementHandle );
\r
312 #if( configUSE_PREEMPTION == 0 )
\r
316 #if( INCLUDE_eTaskGetState == 1 )
\r
318 configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eReady );
\r
320 #endif /* INCLUDE_eTaskGetState */
\r
322 /* Now delay to ensure the other task has processor time. */
\r
323 vTaskDelay( priSLEEP_TIME );
\r
325 /* Check the shared variable again. This time to ensure mutual
\r
326 exclusion the whole scheduler will be locked. This is just for
\r
330 if( ulLastCounter == ulCounter )
\r
332 /* The shared variable has not changed. There is a problem
\r
333 with the continuous count task so flag an error. */
\r
340 /* Second section: */
\r
342 /* Suspend the continuous counter task so it stops accessing the shared
\r
344 vTaskSuspend( xContinuousIncrementHandle );
\r
346 /* Reset the variable. */
\r
347 ulCounter = ( uint32_t ) 0;
\r
349 #if( INCLUDE_eTaskGetState == 1 )
\r
351 configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );
\r
353 #endif /* INCLUDE_eTaskGetState */
\r
355 /* Resume the limited count task which has a higher priority than us.
\r
356 We should therefore not return from this call until the limited count
\r
357 task has suspended itself with a known value in the counter variable. */
\r
358 vTaskResume( xLimitedIncrementHandle );
\r
360 #if( configUSE_PREEMPTION == 0 )
\r
364 /* This task should not run again until xLimitedIncrementHandle has
\r
365 suspended itself. */
\r
366 #if( INCLUDE_eTaskGetState == 1 )
\r
368 configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );
\r
370 #endif /* INCLUDE_eTaskGetState */
\r
372 /* Does the counter variable have the expected value? */
\r
373 if( ulCounter != priMAX_COUNT )
\r
378 if( sError == pdFALSE )
\r
380 /* If no errors have occurred then increment the check variable. */
\r
381 portENTER_CRITICAL();
\r
383 portEXIT_CRITICAL();
\r
386 /* Resume the continuous count task and do it all again. */
\r
387 vTaskResume( xContinuousIncrementHandle );
\r
389 #if( configUSE_PREEMPTION == 0 )
\r
394 /*-----------------------------------------------------------*/
\r
396 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )
\r
398 static uint32_t ulValueToSend = ( uint32_t ) 0;
\r
400 /* Just to stop warning messages. */
\r
401 ( void ) pvParameters;
\r
407 /* We must not block while the scheduler is suspended! */
\r
408 if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
\r
410 xSuspendedQueueSendError = pdTRUE;
\r
415 vTaskDelay( priSLEEP_TIME );
\r
420 /*-----------------------------------------------------------*/
\r
422 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )
\r
424 uint32_t ulReceivedValue;
\r
425 BaseType_t xGotValue;
\r
427 /* Just to stop warning messages. */
\r
428 ( void ) pvParameters;
\r
434 /* Suspending the scheduler here is fairly pointless and
\r
435 undesirable for a normal application. It is done here purely
\r
436 to test the scheduler. The inner xTaskResumeAll() should
\r
437 never return pdTRUE as the scheduler is still locked by the
\r
443 xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
\r
445 if( xTaskResumeAll() != pdFALSE )
\r
447 xSuspendedQueueReceiveError = pdTRUE;
\r
452 #if configUSE_PREEMPTION == 0
\r
458 } while( xGotValue == pdFALSE );
\r
460 if( ulReceivedValue != ulExpectedValue )
\r
462 xSuspendedQueueReceiveError = pdTRUE;
\r
465 if( xSuspendedQueueReceiveError != pdTRUE )
\r
467 /* Only increment the variable if an error has not occurred. This
\r
468 allows xAreDynamicPriorityTasksStillRunning() to check for stalled
\r
469 tasks as well as explicit errors. */
\r
474 /*-----------------------------------------------------------*/
\r
476 /* Called to check that all the created tasks are still running without error. */
\r
477 BaseType_t xAreDynamicPriorityTasksStillRunning( void )
\r
479 /* Keep a history of the check variables so we know if it has been incremented
\r
480 since the last call. */
\r
481 static uint16_t usLastTaskCheck = ( uint16_t ) 0;
\r
482 static uint32_t ulLastExpectedValue = ( uint32_t ) 0U;
\r
483 BaseType_t xReturn = pdTRUE;
\r
485 /* Check the tasks are still running by ensuring the check variable
\r
486 is still incrementing. */
\r
488 if( usCheckVariable == usLastTaskCheck )
\r
490 /* The check has not incremented so an error exists. */
\r
494 if( ulExpectedValue == ulLastExpectedValue )
\r
496 /* The value being received by the queue receive task has not
\r
497 incremented so an error exists. */
\r
501 if( xSuspendedQueueSendError == pdTRUE )
\r
506 if( xSuspendedQueueReceiveError == pdTRUE )
\r
511 usLastTaskCheck = usCheckVariable;
\r
512 ulLastExpectedValue = ulExpectedValue;
\r