2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 * Creates all the demo application tasks and co-routines, then starts the
\r
73 * Main. c also creates a task called "Print". This only executes every
\r
74 * five seconds but has the highest priority so is guaranteed to get
\r
75 * processor time. Its main function is to check that all the other tasks
\r
76 * are still operational. Nearly all the tasks in the demo application
\r
77 * maintain a unique count that is incremented each time the task successfully
\r
78 * completes its function. Should any error occur within the task the count is
\r
79 * permanently halted. The print task checks the count of each task to ensure
\r
80 * it has changed since the last time the print task executed. If any count is
\r
81 * found not to have changed the print task displays an appropriate message.
\r
82 * If all the tasks are still incrementing their unique counts the print task
\r
83 * displays an "OK" message.
\r
85 * The LED flash tasks do not maintain a count as they already provide visual
\r
86 * feedback of their status.
\r
88 * The print task blocks on the queue into which messages that require
\r
89 * displaying are posted. It will therefore only block for the full 5 seconds
\r
90 * if no messages are posted onto the queue.
\r
92 * Main. c also provides a demonstration of how the trace visualisation utility
\r
93 * can be used, and how the scheduler can be stopped.
\r
95 * \page MainC main.c
\r
96 * \ingroup DemoFiles
\r
100 #include <stdlib.h>
\r
102 #include "FreeRTOS.h"
\r
104 #include "croutine.h"
\r
105 #include "partest.h"
\r
106 #include "serial.h"
\r
108 /* Demo file headers. */
\r
109 #include "BlockQ.h"
\r
112 #include "crflash.h"
\r
115 #include "comtest.h"
\r
116 #include "fileio.h"
\r
117 #include "semtest.h"
\r
118 #include "integer.h"
\r
119 #include "dynamic.h"
\r
120 #include "mevents.h"
\r
121 #include "crhook.h"
\r
122 #include "blocktim.h"
\r
123 #include "AltBlock.h"
\r
124 #include "GenQTest.h"
\r
126 #include "countsem.h"
\r
127 #include "AltQTest.h"
\r
128 #include "AltPollQ.h"
\r
129 #include "AltBlckQ.h"
\r
130 #include "RecMutex.h"
\r
132 /* Priority definitions for the tasks in the demo application. */
\r
133 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
134 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
135 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
136 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
137 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
138 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
139 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
140 #define mainGENERIC_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
142 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 512 )
\r
143 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\r
145 /* The number of flash co-routines to create. */
\r
146 #define mainNUM_FLASH_CO_ROUTINES ( 8 )
\r
148 /* Task function for the "Print" task as described at the top of the file. */
\r
149 static void vErrorChecks( void *pvParameters );
\r
151 /* Function that checks the unique count of all the other tasks as described at
\r
152 the top of the file. */
\r
153 static void prvCheckOtherTasksAreStillRunning( void );
\r
155 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
157 static void prvCheckForKeyPresses( void );
\r
159 /* Buffer used by the trace visualisation utility so only needed if the trace
\r
161 #if configUSE_TRACE_FACILITY == 1
\r
162 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
165 /* Constant definition used to turn on/off the pre-emptive scheduler. */
\r
166 static const short sUsingPreemption = configUSE_PREEMPTION;
\r
168 /* Start the math tasks appropriate to the build. The Borland port does
\r
169 not yet support floating point so uses the integer equivalent. */
\r
170 static void prvStartMathTasks( void );
\r
172 /* Check which ever tasks are relevant to this build. */
\r
173 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void );
\r
175 /* Used to demonstrate the "task switched in" callback function. */
\r
176 static portBASE_TYPE prvExampleTaskHook( void * pvParameter );
\r
178 /* Just used to count the number of times the example task callback function is
\r
179 called, and the number of times a queue send passes. */
\r
180 static unsigned long long uxCheckTaskHookCallCount = 0;
\r
181 static unsigned long long uxQueueSendPassedCount = 0;
\r
183 /*-----------------------------------------------------------*/
\r
187 /* Initialise hardware and utilities. */
\r
188 vParTestInitialise();
\r
189 vPrintInitialise();
\r
191 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
192 prvStartMathTasks();
\r
193 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM1, ser115200 );
\r
194 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
195 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
196 vCreateBlockTimeTasks();
\r
197 vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
198 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
199 vStartDynamicPriorityTasks();
\r
200 vStartMultiEventTasks();
\r
201 vStartQueuePeekTasks();
\r
202 vStartCountingSemaphoreTasks();
\r
203 vStartAltGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
204 vCreateAltBlockTimeTasks();
\r
205 vStartAltBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
206 vStartAltPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
207 vStartRecursiveMutexTasks();
\r
209 /* Create the "Print" task as described at the top of the file. */
\r
210 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
212 /* This task has to be created last as it keeps account of the number of tasks
\r
213 it expects to see running. */
\r
214 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
216 /* Create the co-routines that flash the LED's. */
\r
217 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
\r
219 /* Create the co-routines that communicate with the tick hook. */
\r
220 vStartHookCoRoutines();
\r
222 /* Set the scheduler running. This function will not return unless a task
\r
223 calls vTaskEndScheduler(). */
\r
224 vTaskStartScheduler();
\r
228 /*-----------------------------------------------------------*/
\r
230 static portBASE_TYPE prvExampleTaskHook( void * pvParameter )
\r
232 if( pvParameter != ( void * ) 0xabcd )
\r
234 /* The parameter did not contain the value we expected, so cause an
\r
235 error to be detected by setting the call count back to zero. */
\r
236 uxCheckTaskHookCallCount = 0;
\r
240 /* Simply increment a number so we know the callback has been executed. */
\r
241 uxCheckTaskHookCallCount++;
\r
246 /*-----------------------------------------------------------*/
\r
248 void vMainQueueSendPassed( void )
\r
250 /* This is just an example implementation of the "queue send" trace hook. */
\r
251 uxQueueSendPassedCount++;
\r
253 /*-----------------------------------------------------------*/
\r
255 static void vErrorChecks( void *pvParameters )
\r
257 portTickType xExpectedWakeTime;
\r
258 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;
\r
259 const long lMaxAllowableTimeDifference = ( long ) 0;
\r
260 portTickType xWakeTime;
\r
261 long lTimeDifference;
\r
262 const char *pcReceivedMessage;
\r
263 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
264 const char * const pcUnexpectedHookValueMsg = "Task hook has unexpected value!\r\n";
\r
266 ( void ) pvParameters;
\r
268 /* Register our callback function. */
\r
269 vTaskSetApplicationTaskTag( NULL, prvExampleTaskHook );
\r
271 /* Just for test purposes. */
\r
272 if( xTaskGetApplicationTaskTag( NULL ) != prvExampleTaskHook )
\r
274 vPrintDisplayMessage( &pcUnexpectedHookValueMsg );
\r
277 /* Loop continuously, blocking, then checking all the other tasks are still
\r
278 running, before blocking once again. This task blocks on the queue of
\r
279 messages that require displaying so will wake either by its time out expiring,
\r
280 or a message becoming available. */
\r
283 /* Calculate the time we will unblock if no messages are received
\r
284 on the queue. This is used to check that we have not blocked for too long. */
\r
285 xExpectedWakeTime = xTaskGetTickCount();
\r
286 xExpectedWakeTime += xPrintRate;
\r
288 /* Block waiting for either a time out or a message to be posted that
\r
289 required displaying. */
\r
290 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
292 /* Was a message received? */
\r
293 if( pcReceivedMessage == NULL )
\r
295 /* A message was not received so we timed out, did we unblock at the
\r
297 xWakeTime = xTaskGetTickCount();
\r
299 /* Calculate the difference between the time we unblocked and the
\r
300 time we should have unblocked. */
\r
301 if( xWakeTime > xExpectedWakeTime )
\r
303 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
307 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
310 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
312 /* We blocked too long - create a message that will get
\r
313 printed out the next time around. If we are not using
\r
314 preemption then we won't expect the timing to be so
\r
316 if( sUsingPreemption == pdTRUE )
\r
318 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
322 /* Check the other tasks are still running, just in case. */
\r
323 prvCheckOtherTasksAreStillRunning();
\r
327 /* We unblocked due to a message becoming available. Send the message
\r
329 vDisplayMessage( pcReceivedMessage );
\r
332 /* Key presses are used to invoke the trace visualisation utility, or end
\r
334 prvCheckForKeyPresses();
\r
337 /*-----------------------------------------------------------*/
\r
339 static void prvCheckForKeyPresses( void )
\r
343 taskENTER_CRITICAL();
\r
345 /* kbhit can be used in .exe's that are executed from the command
\r
346 line, but not if executed through the debugger. */
\r
351 taskEXIT_CRITICAL();
\r
355 /* Key presses can be used to start/stop the trace utility, or end the
\r
360 /* Only define keys for turning on and off the trace if the trace
\r
362 #if configUSE_TRACE_FACILITY == 1
\r
363 case 't' : vTaskList( pcWriteBuffer );
\r
364 vWriteMessageToDisk( pcWriteBuffer );
\r
366 /* The legacy trace is no longer supported. Use FreeRTOS+Trace instead
\r
367 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
371 unsigned long ulBufferLength;
\r
372 ulBufferLength = ulTaskEndTrace();
\r
373 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
379 default : vTaskEndScheduler();
\r
384 /*-----------------------------------------------------------*/
\r
386 static void prvCheckOtherTasksAreStillRunning( void )
\r
388 static short sErrorHasOccurred = pdFALSE;
\r
389 static unsigned long long uxLastHookCallCount = 0, uxLastQueueSendCount = 0;
\r
391 if( prvCheckMathTasksAreStillRunning() != pdTRUE )
\r
393 vDisplayMessage( "Maths task count unchanged!\r\n" );
\r
394 sErrorHasOccurred = pdTRUE;
\r
397 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
399 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
400 sErrorHasOccurred = pdTRUE;
\r
403 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
405 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
406 sErrorHasOccurred = pdTRUE;
\r
409 if( xAreAltBlockingQueuesStillRunning() != pdTRUE )
\r
411 vDisplayMessage( "Alt blocking queues count unchanged!\r\n" );
\r
412 sErrorHasOccurred = pdTRUE;
\r
415 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
417 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
418 sErrorHasOccurred = pdTRUE;
\r
421 if( xAreAltPollingQueuesStillRunning() != pdTRUE )
\r
423 vDisplayMessage( "Alt polling queue count unchanged!\r\n" );
\r
424 sErrorHasOccurred = pdTRUE;
\r
427 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
429 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
430 sErrorHasOccurred = pdTRUE;
\r
433 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
435 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
436 sErrorHasOccurred = pdTRUE;
\r
439 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
441 vDisplayMessage( "Dynamic priority count unchanged!\r\n" );
\r
442 sErrorHasOccurred = pdTRUE;
\r
445 if( xAreMultiEventTasksStillRunning() != pdTRUE )
\r
447 vDisplayMessage( "Error in multi events tasks!\r\n" );
\r
448 sErrorHasOccurred = pdTRUE;
\r
451 if( xAreFlashCoRoutinesStillRunning() != pdTRUE )
\r
453 vDisplayMessage( "Error in co-routine flash tasks!\r\n" );
\r
454 sErrorHasOccurred = pdTRUE;
\r
457 if( xAreHookCoRoutinesStillRunning() != pdTRUE )
\r
459 vDisplayMessage( "Error in tick hook to co-routine communications!\r\n" );
\r
460 sErrorHasOccurred = pdTRUE;
\r
463 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
465 vDisplayMessage( "Error in block time test tasks!\r\n" );
\r
466 sErrorHasOccurred = pdTRUE;
\r
469 if( xAreAltBlockTimeTestTasksStillRunning() != pdTRUE )
\r
471 vDisplayMessage( "Error in fast block time test tasks!\r\n" );
\r
472 sErrorHasOccurred = pdTRUE;
\r
475 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
477 vDisplayMessage( "Error in generic queue test task!\r\n" );
\r
478 sErrorHasOccurred = pdTRUE;
\r
481 if( xAreAltGenericQueueTasksStillRunning() != pdTRUE )
\r
483 vDisplayMessage( "Error in fast generic queue test task!\r\n" );
\r
484 sErrorHasOccurred = pdTRUE;
\r
487 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
489 vDisplayMessage( "Error in queue peek test task!\r\n" );
\r
490 sErrorHasOccurred = pdTRUE;
\r
493 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
495 vDisplayMessage( "Error in counting semaphore demo task!\r\n" );
\r
496 sErrorHasOccurred = pdTRUE;
\r
499 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
501 vDisplayMessage( "Error in recursive mutex tasks!\r\n" );
\r
502 sErrorHasOccurred = pdTRUE;
\r
505 /* The hook function associated with this task is called each time the task
\r
506 is switched in. We therefore expect the number of times the callback
\r
507 function has been executed to have increrment since the last time this
\r
508 function executed. */
\r
509 if( uxCheckTaskHookCallCount <= uxLastHookCallCount )
\r
511 vDisplayMessage( "Error in task hook call count!\r\n" );
\r
512 sErrorHasOccurred = pdTRUE;
\r
516 uxLastHookCallCount = uxCheckTaskHookCallCount;
\r
519 /* We would expect some queue sending to occur between calls of this
\r
521 if( uxQueueSendPassedCount <= uxLastQueueSendCount )
\r
523 vDisplayMessage( "Error in queue send hook call count!\r\n" );
\r
524 sErrorHasOccurred = pdTRUE;
\r
528 uxLastQueueSendCount = uxQueueSendPassedCount;
\r
531 if( sErrorHasOccurred == pdFALSE )
\r
533 vDisplayMessage( "OK " );
\r
536 /*-----------------------------------------------------------*/
\r
538 static void prvStartMathTasks( void )
\r
540 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
541 /* The Borland project does not yet support floating point. */
\r
542 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
544 vStartMathTasks( tskIDLE_PRIORITY );
\r
547 /*-----------------------------------------------------------*/
\r
549 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void )
\r
551 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
552 /* The Borland project does not yet support floating point. */
\r
553 return xAreIntegerMathsTaskStillRunning();
\r
555 return xAreMathsTaskStillRunning();
\r
558 /*-----------------------------------------------------------*/
\r
560 void vApplicationIdleHook( void )
\r
562 /* The co-routines are executed in the idle task using the idle task
\r
564 vCoRoutineSchedule();
\r
566 /*-----------------------------------------------------------*/
\r