1 |
606 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
/**
|
55 |
|
|
* Create a single persistent task which periodically dynamically creates another
|
56 |
|
|
* two tasks. The original task is called the creator task, the two tasks it
|
57 |
|
|
* creates are called suicidal tasks.
|
58 |
|
|
*
|
59 |
|
|
* One of the created suicidal tasks kill one other suicidal task before killing
|
60 |
|
|
* itself - leaving just the original task remaining.
|
61 |
|
|
*
|
62 |
|
|
* The creator task must be spawned after all of the other demo application tasks
|
63 |
|
|
* as it keeps a check on the number of tasks under the scheduler control. The
|
64 |
|
|
* number of tasks it expects to see running should never be greater than the
|
65 |
|
|
* number of tasks that were in existence when the creator task was spawned, plus
|
66 |
|
|
* one set of four suicidal tasks. If this number is exceeded an error is flagged.
|
67 |
|
|
*
|
68 |
|
|
* \page DeathC death.c
|
69 |
|
|
* \ingroup DemoFiles
|
70 |
|
|
* <HR>
|
71 |
|
|
*/
|
72 |
|
|
|
73 |
|
|
/*
|
74 |
|
|
Changes from V3.0.0
|
75 |
|
|
+ CreationCount sizes changed from unsigned portBASE_TYPE to
|
76 |
|
|
unsigned short to minimize the risk of overflowing.
|
77 |
|
|
|
78 |
|
|
+ Reset of usLastCreationCount added
|
79 |
|
|
|
80 |
|
|
Changes from V3.1.0
|
81 |
|
|
+ Changed the dummy calculation to use variables of type long, rather than
|
82 |
|
|
float. This allows the file to be used with ports that do not support
|
83 |
|
|
floating point.
|
84 |
|
|
|
85 |
|
|
*/
|
86 |
|
|
|
87 |
|
|
#include <stdlib.h>
|
88 |
|
|
|
89 |
|
|
/* Scheduler include files. */
|
90 |
|
|
#include "FreeRTOS.h"
|
91 |
|
|
#include "task.h"
|
92 |
|
|
|
93 |
|
|
/* Demo program include files. */
|
94 |
|
|
#include "death.h"
|
95 |
|
|
|
96 |
|
|
#define deathSTACK_SIZE ( configMINIMAL_STACK_SIZE + 60 )
|
97 |
|
|
|
98 |
|
|
/* The task originally created which is responsible for periodically dynamically
|
99 |
|
|
creating another four tasks. */
|
100 |
|
|
static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );
|
101 |
|
|
|
102 |
|
|
/* The task function of the dynamically created tasks. */
|
103 |
|
|
static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );
|
104 |
|
|
|
105 |
|
|
/* A variable which is incremented every time the dynamic tasks are created. This
|
106 |
|
|
is used to check that the task is still running. */
|
107 |
|
|
static volatile unsigned short usCreationCount = 0;
|
108 |
|
|
|
109 |
|
|
/* Used to store the number of tasks that were originally running so the creator
|
110 |
|
|
task can tell if any of the suicidal tasks have failed to die.
|
111 |
|
|
*/
|
112 |
|
|
static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
|
113 |
|
|
|
114 |
|
|
/* Tasks are deleted by the idle task. Under heavy load the idle task might
|
115 |
|
|
not get much processing time, so it would be legitimate for several tasks to
|
116 |
|
|
remain undeleted for a short period. */
|
117 |
|
|
static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 2;
|
118 |
|
|
|
119 |
|
|
/* Used to store a handle to the task that should be killed by a suicidal task,
|
120 |
|
|
before it kills itself. */
|
121 |
|
|
xTaskHandle xCreatedTask;
|
122 |
|
|
|
123 |
|
|
/*-----------------------------------------------------------*/
|
124 |
|
|
|
125 |
|
|
void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
|
126 |
|
|
{
|
127 |
|
|
unsigned portBASE_TYPE *puxPriority;
|
128 |
|
|
|
129 |
|
|
/* Create the Creator tasks - passing in as a parameter the priority at which
|
130 |
|
|
the suicidal tasks should be created. */
|
131 |
|
|
puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
|
132 |
|
|
*puxPriority = uxPriority;
|
133 |
|
|
|
134 |
|
|
xTaskCreate( vCreateTasks, ( signed char * ) "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
|
135 |
|
|
|
136 |
|
|
/* Record the number of tasks that are running now so we know if any of the
|
137 |
|
|
suicidal tasks have failed to be killed. */
|
138 |
|
|
uxTasksRunningAtStart = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();
|
139 |
|
|
|
140 |
|
|
/* FreeRTOS.org versions before V3.0 started the idle-task as the very
|
141 |
|
|
first task. The idle task was then already included in uxTasksRunningAtStart.
|
142 |
|
|
From FreeRTOS V3.0 on, the idle task is started when the scheduler is
|
143 |
|
|
started. Therefore the idle task is not yet accounted for. We correct
|
144 |
|
|
this by increasing uxTasksRunningAtStart by 1. */
|
145 |
|
|
uxTasksRunningAtStart++;
|
146 |
|
|
}
|
147 |
|
|
/*-----------------------------------------------------------*/
|
148 |
|
|
|
149 |
|
|
static portTASK_FUNCTION( vSuicidalTask, pvParameters )
|
150 |
|
|
{
|
151 |
|
|
volatile long l1, l2;
|
152 |
|
|
xTaskHandle xTaskToKill;
|
153 |
|
|
const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
|
154 |
|
|
|
155 |
|
|
if( pvParameters != NULL )
|
156 |
|
|
{
|
157 |
|
|
/* This task is periodically created four times. Two created tasks are
|
158 |
|
|
passed a handle to the other task so it can kill it before killing itself.
|
159 |
|
|
The other task is passed in null. */
|
160 |
|
|
xTaskToKill = *( xTaskHandle* )pvParameters;
|
161 |
|
|
}
|
162 |
|
|
else
|
163 |
|
|
{
|
164 |
|
|
xTaskToKill = NULL;
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
for( ;; )
|
168 |
|
|
{
|
169 |
|
|
/* Do something random just to use some stack and registers. */
|
170 |
|
|
l1 = 2;
|
171 |
|
|
l2 = 89;
|
172 |
|
|
l2 *= l1;
|
173 |
|
|
vTaskDelay( xDelay );
|
174 |
|
|
|
175 |
|
|
if( xTaskToKill != NULL )
|
176 |
|
|
{
|
177 |
|
|
/* Make sure the other task has a go before we delete it. */
|
178 |
|
|
vTaskDelay( ( portTickType ) 0 );
|
179 |
|
|
|
180 |
|
|
/* Kill the other task that was created by vCreateTasks(). */
|
181 |
|
|
vTaskDelete( xTaskToKill );
|
182 |
|
|
|
183 |
|
|
/* Kill ourselves. */
|
184 |
|
|
vTaskDelete( NULL );
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
}/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
|
188 |
|
|
/*-----------------------------------------------------------*/
|
189 |
|
|
|
190 |
|
|
static portTASK_FUNCTION( vCreateTasks, pvParameters )
|
191 |
|
|
{
|
192 |
|
|
const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;
|
193 |
|
|
unsigned portBASE_TYPE uxPriority;
|
194 |
|
|
|
195 |
|
|
uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
|
196 |
|
|
vPortFree( pvParameters );
|
197 |
|
|
|
198 |
|
|
for( ;; )
|
199 |
|
|
{
|
200 |
|
|
/* Just loop round, delaying then creating the four suicidal tasks. */
|
201 |
|
|
vTaskDelay( xDelay );
|
202 |
|
|
|
203 |
|
|
xCreatedTask = NULL;
|
204 |
|
|
|
205 |
|
|
xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );
|
206 |
|
|
xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );
|
207 |
|
|
|
208 |
|
|
++usCreationCount;
|
209 |
|
|
}
|
210 |
|
|
}
|
211 |
|
|
/*-----------------------------------------------------------*/
|
212 |
|
|
|
213 |
|
|
/* This is called to check that the creator task is still running and that there
|
214 |
|
|
are not any more than four extra tasks. */
|
215 |
|
|
portBASE_TYPE xIsCreateTaskStillRunning( void )
|
216 |
|
|
{
|
217 |
|
|
static unsigned short usLastCreationCount = 0xfff;
|
218 |
|
|
portBASE_TYPE xReturn = pdTRUE;
|
219 |
|
|
static unsigned portBASE_TYPE uxTasksRunningNow;
|
220 |
|
|
|
221 |
|
|
if( usLastCreationCount == usCreationCount )
|
222 |
|
|
{
|
223 |
|
|
xReturn = pdFALSE;
|
224 |
|
|
}
|
225 |
|
|
else
|
226 |
|
|
{
|
227 |
|
|
usLastCreationCount = usCreationCount;
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
uxTasksRunningNow = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();
|
231 |
|
|
|
232 |
|
|
if( uxTasksRunningNow < uxTasksRunningAtStart )
|
233 |
|
|
{
|
234 |
|
|
xReturn = pdFALSE;
|
235 |
|
|
}
|
236 |
|
|
else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
|
237 |
|
|
{
|
238 |
|
|
xReturn = pdFALSE;
|
239 |
|
|
}
|
240 |
|
|
else
|
241 |
|
|
{
|
242 |
|
|
/* Everything is okay. */
|
243 |
|
|
}
|
244 |
|
|
|
245 |
|
|
return xReturn;
|
246 |
|
|
}
|
247 |
|
|
|
248 |
|
|
|