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! Why not get us to quote to get FreeRTOS.org *
\r
30 * running on your hardware - or even write all or part of your application*
\r
31 * for you? See http://www.OpenRTOS.com for details. *
\r
33 ***************************************************************************
\r
34 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
50 * Creates all the demo application tasks and co-routines, then starts the
\r
53 * Main. c also creates a task called "Print". This only executes every
\r
54 * five seconds but has the highest priority so is guaranteed to get
\r
55 * processor time. Its main function is to check that all the other tasks
\r
56 * are still operational. Nearly all the tasks in the demo application
\r
57 * maintain a unique count that is incremented each time the task successfully
\r
58 * completes its function. Should any error occur within the task the count is
\r
59 * permanently halted. The print task checks the count of each task to ensure
\r
60 * it has changed since the last time the print task executed. If any count is
\r
61 * found not to have changed the print task displays an appropriate message.
\r
62 * If all the tasks are still incrementing their unique counts the print task
\r
63 * displays an "OK" message.
\r
65 * The LED flash tasks do not maintain a count as they already provide visual
\r
66 * feedback of their status.
\r
68 * The print task blocks on the queue into which messages that require
\r
69 * displaying are posted. It will therefore only block for the full 5 seconds
\r
70 * if no messages are posted onto the queue.
\r
72 * Main. c also provides a demonstration of how the trace visualisation utility
\r
73 * can be used, and how the scheduler can be stopped.
\r
75 * \page MainC main.c
\r
76 * \ingroup DemoFiles
\r
82 #include "FreeRTOS.h"
\r
84 #include "croutine.h"
\r
85 #include "partest.h"
\r
88 /* Demo file headers. */
\r
92 #include "crflash.h"
\r
95 #include "comtest.h"
\r
97 #include "semtest.h"
\r
98 #include "integer.h"
\r
99 #include "dynamic.h"
\r
100 #include "mevents.h"
\r
101 #include "crhook.h"
\r
102 #include "blocktim.h"
\r
103 #include "AltBlock.h"
\r
104 #include "GenQTest.h"
\r
106 #include "countsem.h"
\r
107 #include "AltQTest.h"
\r
108 #include "AltPollQ.h"
\r
109 #include "AltBlckQ.h"
\r
110 #include "RecMutex.h"
\r
112 /* Priority definitions for the tasks in the demo application. */
\r
113 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
114 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
115 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
116 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
117 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
118 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
119 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
120 #define mainGENERIC_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
122 #define mainPRINT_STACK_SIZE ( ( unsigned portSHORT ) 512 )
\r
123 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned portSHORT ) 20480 )
\r
125 /* The number of flash co-routines to create. */
\r
126 #define mainNUM_FLASH_CO_ROUTINES ( 8 )
\r
128 /* Task function for the "Print" task as described at the top of the file. */
\r
129 static void vErrorChecks( void *pvParameters );
\r
131 /* Function that checks the unique count of all the other tasks as described at
\r
132 the top of the file. */
\r
133 static void prvCheckOtherTasksAreStillRunning( void );
\r
135 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
137 static void prvCheckForKeyPresses( void );
\r
139 /* Buffer used by the trace visualisation utility so only needed if the trace
\r
141 #if configUSE_TRACE_FACILITY == 1
\r
142 static portCHAR pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
145 /* Constant definition used to turn on/off the pre-emptive scheduler. */
\r
146 static const portSHORT sUsingPreemption = configUSE_PREEMPTION;
\r
148 /* Start the math tasks appropriate to the build. The Borland port does
\r
149 not yet support floating point so uses the integer equivalent. */
\r
150 static void prvStartMathTasks( void );
\r
152 /* Check which ever tasks are relevant to this build. */
\r
153 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void );
\r
155 /*-----------------------------------------------------------*/
\r
157 portSHORT main( void )
\r
159 /* Initialise hardware and utilities. */
\r
160 vParTestInitialise();
\r
161 vPrintInitialise();
\r
163 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
164 prvStartMathTasks();
\r
165 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM1, ser115200 );
\r
166 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
167 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
168 vCreateBlockTimeTasks();
\r
169 vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
170 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
171 vStartDynamicPriorityTasks();
\r
172 vStartMultiEventTasks();
\r
173 vStartQueuePeekTasks();
\r
174 vStartCountingSemaphoreTasks();
\r
175 vStartAltGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
176 vCreateAltBlockTimeTasks();
\r
177 vStartAltBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
178 vStartAltPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
179 vStartRecursiveMutexTasks();
\r
181 /* Create the "Print" task as described at the top of the file. */
\r
182 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
184 /* This task has to be created last as it keeps account of the number of tasks
\r
185 it expects to see running. */
\r
186 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
188 /* Create the co-routines that flash the LED's. */
\r
189 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
\r
191 /* Create the co-routines that communicate with the tick hook. */
\r
192 vStartHookCoRoutines();
\r
194 /* Set the scheduler running. This function will not return unless a task
\r
195 calls vTaskEndScheduler(). */
\r
196 vTaskStartScheduler();
\r
200 /*-----------------------------------------------------------*/
\r
202 static void vErrorChecks( void *pvParameters )
\r
204 portTickType xExpectedWakeTime;
\r
205 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;
\r
206 const portLONG lMaxAllowableTimeDifference = ( portLONG ) 0;
\r
207 portTickType xWakeTime;
\r
208 portLONG lTimeDifference;
\r
209 const portCHAR *pcReceivedMessage;
\r
210 const portCHAR * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
212 ( void ) pvParameters;
\r
214 /* Loop continuously, blocking, then checking all the other tasks are still
\r
215 running, before blocking once again. This task blocks on the queue of
\r
216 messages that require displaying so will wake either by its time out expiring,
\r
217 or a message becoming available. */
\r
220 /* Calculate the time we will unblock if no messages are received
\r
221 on the queue. This is used to check that we have not blocked for too long. */
\r
222 xExpectedWakeTime = xTaskGetTickCount();
\r
223 xExpectedWakeTime += xPrintRate;
\r
225 /* Block waiting for either a time out or a message to be posted that
\r
226 required displaying. */
\r
227 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
229 /* Was a message received? */
\r
230 if( pcReceivedMessage == NULL )
\r
232 /* A message was not received so we timed out, did we unblock at the
\r
234 xWakeTime = xTaskGetTickCount();
\r
236 /* Calculate the difference between the time we unblocked and the
\r
237 time we should have unblocked. */
\r
238 if( xWakeTime > xExpectedWakeTime )
\r
240 lTimeDifference = ( portLONG ) ( xWakeTime - xExpectedWakeTime );
\r
244 lTimeDifference = ( portLONG ) ( xExpectedWakeTime - xWakeTime );
\r
247 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
249 /* We blocked too long - create a message that will get
\r
250 printed out the next time around. If we are not using
\r
251 preemption then we won't expect the timing to be so
\r
253 if( sUsingPreemption == pdTRUE )
\r
255 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
259 /* Check the other tasks are still running, just in case. */
\r
260 prvCheckOtherTasksAreStillRunning();
\r
264 /* We unblocked due to a message becoming available. Send the message
\r
266 vDisplayMessage( pcReceivedMessage );
\r
269 /* Key presses are used to invoke the trace visualisation utility, or end
\r
271 prvCheckForKeyPresses();
\r
274 /*-----------------------------------------------------------*/
\r
276 static void prvCheckForKeyPresses( void )
\r
280 taskENTER_CRITICAL();
\r
282 /* kbhit can be used in .exe's that are executed from the command
\r
283 line, but not if executed through the debugger. */
\r
288 taskEXIT_CRITICAL();
\r
292 /* Key presses can be used to start/stop the trace utility, or end the
\r
297 /* Only define keys for turning on and off the trace if the trace
\r
299 #if configUSE_TRACE_FACILITY == 1
\r
300 case 't' : vTaskList( pcWriteBuffer );
\r
301 vWriteMessageToDisk( pcWriteBuffer );
\r
303 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
307 unsigned portLONG ulBufferLength;
\r
308 ulBufferLength = ulTaskEndTrace();
\r
309 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
314 default : vTaskEndScheduler();
\r
319 /*-----------------------------------------------------------*/
\r
321 static void prvCheckOtherTasksAreStillRunning( void )
\r
323 static portSHORT sErrorHasOccurred = pdFALSE;
\r
325 if( prvCheckMathTasksAreStillRunning() != pdTRUE )
\r
327 vDisplayMessage( "Maths task count unchanged!\r\n" );
\r
328 sErrorHasOccurred = pdTRUE;
\r
331 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
333 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
334 sErrorHasOccurred = pdTRUE;
\r
337 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
339 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
340 sErrorHasOccurred = pdTRUE;
\r
343 if( xAreAltBlockingQueuesStillRunning() != pdTRUE )
\r
345 vDisplayMessage( "Alt blocking queues count unchanged!\r\n" );
\r
346 sErrorHasOccurred = pdTRUE;
\r
349 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
351 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
352 sErrorHasOccurred = pdTRUE;
\r
355 if( xAreAltPollingQueuesStillRunning() != pdTRUE )
\r
357 vDisplayMessage( "Alt polling queue count unchanged!\r\n" );
\r
358 sErrorHasOccurred = pdTRUE;
\r
361 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
363 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
364 sErrorHasOccurred = pdTRUE;
\r
367 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
369 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
370 sErrorHasOccurred = pdTRUE;
\r
373 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
375 vDisplayMessage( "Dynamic priority count unchanged!\r\n" );
\r
376 sErrorHasOccurred = pdTRUE;
\r
379 if( xAreMultiEventTasksStillRunning() != pdTRUE )
\r
381 vDisplayMessage( "Error in multi events tasks!\r\n" );
\r
382 sErrorHasOccurred = pdTRUE;
\r
385 if( xAreFlashCoRoutinesStillRunning() != pdTRUE )
\r
387 vDisplayMessage( "Error in co-routine flash tasks!\r\n" );
\r
388 sErrorHasOccurred = pdTRUE;
\r
391 if( xAreHookCoRoutinesStillRunning() != pdTRUE )
\r
393 vDisplayMessage( "Error in tick hook to co-routine communications!\r\n" );
\r
394 sErrorHasOccurred = pdTRUE;
\r
397 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
399 vDisplayMessage( "Error in block time test tasks!\r\n" );
\r
400 sErrorHasOccurred = pdTRUE;
\r
403 if( xAreAltBlockTimeTestTasksStillRunning() != pdTRUE )
\r
405 vDisplayMessage( "Error in fast block time test tasks!\r\n" );
\r
406 sErrorHasOccurred = pdTRUE;
\r
409 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
411 vDisplayMessage( "Error in generic queue test task!\r\n" );
\r
412 sErrorHasOccurred = pdTRUE;
\r
415 if( xAreAltGenericQueueTasksStillRunning() != pdTRUE )
\r
417 vDisplayMessage( "Error in fast generic queue test task!\r\n" );
\r
418 sErrorHasOccurred = pdTRUE;
\r
421 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
423 vDisplayMessage( "Error in queue peek test task!\r\n" );
\r
424 sErrorHasOccurred = pdTRUE;
\r
427 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
429 vDisplayMessage( "Error in counting semaphore demo task!\r\n" );
\r
430 sErrorHasOccurred = pdTRUE;
\r
433 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
435 vDisplayMessage( "Error in recursive mutex tasks!\r\n" );
\r
436 sErrorHasOccurred = pdTRUE;
\r
439 if( sErrorHasOccurred == pdFALSE )
\r
441 vDisplayMessage( "OK " );
\r
444 /*-----------------------------------------------------------*/
\r
446 static void prvStartMathTasks( void )
\r
448 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
449 /* The Borland project does not yet support floating point. */
\r
450 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
452 vStartMathTasks( tskIDLE_PRIORITY );
\r
455 /*-----------------------------------------------------------*/
\r
457 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void )
\r
459 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
460 /* The Borland project does not yet support floating point. */
\r
461 return xAreIntegerMathsTaskStillRunning();
\r
463 return xAreMathsTaskStillRunning();
\r
466 /*-----------------------------------------------------------*/
\r
468 void vApplicationIdleHook( void )
\r
470 /* The co-routines are executed in the idle task using the idle task
\r
472 vCoRoutineSchedule();
\r
474 /*-----------------------------------------------------------*/
\r