2 FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * Creates all the demo application tasks and co-routines, then starts the
\r
69 * Main. c also creates a task called "Print". This only executes every
\r
70 * five seconds but has the highest priority so is guaranteed to get
\r
71 * processor time. Its main function is to check that all the other tasks
\r
72 * are still operational. Nearly all the tasks in the demo application
\r
73 * maintain a unique count that is incremented each time the task successfully
\r
74 * completes its function. Should any error occur within the task the count is
\r
75 * permanently halted. The print task checks the count of each task to ensure
\r
76 * it has changed since the last time the print task executed. If any count is
\r
77 * found not to have changed the print task displays an appropriate message.
\r
78 * If all the tasks are still incrementing their unique counts the print task
\r
79 * displays an "OK" message.
\r
81 * The LED flash tasks do not maintain a count as they already provide visual
\r
82 * feedback of their status.
\r
84 * The print task blocks on the queue into which messages that require
\r
85 * displaying are posted. It will therefore only block for the full 5 seconds
\r
86 * if no messages are posted onto the queue.
\r
88 * Main. c also provides a demonstration of how the trace visualisation utility
\r
89 * can be used, and how the scheduler can be stopped.
\r
91 * \page MainC main.c
\r
92 * \ingroup DemoFiles
\r
98 #include "FreeRTOS.h"
\r
100 #include "croutine.h"
\r
101 #include "partest.h"
\r
102 #include "serial.h"
\r
104 /* Demo file headers. */
\r
105 #include "BlockQ.h"
\r
108 #include "crflash.h"
\r
111 #include "comtest.h"
\r
112 #include "fileio.h"
\r
113 #include "semtest.h"
\r
114 #include "integer.h"
\r
115 #include "dynamic.h"
\r
116 #include "mevents.h"
\r
117 #include "crhook.h"
\r
118 #include "blocktim.h"
\r
119 #include "AltBlock.h"
\r
120 #include "GenQTest.h"
\r
122 #include "countsem.h"
\r
123 #include "AltQTest.h"
\r
124 #include "AltPollQ.h"
\r
125 #include "AltBlckQ.h"
\r
126 #include "RecMutex.h"
\r
128 /* Priority definitions for the tasks in the demo application. */
\r
129 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
130 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
131 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
132 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
133 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
134 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
135 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
136 #define mainGENERIC_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
138 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 512 )
\r
139 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\r
141 /* The number of flash co-routines to create. */
\r
142 #define mainNUM_FLASH_CO_ROUTINES ( 8 )
\r
144 /* Task function for the "Print" task as described at the top of the file. */
\r
145 static void vErrorChecks( void *pvParameters );
\r
147 /* Function that checks the unique count of all the other tasks as described at
\r
148 the top of the file. */
\r
149 static void prvCheckOtherTasksAreStillRunning( void );
\r
151 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
153 static void prvCheckForKeyPresses( void );
\r
155 /* Buffer used by the trace visualisation utility so only needed if the trace
\r
157 #if configUSE_TRACE_FACILITY == 1
\r
158 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
161 /* Constant definition used to turn on/off the pre-emptive scheduler. */
\r
162 static const short sUsingPreemption = configUSE_PREEMPTION;
\r
164 /* Start the math tasks appropriate to the build. The Borland port does
\r
165 not yet support floating point so uses the integer equivalent. */
\r
166 static void prvStartMathTasks( void );
\r
168 /* Check which ever tasks are relevant to this build. */
\r
169 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void );
\r
171 /* Used to demonstrate the "task switched in" callback function. */
\r
172 static portBASE_TYPE prvExampleTaskHook( void * pvParameter );
\r
174 /* Just used to count the number of times the example task callback function is
\r
175 called, and the number of times a queue send passes. */
\r
176 static unsigned long long uxCheckTaskHookCallCount = 0;
\r
177 static unsigned long long uxQueueSendPassedCount = 0;
\r
179 /*-----------------------------------------------------------*/
\r
183 /* Initialise hardware and utilities. */
\r
184 vParTestInitialise();
\r
185 vPrintInitialise();
\r
187 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
188 prvStartMathTasks();
\r
189 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM1, ser115200 );
\r
190 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
191 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
192 vCreateBlockTimeTasks();
\r
193 vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
194 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
195 vStartDynamicPriorityTasks();
\r
196 vStartMultiEventTasks();
\r
197 vStartQueuePeekTasks();
\r
198 vStartCountingSemaphoreTasks();
\r
199 vStartAltGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
200 vCreateAltBlockTimeTasks();
\r
201 vStartAltBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
202 vStartAltPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
203 vStartRecursiveMutexTasks();
\r
205 /* Create the "Print" task as described at the top of the file. */
\r
206 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
208 /* This task has to be created last as it keeps account of the number of tasks
\r
209 it expects to see running. */
\r
210 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
212 /* Create the co-routines that flash the LED's. */
\r
213 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
\r
215 /* Create the co-routines that communicate with the tick hook. */
\r
216 vStartHookCoRoutines();
\r
218 /* Set the scheduler running. This function will not return unless a task
\r
219 calls vTaskEndScheduler(). */
\r
220 vTaskStartScheduler();
\r
224 /*-----------------------------------------------------------*/
\r
226 static portBASE_TYPE prvExampleTaskHook( void * pvParameter )
\r
228 if( pvParameter != ( void * ) 0xabcd )
\r
230 /* The parameter did not contain the value we expected, so cause an
\r
231 error to be detected by setting the call count back to zero. */
\r
232 uxCheckTaskHookCallCount = 0;
\r
236 /* Simply increment a number so we know the callback has been executed. */
\r
237 uxCheckTaskHookCallCount++;
\r
242 /*-----------------------------------------------------------*/
\r
244 void vMainQueueSendPassed( void )
\r
246 /* This is just an example implementation of the "queue send" trace hook. */
\r
247 uxQueueSendPassedCount++;
\r
249 /*-----------------------------------------------------------*/
\r
251 static void vErrorChecks( void *pvParameters )
\r
253 portTickType xExpectedWakeTime;
\r
254 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;
\r
255 const long lMaxAllowableTimeDifference = ( long ) 0;
\r
256 portTickType xWakeTime;
\r
257 long lTimeDifference;
\r
258 const char *pcReceivedMessage;
\r
259 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
260 const char * const pcUnexpectedHookValueMsg = "Task hook has unexpected value!\r\n";
\r
262 ( void ) pvParameters;
\r
264 /* Register our callback function. */
\r
265 vTaskSetApplicationTaskTag( NULL, prvExampleTaskHook );
\r
267 /* Just for test purposes. */
\r
268 if( xTaskGetApplicationTaskTag( NULL ) != prvExampleTaskHook )
\r
270 vPrintDisplayMessage( &pcUnexpectedHookValueMsg );
\r
273 /* Loop continuously, blocking, then checking all the other tasks are still
\r
274 running, before blocking once again. This task blocks on the queue of
\r
275 messages that require displaying so will wake either by its time out expiring,
\r
276 or a message becoming available. */
\r
279 /* Calculate the time we will unblock if no messages are received
\r
280 on the queue. This is used to check that we have not blocked for too long. */
\r
281 xExpectedWakeTime = xTaskGetTickCount();
\r
282 xExpectedWakeTime += xPrintRate;
\r
284 /* Block waiting for either a time out or a message to be posted that
\r
285 required displaying. */
\r
286 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
288 /* Was a message received? */
\r
289 if( pcReceivedMessage == NULL )
\r
291 /* A message was not received so we timed out, did we unblock at the
\r
293 xWakeTime = xTaskGetTickCount();
\r
295 /* Calculate the difference between the time we unblocked and the
\r
296 time we should have unblocked. */
\r
297 if( xWakeTime > xExpectedWakeTime )
\r
299 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
303 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
306 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
308 /* We blocked too long - create a message that will get
\r
309 printed out the next time around. If we are not using
\r
310 preemption then we won't expect the timing to be so
\r
312 if( sUsingPreemption == pdTRUE )
\r
314 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
318 /* Check the other tasks are still running, just in case. */
\r
319 prvCheckOtherTasksAreStillRunning();
\r
323 /* We unblocked due to a message becoming available. Send the message
\r
325 vDisplayMessage( pcReceivedMessage );
\r
328 /* Key presses are used to invoke the trace visualisation utility, or end
\r
330 prvCheckForKeyPresses();
\r
333 /*-----------------------------------------------------------*/
\r
335 static void prvCheckForKeyPresses( void )
\r
339 taskENTER_CRITICAL();
\r
341 /* kbhit can be used in .exe's that are executed from the command
\r
342 line, but not if executed through the debugger. */
\r
347 taskEXIT_CRITICAL();
\r
351 /* Key presses can be used to start/stop the trace utility, or end the
\r
356 /* Only define keys for turning on and off the trace if the trace
\r
358 #if configUSE_TRACE_FACILITY == 1
\r
359 case 't' : vTaskList( pcWriteBuffer );
\r
360 vWriteMessageToDisk( pcWriteBuffer );
\r
362 /* The legacy trace is no longer supported. Use FreeRTOS+Trace instead
\r
363 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
367 unsigned long ulBufferLength;
\r
368 ulBufferLength = ulTaskEndTrace();
\r
369 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
375 default : vTaskEndScheduler();
\r
380 /*-----------------------------------------------------------*/
\r
382 static void prvCheckOtherTasksAreStillRunning( void )
\r
384 static short sErrorHasOccurred = pdFALSE;
\r
385 static unsigned long long uxLastHookCallCount = 0, uxLastQueueSendCount = 0;
\r
387 if( prvCheckMathTasksAreStillRunning() != pdTRUE )
\r
389 vDisplayMessage( "Maths task count unchanged!\r\n" );
\r
390 sErrorHasOccurred = pdTRUE;
\r
393 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
395 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
396 sErrorHasOccurred = pdTRUE;
\r
399 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
401 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
402 sErrorHasOccurred = pdTRUE;
\r
405 if( xAreAltBlockingQueuesStillRunning() != pdTRUE )
\r
407 vDisplayMessage( "Alt blocking queues count unchanged!\r\n" );
\r
408 sErrorHasOccurred = pdTRUE;
\r
411 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
413 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
414 sErrorHasOccurred = pdTRUE;
\r
417 if( xAreAltPollingQueuesStillRunning() != pdTRUE )
\r
419 vDisplayMessage( "Alt polling queue count unchanged!\r\n" );
\r
420 sErrorHasOccurred = pdTRUE;
\r
423 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
425 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
426 sErrorHasOccurred = pdTRUE;
\r
429 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
431 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
432 sErrorHasOccurred = pdTRUE;
\r
435 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
437 vDisplayMessage( "Dynamic priority count unchanged!\r\n" );
\r
438 sErrorHasOccurred = pdTRUE;
\r
441 if( xAreMultiEventTasksStillRunning() != pdTRUE )
\r
443 vDisplayMessage( "Error in multi events tasks!\r\n" );
\r
444 sErrorHasOccurred = pdTRUE;
\r
447 if( xAreFlashCoRoutinesStillRunning() != pdTRUE )
\r
449 vDisplayMessage( "Error in co-routine flash tasks!\r\n" );
\r
450 sErrorHasOccurred = pdTRUE;
\r
453 if( xAreHookCoRoutinesStillRunning() != pdTRUE )
\r
455 vDisplayMessage( "Error in tick hook to co-routine communications!\r\n" );
\r
456 sErrorHasOccurred = pdTRUE;
\r
459 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
461 vDisplayMessage( "Error in block time test tasks!\r\n" );
\r
462 sErrorHasOccurred = pdTRUE;
\r
465 if( xAreAltBlockTimeTestTasksStillRunning() != pdTRUE )
\r
467 vDisplayMessage( "Error in fast block time test tasks!\r\n" );
\r
468 sErrorHasOccurred = pdTRUE;
\r
471 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
473 vDisplayMessage( "Error in generic queue test task!\r\n" );
\r
474 sErrorHasOccurred = pdTRUE;
\r
477 if( xAreAltGenericQueueTasksStillRunning() != pdTRUE )
\r
479 vDisplayMessage( "Error in fast generic queue test task!\r\n" );
\r
480 sErrorHasOccurred = pdTRUE;
\r
483 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
485 vDisplayMessage( "Error in queue peek test task!\r\n" );
\r
486 sErrorHasOccurred = pdTRUE;
\r
489 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
491 vDisplayMessage( "Error in counting semaphore demo task!\r\n" );
\r
492 sErrorHasOccurred = pdTRUE;
\r
495 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
497 vDisplayMessage( "Error in recursive mutex tasks!\r\n" );
\r
498 sErrorHasOccurred = pdTRUE;
\r
501 /* The hook function associated with this task is called each time the task
\r
502 is switched in. We therefore expect the number of times the callback
\r
503 function has been executed to have increrment since the last time this
\r
504 function executed. */
\r
505 if( uxCheckTaskHookCallCount <= uxLastHookCallCount )
\r
507 vDisplayMessage( "Error in task hook call count!\r\n" );
\r
508 sErrorHasOccurred = pdTRUE;
\r
512 uxLastHookCallCount = uxCheckTaskHookCallCount;
\r
515 /* We would expect some queue sending to occur between calls of this
\r
517 if( uxQueueSendPassedCount <= uxLastQueueSendCount )
\r
519 vDisplayMessage( "Error in queue send hook call count!\r\n" );
\r
520 sErrorHasOccurred = pdTRUE;
\r
524 uxLastQueueSendCount = uxQueueSendPassedCount;
\r
527 if( sErrorHasOccurred == pdFALSE )
\r
529 vDisplayMessage( "OK " );
\r
532 /*-----------------------------------------------------------*/
\r
534 static void prvStartMathTasks( void )
\r
536 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
537 /* The Borland project does not yet support floating point. */
\r
538 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
540 vStartMathTasks( tskIDLE_PRIORITY );
\r
543 /*-----------------------------------------------------------*/
\r
545 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void )
\r
547 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
548 /* The Borland project does not yet support floating point. */
\r
549 return xAreIntegerMathsTaskStillRunning();
\r
551 return xAreMathsTaskStillRunning();
\r
554 /*-----------------------------------------------------------*/
\r
556 void vApplicationIdleHook( void )
\r
558 /* The co-routines are executed in the idle task using the idle task
\r
560 vCoRoutineSchedule();
\r
562 /*-----------------------------------------------------------*/
\r