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 |
|
|
* four tasks. The original task is called the creator task, the four tasks it
|
57 |
|
|
* creates are called suicidal tasks.
|
58 |
|
|
*
|
59 |
|
|
* Two of the created suicidal tasks kill one other suicidal task before killing
|
60 |
|
|
* themselves - 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 V2.0.0
|
75 |
|
|
|
76 |
|
|
+ Delay periods are now specified using variables and constants of
|
77 |
|
|
portTickType rather than unsigned long.
|
78 |
|
|
*/
|
79 |
|
|
|
80 |
|
|
#include <stdlib.h>
|
81 |
|
|
|
82 |
|
|
/* Scheduler include files. */
|
83 |
|
|
#include "FreeRTOS.h"
|
84 |
|
|
#include "task.h"
|
85 |
|
|
|
86 |
|
|
/* Demo program include files. */
|
87 |
|
|
#include "death.h"
|
88 |
|
|
#include "print.h"
|
89 |
|
|
|
90 |
|
|
#define deathSTACK_SIZE ( ( unsigned short ) 512 )
|
91 |
|
|
|
92 |
|
|
/* The task originally created which is responsible for periodically dynamically
|
93 |
|
|
creating another four tasks. */
|
94 |
|
|
static void vCreateTasks( void *pvParameters );
|
95 |
|
|
|
96 |
|
|
/* The task function of the dynamically created tasks. */
|
97 |
|
|
static void vSuicidalTask( void *pvParameters );
|
98 |
|
|
|
99 |
|
|
/* A variable which is incremented every time the dynamic tasks are created. This
|
100 |
|
|
is used to check that the task is still running. */
|
101 |
|
|
static volatile short sCreationCount = 0;
|
102 |
|
|
|
103 |
|
|
/* Used to store the number of tasks that were originally running so the creator
|
104 |
|
|
task can tell if any of the suicidal tasks have failed to die. */
|
105 |
|
|
static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
|
106 |
|
|
static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 5;
|
107 |
|
|
|
108 |
|
|
/* Used to store a handle to the tasks that should be killed by a suicidal task,
|
109 |
|
|
before it kills itself. */
|
110 |
|
|
xTaskHandle xCreatedTask1, xCreatedTask2;
|
111 |
|
|
|
112 |
|
|
/*-----------------------------------------------------------*/
|
113 |
|
|
|
114 |
|
|
void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
|
115 |
|
|
{
|
116 |
|
|
unsigned portBASE_TYPE *puxPriority;
|
117 |
|
|
|
118 |
|
|
/* Create the Creator tasks - passing in as a parameter the priority at which
|
119 |
|
|
the suicidal tasks should be created. */
|
120 |
|
|
puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
|
121 |
|
|
*puxPriority = uxPriority;
|
122 |
|
|
|
123 |
|
|
xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
|
124 |
|
|
|
125 |
|
|
/* Record the number of tasks that are running now so we know if any of the
|
126 |
|
|
suicidal tasks have failed to be killed. */
|
127 |
|
|
uxTasksRunningAtStart = uxTaskGetNumberOfTasks();
|
128 |
|
|
}
|
129 |
|
|
/*-----------------------------------------------------------*/
|
130 |
|
|
|
131 |
|
|
static void vSuicidalTask( void *pvParameters )
|
132 |
|
|
{
|
133 |
|
|
portDOUBLE d1, d2;
|
134 |
|
|
xTaskHandle xTaskToKill;
|
135 |
|
|
const portTickType xDelay = ( portTickType ) 500 / portTICK_RATE_MS;
|
136 |
|
|
|
137 |
|
|
if( pvParameters != NULL )
|
138 |
|
|
{
|
139 |
|
|
/* This task is periodically created four times. Tow created tasks are
|
140 |
|
|
passed a handle to the other task so it can kill it before killing itself.
|
141 |
|
|
The other task is passed in null. */
|
142 |
|
|
xTaskToKill = *( xTaskHandle* )pvParameters;
|
143 |
|
|
}
|
144 |
|
|
else
|
145 |
|
|
{
|
146 |
|
|
xTaskToKill = NULL;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
for( ;; )
|
150 |
|
|
{
|
151 |
|
|
/* Do something random just to use some stack and registers. */
|
152 |
|
|
d1 = 2.4;
|
153 |
|
|
d2 = 89.2;
|
154 |
|
|
d2 *= d1;
|
155 |
|
|
vTaskDelay( xDelay );
|
156 |
|
|
|
157 |
|
|
if( xTaskToKill != NULL )
|
158 |
|
|
{
|
159 |
|
|
/* Make sure the other task has a go before we delete it. */
|
160 |
|
|
vTaskDelay( ( portTickType ) 0 );
|
161 |
|
|
/* Kill the other task that was created by vCreateTasks(). */
|
162 |
|
|
vTaskDelete( xTaskToKill );
|
163 |
|
|
/* Kill ourselves. */
|
164 |
|
|
vTaskDelete( NULL );
|
165 |
|
|
}
|
166 |
|
|
}
|
167 |
|
|
}/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
|
168 |
|
|
/*-----------------------------------------------------------*/
|
169 |
|
|
|
170 |
|
|
static void vCreateTasks( void *pvParameters )
|
171 |
|
|
{
|
172 |
|
|
const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;
|
173 |
|
|
unsigned portBASE_TYPE uxPriority;
|
174 |
|
|
const char * const pcTaskStartMsg = "Create task started.\r\n";
|
175 |
|
|
|
176 |
|
|
/* Queue a message for printing to say the task has started. */
|
177 |
|
|
vPrintDisplayMessage( &pcTaskStartMsg );
|
178 |
|
|
|
179 |
|
|
uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
|
180 |
|
|
vPortFree( pvParameters );
|
181 |
|
|
|
182 |
|
|
for( ;; )
|
183 |
|
|
{
|
184 |
|
|
/* Just loop round, delaying then creating the four suicidal tasks. */
|
185 |
|
|
vTaskDelay( xDelay );
|
186 |
|
|
|
187 |
|
|
xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask1 );
|
188 |
|
|
xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask1, uxPriority, NULL );
|
189 |
|
|
|
190 |
|
|
xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask2 );
|
191 |
|
|
xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask2, uxPriority, NULL );
|
192 |
|
|
|
193 |
|
|
++sCreationCount;
|
194 |
|
|
}
|
195 |
|
|
}
|
196 |
|
|
/*-----------------------------------------------------------*/
|
197 |
|
|
|
198 |
|
|
/* This is called to check that the creator task is still running and that there
|
199 |
|
|
are not any more than four extra tasks. */
|
200 |
|
|
portBASE_TYPE xIsCreateTaskStillRunning( void )
|
201 |
|
|
{
|
202 |
|
|
static short sLastCreationCount = 0;
|
203 |
|
|
short sReturn = pdTRUE;
|
204 |
|
|
unsigned portBASE_TYPE uxTasksRunningNow;
|
205 |
|
|
|
206 |
|
|
if( sLastCreationCount == sCreationCount )
|
207 |
|
|
{
|
208 |
|
|
sReturn = pdFALSE;
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
uxTasksRunningNow = uxTaskGetNumberOfTasks();
|
212 |
|
|
|
213 |
|
|
if( uxTasksRunningNow < uxTasksRunningAtStart )
|
214 |
|
|
{
|
215 |
|
|
sReturn = pdFALSE;
|
216 |
|
|
}
|
217 |
|
|
else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
|
218 |
|
|
{
|
219 |
|
|
sReturn = pdFALSE;
|
220 |
|
|
}
|
221 |
|
|
else
|
222 |
|
|
{
|
223 |
|
|
/* Everything is okay. */
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
return sReturn;
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
|