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