2 FreeRTOS V7.4.2 - Copyright (C) 2013 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
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 * Creates all the demo application tasks and co-routines, then starts the
\r
79 * Main. c also creates a task called "Print". This only executes every
\r
80 * five seconds but has the highest priority so is guaranteed to get
\r
81 * processor time. Its main function is to check that all the other tasks
\r
82 * are still operational. Nearly all the tasks in the demo application
\r
83 * maintain a unique count that is incremented each time the task successfully
\r
84 * completes its function. Should any error occur within the task the count is
\r
85 * permanently halted. The print task checks the count of each task to ensure
\r
86 * it has changed since the last time the print task executed. If any count is
\r
87 * found not to have changed the print task displays an appropriate message.
\r
88 * If all the tasks are still incrementing their unique counts the print task
\r
89 * displays an "OK" message.
\r
91 * The LED flash tasks do not maintain a count as they already provide visual
\r
92 * feedback of their status.
\r
94 * The print task blocks on the queue into which messages that require
\r
95 * displaying are posted. It will therefore only block for the full 5 seconds
\r
96 * if no messages are posted onto the queue.
\r
98 * Main. c also provides a demonstration of how the trace visualisation utility
\r
99 * can be used, and how the scheduler can be stopped.
\r
101 * \page MainC main.c
\r
102 * \ingroup DemoFiles
\r
106 #include <stdlib.h>
\r
108 #include "FreeRTOS.h"
\r
110 #include "croutine.h"
\r
111 #include "partest.h"
\r
112 #include "serial.h"
\r
114 /* Demo file headers. */
\r
115 #include "BlockQ.h"
\r
118 #include "crflash.h"
\r
121 #include "comtest.h"
\r
122 #include "fileio.h"
\r
123 #include "semtest.h"
\r
124 #include "integer.h"
\r
125 #include "dynamic.h"
\r
126 #include "mevents.h"
\r
127 #include "crhook.h"
\r
128 #include "blocktim.h"
\r
129 #include "AltBlock.h"
\r
130 #include "GenQTest.h"
\r
132 #include "countsem.h"
\r
133 #include "AltQTest.h"
\r
134 #include "AltPollQ.h"
\r
135 #include "AltBlckQ.h"
\r
136 #include "RecMutex.h"
\r
138 /* Priority definitions for the tasks in the demo application. */
\r
139 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
140 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
141 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
142 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
143 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
144 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
145 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
146 #define mainGENERIC_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
148 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 512 )
\r
149 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\r
151 /* The number of flash co-routines to create. */
\r
152 #define mainNUM_FLASH_CO_ROUTINES ( 8 )
\r
154 /* Task function for the "Print" task as described at the top of the file. */
\r
155 static void vErrorChecks( void *pvParameters );
\r
157 /* Function that checks the unique count of all the other tasks as described at
\r
158 the top of the file. */
\r
159 static void prvCheckOtherTasksAreStillRunning( void );
\r
161 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
163 static void prvCheckForKeyPresses( void );
\r
165 /* Buffer used by the trace visualisation utility so only needed if the trace
\r
167 #if configUSE_TRACE_FACILITY == 1
\r
168 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
171 /* Constant definition used to turn on/off the pre-emptive scheduler. */
\r
172 static const short sUsingPreemption = configUSE_PREEMPTION;
\r
174 /* Start the math tasks appropriate to the build. The Borland port does
\r
175 not yet support floating point so uses the integer equivalent. */
\r
176 static void prvStartMathTasks( void );
\r
178 /* Check which ever tasks are relevant to this build. */
\r
179 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void );
\r
181 /* Used to demonstrate the "task switched in" callback function. */
\r
182 static portBASE_TYPE prvExampleTaskHook( void * pvParameter );
\r
184 /* Just used to count the number of times the example task callback function is
\r
185 called, and the number of times a queue send passes. */
\r
186 static unsigned long long uxCheckTaskHookCallCount = 0;
\r
187 static unsigned long long uxQueueSendPassedCount = 0;
\r
189 /*-----------------------------------------------------------*/
\r
193 /* Initialise hardware and utilities. */
\r
194 vParTestInitialise();
\r
195 vPrintInitialise();
\r
197 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
198 prvStartMathTasks();
\r
199 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM1, ser115200 );
\r
200 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
201 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
202 vCreateBlockTimeTasks();
\r
203 vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
204 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
205 vStartDynamicPriorityTasks();
\r
206 vStartMultiEventTasks();
\r
207 vStartQueuePeekTasks();
\r
208 vStartCountingSemaphoreTasks();
\r
209 vStartAltGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
210 vCreateAltBlockTimeTasks();
\r
211 vStartAltBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
212 vStartAltPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
213 vStartRecursiveMutexTasks();
\r
215 /* Create the "Print" task as described at the top of the file. */
\r
216 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
218 /* This task has to be created last as it keeps account of the number of tasks
\r
219 it expects to see running. */
\r
220 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
222 /* Create the co-routines that flash the LED's. */
\r
223 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
\r
225 /* Create the co-routines that communicate with the tick hook. */
\r
226 vStartHookCoRoutines();
\r
228 /* Set the scheduler running. This function will not return unless a task
\r
229 calls vTaskEndScheduler(). */
\r
230 vTaskStartScheduler();
\r
234 /*-----------------------------------------------------------*/
\r
236 static portBASE_TYPE prvExampleTaskHook( void * pvParameter )
\r
238 if( pvParameter != ( void * ) 0xabcd )
\r
240 /* The parameter did not contain the value we expected, so cause an
\r
241 error to be detected by setting the call count back to zero. */
\r
242 uxCheckTaskHookCallCount = 0;
\r
246 /* Simply increment a number so we know the callback has been executed. */
\r
247 uxCheckTaskHookCallCount++;
\r
252 /*-----------------------------------------------------------*/
\r
254 void vMainQueueSendPassed( void )
\r
256 /* This is just an example implementation of the "queue send" trace hook. */
\r
257 uxQueueSendPassedCount++;
\r
259 /*-----------------------------------------------------------*/
\r
261 static void vErrorChecks( void *pvParameters )
\r
263 portTickType xExpectedWakeTime;
\r
264 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;
\r
265 const long lMaxAllowableTimeDifference = ( long ) 0;
\r
266 portTickType xWakeTime;
\r
267 long lTimeDifference;
\r
268 const char *pcReceivedMessage;
\r
269 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
270 const char * const pcUnexpectedHookValueMsg = "Task hook has unexpected value!\r\n";
\r
272 ( void ) pvParameters;
\r
274 /* Register our callback function. */
\r
275 vTaskSetApplicationTaskTag( NULL, prvExampleTaskHook );
\r
277 /* Just for test purposes. */
\r
278 if( xTaskGetApplicationTaskTag( NULL ) != prvExampleTaskHook )
\r
280 vPrintDisplayMessage( &pcUnexpectedHookValueMsg );
\r
283 /* Loop continuously, blocking, then checking all the other tasks are still
\r
284 running, before blocking once again. This task blocks on the queue of
\r
285 messages that require displaying so will wake either by its time out expiring,
\r
286 or a message becoming available. */
\r
289 /* Calculate the time we will unblock if no messages are received
\r
290 on the queue. This is used to check that we have not blocked for too long. */
\r
291 xExpectedWakeTime = xTaskGetTickCount();
\r
292 xExpectedWakeTime += xPrintRate;
\r
294 /* Block waiting for either a time out or a message to be posted that
\r
295 required displaying. */
\r
296 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
298 /* Was a message received? */
\r
299 if( pcReceivedMessage == NULL )
\r
301 /* A message was not received so we timed out, did we unblock at the
\r
303 xWakeTime = xTaskGetTickCount();
\r
305 /* Calculate the difference between the time we unblocked and the
\r
306 time we should have unblocked. */
\r
307 if( xWakeTime > xExpectedWakeTime )
\r
309 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
313 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
316 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
318 /* We blocked too long - create a message that will get
\r
319 printed out the next time around. If we are not using
\r
320 preemption then we won't expect the timing to be so
\r
322 if( sUsingPreemption == pdTRUE )
\r
324 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
328 /* Check the other tasks are still running, just in case. */
\r
329 prvCheckOtherTasksAreStillRunning();
\r
333 /* We unblocked due to a message becoming available. Send the message
\r
335 vDisplayMessage( pcReceivedMessage );
\r
338 /* Key presses are used to invoke the trace visualisation utility, or end
\r
340 prvCheckForKeyPresses();
\r
343 /*-----------------------------------------------------------*/
\r
345 static void prvCheckForKeyPresses( void )
\r
349 taskENTER_CRITICAL();
\r
351 /* kbhit can be used in .exe's that are executed from the command
\r
352 line, but not if executed through the debugger. */
\r
357 taskEXIT_CRITICAL();
\r
361 /* Key presses can be used to start/stop the trace utility, or end the
\r
366 /* Only define keys for turning on and off the trace if the trace
\r
368 #if configUSE_TRACE_FACILITY == 1
\r
369 case 't' : vTaskList( pcWriteBuffer );
\r
370 vWriteMessageToDisk( pcWriteBuffer );
\r
372 /* The legacy trace is no longer supported. Use FreeRTOS+Trace instead
\r
373 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
377 unsigned long ulBufferLength;
\r
378 ulBufferLength = ulTaskEndTrace();
\r
379 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
385 default : vTaskEndScheduler();
\r
390 /*-----------------------------------------------------------*/
\r
392 static void prvCheckOtherTasksAreStillRunning( void )
\r
394 static short sErrorHasOccurred = pdFALSE;
\r
395 static unsigned long long uxLastHookCallCount = 0, uxLastQueueSendCount = 0;
\r
397 if( prvCheckMathTasksAreStillRunning() != pdTRUE )
\r
399 vDisplayMessage( "Maths task count unchanged!\r\n" );
\r
400 sErrorHasOccurred = pdTRUE;
\r
403 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
405 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
406 sErrorHasOccurred = pdTRUE;
\r
409 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
411 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
412 sErrorHasOccurred = pdTRUE;
\r
415 if( xAreAltBlockingQueuesStillRunning() != pdTRUE )
\r
417 vDisplayMessage( "Alt blocking queues count unchanged!\r\n" );
\r
418 sErrorHasOccurred = pdTRUE;
\r
421 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
423 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
424 sErrorHasOccurred = pdTRUE;
\r
427 if( xAreAltPollingQueuesStillRunning() != pdTRUE )
\r
429 vDisplayMessage( "Alt polling queue count unchanged!\r\n" );
\r
430 sErrorHasOccurred = pdTRUE;
\r
433 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
435 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
436 sErrorHasOccurred = pdTRUE;
\r
439 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
441 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
442 sErrorHasOccurred = pdTRUE;
\r
445 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
447 vDisplayMessage( "Dynamic priority count unchanged!\r\n" );
\r
448 sErrorHasOccurred = pdTRUE;
\r
451 if( xAreMultiEventTasksStillRunning() != pdTRUE )
\r
453 vDisplayMessage( "Error in multi events tasks!\r\n" );
\r
454 sErrorHasOccurred = pdTRUE;
\r
457 if( xAreFlashCoRoutinesStillRunning() != pdTRUE )
\r
459 vDisplayMessage( "Error in co-routine flash tasks!\r\n" );
\r
460 sErrorHasOccurred = pdTRUE;
\r
463 if( xAreHookCoRoutinesStillRunning() != pdTRUE )
\r
465 vDisplayMessage( "Error in tick hook to co-routine communications!\r\n" );
\r
466 sErrorHasOccurred = pdTRUE;
\r
469 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
471 vDisplayMessage( "Error in block time test tasks!\r\n" );
\r
472 sErrorHasOccurred = pdTRUE;
\r
475 if( xAreAltBlockTimeTestTasksStillRunning() != pdTRUE )
\r
477 vDisplayMessage( "Error in fast block time test tasks!\r\n" );
\r
478 sErrorHasOccurred = pdTRUE;
\r
481 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
483 vDisplayMessage( "Error in generic queue test task!\r\n" );
\r
484 sErrorHasOccurred = pdTRUE;
\r
487 if( xAreAltGenericQueueTasksStillRunning() != pdTRUE )
\r
489 vDisplayMessage( "Error in fast generic queue test task!\r\n" );
\r
490 sErrorHasOccurred = pdTRUE;
\r
493 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
495 vDisplayMessage( "Error in queue peek test task!\r\n" );
\r
496 sErrorHasOccurred = pdTRUE;
\r
499 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
501 vDisplayMessage( "Error in counting semaphore demo task!\r\n" );
\r
502 sErrorHasOccurred = pdTRUE;
\r
505 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
507 vDisplayMessage( "Error in recursive mutex tasks!\r\n" );
\r
508 sErrorHasOccurred = pdTRUE;
\r
511 /* The hook function associated with this task is called each time the task
\r
512 is switched in. We therefore expect the number of times the callback
\r
513 function has been executed to have increrment since the last time this
\r
514 function executed. */
\r
515 if( uxCheckTaskHookCallCount <= uxLastHookCallCount )
\r
517 vDisplayMessage( "Error in task hook call count!\r\n" );
\r
518 sErrorHasOccurred = pdTRUE;
\r
522 uxLastHookCallCount = uxCheckTaskHookCallCount;
\r
525 /* We would expect some queue sending to occur between calls of this
\r
527 if( uxQueueSendPassedCount <= uxLastQueueSendCount )
\r
529 vDisplayMessage( "Error in queue send hook call count!\r\n" );
\r
530 sErrorHasOccurred = pdTRUE;
\r
534 uxLastQueueSendCount = uxQueueSendPassedCount;
\r
537 if( sErrorHasOccurred == pdFALSE )
\r
539 vDisplayMessage( "OK " );
\r
542 /*-----------------------------------------------------------*/
\r
544 static void prvStartMathTasks( void )
\r
546 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
547 /* The Borland project does not yet support floating point. */
\r
548 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
550 vStartMathTasks( tskIDLE_PRIORITY );
\r
553 /*-----------------------------------------------------------*/
\r
555 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void )
\r
557 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
558 /* The Borland project does not yet support floating point. */
\r
559 return xAreIntegerMathsTaskStillRunning();
\r
561 return xAreMathsTaskStillRunning();
\r
564 /*-----------------------------------------------------------*/
\r
566 void vApplicationIdleHook( void )
\r
568 /* The co-routines are executed in the idle task using the idle task
\r
570 vCoRoutineSchedule();
\r
572 /*-----------------------------------------------------------*/
\r