]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MSVC/main_full.c
d5a4cb493739477178537597f12572541e7ac8e3
[freertos] / FreeRTOS / Demo / WIN32-MSVC / main_full.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \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
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\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
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \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 \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
36     kernel.\r
37 \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
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \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
65 \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
69     \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
73 */\r
74 \r
75 /*\r
76  *******************************************************************************\r
77  * NOTE 1: 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
81  *\r
82  * In this example, one simulated millisecond will take approximately 40ms to\r
83  * execute, and Windows will not be running the FreeRTOS simulator threads\r
84  * continuously, so the timing information in the FreeRTOS+Trace logs have no\r
85  * meaningful units.  See the documentation page for the Windows simulator for\r
86  * an explanation of the slow timing:\r
87  * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html\r
88  * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -\r
89  *\r
90  * NOTE 2:  This project provides two demo applications.  A simple blinky style\r
91  * project, and a more comprehensive test and demo application.  The\r
92  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select \r
93  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY \r
94  * in main.c.  This file implements the comprehensive test and demo version.\r
95  *\r
96  * NOTE 3:  This file only contains the source code that is specific to the\r
97  * basic demo.  Generic functions, such FreeRTOS hook functions, are defined in \r
98  * main.c.\r
99  *******************************************************************************\r
100  *\r
101  * main() creates all the demo application tasks, then starts the scheduler.  \r
102  * The web documentation provides more details of the standard demo application \r
103  * tasks, which provide no particular functionality but do provide a good \r
104  * example of how to use the FreeRTOS API.\r
105  *\r
106  * In addition to the standard demo tasks, the following tasks and tests are\r
107  * defined and/or created within this file:\r
108  *\r
109  * "Check" task - This only executes every five seconds but has a high priority\r
110  * to ensure it gets processor time.  Its main function is to check that all the\r
111  * standard demo tasks are still operational.  While no errors have been\r
112  * discovered the check task will print out "OK" and the current simulated tick\r
113  * time.  If an error is discovered in the execution of a task then the check\r
114  * task will print out an appropriate error message.\r
115  *\r
116  */\r
117 \r
118 \r
119 /* Standard includes. */\r
120 #include <stdio.h>\r
121 #include <stdlib.h>\r
122 \r
123 /* Kernel includes. */\r
124 #include <FreeRTOS.h>\r
125 #include "task.h"\r
126 #include "queue.h"\r
127 #include "timers.h"\r
128 #include "semphr.h"\r
129 \r
130 /* Standard demo includes. */\r
131 #include "BlockQ.h"\r
132 #include "integer.h"\r
133 #include "semtest.h"\r
134 #include "PollQ.h"\r
135 #include "GenQTest.h"\r
136 #include "QPeek.h"\r
137 #include "recmutex.h"\r
138 #include "flop.h"\r
139 #include "TimerDemo.h"\r
140 #include "countsem.h"\r
141 #include "death.h"\r
142 #include "dynamic.h"\r
143 #include "QueueSet.h"\r
144 #include "QueueOverwrite.h"\r
145 \r
146 /* Priorities at which the tasks are created. */\r
147 #define mainCHECK_TASK_PRIORITY                 ( configMAX_PRIORITIES - 1 )\r
148 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 1 )\r
149 #define mainSEM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
150 #define mainBLOCK_Q_PRIORITY                    ( tskIDLE_PRIORITY + 2 )\r
151 #define mainCREATOR_TASK_PRIORITY               ( tskIDLE_PRIORITY + 3 )\r
152 #define mainFLASH_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 1 )\r
153 #define mainuIP_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
154 #define mainINTEGER_TASK_PRIORITY               ( tskIDLE_PRIORITY )\r
155 #define mainGEN_QUEUE_TASK_PRIORITY             ( tskIDLE_PRIORITY )\r
156 #define mainFLOP_TASK_PRIORITY                  ( tskIDLE_PRIORITY )\r
157 #define mainQUEUE_OVERWRITE_PRIORITY    ( tskIDLE_PRIORITY )\r
158 \r
159 #define mainTIMER_TEST_PERIOD                   ( 50 )\r
160 \r
161 /* Task function prototypes. */\r
162 static void prvCheckTask( void *pvParameters );\r
163 \r
164 /* A task that is created from the idle task to test the functionality of \r
165 eTaskStateGet(). */\r
166 static void prvTestTask( void *pvParameters );\r
167 \r
168 /*\r
169  * Called from the idle task hook function to demonstrate a few utility\r
170  * functions that are not demonstrated by any of the standard demo tasks.\r
171  */\r
172 static void prvDemonstrateTaskStateAndHandleGetFunctions( void );\r
173 \r
174 /*-----------------------------------------------------------*/\r
175 \r
176 /* The variable into which error messages are latched. */\r
177 static char *pcStatusMessage = "OK";\r
178 \r
179 /* This semaphore is created purely to test using the vSemaphoreDelete() and\r
180 semaphore tracing API functions.  It has no other purpose. */\r
181 static xSemaphoreHandle xMutexToDelete = NULL;\r
182 \r
183 /*-----------------------------------------------------------*/\r
184 \r
185 int main_full( void )\r
186 {\r
187         /* Start the check task as described at the top of this file. */\r
188         xTaskCreate( prvCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
189 \r
190         /* Create the standard demo tasks. */\r
191         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
192         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
193         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
194         vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
195         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
196         vStartQueuePeekTasks();\r
197         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
198         vStartRecursiveMutexTasks();\r
199         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
200         vStartCountingSemaphoreTasks();\r
201         vStartDynamicPriorityTasks();\r
202         vStartQueueSetTasks();\r
203         vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );\r
204 \r
205         /* The suicide tasks must be created last as they need to know how many\r
206         tasks were running prior to their creation.  This then allows them to \r
207         ascertain whether or not the correct/expected number of tasks are running at \r
208         any given time. */\r
209         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
210 \r
211         /* Create the semaphore that will be deleted in the idle task hook.  This\r
212         is done purely to test the use of vSemaphoreDelete(). */\r
213         xMutexToDelete = xSemaphoreCreateMutex();\r
214 \r
215         /* Start the scheduler itself. */\r
216         vTaskStartScheduler();\r
217 \r
218     /* Should never get here unless there was not enough heap space to create \r
219         the idle and other system tasks. */\r
220     return 0;\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 static void prvCheckTask( void *pvParameters )\r
225 {\r
226 portTickType xNextWakeTime;\r
227 const portTickType xCycleFrequency = 1000 / portTICK_RATE_MS;\r
228 \r
229         /* Just to remove compiler warning. */\r
230         ( void ) pvParameters;\r
231 \r
232         /* Initialise xNextWakeTime - this only needs to be done once. */\r
233         xNextWakeTime = xTaskGetTickCount();\r
234 \r
235         for( ;; )\r
236         {\r
237                 /* Place this task in the blocked state until it is time to run again. */\r
238                 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );\r
239 \r
240                 /* Check the standard demo tasks are running without error. */\r
241                 if( xAreTimerDemoTasksStillRunning( xCycleFrequency ) != pdTRUE )\r
242                 {\r
243                         pcStatusMessage = "Error: TimerDemo";\r
244                 }\r
245             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
246             {\r
247                         pcStatusMessage = "Error: IntMath";\r
248             }   \r
249                 else if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
250                 {                       \r
251                         pcStatusMessage = "Error: GenQueue";\r
252                 }\r
253                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
254                 {\r
255                         pcStatusMessage = "Error: QueuePeek";\r
256                 }\r
257                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
258                 {\r
259                         pcStatusMessage = "Error: BlockQueue";\r
260                 }\r
261             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
262             {\r
263                         pcStatusMessage = "Error: SemTest";\r
264             }\r
265             else if( xArePollingQueuesStillRunning() != pdTRUE )\r
266             {\r
267                         pcStatusMessage = "Error: PollQueue";\r
268             }\r
269                 else if( xAreMathsTaskStillRunning() != pdPASS )\r
270                 {\r
271                         pcStatusMessage = "Error: Flop";\r
272                 }\r
273             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
274             {\r
275                         pcStatusMessage = "Error: RecMutex";\r
276                 }\r
277                 else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
278                 {\r
279                         pcStatusMessage = "Error: CountSem";\r
280                 }\r
281                 else if( xIsCreateTaskStillRunning() != pdTRUE )\r
282                 {\r
283                         pcStatusMessage = "Error: Death";\r
284                 }\r
285                 else if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
286                 {\r
287                         pcStatusMessage = "Error: Dynamic\r\n";\r
288                 }\r
289                 else if( xAreQueueSetTasksStillRunning() != pdPASS )\r
290                 {\r
291                         pcStatusMessage = "Error: Queue set\r\n";\r
292                 }\r
293                 else if( xIsQueueOverwriteTaskStillRunning() != pdPASS )\r
294                 {\r
295                         pcStatusMessage = "Error: Queue overwrite\r\n";\r
296                 }\r
297 \r
298                 /* This is the only task that uses stdout so its ok to call printf() \r
299                 directly. */\r
300                 printf( "%s - %d\r\n", pcStatusMessage, xTaskGetTickCount() );\r
301         }\r
302 }\r
303 /*-----------------------------------------------------------*/\r
304 \r
305 static void prvTestTask( void *pvParameters )\r
306 {\r
307 const unsigned long ulMSToSleep = 5;\r
308 \r
309         /* Just to remove compiler warnings. */\r
310         ( void ) pvParameters;\r
311 \r
312         /* This task is just used to test the eTaskStateGet() function.  It\r
313         does not have anything to do. */\r
314         for( ;; )\r
315         {\r
316                 /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are\r
317                 tasks waiting to be terminated by the idle task. */\r
318                 Sleep( ulMSToSleep );\r
319         }\r
320 }\r
321 /*-----------------------------------------------------------*/\r
322 \r
323 /* Called from vApplicationIdleHook(), which is defined in main.c. */\r
324 void vFullDemoIdleFunction( void )\r
325 {\r
326 const unsigned long ulMSToSleep = 15;\r
327 const unsigned char ucConstQueueNumber = 0xaaU;\r
328 void *pvAllocated;\r
329 \r
330 /* These three functions are only meant for use by trace code, and not for\r
331 direct use from application code, hence their prototypes are not in queue.h. */\r
332 extern void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber );\r
333 extern unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue );\r
334 extern unsigned char ucQueueGetQueueType( xQueueHandle pxQueue );\r
335 extern void vTaskSetTaskNumber( xTaskHandle xTask, unsigned portBASE_TYPE uxHandle );\r
336 extern unsigned portBASE_TYPE uxTaskGetTaskNumber( xTaskHandle xTask );\r
337 \r
338         /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are\r
339         tasks waiting to be terminated by the idle task. */\r
340         Sleep( ulMSToSleep );\r
341 \r
342         /* Demonstrate a few utility functions that are not demonstrated by any of\r
343         the standard demo tasks. */\r
344         prvDemonstrateTaskStateAndHandleGetFunctions();\r
345 \r
346         /* If xMutexToDelete has not already been deleted, then delete it now.\r
347         This is done purely to demonstrate the use of, and test, the \r
348         vSemaphoreDelete() macro.  Care must be taken not to delete a semaphore\r
349         that has tasks blocked on it. */\r
350         if( xMutexToDelete != NULL )\r
351         {\r
352                 /* Before deleting the semaphore, test the function used to set its\r
353                 number.  This would normally only be done from trace software, rather\r
354                 than application code. */\r
355                 vQueueSetQueueNumber( xMutexToDelete, ucConstQueueNumber );\r
356 \r
357                 /* Before deleting the semaphore, test the functions used to get its\r
358                 type and number.  Again, these would normally only be done from trace\r
359                 software, rather than application code. */\r
360                 configASSERT( ucQueueGetQueueNumber( xMutexToDelete ) == ucConstQueueNumber );\r
361                 configASSERT( ucQueueGetQueueType( xMutexToDelete ) == queueQUEUE_TYPE_MUTEX );\r
362                 vSemaphoreDelete( xMutexToDelete );\r
363                 xMutexToDelete = NULL;\r
364         }\r
365 \r
366         /* Exercise heap_4 a bit.  The malloc failed hook will trap failed \r
367         allocations so there is no need to test here. */\r
368         pvAllocated = pvPortMalloc( ( rand() % 100 ) + 1 );\r
369         vPortFree( pvAllocated );\r
370 }\r
371 /*-----------------------------------------------------------*/\r
372 \r
373 /* Called by vApplicationTickHook(), which is defined in main.c. */\r
374 void vFullDemoTickHookFunction( void )\r
375 {\r
376         /* Call the periodic timer test, which tests the timer API functions that\r
377         can be called from an ISR. */\r
378         vTimerPeriodicISRTests();\r
379 \r
380         /* Call the periodic queue overwrite from ISR demo. */\r
381         vQueueOverwritePeriodicISRDemo();\r
382 \r
383         /* Write to a queue that is in use as part of the queue set demo to \r
384         demonstrate using queue sets from an ISR. */\r
385         vQueueSetAccessQueueSetFromISR();\r
386 }\r
387 /*-----------------------------------------------------------*/\r
388 \r
389 static void prvDemonstrateTaskStateAndHandleGetFunctions( void )\r
390 {\r
391 xTaskHandle xIdleTaskHandle, xTimerTaskHandle;\r
392 const unsigned char ucConstTaskNumber = 0x55U;\r
393 signed char *pcTaskName;\r
394 static portBASE_TYPE xPerformedOneShotTests = pdFALSE;\r
395 xTaskHandle xTestTask;\r
396 \r
397         /* Demonstrate the use of the xTimerGetTimerDaemonTaskHandle() and \r
398         xTaskGetIdleTaskHandle() functions.  Also try using the function that sets\r
399         the task number. */\r
400         xIdleTaskHandle = xTaskGetIdleTaskHandle();\r
401         xTimerTaskHandle = xTimerGetTimerDaemonTaskHandle();\r
402         vTaskSetTaskNumber( xIdleTaskHandle, ( unsigned long ) ucConstTaskNumber );\r
403         configASSERT( uxTaskGetTaskNumber( xIdleTaskHandle ) == ucConstTaskNumber );\r
404 \r
405         /* This is the idle hook, so the current task handle should equal the \r
406         returned idle task handle. */\r
407         if( xTaskGetCurrentTaskHandle() != xIdleTaskHandle )\r
408         {\r
409                 pcStatusMessage = "Error:  Returned idle task handle was incorrect";\r
410         }\r
411 \r
412         /* Check the timer task handle was returned correctly. */\r
413         pcTaskName = pcTaskGetTaskName( xTimerTaskHandle );\r
414         if( strcmp( pcTaskName, "Tmr Svc" ) != 0 )\r
415         {\r
416                 pcStatusMessage = "Error:  Returned timer task handle was incorrect";\r
417         }\r
418 \r
419         /* This task is running, make sure it's state is returned as running. */\r
420         if( eTaskStateGet( xIdleTaskHandle ) != eRunning )\r
421         {\r
422                 pcStatusMessage = "Error:  Returned idle task state was incorrect";\r
423         }\r
424 \r
425         /* If this task is running, then the timer task must be blocked. */\r
426         if( eTaskStateGet( xTimerTaskHandle ) != eBlocked )\r
427         {\r
428                 pcStatusMessage = "Error:  Returned timer task state was incorrect";\r
429         }\r
430 \r
431         /* Other tests that should only be performed once follow.  The test task\r
432         is not created on each iteration because to do so would cause the death\r
433         task to report an error (too many tasks running). */\r
434         if( xPerformedOneShotTests == pdFALSE )\r
435         {\r
436                 /* Don't run this part of the test again. */\r
437                 xPerformedOneShotTests = pdTRUE;\r
438 \r
439                 /* Create a test task to use to test other eTaskStateGet() return values. */\r
440                 if( xTaskCreate( prvTestTask, "Test", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xTestTask ) == pdPASS )\r
441                 {\r
442                         /* If this task is running, the test task must be in the ready state. */\r
443                         if( eTaskStateGet( xTestTask ) != eReady )\r
444                         {\r
445                                 pcStatusMessage = "Error: Returned test task state was incorrect 1";\r
446                         }\r
447 \r
448                         /* Now suspend the test task and check its state is reported correctly. */\r
449                         vTaskSuspend( xTestTask );\r
450                         if( eTaskStateGet( xTestTask ) != eSuspended )\r
451                         {\r
452                                 pcStatusMessage = "Error: Returned test task state was incorrect 2";\r
453                         }\r
454 \r
455                         /* Now delete the task and check its state is reported correctly. */\r
456                         vTaskDelete( xTestTask );\r
457                         if( eTaskStateGet( xTestTask ) != eDeleted )\r
458                         {\r
459                                 pcStatusMessage = "Error: Returned test task state was incorrect 3";\r
460                         }\r
461                 }\r
462         }\r
463 }\r
464 /*-----------------------------------------------------------*/\r
465 \r
466 \r