2 FreeRTOS.org V4.8.0 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
51 * Creates all the demo application tasks and co-routines, then starts the
\r
54 * Main. c also creates a task called "Print". This only executes every
\r
55 * five seconds but has the highest priority so is guaranteed to get
\r
56 * processor time. Its main function is to check that all the other tasks
\r
57 * are still operational. Nearly all the tasks in the demo application
\r
58 * maintain a unique count that is incremented each time the task successfully
\r
59 * completes its function. Should any error occur within the task the count is
\r
60 * permanently halted. The print task checks the count of each task to ensure
\r
61 * it has changed since the last time the print task executed. If any count is
\r
62 * found not to have changed the print task displays an appropriate message.
\r
63 * If all the tasks are still incrementing their unique counts the print task
\r
64 * displays an "OK" message.
\r
66 * The LED flash tasks do not maintain a count as they already provide visual
\r
67 * feedback of their status.
\r
69 * The print task blocks on the queue into which messages that require
\r
70 * displaying are posted. It will therefore only block for the full 5 seconds
\r
71 * if no messages are posted onto the queue.
\r
73 * Main. c also provides a demonstration of how the trace visualisation utility
\r
74 * can be used, and how the scheduler can be stopped.
\r
76 * \page MainC main.c
\r
77 * \ingroup DemoFiles
\r
83 #include "FreeRTOS.h"
\r
85 #include "croutine.h"
\r
86 #include "partest.h"
\r
89 /* Demo file headers. */
\r
93 #include "crflash.h"
\r
96 #include "comtest.h"
\r
98 #include "semtest.h"
\r
99 #include "integer.h"
\r
100 #include "dynamic.h"
\r
101 #include "mevents.h"
\r
102 #include "crhook.h"
\r
103 #include "blocktim.h"
\r
104 #include "AltBlock.h"
\r
105 #include "GenQTest.h"
\r
107 #include "countsem.h"
\r
108 #include "AltQTest.h"
\r
109 #include "AltPollQ.h"
\r
110 #include "AltBlckQ.h"
\r
111 #include "RecMutex.h"
\r
113 /* Priority definitions for the tasks in the demo application. */
\r
114 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
115 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
116 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
117 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
118 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
119 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
120 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
121 #define mainGENERIC_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
123 #define mainPRINT_STACK_SIZE ( ( unsigned portSHORT ) 512 )
\r
124 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned portSHORT ) 20480 )
\r
126 /* The number of flash co-routines to create. */
\r
127 #define mainNUM_FLASH_CO_ROUTINES ( 8 )
\r
129 /* Task function for the "Print" task as described at the top of the file. */
\r
130 static void vErrorChecks( void *pvParameters );
\r
132 /* Function that checks the unique count of all the other tasks as described at
\r
133 the top of the file. */
\r
134 static void prvCheckOtherTasksAreStillRunning( void );
\r
136 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
138 static void prvCheckForKeyPresses( void );
\r
140 /* Buffer used by the trace visualisation utility so only needed if the trace
\r
142 #if configUSE_TRACE_FACILITY == 1
\r
143 static portCHAR pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
146 /* Constant definition used to turn on/off the pre-emptive scheduler. */
\r
147 static const portSHORT sUsingPreemption = configUSE_PREEMPTION;
\r
149 /* Start the math tasks appropriate to the build. The Borland port does
\r
150 not yet support floating point so uses the integer equivalent. */
\r
151 static void prvStartMathTasks( void );
\r
153 /* Check which ever tasks are relevant to this build. */
\r
154 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void );
\r
156 /*-----------------------------------------------------------*/
\r
158 portSHORT main( void )
\r
160 /* Initialise hardware and utilities. */
\r
161 vParTestInitialise();
\r
162 vPrintInitialise();
\r
164 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
165 prvStartMathTasks();
\r
166 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM1, ser115200 );
\r
167 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
168 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
169 vCreateBlockTimeTasks();
\r
170 vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
171 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
172 vStartDynamicPriorityTasks();
\r
173 vStartMultiEventTasks();
\r
174 vStartQueuePeekTasks();
\r
175 vStartCountingSemaphoreTasks();
\r
176 vStartAltGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
177 vCreateAltBlockTimeTasks();
\r
178 vStartAltBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
179 vStartAltPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
180 vStartRecursiveMutexTasks();
\r
182 /* Create the "Print" task as described at the top of the file. */
\r
183 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
185 /* This task has to be created last as it keeps account of the number of tasks
\r
186 it expects to see running. */
\r
187 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
189 /* Create the co-routines that flash the LED's. */
\r
190 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
\r
192 /* Create the co-routines that communicate with the tick hook. */
\r
193 vStartHookCoRoutines();
\r
195 /* Set the scheduler running. This function will not return unless a task
\r
196 calls vTaskEndScheduler(). */
\r
197 vTaskStartScheduler();
\r
201 /*-----------------------------------------------------------*/
\r
203 static void vErrorChecks( void *pvParameters )
\r
205 portTickType xExpectedWakeTime;
\r
206 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;
\r
207 const portLONG lMaxAllowableTimeDifference = ( portLONG ) 0;
\r
208 portTickType xWakeTime;
\r
209 portLONG lTimeDifference;
\r
210 const portCHAR *pcReceivedMessage;
\r
211 const portCHAR * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
213 ( void ) pvParameters;
\r
215 /* Loop continuously, blocking, then checking all the other tasks are still
\r
216 running, before blocking once again. This task blocks on the queue of
\r
217 messages that require displaying so will wake either by its time out expiring,
\r
218 or a message becoming available. */
\r
221 /* Calculate the time we will unblock if no messages are received
\r
222 on the queue. This is used to check that we have not blocked for too long. */
\r
223 xExpectedWakeTime = xTaskGetTickCount();
\r
224 xExpectedWakeTime += xPrintRate;
\r
226 /* Block waiting for either a time out or a message to be posted that
\r
227 required displaying. */
\r
228 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
230 /* Was a message received? */
\r
231 if( pcReceivedMessage == NULL )
\r
233 /* A message was not received so we timed out, did we unblock at the
\r
235 xWakeTime = xTaskGetTickCount();
\r
237 /* Calculate the difference between the time we unblocked and the
\r
238 time we should have unblocked. */
\r
239 if( xWakeTime > xExpectedWakeTime )
\r
241 lTimeDifference = ( portLONG ) ( xWakeTime - xExpectedWakeTime );
\r
245 lTimeDifference = ( portLONG ) ( xExpectedWakeTime - xWakeTime );
\r
248 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
250 /* We blocked too long - create a message that will get
\r
251 printed out the next time around. If we are not using
\r
252 preemption then we won't expect the timing to be so
\r
254 if( sUsingPreemption == pdTRUE )
\r
256 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
260 /* Check the other tasks are still running, just in case. */
\r
261 prvCheckOtherTasksAreStillRunning();
\r
265 /* We unblocked due to a message becoming available. Send the message
\r
267 vDisplayMessage( pcReceivedMessage );
\r
270 /* Key presses are used to invoke the trace visualisation utility, or end
\r
272 prvCheckForKeyPresses();
\r
275 /*-----------------------------------------------------------*/
\r
277 static void prvCheckForKeyPresses( void )
\r
281 taskENTER_CRITICAL();
\r
283 /* kbhit can be used in .exe's that are executed from the command
\r
284 line, but not if executed through the debugger. */
\r
289 taskEXIT_CRITICAL();
\r
293 /* Key presses can be used to start/stop the trace utility, or end the
\r
298 /* Only define keys for turning on and off the trace if the trace
\r
300 #if configUSE_TRACE_FACILITY == 1
\r
301 case 't' : vTaskList( pcWriteBuffer );
\r
302 vWriteMessageToDisk( pcWriteBuffer );
\r
304 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
308 unsigned portLONG ulBufferLength;
\r
309 ulBufferLength = ulTaskEndTrace();
\r
310 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
315 default : vTaskEndScheduler();
\r
320 /*-----------------------------------------------------------*/
\r
322 static void prvCheckOtherTasksAreStillRunning( void )
\r
324 static portSHORT sErrorHasOccurred = pdFALSE;
\r
326 if( prvCheckMathTasksAreStillRunning() != pdTRUE )
\r
328 vDisplayMessage( "Maths task count unchanged!\r\n" );
\r
329 sErrorHasOccurred = pdTRUE;
\r
332 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
334 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
335 sErrorHasOccurred = pdTRUE;
\r
338 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
340 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
341 sErrorHasOccurred = pdTRUE;
\r
344 if( xAreAltBlockingQueuesStillRunning() != pdTRUE )
\r
346 vDisplayMessage( "Alt blocking queues count unchanged!\r\n" );
\r
347 sErrorHasOccurred = pdTRUE;
\r
350 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
352 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
353 sErrorHasOccurred = pdTRUE;
\r
356 if( xAreAltPollingQueuesStillRunning() != pdTRUE )
\r
358 vDisplayMessage( "Alt polling queue count unchanged!\r\n" );
\r
359 sErrorHasOccurred = pdTRUE;
\r
362 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
364 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
365 sErrorHasOccurred = pdTRUE;
\r
368 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
370 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
371 sErrorHasOccurred = pdTRUE;
\r
374 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
376 vDisplayMessage( "Dynamic priority count unchanged!\r\n" );
\r
377 sErrorHasOccurred = pdTRUE;
\r
380 if( xAreMultiEventTasksStillRunning() != pdTRUE )
\r
382 vDisplayMessage( "Error in multi events tasks!\r\n" );
\r
383 sErrorHasOccurred = pdTRUE;
\r
386 if( xAreFlashCoRoutinesStillRunning() != pdTRUE )
\r
388 vDisplayMessage( "Error in co-routine flash tasks!\r\n" );
\r
389 sErrorHasOccurred = pdTRUE;
\r
392 if( xAreHookCoRoutinesStillRunning() != pdTRUE )
\r
394 vDisplayMessage( "Error in tick hook to co-routine communications!\r\n" );
\r
395 sErrorHasOccurred = pdTRUE;
\r
398 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
400 vDisplayMessage( "Error in block time test tasks!\r\n" );
\r
401 sErrorHasOccurred = pdTRUE;
\r
404 if( xAreAltBlockTimeTestTasksStillRunning() != pdTRUE )
\r
406 vDisplayMessage( "Error in fast block time test tasks!\r\n" );
\r
407 sErrorHasOccurred = pdTRUE;
\r
410 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
412 vDisplayMessage( "Error in generic queue test task!\r\n" );
\r
413 sErrorHasOccurred = pdTRUE;
\r
416 if( xAreAltGenericQueueTasksStillRunning() != pdTRUE )
\r
418 vDisplayMessage( "Error in fast generic queue test task!\r\n" );
\r
419 sErrorHasOccurred = pdTRUE;
\r
422 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
424 vDisplayMessage( "Error in queue peek test task!\r\n" );
\r
425 sErrorHasOccurred = pdTRUE;
\r
428 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
430 vDisplayMessage( "Error in counting semaphore demo task!\r\n" );
\r
431 sErrorHasOccurred = pdTRUE;
\r
434 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
436 vDisplayMessage( "Error in recursive mutex tasks!\r\n" );
\r
437 sErrorHasOccurred = pdTRUE;
\r
440 if( sErrorHasOccurred == pdFALSE )
\r
442 vDisplayMessage( "OK " );
\r
445 /*-----------------------------------------------------------*/
\r
447 static void prvStartMathTasks( void )
\r
449 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
450 /* The Borland project does not yet support floating point. */
\r
451 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
453 vStartMathTasks( tskIDLE_PRIORITY );
\r
456 /*-----------------------------------------------------------*/
\r
458 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void )
\r
460 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
461 /* The Borland project does not yet support floating point. */
\r
462 return xAreIntegerMathsTaskStillRunning();
\r
464 return xAreMathsTaskStillRunning();
\r
467 /*-----------------------------------------------------------*/
\r
469 void vApplicationIdleHook( void )
\r
471 /* The co-routines are executed in the idle task using the idle task
\r
473 vCoRoutineSchedule();
\r
475 /*-----------------------------------------------------------*/
\r