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 *******************************************************************************
\r
71 * -NOTE- The Win32 port is a simulation (or is that emulation?) only! Do not
\r
72 * expect to get real time behaviour from the Win32 port or this demo
\r
73 * application. It is provided as a convenient development and demonstration
\r
74 * test bed only. This was tested using Windows XP on a dual core laptop.
\r
76 * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -
\r
77 *******************************************************************************
\r
79 * main() creates all the demo application tasks, then starts the scheduler.
\r
80 * The web documentation provides more details of the standard demo application
\r
81 * tasks, which provide no particular functionality but do provide a good
\r
82 * example of how to use the FreeRTOS API.
\r
84 * In addition to the standard demo tasks, the following tasks and tests are
\r
85 * defined and/or created within this file:
\r
87 * "Check" task - This only executes every five seconds but has a high priority
\r
88 * to ensure it gets processor time. Its main function is to check that all the
\r
89 * standard demo tasks are still operational. While no errors have been
\r
90 * discovered the check task will print out "OK" and the current simulated tick
\r
91 * time. If an error is discovered in the execution of a task then the check
\r
92 * task will print out an appropriate error message.
\r
97 /* Standard includes. */
\r
101 /* Kernel includes. */
\r
102 #include <FreeRTOS.h>
\r
105 #include "timers.h"
\r
106 #include "semphr.h"
\r
108 /* Standard demo includes. */
\r
109 #include "BlockQ.h"
\r
110 #include "integer.h"
\r
111 #include "semtest.h"
\r
113 #include "GenQTest.h"
\r
115 #include "recmutex.h"
\r
117 #include "TimerDemo.h"
\r
118 #include "countsem.h"
\r
120 #include "dynamic.h"
\r
121 #include "QueueSet.h"
\r
123 /* Priorities at which the tasks are created. */
\r
124 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
125 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
126 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
127 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
128 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
129 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
130 #define mainuIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
131 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
132 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
133 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
135 #define mainTIMER_TEST_PERIOD ( 50 )
\r
137 /* Task function prototypes. */
\r
138 static void prvCheckTask( void *pvParameters );
\r
140 /* A task that is created from the idle task to test the functionality of
\r
141 eTaskStateGet(). */
\r
142 static void prvTestTask( void *pvParameters );
\r
144 /* The variable into which error messages are latched. */
\r
145 static char *pcStatusMessage = "OK";
\r
147 /* This semaphore is created purely to test using the vSemaphoreDelete() and
\r
148 semaphore tracing API functions. It has no other purpose. */
\r
149 static xSemaphoreHandle xMutexToDelete = NULL;
\r
151 /*-----------------------------------------------------------*/
\r
155 /* Start the check task as described at the top of this file. */
\r
156 xTaskCreate( prvCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
158 /* Create the standard demo tasks. */
\r
159 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
160 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
161 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
162 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
163 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
164 vStartQueuePeekTasks();
\r
165 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
166 vStartRecursiveMutexTasks();
\r
167 vStartTimerDemoTask( mainTIMER_TEST_PERIOD );
\r
168 vStartCountingSemaphoreTasks();
\r
169 vStartDynamicPriorityTasks();
\r
170 vStartQueueSetTasks();
\r
172 /* The suicide tasks must be created last as they need to know how many
\r
173 tasks were running prior to their creation. This then allows them to
\r
174 ascertain whether or not the correct/expected number of tasks are running at
\r
176 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
178 /* Create the semaphore that will be deleted in the idle task hook. This
\r
179 is done purely to test the use of vSemaphoreDelete(). */
\r
180 xMutexToDelete = xSemaphoreCreateMutex();
\r
182 /* Start the trace recording - the recording is written to a file if
\r
183 configASSERT() is called. */
\r
186 /* Start the scheduler itself. */
\r
187 vTaskStartScheduler();
\r
189 /* Should never get here unless there was not enough heap space to create
\r
190 the idle and other system tasks. */
\r
193 /*-----------------------------------------------------------*/
\r
195 static void prvCheckTask( void *pvParameters )
\r
197 portTickType xNextWakeTime;
\r
198 const portTickType xCycleFrequency = 1000 / portTICK_RATE_MS;
\r
200 /* Just to remove compiler warning. */
\r
201 ( void ) pvParameters;
\r
203 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
204 xNextWakeTime = xTaskGetTickCount();
\r
208 /* Place this task in the blocked state until it is time to run again. */
\r
209 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
211 /* Check the standard demo tasks are running without error. */
\r
212 if( xAreTimerDemoTasksStillRunning( xCycleFrequency ) != pdTRUE )
\r
214 pcStatusMessage = "Error: TimerDemo";
\r
216 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
218 pcStatusMessage = "Error: IntMath";
\r
220 else if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
222 pcStatusMessage = "Error: GenQueue";
\r
224 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
226 pcStatusMessage = "Error: QueuePeek";
\r
228 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
230 pcStatusMessage = "Error: BlockQueue";
\r
232 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
234 pcStatusMessage = "Error: SemTest";
\r
236 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
238 pcStatusMessage = "Error: PollQueue";
\r
240 else if( xAreMathsTaskStillRunning() != pdPASS )
\r
242 pcStatusMessage = "Error: Flop";
\r
244 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
246 pcStatusMessage = "Error: RecMutex";
\r
248 else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
250 pcStatusMessage = "Error: CountSem";
\r
252 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
254 pcStatusMessage = "Error: Death";
\r
256 else if( xAreDynamicPriorityTasksStillRunning() != pdPASS )
\r
258 pcStatusMessage = "Error: Dynamic\r\n";
\r
260 else if( xAreQueueSetTasksStillRunning() != pdPASS )
\r
262 pcStatusMessage = "Error: Queue set\r\n";
\r
265 /* This is the only task that uses stdout so its ok to call printf()
\r
267 printf( "%s - %d\r\n", pcStatusMessage, xTaskGetTickCount() );
\r
270 /*-----------------------------------------------------------*/
\r
272 static void prvTestTask( void *pvParameters )
\r
274 const unsigned long ulMSToSleep = 5;
\r
276 /* Just to remove compiler warnings. */
\r
277 ( void ) pvParameters;
\r
279 /* This task is just used to test the eTaskStateGet() function. It
\r
280 does not have anything to do. */
\r
283 /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are
\r
284 tasks waiting to be terminated by the idle task. */
\r
285 Sleep( ulMSToSleep );
\r
288 /*-----------------------------------------------------------*/
\r
290 void vApplicationIdleHook( void )
\r
292 const unsigned long ulMSToSleep = 15;
\r
293 xTaskHandle xIdleTaskHandle, xTimerTaskHandle, xTestTask;
\r
294 signed char *pcTaskName;
\r
295 const unsigned char ucConstQueueNumber = 0xaaU, ucConstTaskNumber = 0x55U;
\r
298 /* These three functions are only meant for use by trace code, and not for
\r
299 direct use from application code, hence their prototypes are not in queue.h. */
\r
300 extern void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber );
\r
301 extern unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue );
\r
302 extern unsigned char ucQueueGetQueueType( xQueueHandle pxQueue );
\r
303 extern void vTaskSetTaskNumber( xTaskHandle xTask, unsigned portBASE_TYPE uxHandle );
\r
304 extern unsigned portBASE_TYPE uxTaskGetTaskNumber( xTaskHandle xTask );
\r
306 /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are
\r
307 tasks waiting to be terminated by the idle task. */
\r
308 Sleep( ulMSToSleep );
\r
310 /* Demonstrate the use of the xTimerGetTimerDaemonTaskHandle() and
\r
311 xTaskGetIdleTaskHandle() functions. Also try using the function that sets
\r
312 the task number. */
\r
313 xIdleTaskHandle = xTaskGetIdleTaskHandle();
\r
314 xTimerTaskHandle = xTimerGetTimerDaemonTaskHandle();
\r
315 vTaskSetTaskNumber( xIdleTaskHandle, ( unsigned long ) ucConstTaskNumber );
\r
316 configASSERT( uxTaskGetTaskNumber( xIdleTaskHandle ) == ucConstTaskNumber );
\r
318 /* This is the idle hook, so the current task handle should equal the
\r
319 returned idle task handle. */
\r
320 if( xTaskGetCurrentTaskHandle() != xIdleTaskHandle )
\r
322 pcStatusMessage = "Error: Returned idle task handle was incorrect";
\r
325 /* Check the timer task handle was returned correctly. */
\r
326 pcTaskName = pcTaskGetTaskName( xTimerTaskHandle );
\r
327 if( strcmp( pcTaskName, "Tmr Svc" ) != 0 )
\r
329 pcStatusMessage = "Error: Returned timer task handle was incorrect";
\r
332 /* This task is running, make sure its state is returned as running. */
\r
333 if( eTaskStateGet( xIdleTaskHandle ) != eRunning )
\r
335 pcStatusMessage = "Error: Returned idle task state was incorrect";
\r
338 /* If this task is running, then the timer task must be blocked. */
\r
339 if( eTaskStateGet( xTimerTaskHandle ) != eBlocked )
\r
341 pcStatusMessage = "Error: Returned timer task state was incorrect";
\r
344 /* If xMutexToDelete has not already been deleted, then delete it now.
\r
345 This is done purely to demonstrate the use of, and test, the
\r
346 vSemaphoreDelete() macro. Care must be taken not to delete a semaphore
\r
347 that has tasks blocked on it. */
\r
348 if( xMutexToDelete != NULL )
\r
350 /* Before deleting the semaphore, test the function used to set its
\r
351 number. This would normally only be done from trace software, rather
\r
352 than application code. */
\r
353 vQueueSetQueueNumber( xMutexToDelete, ucConstQueueNumber );
\r
355 /* Before deleting the semaphore, test the functions used to get its
\r
356 type and number. Again, these would normally only be done from trace
\r
357 software, rather than application code. */
\r
358 configASSERT( ucQueueGetQueueNumber( xMutexToDelete ) == ucConstQueueNumber );
\r
359 configASSERT( ucQueueGetQueueType( xMutexToDelete ) == queueQUEUE_TYPE_MUTEX );
\r
360 vSemaphoreDelete( xMutexToDelete );
\r
361 xMutexToDelete = NULL;
\r
363 /* Other tests that should only be performed once follow. The test task
\r
364 is not created on each iteration because to do so would cause the death
\r
365 task to report an error (too many tasks running). */
\r
367 /* Create a test task to use to test other eTaskStateGet() return values. */
\r
368 if( xTaskCreate( prvTestTask, "Test", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xTestTask ) == pdPASS )
\r
370 /* If this task is running, the test task must be in the ready state. */
\r
371 if( eTaskStateGet( xTestTask ) != eReady )
\r
373 pcStatusMessage = "Error: Returned test task state was incorrect 1";
\r
376 /* Now suspend the test task and check its state is reported correctly. */
\r
377 vTaskSuspend( xTestTask );
\r
378 if( eTaskStateGet( xTestTask ) != eSuspended )
\r
380 pcStatusMessage = "Error: Returned test task state was incorrect 2";
\r
383 /* Now delete the task and check its state is reported correctly. */
\r
384 vTaskDelete( xTestTask );
\r
385 if( eTaskStateGet( xTestTask ) != eDeleted )
\r
387 pcStatusMessage = "Error: Returned test task state was incorrect 3";
\r
392 /* Exercise heap_4 a bit. The malloc failed hook will trap failed
\r
393 allocations so there is no need to test here. */
\r
394 pvAllocated = pvPortMalloc( ( rand() % 100 ) + 1 );
\r
395 vPortFree( pvAllocated );
\r
397 /*-----------------------------------------------------------*/
\r
399 void vApplicationMallocFailedHook( void )
\r
403 /*-----------------------------------------------------------*/
\r
405 void vApplicationStackOverflowHook( void )
\r
407 /* Can be implemented if required, but not required in this
\r
408 environment and running this demo. */
\r
410 /*-----------------------------------------------------------*/
\r
412 void vApplicationTickHook( void )
\r
414 /* Call the periodic timer test, which tests the timer API functions that
\r
415 can be called from an ISR. */
\r
416 vTimerPeriodicISRTests();
\r
418 /* Write to a queue that is in use as part of the queue set demo to
\r
419 demonstrate using queue sets from an ISR. */
\r
420 vQueueSetAccessQueueSetFromISR();
\r
422 /*-----------------------------------------------------------*/
\r
424 void vAssertCalled( void )
\r
426 taskDISABLE_INTERRUPTS();
\r
428 /* Stop the trace recording. */
\r
434 /*-----------------------------------------------------------*/
\r