2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 * Create a single persistent task which periodically dynamically creates another
\r
69 * two tasks. The original task is called the creator task, the two tasks it
\r
70 * creates are called suicidal tasks.
\r
72 * One of the created suicidal tasks kill one other suicidal task before killing
\r
73 * itself - leaving just the original task remaining.
\r
75 * The creator task must be spawned after all of the other demo application tasks
\r
76 * as it keeps a check on the number of tasks under the scheduler control. The
\r
77 * number of tasks it expects to see running should never be greater than the
\r
78 * number of tasks that were in existence when the creator task was spawned, plus
\r
79 * one set of four suicidal tasks. If this number is exceeded an error is flagged.
\r
81 * \page DeathC death.c
\r
82 * \ingroup DemoFiles
\r
88 + CreationCount sizes changed from unsigned portBASE_TYPE to
\r
89 unsigned short to minimize the risk of overflowing.
\r
91 + Reset of usLastCreationCount added
\r
94 + Changed the dummy calculation to use variables of type long, rather than
\r
95 float. This allows the file to be used with ports that do not support
\r
100 #include <stdlib.h>
\r
102 /* Scheduler include files. */
\r
103 #include "FreeRTOS.h"
\r
106 /* Demo program include files. */
\r
109 #define deathSTACK_SIZE ( configMINIMAL_STACK_SIZE + 60 )
\r
111 /* The task originally created which is responsible for periodically dynamically
\r
112 creating another four tasks. */
\r
113 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );
\r
115 /* The task function of the dynamically created tasks. */
\r
116 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );
\r
118 /* A variable which is incremented every time the dynamic tasks are created. This
\r
119 is used to check that the task is still running. */
\r
120 static volatile unsigned short usCreationCount = 0;
\r
122 /* Used to store the number of tasks that were originally running so the creator
\r
123 task can tell if any of the suicidal tasks have failed to die.
\r
125 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
\r
127 /* Tasks are deleted by the idle task. Under heavy load the idle task might
\r
128 not get much processing time, so it would be legitimate for several tasks to
\r
129 remain undeleted for a short period. */
\r
130 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 2;
\r
132 /* Used to store a handle to the task that should be killed by a suicidal task,
\r
133 before it kills itself. */
\r
134 xTaskHandle xCreatedTask;
\r
136 /*-----------------------------------------------------------*/
\r
138 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
\r
140 unsigned portBASE_TYPE *puxPriority;
\r
142 /* Create the Creator tasks - passing in as a parameter the priority at which
\r
143 the suicidal tasks should be created. */
\r
144 puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
\r
145 *puxPriority = uxPriority;
\r
147 xTaskCreate( vCreateTasks, ( signed char * ) "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
\r
149 /* Record the number of tasks that are running now so we know if any of the
\r
150 suicidal tasks have failed to be killed. */
\r
151 uxTasksRunningAtStart = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();
\r
153 /* FreeRTOS.org versions before V3.0 started the idle-task as the very
\r
154 first task. The idle task was then already included in uxTasksRunningAtStart.
\r
155 From FreeRTOS V3.0 on, the idle task is started when the scheduler is
\r
156 started. Therefore the idle task is not yet accounted for. We correct
\r
157 this by increasing uxTasksRunningAtStart by 1. */
\r
158 uxTasksRunningAtStart++;
\r
160 /* From FreeRTOS version 7.0.0 can optionally create a timer service task.
\r
161 If this is done, then uxTasksRunningAtStart needs incrementing again as that
\r
162 too is created when the scheduler is started. */
\r
163 #if configUSE_TIMERS == 1
\r
164 uxTasksRunningAtStart++;
\r
167 /*-----------------------------------------------------------*/
\r
169 static portTASK_FUNCTION( vSuicidalTask, pvParameters )
\r
171 volatile long l1, l2;
\r
172 xTaskHandle xTaskToKill;
\r
173 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
175 if( pvParameters != NULL )
\r
177 /* This task is periodically created four times. Two created tasks are
\r
178 passed a handle to the other task so it can kill it before killing itself.
\r
179 The other task is passed in null. */
\r
180 xTaskToKill = *( xTaskHandle* )pvParameters;
\r
184 xTaskToKill = NULL;
\r
189 /* Do something random just to use some stack and registers. */
\r
193 vTaskDelay( xDelay );
\r
195 if( xTaskToKill != NULL )
\r
197 /* Make sure the other task has a go before we delete it. */
\r
198 vTaskDelay( ( portTickType ) 0 );
\r
200 /* Kill the other task that was created by vCreateTasks(). */
\r
201 vTaskDelete( xTaskToKill );
\r
203 /* Kill ourselves. */
\r
204 vTaskDelete( NULL );
\r
207 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
\r
208 /*-----------------------------------------------------------*/
\r
210 static portTASK_FUNCTION( vCreateTasks, pvParameters )
\r
212 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;
\r
213 unsigned portBASE_TYPE uxPriority;
\r
215 uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
\r
216 vPortFree( pvParameters );
\r
220 /* Just loop round, delaying then creating the four suicidal tasks. */
\r
221 vTaskDelay( xDelay );
\r
223 xCreatedTask = NULL;
\r
225 xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );
\r
226 xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );
\r
231 /*-----------------------------------------------------------*/
\r
233 /* This is called to check that the creator task is still running and that there
\r
234 are not any more than four extra tasks. */
\r
235 portBASE_TYPE xIsCreateTaskStillRunning( void )
\r
237 static unsigned short usLastCreationCount = 0xfff;
\r
238 portBASE_TYPE xReturn = pdTRUE;
\r
239 static unsigned portBASE_TYPE uxTasksRunningNow;
\r
241 if( usLastCreationCount == usCreationCount )
\r
247 usLastCreationCount = usCreationCount;
\r
250 uxTasksRunningNow = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();
\r
252 if( uxTasksRunningNow < uxTasksRunningAtStart )
\r
256 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
\r
262 /* Everything is okay. */
\r