2 FreeRTOS V6.0.1 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\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 exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 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 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * Creates all the demo application tasks and co-routines, then starts the
\r
58 * Main. c also creates a task called "Print". This only executes every
\r
59 * five seconds but has the highest priority so is guaranteed to get
\r
60 * processor time. Its main function is to check that all the other tasks
\r
61 * are still operational. Nearly all the tasks in the demo application
\r
62 * maintain a unique count that is incremented each time the task successfully
\r
63 * completes its function. Should any error occur within the task the count is
\r
64 * permanently halted. The print task checks the count of each task to ensure
\r
65 * it has changed since the last time the print task executed. If any count is
\r
66 * found not to have changed the print task displays an appropriate message.
\r
67 * If all the tasks are still incrementing their unique counts the print task
\r
68 * displays an "OK" message.
\r
70 * The LED flash tasks do not maintain a count as they already provide visual
\r
71 * feedback of their status.
\r
73 * The print task blocks on the queue into which messages that require
\r
74 * displaying are posted. It will therefore only block for the full 5 seconds
\r
75 * if no messages are posted onto the queue.
\r
77 * Main. c also provides a demonstration of how the trace visualisation utility
\r
78 * can be used, and how the scheduler can be stopped.
\r
80 * \page MainC main.c
\r
81 * \ingroup DemoFiles
\r
87 #include "FreeRTOS.h"
\r
89 #include "croutine.h"
\r
90 #include "partest.h"
\r
93 /* Demo file headers. */
\r
97 #include "crflash.h"
\r
100 #include "comtest.h"
\r
101 #include "fileio.h"
\r
102 #include "semtest.h"
\r
103 #include "integer.h"
\r
104 #include "dynamic.h"
\r
105 #include "mevents.h"
\r
106 #include "crhook.h"
\r
107 #include "blocktim.h"
\r
108 #include "AltBlock.h"
\r
109 #include "GenQTest.h"
\r
111 #include "countsem.h"
\r
112 #include "AltQTest.h"
\r
113 #include "AltPollQ.h"
\r
114 #include "AltBlckQ.h"
\r
115 #include "RecMutex.h"
\r
117 /* Priority definitions for the tasks in the demo application. */
\r
118 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
119 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
120 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
121 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
122 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
123 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
124 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
125 #define mainGENERIC_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
127 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 512 )
\r
128 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\r
130 /* The number of flash co-routines to create. */
\r
131 #define mainNUM_FLASH_CO_ROUTINES ( 8 )
\r
133 /* Task function for the "Print" task as described at the top of the file. */
\r
134 static void vErrorChecks( void *pvParameters );
\r
136 /* Function that checks the unique count of all the other tasks as described at
\r
137 the top of the file. */
\r
138 static void prvCheckOtherTasksAreStillRunning( void );
\r
140 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
142 static void prvCheckForKeyPresses( void );
\r
144 /* Buffer used by the trace visualisation utility so only needed if the trace
\r
146 #if configUSE_TRACE_FACILITY == 1
\r
147 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
150 /* Constant definition used to turn on/off the pre-emptive scheduler. */
\r
151 static const short sUsingPreemption = configUSE_PREEMPTION;
\r
153 /* Start the math tasks appropriate to the build. The Borland port does
\r
154 not yet support floating point so uses the integer equivalent. */
\r
155 static void prvStartMathTasks( void );
\r
157 /* Check which ever tasks are relevant to this build. */
\r
158 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void );
\r
160 /* Used to demonstrate the "task switched in" callback function. */
\r
161 static portBASE_TYPE prvExampleTaskHook( void * pvParameter );
\r
163 /* Just used to count the number of times the example task callback function is
\r
164 called, and the number of times a queue send passes. */
\r
165 static unsigned long long uxCheckTaskHookCallCount = 0;
\r
166 static unsigned long long uxQueueSendPassedCount = 0;
\r
168 /*-----------------------------------------------------------*/
\r
172 /* Initialise hardware and utilities. */
\r
173 vParTestInitialise();
\r
174 vPrintInitialise();
\r
176 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
177 prvStartMathTasks();
\r
178 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM1, ser115200 );
\r
179 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
180 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
181 vCreateBlockTimeTasks();
\r
182 vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
183 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
184 vStartDynamicPriorityTasks();
\r
185 vStartMultiEventTasks();
\r
186 vStartQueuePeekTasks();
\r
187 vStartCountingSemaphoreTasks();
\r
188 vStartAltGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
189 vCreateAltBlockTimeTasks();
\r
190 vStartAltBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
191 vStartAltPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
192 vStartRecursiveMutexTasks();
\r
194 /* Create the "Print" task as described at the top of the file. */
\r
195 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
197 /* This task has to be created last as it keeps account of the number of tasks
\r
198 it expects to see running. */
\r
199 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
201 /* Create the co-routines that flash the LED's. */
\r
202 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
\r
204 /* Create the co-routines that communicate with the tick hook. */
\r
205 vStartHookCoRoutines();
\r
207 /* Set the scheduler running. This function will not return unless a task
\r
208 calls vTaskEndScheduler(). */
\r
209 vTaskStartScheduler();
\r
213 /*-----------------------------------------------------------*/
\r
215 static portBASE_TYPE prvExampleTaskHook( void * pvParameter )
\r
217 if( pvParameter != ( void * ) 0xabcd )
\r
219 /* The parameter did not contain the value we expected, so cause an
\r
220 error to be detected by setting the call count back to zero. */
\r
221 uxCheckTaskHookCallCount = 0;
\r
225 /* Simply increment a number so we know the callback has been executed. */
\r
226 uxCheckTaskHookCallCount++;
\r
231 /*-----------------------------------------------------------*/
\r
233 void vMainQueueSendPassed( void )
\r
235 /* This is just an example implementation of the "queue send" trace hook. */
\r
236 uxQueueSendPassedCount++;
\r
238 /*-----------------------------------------------------------*/
\r
240 static void vErrorChecks( void *pvParameters )
\r
242 portTickType xExpectedWakeTime;
\r
243 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;
\r
244 const long lMaxAllowableTimeDifference = ( long ) 0;
\r
245 portTickType xWakeTime;
\r
246 long lTimeDifference;
\r
247 const char *pcReceivedMessage;
\r
248 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
249 const char * const pcUnexpectedHookValueMsg = "Task hook has unexpected value!\r\n";
\r
251 ( void ) pvParameters;
\r
253 /* Register our callback function. */
\r
254 vTaskSetApplicationTaskTag( NULL, prvExampleTaskHook );
\r
256 /* Just for test purposes. */
\r
257 if( xTaskGetApplicationTaskTag( NULL ) != prvExampleTaskHook )
\r
259 vPrintDisplayMessage( &pcUnexpectedHookValueMsg );
\r
262 /* Loop continuously, blocking, then checking all the other tasks are still
\r
263 running, before blocking once again. This task blocks on the queue of
\r
264 messages that require displaying so will wake either by its time out expiring,
\r
265 or a message becoming available. */
\r
268 /* Calculate the time we will unblock if no messages are received
\r
269 on the queue. This is used to check that we have not blocked for too long. */
\r
270 xExpectedWakeTime = xTaskGetTickCount();
\r
271 xExpectedWakeTime += xPrintRate;
\r
273 /* Block waiting for either a time out or a message to be posted that
\r
274 required displaying. */
\r
275 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
277 /* Was a message received? */
\r
278 if( pcReceivedMessage == NULL )
\r
280 /* A message was not received so we timed out, did we unblock at the
\r
282 xWakeTime = xTaskGetTickCount();
\r
284 /* Calculate the difference between the time we unblocked and the
\r
285 time we should have unblocked. */
\r
286 if( xWakeTime > xExpectedWakeTime )
\r
288 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
292 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
295 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
297 /* We blocked too long - create a message that will get
\r
298 printed out the next time around. If we are not using
\r
299 preemption then we won't expect the timing to be so
\r
301 if( sUsingPreemption == pdTRUE )
\r
303 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
307 /* Check the other tasks are still running, just in case. */
\r
308 prvCheckOtherTasksAreStillRunning();
\r
312 /* We unblocked due to a message becoming available. Send the message
\r
314 vDisplayMessage( pcReceivedMessage );
\r
317 /* Key presses are used to invoke the trace visualisation utility, or end
\r
319 prvCheckForKeyPresses();
\r
322 /*-----------------------------------------------------------*/
\r
324 static void prvCheckForKeyPresses( void )
\r
328 taskENTER_CRITICAL();
\r
330 /* kbhit can be used in .exe's that are executed from the command
\r
331 line, but not if executed through the debugger. */
\r
336 taskEXIT_CRITICAL();
\r
340 /* Key presses can be used to start/stop the trace utility, or end the
\r
345 /* Only define keys for turning on and off the trace if the trace
\r
347 #if configUSE_TRACE_FACILITY == 1
\r
348 case 't' : vTaskList( pcWriteBuffer );
\r
349 vWriteMessageToDisk( pcWriteBuffer );
\r
351 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
355 unsigned long ulBufferLength;
\r
356 ulBufferLength = ulTaskEndTrace();
\r
357 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
362 default : vTaskEndScheduler();
\r
367 /*-----------------------------------------------------------*/
\r
369 static void prvCheckOtherTasksAreStillRunning( void )
\r
371 static short sErrorHasOccurred = pdFALSE;
\r
372 static unsigned long long uxLastHookCallCount = 0, uxLastQueueSendCount = 0;
\r
374 if( prvCheckMathTasksAreStillRunning() != pdTRUE )
\r
376 vDisplayMessage( "Maths task count unchanged!\r\n" );
\r
377 sErrorHasOccurred = pdTRUE;
\r
380 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
382 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
383 sErrorHasOccurred = pdTRUE;
\r
386 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
388 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
389 sErrorHasOccurred = pdTRUE;
\r
392 if( xAreAltBlockingQueuesStillRunning() != pdTRUE )
\r
394 vDisplayMessage( "Alt blocking queues count unchanged!\r\n" );
\r
395 sErrorHasOccurred = pdTRUE;
\r
398 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
400 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
401 sErrorHasOccurred = pdTRUE;
\r
404 if( xAreAltPollingQueuesStillRunning() != pdTRUE )
\r
406 vDisplayMessage( "Alt polling queue count unchanged!\r\n" );
\r
407 sErrorHasOccurred = pdTRUE;
\r
410 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
412 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
413 sErrorHasOccurred = pdTRUE;
\r
416 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
418 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
419 sErrorHasOccurred = pdTRUE;
\r
422 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
424 vDisplayMessage( "Dynamic priority count unchanged!\r\n" );
\r
425 sErrorHasOccurred = pdTRUE;
\r
428 if( xAreMultiEventTasksStillRunning() != pdTRUE )
\r
430 vDisplayMessage( "Error in multi events tasks!\r\n" );
\r
431 sErrorHasOccurred = pdTRUE;
\r
434 if( xAreFlashCoRoutinesStillRunning() != pdTRUE )
\r
436 vDisplayMessage( "Error in co-routine flash tasks!\r\n" );
\r
437 sErrorHasOccurred = pdTRUE;
\r
440 if( xAreHookCoRoutinesStillRunning() != pdTRUE )
\r
442 vDisplayMessage( "Error in tick hook to co-routine communications!\r\n" );
\r
443 sErrorHasOccurred = pdTRUE;
\r
446 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
448 vDisplayMessage( "Error in block time test tasks!\r\n" );
\r
449 sErrorHasOccurred = pdTRUE;
\r
452 if( xAreAltBlockTimeTestTasksStillRunning() != pdTRUE )
\r
454 vDisplayMessage( "Error in fast block time test tasks!\r\n" );
\r
455 sErrorHasOccurred = pdTRUE;
\r
458 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
460 vDisplayMessage( "Error in generic queue test task!\r\n" );
\r
461 sErrorHasOccurred = pdTRUE;
\r
464 if( xAreAltGenericQueueTasksStillRunning() != pdTRUE )
\r
466 vDisplayMessage( "Error in fast generic queue test task!\r\n" );
\r
467 sErrorHasOccurred = pdTRUE;
\r
470 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
472 vDisplayMessage( "Error in queue peek test task!\r\n" );
\r
473 sErrorHasOccurred = pdTRUE;
\r
476 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
478 vDisplayMessage( "Error in counting semaphore demo task!\r\n" );
\r
479 sErrorHasOccurred = pdTRUE;
\r
482 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
484 vDisplayMessage( "Error in recursive mutex tasks!\r\n" );
\r
485 sErrorHasOccurred = pdTRUE;
\r
488 /* The hook function associated with this task is called each time the task
\r
489 is switched in. We therefore expect the number of times the callback
\r
490 function has been executed to have increrment since the last time this
\r
491 function executed. */
\r
492 if( uxCheckTaskHookCallCount <= uxLastHookCallCount )
\r
494 vDisplayMessage( "Error in task hook call count!\r\n" );
\r
495 sErrorHasOccurred = pdTRUE;
\r
499 uxLastHookCallCount = uxCheckTaskHookCallCount;
\r
502 /* We would expect some queue sending to occur between calls of this
\r
504 if( uxQueueSendPassedCount <= uxLastQueueSendCount )
\r
506 vDisplayMessage( "Error in queue send hook call count!\r\n" );
\r
507 sErrorHasOccurred = pdTRUE;
\r
511 uxLastQueueSendCount = uxQueueSendPassedCount;
\r
514 if( sErrorHasOccurred == pdFALSE )
\r
516 vDisplayMessage( "OK " );
\r
519 /*-----------------------------------------------------------*/
\r
521 static void prvStartMathTasks( void )
\r
523 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
524 /* The Borland project does not yet support floating point. */
\r
525 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
527 vStartMathTasks( tskIDLE_PRIORITY );
\r
530 /*-----------------------------------------------------------*/
\r
532 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void )
\r
534 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
535 /* The Borland project does not yet support floating point. */
\r
536 return xAreIntegerMathsTaskStillRunning();
\r
538 return xAreMathsTaskStillRunning();
\r
541 /*-----------------------------------------------------------*/
\r
543 void vApplicationIdleHook( void )
\r
545 /* The co-routines are executed in the idle task using the idle task
\r
547 vCoRoutineSchedule();
\r
549 /*-----------------------------------------------------------*/
\r