2 FreeRTOS V7.5.3 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 *******************************************************************************
\r
68 * NOTE 1: The Win32 port is a simulation (or is that emulation?) only! Do not
\r
69 * expect to get real time behaviour from the Win32 port or this demo
\r
70 * application. It is provided as a convenient development and demonstration
\r
71 * test bed only. This was tested using Windows XP on a dual core laptop.
\r
73 * In this example, one simulated millisecond will take approximately 40ms to
\r
74 * execute, and Windows will not be running the FreeRTOS simulator threads
\r
75 * continuously, so the timing information in the FreeRTOS+Trace logs have no
\r
76 * meaningful units. See the documentation page for the Windows simulator for
\r
77 * an explanation of the slow timing:
\r
78 * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html
\r
79 * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -
\r
81 * NOTE 2: This project provides two demo applications. A simple blinky style
\r
82 * project, and a more comprehensive test and demo application. The
\r
83 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
\r
84 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
\r
85 * in main.c. This file implements the comprehensive test and demo version.
\r
87 * NOTE 3: This file only contains the source code that is specific to the
\r
88 * basic demo. Generic functions, such FreeRTOS hook functions, are defined in
\r
90 *******************************************************************************
\r
92 * main() creates all the demo application tasks, then starts the scheduler.
\r
93 * The web documentation provides more details of the standard demo application
\r
94 * tasks, which provide no particular functionality but do provide a good
\r
95 * example of how to use the FreeRTOS API.
\r
97 * In addition to the standard demo tasks, the following tasks and tests are
\r
98 * defined and/or created within this file:
\r
100 * "Check" task - This only executes every five seconds but has a high priority
\r
101 * to ensure it gets processor time. Its main function is to check that all the
\r
102 * standard demo tasks are still operational. While no errors have been
\r
103 * discovered the check task will print out "OK" and the current simulated tick
\r
104 * time. If an error is discovered in the execution of a task then the check
\r
105 * task will print out an appropriate error message.
\r
110 /* Standard includes. */
\r
112 #include <stdlib.h>
\r
114 /* Kernel includes. */
\r
115 #include <FreeRTOS.h>
\r
118 #include "timers.h"
\r
119 #include "semphr.h"
\r
121 /* Standard demo includes. */
\r
122 #include "BlockQ.h"
\r
123 #include "integer.h"
\r
124 #include "semtest.h"
\r
126 #include "GenQTest.h"
\r
128 #include "recmutex.h"
\r
130 #include "TimerDemo.h"
\r
131 #include "countsem.h"
\r
133 #include "dynamic.h"
\r
134 #include "QueueSet.h"
\r
135 #include "QueueOverwrite.h"
\r
137 /* Priorities at which the tasks are created. */
\r
138 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
139 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
140 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
141 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
142 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
143 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
144 #define mainuIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
145 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
146 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
147 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
148 #define mainQUEUE_OVERWRITE_PRIORITY ( tskIDLE_PRIORITY )
\r
150 #define mainTIMER_TEST_PERIOD ( 50 )
\r
152 /* Task function prototypes. */
\r
153 static void prvCheckTask( void *pvParameters );
\r
155 /* A task that is created from the idle task to test the functionality of
\r
156 eTaskStateGet(). */
\r
157 static void prvTestTask( void *pvParameters );
\r
160 * Called from the idle task hook function to demonstrate a few utility
\r
161 * functions that are not demonstrated by any of the standard demo tasks.
\r
163 static void prvDemonstrateTaskStateAndHandleGetFunctions( void );
\r
166 * A task to demonstrate the use of the xQueueSpacesAvailable() function.
\r
168 static void prvDemoQueueSpaceFunctions( void *pvParameters );
\r
170 /*-----------------------------------------------------------*/
\r
172 /* The variable into which error messages are latched. */
\r
173 static char *pcStatusMessage = "OK";
\r
175 /* This semaphore is created purely to test using the vSemaphoreDelete() and
\r
176 semaphore tracing API functions. It has no other purpose. */
\r
177 static xSemaphoreHandle xMutexToDelete = NULL;
\r
179 /*-----------------------------------------------------------*/
\r
181 int main_full( void )
\r
183 /* Start the check task as described at the top of this file. */
\r
184 xTaskCreate( prvCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
186 /* Create the standard demo tasks. */
\r
187 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
188 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
189 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
190 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
191 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
192 vStartQueuePeekTasks();
\r
193 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
194 vStartRecursiveMutexTasks();
\r
195 vStartTimerDemoTask( mainTIMER_TEST_PERIOD );
\r
196 vStartCountingSemaphoreTasks();
\r
197 vStartDynamicPriorityTasks();
\r
198 vStartQueueSetTasks();
\r
199 vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );
\r
200 xTaskCreate( prvDemoQueueSpaceFunctions, ( signed char * ) "QSpace", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
202 /* The suicide tasks must be created last as they need to know how many
\r
203 tasks were running prior to their creation. This then allows them to
\r
204 ascertain whether or not the correct/expected number of tasks are running at
\r
206 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
208 /* Create the semaphore that will be deleted in the idle task hook. This
\r
209 is done purely to test the use of vSemaphoreDelete(). */
\r
210 xMutexToDelete = xSemaphoreCreateMutex();
\r
212 /* Start the scheduler itself. */
\r
213 vTaskStartScheduler();
\r
215 /* Should never get here unless there was not enough heap space to create
\r
216 the idle and other system tasks. */
\r
219 /*-----------------------------------------------------------*/
\r
221 static void prvCheckTask( void *pvParameters )
\r
223 portTickType xNextWakeTime;
\r
224 const portTickType xCycleFrequency = 1000 / portTICK_RATE_MS;
\r
226 /* Just to remove compiler warning. */
\r
227 ( void ) pvParameters;
\r
229 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
230 xNextWakeTime = xTaskGetTickCount();
\r
234 /* Place this task in the blocked state until it is time to run again. */
\r
235 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
237 /* Check the standard demo tasks are running without error. */
\r
238 if( xAreTimerDemoTasksStillRunning( xCycleFrequency ) != pdTRUE )
\r
240 pcStatusMessage = "Error: TimerDemo";
\r
242 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
244 pcStatusMessage = "Error: IntMath";
\r
246 else if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
248 pcStatusMessage = "Error: GenQueue";
\r
250 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
252 pcStatusMessage = "Error: QueuePeek";
\r
254 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
256 pcStatusMessage = "Error: BlockQueue";
\r
258 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
260 pcStatusMessage = "Error: SemTest";
\r
262 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
264 pcStatusMessage = "Error: PollQueue";
\r
266 else if( xAreMathsTaskStillRunning() != pdPASS )
\r
268 pcStatusMessage = "Error: Flop";
\r
270 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
272 pcStatusMessage = "Error: RecMutex";
\r
274 else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
276 pcStatusMessage = "Error: CountSem";
\r
278 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
280 pcStatusMessage = "Error: Death";
\r
282 else if( xAreDynamicPriorityTasksStillRunning() != pdPASS )
\r
284 pcStatusMessage = "Error: Dynamic\r\n";
\r
286 else if( xAreQueueSetTasksStillRunning() != pdPASS )
\r
288 pcStatusMessage = "Error: Queue set\r\n";
\r
290 else if( xIsQueueOverwriteTaskStillRunning() != pdPASS )
\r
292 pcStatusMessage = "Error: Queue overwrite\r\n";
\r
295 /* This is the only task that uses stdout so its ok to call printf()
\r
297 printf( "%s - %d\r\n", pcStatusMessage, xTaskGetTickCount() );
\r
300 /*-----------------------------------------------------------*/
\r
302 static void prvTestTask( void *pvParameters )
\r
304 const unsigned long ulMSToSleep = 5;
\r
306 /* Just to remove compiler warnings. */
\r
307 ( void ) pvParameters;
\r
309 /* This task is just used to test the eTaskStateGet() function. It
\r
310 does not have anything to do. */
\r
313 /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are
\r
314 tasks waiting to be terminated by the idle task. */
\r
315 Sleep( ulMSToSleep );
\r
318 /*-----------------------------------------------------------*/
\r
320 /* Called from vApplicationIdleHook(), which is defined in main.c. */
\r
321 void vFullDemoIdleFunction( void )
\r
323 const unsigned long ulMSToSleep = 15;
\r
324 const unsigned char ucConstQueueNumber = 0xaaU;
\r
327 /* These three functions are only meant for use by trace code, and not for
\r
328 direct use from application code, hence their prototypes are not in queue.h. */
\r
329 extern void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber );
\r
330 extern unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue );
\r
331 extern unsigned char ucQueueGetQueueType( xQueueHandle pxQueue );
\r
332 extern void vTaskSetTaskNumber( xTaskHandle xTask, unsigned portBASE_TYPE uxHandle );
\r
333 extern unsigned portBASE_TYPE uxTaskGetTaskNumber( xTaskHandle xTask );
\r
335 /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are
\r
336 tasks waiting to be terminated by the idle task. */
\r
337 Sleep( ulMSToSleep );
\r
339 /* Demonstrate a few utility functions that are not demonstrated by any of
\r
340 the standard demo tasks. */
\r
341 prvDemonstrateTaskStateAndHandleGetFunctions();
\r
343 /* If xMutexToDelete has not already been deleted, then delete it now.
\r
344 This is done purely to demonstrate the use of, and test, the
\r
345 vSemaphoreDelete() macro. Care must be taken not to delete a semaphore
\r
346 that has tasks blocked on it. */
\r
347 if( xMutexToDelete != NULL )
\r
349 /* Before deleting the semaphore, test the function used to set its
\r
350 number. This would normally only be done from trace software, rather
\r
351 than application code. */
\r
352 vQueueSetQueueNumber( xMutexToDelete, ucConstQueueNumber );
\r
354 /* Before deleting the semaphore, test the functions used to get its
\r
355 type and number. Again, these would normally only be done from trace
\r
356 software, rather than application code. */
\r
357 configASSERT( ucQueueGetQueueNumber( xMutexToDelete ) == ucConstQueueNumber );
\r
358 configASSERT( ucQueueGetQueueType( xMutexToDelete ) == queueQUEUE_TYPE_MUTEX );
\r
359 vSemaphoreDelete( xMutexToDelete );
\r
360 xMutexToDelete = NULL;
\r
363 /* Exercise heap_4 a bit. The malloc failed hook will trap failed
\r
364 allocations so there is no need to test here. */
\r
365 pvAllocated = pvPortMalloc( ( rand() % 100 ) + 1 );
\r
366 vPortFree( pvAllocated );
\r
368 /*-----------------------------------------------------------*/
\r
370 /* Called by vApplicationTickHook(), which is defined in main.c. */
\r
371 void vFullDemoTickHookFunction( void )
\r
373 /* Call the periodic timer test, which tests the timer API functions that
\r
374 can be called from an ISR. */
\r
375 vTimerPeriodicISRTests();
\r
377 /* Call the periodic queue overwrite from ISR demo. */
\r
378 vQueueOverwritePeriodicISRDemo();
\r
380 /* Write to a queue that is in use as part of the queue set demo to
\r
381 demonstrate using queue sets from an ISR. */
\r
382 vQueueSetAccessQueueSetFromISR();
\r
384 /*-----------------------------------------------------------*/
\r
386 static void prvDemonstrateTaskStateAndHandleGetFunctions( void )
\r
388 xTaskHandle xIdleTaskHandle, xTimerTaskHandle;
\r
389 const unsigned char ucConstTaskNumber = 0x55U;
\r
390 signed char *pcTaskName;
\r
391 static portBASE_TYPE xPerformedOneShotTests = pdFALSE;
\r
392 xTaskHandle xTestTask;
\r
394 /* Demonstrate the use of the xTimerGetTimerDaemonTaskHandle() and
\r
395 xTaskGetIdleTaskHandle() functions. Also try using the function that sets
\r
396 the task number. */
\r
397 xIdleTaskHandle = xTaskGetIdleTaskHandle();
\r
398 xTimerTaskHandle = xTimerGetTimerDaemonTaskHandle();
\r
399 vTaskSetTaskNumber( xIdleTaskHandle, ( unsigned long ) ucConstTaskNumber );
\r
400 configASSERT( uxTaskGetTaskNumber( xIdleTaskHandle ) == ucConstTaskNumber );
\r
402 /* This is the idle hook, so the current task handle should equal the
\r
403 returned idle task handle. */
\r
404 if( xTaskGetCurrentTaskHandle() != xIdleTaskHandle )
\r
406 pcStatusMessage = "Error: Returned idle task handle was incorrect";
\r
409 /* Check the timer task handle was returned correctly. */
\r
410 pcTaskName = pcTaskGetTaskName( xTimerTaskHandle );
\r
411 if( strcmp( pcTaskName, "Tmr Svc" ) != 0 )
\r
413 pcStatusMessage = "Error: Returned timer task handle was incorrect";
\r
416 /* This task is running, make sure it's state is returned as running. */
\r
417 if( eTaskStateGet( xIdleTaskHandle ) != eRunning )
\r
419 pcStatusMessage = "Error: Returned idle task state was incorrect";
\r
422 /* If this task is running, then the timer task must be blocked. */
\r
423 if( eTaskStateGet( xTimerTaskHandle ) != eBlocked )
\r
425 pcStatusMessage = "Error: Returned timer task state was incorrect";
\r
428 /* Other tests that should only be performed once follow. The test task
\r
429 is not created on each iteration because to do so would cause the death
\r
430 task to report an error (too many tasks running). */
\r
431 if( xPerformedOneShotTests == pdFALSE )
\r
433 /* Don't run this part of the test again. */
\r
434 xPerformedOneShotTests = pdTRUE;
\r
436 /* Create a test task to use to test other eTaskStateGet() return values. */
\r
437 if( xTaskCreate( prvTestTask, "Test", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xTestTask ) == pdPASS )
\r
439 /* If this task is running, the test task must be in the ready state. */
\r
440 if( eTaskStateGet( xTestTask ) != eReady )
\r
442 pcStatusMessage = "Error: Returned test task state was incorrect 1";
\r
445 /* Now suspend the test task and check its state is reported correctly. */
\r
446 vTaskSuspend( xTestTask );
\r
447 if( eTaskStateGet( xTestTask ) != eSuspended )
\r
449 pcStatusMessage = "Error: Returned test task state was incorrect 2";
\r
452 /* Now delete the task and check its state is reported correctly. */
\r
453 vTaskDelete( xTestTask );
\r
454 if( eTaskStateGet( xTestTask ) != eDeleted )
\r
456 pcStatusMessage = "Error: Returned test task state was incorrect 3";
\r
461 /*-----------------------------------------------------------*/
\r
463 static void prvDemoQueueSpaceFunctions( void *pvParameters )
\r
465 xQueueHandle xQueue = NULL;
\r
466 const unsigned portBASE_TYPE uxQueueLength = 10;
\r
467 unsigned portBASE_TYPE uxReturn, x;
\r
469 /* Remove compiler warnings. */
\r
470 ( void ) pvParameters;
\r
472 /* Create the queue that will be used. Nothing is actually going to be
\r
473 sent or received so the queue item size is set to 0. */
\r
474 xQueue = xQueueCreate( uxQueueLength, 0 );
\r
475 configASSERT( xQueue );
\r
479 for( x = 0; x < uxQueueLength; x++ )
\r
481 /* Ask how many messages are available... */
\r
482 uxReturn = uxQueueMessagesWaiting( xQueue );
\r
484 /* Check the number of messages being reported as being available
\r
485 is as expected, and force an assert if not. */
\r
486 if( uxReturn != x )
\r
488 /* xQueue cannot be NULL so this is deliberately causing an
\r
489 assert to be triggered as there is an error. */
\r
490 configASSERT( xQueue == NULL );
\r
493 /* Ask how many spaces remain in the queue... */
\r
494 uxReturn = uxQueueSpacesAvailable( xQueue );
\r
496 /* Check the number of spaces being reported as being available
\r
497 is as expected, and force an assert if not. */
\r
498 if( uxReturn != ( uxQueueLength - x ) )
\r
500 /* xQueue cannot be NULL so this is deliberately causing an
\r
501 assert to be triggered as there is an error. */
\r
502 configASSERT( xQueue == NULL );
\r
505 /* Fill one more space in the queue. */
\r
506 xQueueSendToBack( xQueue, NULL, 0 );
\r
509 /* Perform the same check while the queue is full. */
\r
510 uxReturn = uxQueueMessagesWaiting( xQueue );
\r
511 if( uxReturn != uxQueueLength )
\r
513 configASSERT( xQueue == NULL );
\r
516 uxReturn = uxQueueSpacesAvailable( xQueue );
\r
518 if( uxReturn != 0 )
\r
520 configASSERT( xQueue == NULL );
\r
523 /* The queue is full, start again. */
\r
524 xQueueReset( xQueue );
\r