2 FreeRTOS V8.2.1 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 * Creates all the demo application tasks and co-routines, then starts the
\r
74 * Main. c also creates a task called "Print". This only executes every
\r
75 * five seconds but has the highest priority so is guaranteed to get
\r
76 * processor time. Its main function is to check that all the other tasks
\r
77 * are still operational. Nearly all the tasks in the demo application
\r
78 * maintain a unique count that is incremented each time the task successfully
\r
79 * completes its function. Should any error occur within the task the count is
\r
80 * permanently halted. The print task checks the count of each task to ensure
\r
81 * it has changed since the last time the print task executed. If any count is
\r
82 * found not to have changed the print task displays an appropriate message.
\r
83 * If all the tasks are still incrementing their unique counts the print task
\r
84 * displays an "OK" message.
\r
86 * The LED flash tasks do not maintain a count as they already provide visual
\r
87 * feedback of their status.
\r
89 * The print task blocks on the queue into which messages that require
\r
90 * displaying are posted. It will therefore only block for the full 5 seconds
\r
91 * if no messages are posted onto the queue.
\r
93 * Main. c also provides a demonstration of how the trace visualisation utility
\r
94 * can be used, and how the scheduler can be stopped.
\r
96 * \page MainC main.c
\r
97 * \ingroup DemoFiles
\r
101 #include <stdlib.h>
\r
103 #include "FreeRTOS.h"
\r
105 #include "croutine.h"
\r
106 #include "partest.h"
\r
107 #include "serial.h"
\r
109 /* Demo file headers. */
\r
110 #include "BlockQ.h"
\r
113 #include "crflash.h"
\r
116 #include "comtest.h"
\r
117 #include "fileio.h"
\r
118 #include "semtest.h"
\r
119 #include "integer.h"
\r
120 #include "dynamic.h"
\r
121 #include "mevents.h"
\r
122 #include "crhook.h"
\r
123 #include "blocktim.h"
\r
124 #include "AltBlock.h"
\r
125 #include "GenQTest.h"
\r
127 #include "countsem.h"
\r
128 #include "AltQTest.h"
\r
129 #include "AltPollQ.h"
\r
130 #include "AltBlckQ.h"
\r
131 #include "RecMutex.h"
\r
133 /* Priority definitions for the tasks in the demo application. */
\r
134 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
135 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
136 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
137 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
138 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
139 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
140 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
141 #define mainGENERIC_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
143 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 512 )
\r
144 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\r
146 /* The number of flash co-routines to create. */
\r
147 #define mainNUM_FLASH_CO_ROUTINES ( 8 )
\r
149 /* Task function for the "Print" task as described at the top of the file. */
\r
150 static void vErrorChecks( void *pvParameters );
\r
152 /* Function that checks the unique count of all the other tasks as described at
\r
153 the top of the file. */
\r
154 static void prvCheckOtherTasksAreStillRunning( void );
\r
156 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
158 static void prvCheckForKeyPresses( void );
\r
160 /* Buffer used by the trace visualisation utility so only needed if the trace
\r
162 #if configUSE_TRACE_FACILITY == 1
\r
163 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
166 /* Constant definition used to turn on/off the pre-emptive scheduler. */
\r
167 static const short sUsingPreemption = configUSE_PREEMPTION;
\r
169 /* Start the math tasks appropriate to the build. The Borland port does
\r
170 not yet support floating point so uses the integer equivalent. */
\r
171 static void prvStartMathTasks( void );
\r
173 /* Check which ever tasks are relevant to this build. */
\r
174 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void );
\r
176 /* Used to demonstrate the "task switched in" callback function. */
\r
177 static portBASE_TYPE prvExampleTaskHook( void * pvParameter );
\r
179 /* Just used to count the number of times the example task callback function is
\r
180 called, and the number of times a queue send passes. */
\r
181 static unsigned long long uxCheckTaskHookCallCount = 0;
\r
182 static unsigned long long uxQueueSendPassedCount = 0;
\r
184 /*-----------------------------------------------------------*/
\r
188 /* Initialise hardware and utilities. */
\r
189 vParTestInitialise();
\r
190 vPrintInitialise();
\r
192 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
193 prvStartMathTasks();
\r
194 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM1, ser115200 );
\r
195 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
196 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
197 vCreateBlockTimeTasks();
\r
198 vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
199 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
200 vStartDynamicPriorityTasks();
\r
201 vStartMultiEventTasks();
\r
202 vStartQueuePeekTasks();
\r
203 vStartCountingSemaphoreTasks();
\r
204 vStartAltGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
205 vCreateAltBlockTimeTasks();
\r
206 vStartAltBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
207 vStartAltPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
208 vStartRecursiveMutexTasks();
\r
210 /* Create the "Print" task as described at the top of the file. */
\r
211 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
213 /* This task has to be created last as it keeps account of the number of tasks
\r
214 it expects to see running. */
\r
215 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
217 /* Create the co-routines that flash the LED's. */
\r
218 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
\r
220 /* Create the co-routines that communicate with the tick hook. */
\r
221 vStartHookCoRoutines();
\r
223 /* Set the scheduler running. This function will not return unless a task
\r
224 calls vTaskEndScheduler(). */
\r
225 vTaskStartScheduler();
\r
229 /*-----------------------------------------------------------*/
\r
231 static portBASE_TYPE prvExampleTaskHook( void * pvParameter )
\r
233 if( pvParameter != ( void * ) 0xabcd )
\r
235 /* The parameter did not contain the value we expected, so cause an
\r
236 error to be detected by setting the call count back to zero. */
\r
237 uxCheckTaskHookCallCount = 0;
\r
241 /* Simply increment a number so we know the callback has been executed. */
\r
242 uxCheckTaskHookCallCount++;
\r
247 /*-----------------------------------------------------------*/
\r
249 void vMainQueueSendPassed( void )
\r
251 /* This is just an example implementation of the "queue send" trace hook. */
\r
252 uxQueueSendPassedCount++;
\r
254 /*-----------------------------------------------------------*/
\r
256 static void vErrorChecks( void *pvParameters )
\r
258 TickType_t xExpectedWakeTime;
\r
259 const TickType_t xPrintRate = ( TickType_t ) 5000 / portTICK_PERIOD_MS;
\r
260 const long lMaxAllowableTimeDifference = ( long ) 0;
\r
261 TickType_t xWakeTime;
\r
262 long lTimeDifference;
\r
263 const char *pcReceivedMessage;
\r
264 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
265 const char * const pcUnexpectedHookValueMsg = "Task hook has unexpected value!\r\n";
\r
267 ( void ) pvParameters;
\r
269 /* Register our callback function. */
\r
270 vTaskSetApplicationTaskTag( NULL, prvExampleTaskHook );
\r
272 /* Just for test purposes. */
\r
273 if( xTaskGetApplicationTaskTag( NULL ) != prvExampleTaskHook )
\r
275 vPrintDisplayMessage( &pcUnexpectedHookValueMsg );
\r
278 /* Loop continuously, blocking, then checking all the other tasks are still
\r
279 running, before blocking once again. This task blocks on the queue of
\r
280 messages that require displaying so will wake either by its time out expiring,
\r
281 or a message becoming available. */
\r
284 /* Calculate the time we will unblock if no messages are received
\r
285 on the queue. This is used to check that we have not blocked for too long. */
\r
286 xExpectedWakeTime = xTaskGetTickCount();
\r
287 xExpectedWakeTime += xPrintRate;
\r
289 /* Block waiting for either a time out or a message to be posted that
\r
290 required displaying. */
\r
291 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
293 /* Was a message received? */
\r
294 if( pcReceivedMessage == NULL )
\r
296 /* A message was not received so we timed out, did we unblock at the
\r
298 xWakeTime = xTaskGetTickCount();
\r
300 /* Calculate the difference between the time we unblocked and the
\r
301 time we should have unblocked. */
\r
302 if( xWakeTime > xExpectedWakeTime )
\r
304 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
308 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
311 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
313 /* We blocked too long - create a message that will get
\r
314 printed out the next time around. If we are not using
\r
315 preemption then we won't expect the timing to be so
\r
317 if( sUsingPreemption == pdTRUE )
\r
319 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
323 /* Check the other tasks are still running, just in case. */
\r
324 prvCheckOtherTasksAreStillRunning();
\r
328 /* We unblocked due to a message becoming available. Send the message
\r
330 vDisplayMessage( pcReceivedMessage );
\r
333 /* Key presses are used to invoke the trace visualisation utility, or end
\r
335 prvCheckForKeyPresses();
\r
338 /*-----------------------------------------------------------*/
\r
340 static void prvCheckForKeyPresses( void )
\r
344 taskENTER_CRITICAL();
\r
346 /* kbhit can be used in .exe's that are executed from the command
\r
347 line, but not if executed through the debugger. */
\r
352 taskEXIT_CRITICAL();
\r
356 /* Key presses can be used to start/stop the trace utility, or end the
\r
361 /* Only define keys for turning on and off the trace if the trace
\r
363 #if configUSE_TRACE_FACILITY == 1
\r
364 case 't' : vTaskList( pcWriteBuffer );
\r
365 vWriteMessageToDisk( pcWriteBuffer );
\r
367 /* The legacy trace is no longer supported. Use FreeRTOS+Trace instead
\r
368 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
372 unsigned long ulBufferLength;
\r
373 ulBufferLength = ulTaskEndTrace();
\r
374 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
380 default : vTaskEndScheduler();
\r
385 /*-----------------------------------------------------------*/
\r
387 static void prvCheckOtherTasksAreStillRunning( void )
\r
389 static short sErrorHasOccurred = pdFALSE;
\r
390 static unsigned long long uxLastHookCallCount = 0, uxLastQueueSendCount = 0;
\r
392 if( prvCheckMathTasksAreStillRunning() != pdTRUE )
\r
394 vDisplayMessage( "Maths task count unchanged!\r\n" );
\r
395 sErrorHasOccurred = pdTRUE;
\r
398 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
400 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
401 sErrorHasOccurred = pdTRUE;
\r
404 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
406 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
407 sErrorHasOccurred = pdTRUE;
\r
410 if( xAreAltBlockingQueuesStillRunning() != pdTRUE )
\r
412 vDisplayMessage( "Alt blocking queues count unchanged!\r\n" );
\r
413 sErrorHasOccurred = pdTRUE;
\r
416 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
418 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
419 sErrorHasOccurred = pdTRUE;
\r
422 if( xAreAltPollingQueuesStillRunning() != pdTRUE )
\r
424 vDisplayMessage( "Alt polling queue count unchanged!\r\n" );
\r
425 sErrorHasOccurred = pdTRUE;
\r
428 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
430 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
431 sErrorHasOccurred = pdTRUE;
\r
434 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
436 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
437 sErrorHasOccurred = pdTRUE;
\r
440 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
442 vDisplayMessage( "Dynamic priority count unchanged!\r\n" );
\r
443 sErrorHasOccurred = pdTRUE;
\r
446 if( xAreMultiEventTasksStillRunning() != pdTRUE )
\r
448 vDisplayMessage( "Error in multi events tasks!\r\n" );
\r
449 sErrorHasOccurred = pdTRUE;
\r
452 if( xAreFlashCoRoutinesStillRunning() != pdTRUE )
\r
454 vDisplayMessage( "Error in co-routine flash tasks!\r\n" );
\r
455 sErrorHasOccurred = pdTRUE;
\r
458 if( xAreHookCoRoutinesStillRunning() != pdTRUE )
\r
460 vDisplayMessage( "Error in tick hook to co-routine communications!\r\n" );
\r
461 sErrorHasOccurred = pdTRUE;
\r
464 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
466 vDisplayMessage( "Error in block time test tasks!\r\n" );
\r
467 sErrorHasOccurred = pdTRUE;
\r
470 if( xAreAltBlockTimeTestTasksStillRunning() != pdTRUE )
\r
472 vDisplayMessage( "Error in fast block time test tasks!\r\n" );
\r
473 sErrorHasOccurred = pdTRUE;
\r
476 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
478 vDisplayMessage( "Error in generic queue test task!\r\n" );
\r
479 sErrorHasOccurred = pdTRUE;
\r
482 if( xAreAltGenericQueueTasksStillRunning() != pdTRUE )
\r
484 vDisplayMessage( "Error in fast generic queue test task!\r\n" );
\r
485 sErrorHasOccurred = pdTRUE;
\r
488 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
490 vDisplayMessage( "Error in queue peek test task!\r\n" );
\r
491 sErrorHasOccurred = pdTRUE;
\r
494 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
496 vDisplayMessage( "Error in counting semaphore demo task!\r\n" );
\r
497 sErrorHasOccurred = pdTRUE;
\r
500 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
502 vDisplayMessage( "Error in recursive mutex tasks!\r\n" );
\r
503 sErrorHasOccurred = pdTRUE;
\r
506 /* The hook function associated with this task is called each time the task
\r
507 is switched in. We therefore expect the number of times the callback
\r
508 function has been executed to have increrment since the last time this
\r
509 function executed. */
\r
510 if( uxCheckTaskHookCallCount <= uxLastHookCallCount )
\r
512 vDisplayMessage( "Error in task hook call count!\r\n" );
\r
513 sErrorHasOccurred = pdTRUE;
\r
517 uxLastHookCallCount = uxCheckTaskHookCallCount;
\r
520 /* We would expect some queue sending to occur between calls of this
\r
522 if( uxQueueSendPassedCount <= uxLastQueueSendCount )
\r
524 vDisplayMessage( "Error in queue send hook call count!\r\n" );
\r
525 sErrorHasOccurred = pdTRUE;
\r
529 uxLastQueueSendCount = uxQueueSendPassedCount;
\r
532 if( sErrorHasOccurred == pdFALSE )
\r
534 vDisplayMessage( "OK " );
\r
537 /*-----------------------------------------------------------*/
\r
539 static void prvStartMathTasks( void )
\r
541 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
542 /* The Borland project does not yet support floating point. */
\r
543 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
545 vStartMathTasks( tskIDLE_PRIORITY );
\r
548 /*-----------------------------------------------------------*/
\r
550 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void )
\r
552 #ifdef BCC_INDUSTRIAL_PC_PORT
\r
553 /* The Borland project does not yet support floating point. */
\r
554 return xAreIntegerMathsTaskStillRunning();
\r
556 return xAreMathsTaskStillRunning();
\r
559 /*-----------------------------------------------------------*/
\r
561 void vApplicationIdleHook( void )
\r
563 /* The co-routines are executed in the idle task using the idle task
\r
565 vCoRoutineSchedule();
\r
567 /*-----------------------------------------------------------*/
\r