2 FreeRTOS.org V4.6.1 - Copyright (C) 2003-2007 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 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com a version that has been certified for use
\r
32 in safety critical systems, plus commercial licensing, development and
\r
34 ***************************************************************************
\r
38 * Creates all the demo application tasks and co-routines, then starts the
\r
41 * Main. c also creates a task called "Print". This only executes every
\r
42 * five seconds but has the highest priority so is guaranteed to get
\r
43 * processor time. Its main function is to check that all the other tasks
\r
44 * are still operational. Nearly all the tasks in the demo application
\r
45 * maintain a unique count that is incremented each time the task successfully
\r
46 * completes its function. Should any error occur within the task the count is
\r
47 * permanently halted. The print task checks the count of each task to ensure
\r
48 * it has changed since the last time the print task executed. If any count is
\r
49 * found not to have changed the print task displays an appropriate message.
\r
50 * If all the tasks are still incrementing their unique counts the print task
\r
51 * displays an "OK" message.
\r
53 * The LED flash tasks do not maintain a count as they already provide visual
\r
54 * feedback of their status.
\r
56 * The print task blocks on the queue into which messages that require
\r
57 * displaying are posted. It will therefore only block for the full 5 seconds
\r
58 * if no messages are posted onto the queue.
\r
60 * Main. c also provides a demonstration of how the trace visualisation utility
\r
61 * can be used, and how the scheduler can be stopped.
\r
63 * \page MainC main.c
\r
64 * \ingroup DemoFiles
\r
70 #include "FreeRTOS.h"
\r
72 #include "croutine.h"
\r
73 #include "partest.h"
\r
76 /* Demo file headers. */
\r
80 #include "crflash.h"
\r
83 #include "comtest.h"
\r
85 #include "semtest.h"
\r
86 #include "integer.h"
\r
87 #include "dynamic.h"
\r
88 #include "mevents.h"
\r
90 #include "blocktim.h"
\r
91 #include "GenQTest.h"
\r
93 #include "countsem.h"
\r
95 /* Priority definitions for the tasks in the demo application. */
\r
96 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
97 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
98 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
99 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
100 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
101 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
102 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
103 #define mainGENERIC_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
105 #define mainPRINT_STACK_SIZE ( ( unsigned portSHORT ) 512 )
\r
106 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned portSHORT ) 20480 )
\r
108 /* The number of flash co-routines to create. */
\r
109 #define mainNUM_FLASH_CO_ROUTINES ( 8 )
\r
111 /* Task function for the "Print" task as described at the top of the file. */
\r
112 static void vErrorChecks( void *pvParameters );
\r
114 /* Function that checks the unique count of all the other tasks as described at
\r
115 the top of the file. */
\r
116 static void prvCheckOtherTasksAreStillRunning( void );
\r
118 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
120 static void prvCheckForKeyPresses( void );
\r
122 /* Buffer used by the trace visualisation utility so only needed if the trace
\r
124 #if configUSE_TRACE_FACILITY == 1
\r
125 static portCHAR pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
128 /* Constant definition used to turn on/off the pre-emptive scheduler. */
\r
129 static const portSHORT sUsingPreemption = configUSE_PREEMPTION;
\r
131 /* Start the math tasks appropriate to the build. The Borland port does
\r
132 not yet support floating point so uses the integer equivalent. */
\r
133 static void prvStartMathTasks( void );
\r
135 /* Check which ever tasks are relevant to this build. */
\r
136 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void );
\r
138 /*-----------------------------------------------------------*/
\r
140 portSHORT main( void )
\r
142 /* Initialise hardware and utilities. */
\r
143 vParTestInitialise();
\r
144 vPrintInitialise();
\r
146 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
147 prvStartMathTasks();
\r
148 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM1, ser115200 );
\r
149 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
150 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
151 vCreateBlockTimeTasks();
\r
152 vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
153 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
154 vStartDynamicPriorityTasks();
\r
155 vStartMultiEventTasks();
\r
156 vStartQueuePeekTasks();
\r
157 vStartCountingSemaphoreTasks();
\r
159 /* Create the "Print" task as described at the top of the file. */
\r
160 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
162 /* This task has to be created last as it keeps account of the number of tasks
\r
163 it expects to see running. */
\r
164 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
166 /* Create the co-routines that flash the LED's. */
\r
167 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
\r
169 /* Create the co-routines that communicate with the tick hook. */
\r
170 vStartHookCoRoutines();
\r
172 /* Set the scheduler running. This function will not return unless a task
\r
173 calls vTaskEndScheduler(). */
\r
174 vTaskStartScheduler();
\r
178 /*-----------------------------------------------------------*/
\r
180 static void vErrorChecks( void *pvParameters )
\r
182 portTickType xExpectedWakeTime;
\r
183 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;
\r
184 const portLONG lMaxAllowableTimeDifference = ( portLONG ) 0;
\r
185 portTickType xWakeTime;
\r
186 portLONG lTimeDifference;
\r
187 const portCHAR *pcReceivedMessage;
\r
188 const portCHAR * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
190 ( void ) pvParameters;
\r
192 /* Loop continuously, blocking, then checking all the other tasks are still
\r
193 running, before blocking once again. This task blocks on the queue of
\r
194 messages that require displaying so will wake either by its time out expiring,
\r
195 or a message becoming available. */
\r
198 /* Calculate the time we will unblock if no messages are received
\r
199 on the queue. This is used to check that we have not blocked for too long. */
\r
200 xExpectedWakeTime = xTaskGetTickCount();
\r
201 xExpectedWakeTime += xPrintRate;
\r
203 /* Block waiting for either a time out or a message to be posted that
\r
204 required displaying. */
\r
205 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
207 /* Was a message received? */
\r
208 if( pcReceivedMessage == NULL )
\r
210 /* A message was not received so we timed out, did we unblock at the
\r
212 xWakeTime = xTaskGetTickCount();
\r
214 /* Calculate the difference between the time we unblocked and the
\r
215 time we should have unblocked. */
\r
216 if( xWakeTime > xExpectedWakeTime )
\r
218 lTimeDifference = ( portLONG ) ( xWakeTime - xExpectedWakeTime );
\r
222 lTimeDifference = ( portLONG ) ( xExpectedWakeTime - xWakeTime );
\r
225 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
227 /* We blocked too long - create a message that will get
\r
228 printed out the next time around. If we are not using
\r
229 preemption then we won't expect the timing to be so
\r
231 if( sUsingPreemption == pdTRUE )
\r
233 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
237 /* Check the other tasks are still running, just in case. */
\r
238 prvCheckOtherTasksAreStillRunning();
\r
242 /* We unblocked due to a message becoming available. Send the message
\r
244 vDisplayMessage( pcReceivedMessage );
\r
247 /* Key presses are used to invoke the trace visualisation utility, or end
\r
249 prvCheckForKeyPresses();
\r
252 /*-----------------------------------------------------------*/
\r
254 static void prvCheckForKeyPresses( void )
\r
258 taskENTER_CRITICAL();
\r
260 /* kbhit can be used in .exe's that are executed from the command
\r
261 line, but not if executed through the debugger. */
\r
266 taskEXIT_CRITICAL();
\r
270 /* Key presses can be used to start/stop the trace utility, or end the
\r
275 /* Only define keys for turning on and off the trace if the trace
\r
277 #if configUSE_TRACE_FACILITY == 1
\r
278 case 't' : vTaskList( pcWriteBuffer );
\r
279 vWriteMessageToDisk( pcWriteBuffer );
\r
281 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
285 unsigned portLONG ulBufferLength;
\r
286 ulBufferLength = ulTaskEndTrace();
\r
287 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
292 default : vTaskEndScheduler();
\r
297 /*-----------------------------------------------------------*/
\r
299 static void prvCheckOtherTasksAreStillRunning( void )
\r
301 static portSHORT sErrorHasOccurred = pdFALSE;
\r
303 if( prvCheckMathTasksAreStillRunning() != pdTRUE )
\r
305 vDisplayMessage( "Maths task count unchanged!\r\n" );
\r
306 sErrorHasOccurred = pdTRUE;
\r
309 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
311 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
312 sErrorHasOccurred = pdTRUE;
\r
315 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
317 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
318 sErrorHasOccurred = pdTRUE;
\r
321 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
323 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
324 sErrorHasOccurred = pdTRUE;
\r
327 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
329 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
330 sErrorHasOccurred = pdTRUE;
\r
333 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
335 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
336 sErrorHasOccurred = pdTRUE;
\r
339 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
341 vDisplayMessage( "Dynamic priority count unchanged!\r\n" );
\r
342 sErrorHasOccurred = pdTRUE;
\r
345 if( xAreMultiEventTasksStillRunning() != pdTRUE )
\r
347 vDisplayMessage( "Error in multi events tasks!\r\n" );
\r
348 sErrorHasOccurred = pdTRUE;
\r
351 if( xAreFlashCoRoutinesStillRunning() != pdTRUE )
\r
353 vDisplayMessage( "Error in co-routine flash tasks!\r\n" );
\r
354 sErrorHasOccurred = pdTRUE;
\r
357 if( xAreHookCoRoutinesStillRunning() != pdTRUE )
\r
359 vDisplayMessage( "Error in tick hook to co-routine communications!\r\n" );
\r
360 sErrorHasOccurred = pdTRUE;
\r
363 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
365 vDisplayMessage( "Error in block time test tasks!\r\n" );
\r
366 sErrorHasOccurred = pdTRUE;
\r
369 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
371 vDisplayMessage( "Error in generic queue test task!\r\n" );
\r
372 sErrorHasOccurred = pdTRUE;
\r
375 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
377 vDisplayMessage( "Error in queue peek test task!\r\n" );
\r
378 sErrorHasOccurred = pdTRUE;
\r
381 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
383 vDisplayMessage( "Error in counting semaphore demo task!\r\n" );
\r
384 sErrorHasOccurred = pdTRUE;
\r
387 if( sErrorHasOccurred == pdFALSE )
\r
389 vDisplayMessage( "OK " );
\r
392 /*-----------------------------------------------------------*/
\r
394 static void prvStartMathTasks( void )
\r
396 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
397 /* The Borland project does not yet support floating point. */
\r
398 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
400 vStartMathTasks( tskIDLE_PRIORITY );
\r
403 /*-----------------------------------------------------------*/
\r
405 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void )
\r
407 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
408 /* The Borland project does not yet support floating point. */
\r
409 return xAreIntegerMathsTaskStillRunning();
\r
411 return xAreMathsTaskStillRunning();
\r
414 /*-----------------------------------------------------------*/
\r
416 void vApplicationIdleHook( void )
\r
418 /* The co-routines are executed in the idle task using the idle task
\r
420 vCoRoutineSchedule();
\r
422 /*-----------------------------------------------------------*/
\r