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