1 |
588 |
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 |
|
|
* Creates all the demo application tasks, then starts the scheduler. The WEB
|
56 |
|
|
* documentation provides more details of the demo application tasks.
|
57 |
|
|
*
|
58 |
|
|
* Main.c also creates a task called "Check". This only executes every three
|
59 |
|
|
* seconds but has the highest priority so is guaranteed to get processor time.
|
60 |
|
|
* Its main function is to check that all the other tasks are still operational.
|
61 |
|
|
* Each task (other than the "flash" tasks) maintains a unique count that is
|
62 |
|
|
* incremented each time the task successfully completes its function. Should
|
63 |
|
|
* any error occur within such a task the count is permanently halted. The
|
64 |
|
|
* check task inspects the count of each task to ensure it has changed since
|
65 |
|
|
* the last time the check task executed. If all the count variables have
|
66 |
|
|
* changed all the tasks are still executing error free, and the check task
|
67 |
|
|
* toggles the onboard LED. Should any task contain an error at any time
|
68 |
|
|
* the LED toggle rate will change from 3 seconds to 500ms.
|
69 |
|
|
*
|
70 |
|
|
* To check the operation of the memory allocator the check task also
|
71 |
|
|
* dynamically creates a task before delaying, and deletes it again when it
|
72 |
|
|
* wakes. If memory cannot be allocated for the new task the call to xTaskCreate
|
73 |
|
|
* will fail and an error is signalled. The dynamically created task itself
|
74 |
|
|
* allocates and frees memory just to give the allocator a bit more exercise.
|
75 |
|
|
*
|
76 |
|
|
*/
|
77 |
|
|
|
78 |
|
|
/* Standard includes. */
|
79 |
|
|
#include <stdlib.h>
|
80 |
|
|
#include <string.h>
|
81 |
|
|
|
82 |
|
|
/* Scheduler include files. */
|
83 |
|
|
#include "FreeRTOS.h"
|
84 |
|
|
#include "task.h"
|
85 |
|
|
|
86 |
|
|
/* Demo application file headers. */
|
87 |
|
|
#include "flash.h"
|
88 |
|
|
#include "integer.h"
|
89 |
|
|
#include "PollQ.h"
|
90 |
|
|
#include "comtest2.h"
|
91 |
|
|
#include "semtest.h"
|
92 |
|
|
#include "flop.h"
|
93 |
|
|
#include "dynamic.h"
|
94 |
|
|
#include "BlockQ.h"
|
95 |
|
|
#include "serial.h"
|
96 |
|
|
#include "partest.h"
|
97 |
|
|
|
98 |
|
|
/* Priority definitions for most of the tasks in the demo application. Some
|
99 |
|
|
tasks just use the idle priority. */
|
100 |
|
|
#define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
|
101 |
|
|
#define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
102 |
|
|
#define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
103 |
|
|
#define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
|
104 |
|
|
#define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
|
105 |
|
|
#define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
106 |
|
|
|
107 |
|
|
/* Baud rate used by the serial port tasks (ComTest tasks). */
|
108 |
|
|
#define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
|
109 |
|
|
|
110 |
|
|
/* LED used by the serial port tasks. This is toggled on each character Tx,
|
111 |
|
|
and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
|
112 |
|
|
#define mainCOM_TEST_LED ( 3 )
|
113 |
|
|
|
114 |
|
|
/* LED that is toggled by the check task. The check task periodically checks
|
115 |
|
|
that all the other tasks are operating without error. If no errors are found
|
116 |
|
|
the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
|
117 |
|
|
the the toggle rate increases to mainERROR_CHECK_PERIOD. */
|
118 |
|
|
#define mainCHECK_TASK_LED ( 5 )
|
119 |
|
|
#define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
|
120 |
|
|
#define mainERROR_CHECK_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
|
121 |
|
|
|
122 |
|
|
/* Constants used by the vMemCheckTask() task. */
|
123 |
|
|
#define mainCOUNT_INITIAL_VALUE ( ( unsigned long ) 0 )
|
124 |
|
|
#define mainNO_TASK ( 0 )
|
125 |
|
|
|
126 |
|
|
/* The size of the memory blocks allocated by the vMemCheckTask() task. */
|
127 |
|
|
#define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
|
128 |
|
|
#define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
|
129 |
|
|
#define mainMEM_CHECK_SIZE_3 ( ( size_t ) 151 )
|
130 |
|
|
|
131 |
|
|
/*
|
132 |
|
|
* The 'Check' task.
|
133 |
|
|
*/
|
134 |
|
|
static void vErrorChecks( void *pvParameters );
|
135 |
|
|
|
136 |
|
|
/*
|
137 |
|
|
* Checks the unique counts of other tasks to ensure they are still operational.
|
138 |
|
|
*/
|
139 |
|
|
static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );
|
140 |
|
|
|
141 |
|
|
/*
|
142 |
|
|
* Dynamically created and deleted during each cycle of the vErrorChecks()
|
143 |
|
|
* task. This is done to check the operation of the memory allocator.
|
144 |
|
|
* See the top of vErrorChecks for more details.
|
145 |
|
|
*/
|
146 |
|
|
static void vMemCheckTask( void *pvParameters );
|
147 |
|
|
|
148 |
|
|
/*-----------------------------------------------------------*/
|
149 |
|
|
|
150 |
|
|
/*
|
151 |
|
|
* Start all the tasks then start the scheduler.
|
152 |
|
|
*/
|
153 |
|
|
int main( void )
|
154 |
|
|
{
|
155 |
|
|
/* Setup the LED's for output. */
|
156 |
|
|
vParTestInitialise();
|
157 |
|
|
|
158 |
|
|
/* Start the various standard demo application tasks. */
|
159 |
|
|
vStartIntegerMathTasks( tskIDLE_PRIORITY );
|
160 |
|
|
vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
|
161 |
|
|
vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
|
162 |
|
|
vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
|
163 |
|
|
vStartMathTasks( tskIDLE_PRIORITY );
|
164 |
|
|
vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
|
165 |
|
|
vStartDynamicPriorityTasks();
|
166 |
|
|
vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
|
167 |
|
|
|
168 |
|
|
/* Start the 'Check' task. */
|
169 |
|
|
xTaskCreate( vErrorChecks, ( signed char * )"Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
|
170 |
|
|
|
171 |
|
|
/* In this port, to use preemptive scheduler define configUSE_PREEMPTION
|
172 |
|
|
as 1 in portmacro.h. To use the cooperative scheduler define
|
173 |
|
|
configUSE_PREEMPTION as 0. */
|
174 |
|
|
vTaskStartScheduler();
|
175 |
|
|
|
176 |
|
|
/* Should never get here! */
|
177 |
|
|
return 0;
|
178 |
|
|
}
|
179 |
|
|
/*-----------------------------------------------------------*/
|
180 |
|
|
|
181 |
|
|
/*
|
182 |
|
|
* Cycle for ever, delaying then checking all the other tasks are still
|
183 |
|
|
* operating without error. If an error is detected then the delay period
|
184 |
|
|
* is decreased from mainCHECK_PERIOD to mainERROR_CHECK_PERIOD so
|
185 |
|
|
* the on board LED flash rate will increase.
|
186 |
|
|
*
|
187 |
|
|
* In addition to the standard tests the memory allocator is tested through
|
188 |
|
|
* the dynamic creation and deletion of a task each cycle. Each time the
|
189 |
|
|
* task is created memory must be allocated for its stack. When the task is
|
190 |
|
|
* deleted this memory is returned to the heap. If the task cannot be created
|
191 |
|
|
* then it is likely that the memory allocation failed. In addition the
|
192 |
|
|
* dynamically created task allocates and frees memory while it runs.
|
193 |
|
|
*/
|
194 |
|
|
static void vErrorChecks( void *pvParameters )
|
195 |
|
|
{
|
196 |
|
|
portTickType xDelayPeriod = mainCHECK_PERIOD;
|
197 |
|
|
volatile unsigned long ulMemCheckTaskRunningCount;
|
198 |
|
|
xTaskHandle xCreatedTask;
|
199 |
|
|
portTickType xLastWakeTime;
|
200 |
|
|
|
201 |
|
|
/* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
|
202 |
|
|
functions correctly. */
|
203 |
|
|
xLastWakeTime = xTaskGetTickCount();
|
204 |
|
|
|
205 |
|
|
for( ;; )
|
206 |
|
|
{
|
207 |
|
|
/* Set ulMemCheckTaskRunningCount to a known value so we can check
|
208 |
|
|
later that it has changed. */
|
209 |
|
|
ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
|
210 |
|
|
|
211 |
|
|
/* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
|
212 |
|
|
parameter. */
|
213 |
|
|
xCreatedTask = mainNO_TASK;
|
214 |
|
|
if( xTaskCreate( vMemCheckTask, ( signed char * ) "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
|
215 |
|
|
{
|
216 |
|
|
/* Could not create the task - we have probably run out of heap. */
|
217 |
|
|
xDelayPeriod = mainERROR_CHECK_PERIOD;
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
/* Delay until it is time to execute again. The delay period is
|
222 |
|
|
shorter following an error. */
|
223 |
|
|
vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
|
224 |
|
|
|
225 |
|
|
|
226 |
|
|
/* Delete the dynamically created task. */
|
227 |
|
|
if( xCreatedTask != mainNO_TASK )
|
228 |
|
|
{
|
229 |
|
|
vTaskDelete( xCreatedTask );
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
/* Check all the standard demo application tasks are executing without
|
233 |
|
|
error. ulMemCheckTaskRunningCount is checked to ensure it was
|
234 |
|
|
modified by the task just deleted. */
|
235 |
|
|
if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )
|
236 |
|
|
{
|
237 |
|
|
/* An error has been detected in one of the tasks - flash faster. */
|
238 |
|
|
xDelayPeriod = mainERROR_CHECK_PERIOD;
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
vParTestToggleLED( mainCHECK_TASK_LED );
|
242 |
|
|
}
|
243 |
|
|
}
|
244 |
|
|
/*-----------------------------------------------------------*/
|
245 |
|
|
|
246 |
|
|
/*
|
247 |
|
|
* Check each set of tasks in turn to see if they have experienced any
|
248 |
|
|
* error conditions.
|
249 |
|
|
*/
|
250 |
|
|
static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )
|
251 |
|
|
{
|
252 |
|
|
long lNoErrorsDiscovered = ( long ) pdTRUE;
|
253 |
|
|
|
254 |
|
|
if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
|
255 |
|
|
{
|
256 |
|
|
lNoErrorsDiscovered = pdFALSE;
|
257 |
|
|
}
|
258 |
|
|
|
259 |
|
|
if( xAreComTestTasksStillRunning() != pdTRUE )
|
260 |
|
|
{
|
261 |
|
|
lNoErrorsDiscovered = pdFALSE;
|
262 |
|
|
}
|
263 |
|
|
|
264 |
|
|
if( xArePollingQueuesStillRunning() != pdTRUE )
|
265 |
|
|
{
|
266 |
|
|
lNoErrorsDiscovered = pdFALSE;
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
if( xAreMathsTaskStillRunning() != pdTRUE )
|
270 |
|
|
{
|
271 |
|
|
lNoErrorsDiscovered = pdFALSE;
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
if( xAreSemaphoreTasksStillRunning() != pdTRUE )
|
275 |
|
|
{
|
276 |
|
|
lNoErrorsDiscovered = pdFALSE;
|
277 |
|
|
}
|
278 |
|
|
|
279 |
|
|
if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
|
280 |
|
|
{
|
281 |
|
|
lNoErrorsDiscovered = pdFALSE;
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
if( xAreBlockingQueuesStillRunning() != pdTRUE )
|
285 |
|
|
{
|
286 |
|
|
lNoErrorsDiscovered = pdFALSE;
|
287 |
|
|
}
|
288 |
|
|
|
289 |
|
|
if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )
|
290 |
|
|
{
|
291 |
|
|
/* The vMemCheckTask task did not increment the counter - it must
|
292 |
|
|
have failed. */
|
293 |
|
|
lNoErrorsDiscovered = pdFALSE;
|
294 |
|
|
}
|
295 |
|
|
|
296 |
|
|
return lNoErrorsDiscovered;
|
297 |
|
|
}
|
298 |
|
|
/*-----------------------------------------------------------*/
|
299 |
|
|
|
300 |
|
|
static void vMemCheckTask( void *pvParameters )
|
301 |
|
|
{
|
302 |
|
|
unsigned long *pulMemCheckTaskRunningCounter;
|
303 |
|
|
void *pvMem1, *pvMem2, *pvMem3;
|
304 |
|
|
static long lErrorOccurred = pdFALSE;
|
305 |
|
|
|
306 |
|
|
/* This task is dynamically created then deleted during each cycle of the
|
307 |
|
|
vErrorChecks task to check the operation of the memory allocator. Each time
|
308 |
|
|
the task is created memory is allocated for the stack and TCB. Each time
|
309 |
|
|
the task is deleted this memory is returned to the heap. This task itself
|
310 |
|
|
exercises the allocator by allocating and freeing blocks.
|
311 |
|
|
|
312 |
|
|
The task executes at the idle priority so does not require a delay.
|
313 |
|
|
|
314 |
|
|
pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
|
315 |
|
|
vErrorChecks() task that this task is still executing without error. */
|
316 |
|
|
|
317 |
|
|
pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
|
318 |
|
|
|
319 |
|
|
for( ;; )
|
320 |
|
|
{
|
321 |
|
|
if( lErrorOccurred == pdFALSE )
|
322 |
|
|
{
|
323 |
|
|
/* We have never seen an error so increment the counter. */
|
324 |
|
|
( *pulMemCheckTaskRunningCounter )++;
|
325 |
|
|
}
|
326 |
|
|
else
|
327 |
|
|
{
|
328 |
|
|
/* Reset the count so an error is detected by the
|
329 |
|
|
prvCheckOtherTasksAreStillRunning() function. */
|
330 |
|
|
*pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;
|
331 |
|
|
}
|
332 |
|
|
|
333 |
|
|
/* Allocate some memory - just to give the allocator some extra
|
334 |
|
|
exercise. This has to be in a critical section to ensure the
|
335 |
|
|
task does not get deleted while it has memory allocated. */
|
336 |
|
|
vTaskSuspendAll();
|
337 |
|
|
{
|
338 |
|
|
pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
|
339 |
|
|
if( pvMem1 == NULL )
|
340 |
|
|
{
|
341 |
|
|
lErrorOccurred = pdTRUE;
|
342 |
|
|
}
|
343 |
|
|
else
|
344 |
|
|
{
|
345 |
|
|
memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
|
346 |
|
|
vPortFree( pvMem1 );
|
347 |
|
|
}
|
348 |
|
|
}
|
349 |
|
|
xTaskResumeAll();
|
350 |
|
|
|
351 |
|
|
/* Again - with a different size block. */
|
352 |
|
|
vTaskSuspendAll();
|
353 |
|
|
{
|
354 |
|
|
pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
|
355 |
|
|
if( pvMem2 == NULL )
|
356 |
|
|
{
|
357 |
|
|
lErrorOccurred = pdTRUE;
|
358 |
|
|
}
|
359 |
|
|
else
|
360 |
|
|
{
|
361 |
|
|
memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
|
362 |
|
|
vPortFree( pvMem2 );
|
363 |
|
|
}
|
364 |
|
|
}
|
365 |
|
|
xTaskResumeAll();
|
366 |
|
|
|
367 |
|
|
/* Again - with a different size block. */
|
368 |
|
|
vTaskSuspendAll();
|
369 |
|
|
{
|
370 |
|
|
pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
|
371 |
|
|
if( pvMem3 == NULL )
|
372 |
|
|
{
|
373 |
|
|
lErrorOccurred = pdTRUE;
|
374 |
|
|
}
|
375 |
|
|
else
|
376 |
|
|
{
|
377 |
|
|
memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
|
378 |
|
|
vPortFree( pvMem3 );
|
379 |
|
|
}
|
380 |
|
|
}
|
381 |
|
|
xTaskResumeAll();
|
382 |
|
|
}
|
383 |
|
|
}
|
384 |
|
|
/*-----------------------------------------------------------*/
|
385 |
|
|
|
386 |
|
|
/*
|
387 |
|
|
* Called by the startup code. Initial processor setup can be placed in this
|
388 |
|
|
* function.
|
389 |
|
|
*/
|
390 |
|
|
void hw_initialise (void)
|
391 |
|
|
{
|
392 |
|
|
}
|
393 |
|
|
|