]> git.sur5r.net Git - freertos/blob - Demo/PC/main.c
Correct merge.
[freertos] / Demo / PC / main.c
1 /*\r
2         FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27 \r
28         Please ensure to read the configuration and relevant port sections of the \r
29         online documentation.\r
30 \r
31         +++ http://www.FreeRTOS.org +++\r
32         Documentation, latest information, license and contact details.  \r
33 \r
34         +++ http://www.SafeRTOS.com +++\r
35         A version that is certified for use in safety critical systems.\r
36 \r
37         +++ http://www.OpenRTOS.com +++\r
38         Commercial support, development, porting, licensing and training services.\r
39 \r
40         ***************************************************************************\r
41 */\r
42 \r
43 /**\r
44  * Creates all the demo application tasks and co-routines, then starts the \r
45  * scheduler.\r
46  *\r
47  * Main. c also creates a task called "Print".  This only executes every \r
48  * five seconds but has the highest priority so is guaranteed to get \r
49  * processor time.  Its main function is to check that all the other tasks \r
50  * are still operational.  Nearly all the tasks in the demo application \r
51  * maintain a unique count that is incremented each time the task successfully \r
52  * completes its function.  Should any error occur within the task the count is \r
53  * permanently halted.  The print task checks the count of each task to ensure \r
54  * it has changed since the last time the print task executed.  If any count is \r
55  * found not to have changed the print task displays an appropriate message.  \r
56  * If all the tasks are still incrementing their unique counts the print task \r
57  * displays an "OK" message.\r
58  *\r
59  * The LED flash tasks do not maintain a count as they already provide visual \r
60  * feedback of their status.\r
61  *\r
62  * The print task blocks on the queue into which messages that require \r
63  * displaying are posted.  It will therefore only block for the full 5 seconds\r
64  * if no messages are posted onto the queue.\r
65  *\r
66  * Main. c also provides a demonstration of how the trace visualisation utility\r
67  * can be used, and how the scheduler can be stopped.\r
68  *\r
69  * \page MainC main.c\r
70  * \ingroup DemoFiles\r
71  * <HR>\r
72  */\r
73 \r
74 #include <stdlib.h>\r
75 #include <conio.h>\r
76 #include "FreeRTOS.h"\r
77 #include "task.h"\r
78 #include "croutine.h"\r
79 #include "partest.h"\r
80 #include "serial.h"\r
81 \r
82 /* Demo file headers. */\r
83 #include "BlockQ.h"\r
84 #include "PollQ.h"\r
85 #include "death.h"\r
86 #include "crflash.h"\r
87 #include "flop.h"\r
88 #include "print.h"\r
89 #include "comtest.h"\r
90 #include "fileio.h"\r
91 #include "semtest.h"\r
92 #include "integer.h"\r
93 #include "dynamic.h"\r
94 #include "mevents.h"\r
95 #include "crhook.h"\r
96 #include "blocktim.h"\r
97 #include "AltBlock.h"\r
98 #include "GenQTest.h"\r
99 #include "QPeek.h"\r
100 #include "countsem.h"\r
101 #include "AltQTest.h"\r
102 #include "AltPollQ.h"\r
103 #include "AltBlckQ.h"\r
104 #include "RecMutex.h"\r
105 \r
106 /* Priority definitions for the tasks in the demo application. */\r
107 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
108 #define mainCREATOR_TASK_PRIORITY       ( tskIDLE_PRIORITY + 3 )\r
109 #define mainPRINT_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
110 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
111 #define mainQUEUE_BLOCK_PRIORITY        ( tskIDLE_PRIORITY + 3 )\r
112 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
113 #define mainSEMAPHORE_TASK_PRIORITY     ( tskIDLE_PRIORITY + 1 )\r
114 #define mainGENERIC_QUEUE_PRIORITY      ( tskIDLE_PRIORITY )\r
115 \r
116 #define mainPRINT_STACK_SIZE            ( ( unsigned portSHORT ) 512 )\r
117 #define mainDEBUG_LOG_BUFFER_SIZE       ( ( unsigned portSHORT ) 20480 )\r
118 \r
119 /* The number of flash co-routines to create. */\r
120 #define mainNUM_FLASH_CO_ROUTINES       ( 8 )\r
121 \r
122 /* Task function for the "Print" task as described at the top of the file. */\r
123 static void vErrorChecks( void *pvParameters );\r
124 \r
125 /* Function that checks the unique count of all the other tasks as described at\r
126 the top of the file. */\r
127 static void prvCheckOtherTasksAreStillRunning( void );\r
128 \r
129 /* Key presses can be used to start/stop the trace visualisation utility or stop\r
130 the scheduler. */\r
131 static void     prvCheckForKeyPresses( void );\r
132 \r
133 /* Buffer used by the trace visualisation utility so only needed if the trace\r
134 being used. */\r
135 #if configUSE_TRACE_FACILITY == 1\r
136         static portCHAR pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];\r
137 #endif\r
138 \r
139 /* Constant definition used to turn on/off the pre-emptive scheduler. */\r
140 static const portSHORT sUsingPreemption = configUSE_PREEMPTION;\r
141 \r
142 /* Start the math tasks appropriate to the build.  The Borland port does\r
143 not yet support floating point so uses the integer equivalent. */\r
144 static void prvStartMathTasks( void );\r
145 \r
146 /* Check which ever tasks are relevant to this build. */\r
147 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void );\r
148 \r
149 /*-----------------------------------------------------------*/\r
150 \r
151 portSHORT main( void )\r
152 {\r
153         /* Initialise hardware and utilities. */\r
154         vParTestInitialise();\r
155         vPrintInitialise();\r
156         \r
157         /* CREATE ALL THE DEMO APPLICATION TASKS. */\r
158         prvStartMathTasks();\r
159         vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM1, ser115200 );\r
160         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
161         vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );\r
162         vCreateBlockTimeTasks();\r
163         vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );\r
164         vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );\r
165         vStartDynamicPriorityTasks();\r
166         vStartMultiEventTasks();\r
167         vStartQueuePeekTasks();\r
168         vStartCountingSemaphoreTasks();\r
169         vStartAltGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );\r
170         vCreateAltBlockTimeTasks();\r
171         vStartAltBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );        \r
172         vStartAltPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
173         vStartRecursiveMutexTasks();\r
174                 \r
175         /* Create the "Print" task as described at the top of the file. */\r
176         xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );\r
177 \r
178         /* This task has to be created last as it keeps account of the number of tasks\r
179         it expects to see running. */\r
180         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
181 \r
182         /* Create the co-routines that flash the LED's. */\r
183         vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );\r
184 \r
185         /* Create the co-routines that communicate with the tick hook. */\r
186         vStartHookCoRoutines();\r
187 \r
188         /* Set the scheduler running.  This function will not return unless a task\r
189         calls vTaskEndScheduler(). */\r
190         vTaskStartScheduler();\r
191 \r
192         return 1;\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static void vErrorChecks( void *pvParameters )\r
197 {\r
198 portTickType xExpectedWakeTime;\r
199 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;\r
200 const portLONG lMaxAllowableTimeDifference = ( portLONG ) 0;\r
201 portTickType xWakeTime;\r
202 portLONG lTimeDifference;\r
203 const portCHAR *pcReceivedMessage;\r
204 const portCHAR * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";\r
205 \r
206         ( void ) pvParameters;\r
207 \r
208         /* Loop continuously, blocking, then checking all the other tasks are still\r
209         running, before blocking once again.  This task blocks on the queue of\r
210         messages that require displaying so will wake either by its time out expiring,\r
211         or a message becoming available. */\r
212         for( ;; )\r
213         {\r
214                 /* Calculate the time we will unblock if no messages are received\r
215                 on the queue.  This is used to check that we have not blocked for too long. */\r
216                 xExpectedWakeTime = xTaskGetTickCount();\r
217                 xExpectedWakeTime += xPrintRate;\r
218 \r
219                 /* Block waiting for either a time out or a message to be posted that\r
220                 required displaying. */\r
221                 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );\r
222 \r
223                 /* Was a message received? */\r
224                 if( pcReceivedMessage == NULL )\r
225                 {\r
226                         /* A message was not received so we timed out, did we unblock at the\r
227                         expected time? */\r
228                         xWakeTime = xTaskGetTickCount();\r
229 \r
230                         /* Calculate the difference between the time we unblocked and the\r
231                         time we should have unblocked. */\r
232                         if( xWakeTime > xExpectedWakeTime )\r
233                         {\r
234                                 lTimeDifference = ( portLONG ) ( xWakeTime - xExpectedWakeTime );\r
235                         }\r
236                         else\r
237                         {\r
238                                 lTimeDifference = ( portLONG ) ( xExpectedWakeTime - xWakeTime );\r
239                         }\r
240 \r
241                         if( lTimeDifference > lMaxAllowableTimeDifference )\r
242                         {\r
243                                 /* We blocked too long - create a message that will get\r
244                                 printed out the next time around.  If we are not using\r
245                                 preemption then we won't expect the timing to be so\r
246                                 accurate. */\r
247                                 if( sUsingPreemption == pdTRUE )\r
248                                 {\r
249                                         vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );\r
250                                 }\r
251                         }\r
252 \r
253                         /* Check the other tasks are still running, just in case. */\r
254                         prvCheckOtherTasksAreStillRunning();\r
255                 }\r
256                 else\r
257                 {\r
258                         /* We unblocked due to a message becoming available.  Send the message\r
259                         for printing. */\r
260                         vDisplayMessage( pcReceivedMessage );\r
261                 }\r
262 \r
263                 /* Key presses are used to invoke the trace visualisation utility, or end\r
264                 the program. */\r
265                 prvCheckForKeyPresses();\r
266         }\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 static void     prvCheckForKeyPresses( void )\r
271 {\r
272 portSHORT sIn;\r
273 \r
274         taskENTER_CRITICAL();\r
275                 #ifdef DEBUG_BUILD\r
276                         /* kbhit can be used in .exe's that are executed from the command\r
277                         line, but not if executed through the debugger. */\r
278                         sIn = 0;\r
279                 #else\r
280                         sIn = kbhit();\r
281                 #endif\r
282         taskEXIT_CRITICAL();\r
283 \r
284         if( sIn )\r
285         {\r
286                 /* Key presses can be used to start/stop the trace utility, or end the \r
287                 program. */\r
288                 sIn = getch();\r
289                 switch( sIn )\r
290                 {\r
291                         /* Only define keys for turning on and off the trace if the trace\r
292                         is being used. */\r
293                         #if configUSE_TRACE_FACILITY == 1\r
294                                 case 't' :      vTaskList( pcWriteBuffer );\r
295                                                         vWriteMessageToDisk( pcWriteBuffer );\r
296                                                         break;                  \r
297                                 case 's' :      vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );\r
298                                                         break;\r
299 \r
300                                 case 'e' :      {\r
301                                                                 unsigned portLONG ulBufferLength;\r
302                                                                 ulBufferLength = ulTaskEndTrace();\r
303                                                                 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );\r
304                                                         }\r
305                                                         break;\r
306                         #endif\r
307 \r
308                         default  :      vTaskEndScheduler();\r
309                                                 break;\r
310                 }\r
311         }\r
312 }\r
313 /*-----------------------------------------------------------*/\r
314 \r
315 static void prvCheckOtherTasksAreStillRunning( void )\r
316 {\r
317 static portSHORT sErrorHasOccurred = pdFALSE;\r
318 \r
319         if( prvCheckMathTasksAreStillRunning() != pdTRUE )\r
320         {\r
321                 vDisplayMessage( "Maths task count unchanged!\r\n" );\r
322                 sErrorHasOccurred = pdTRUE;\r
323         }\r
324 \r
325         if( xAreComTestTasksStillRunning() != pdTRUE )\r
326         {\r
327                 vDisplayMessage( "Com test count unchanged!\r\n" );\r
328                 sErrorHasOccurred = pdTRUE;\r
329         }\r
330 \r
331         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
332         {\r
333                 vDisplayMessage( "Blocking queues count unchanged!\r\n" );\r
334                 sErrorHasOccurred = pdTRUE;\r
335         }\r
336 \r
337         if( xAreAltBlockingQueuesStillRunning() != pdTRUE )\r
338         {\r
339                 vDisplayMessage( "Alt blocking queues count unchanged!\r\n" );\r
340                 sErrorHasOccurred = pdTRUE;\r
341         }\r
342 \r
343         if( xArePollingQueuesStillRunning() != pdTRUE )\r
344         {\r
345                 vDisplayMessage( "Polling queue count unchanged!\r\n" );\r
346                 sErrorHasOccurred = pdTRUE;\r
347         }\r
348 \r
349         if( xAreAltPollingQueuesStillRunning() != pdTRUE )\r
350         {\r
351                 vDisplayMessage( "Alt polling queue count unchanged!\r\n" );\r
352                 sErrorHasOccurred = pdTRUE;\r
353         }\r
354 \r
355         if( xIsCreateTaskStillRunning() != pdTRUE )\r
356         {\r
357                 vDisplayMessage( "Incorrect number of tasks running!\r\n" );\r
358                 sErrorHasOccurred = pdTRUE;\r
359         }\r
360 \r
361         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
362         {\r
363                 vDisplayMessage( "Semaphore take count unchanged!\r\n" );\r
364                 sErrorHasOccurred = pdTRUE;\r
365         }\r
366 \r
367         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
368         {\r
369                 vDisplayMessage( "Dynamic priority count unchanged!\r\n" );\r
370                 sErrorHasOccurred = pdTRUE;\r
371         }\r
372         \r
373         if( xAreMultiEventTasksStillRunning() != pdTRUE )\r
374         {\r
375                 vDisplayMessage( "Error in multi events tasks!\r\n" );\r
376                 sErrorHasOccurred = pdTRUE;\r
377         }\r
378 \r
379         if( xAreFlashCoRoutinesStillRunning() != pdTRUE )\r
380         {\r
381                 vDisplayMessage( "Error in co-routine flash tasks!\r\n" );\r
382                 sErrorHasOccurred = pdTRUE;\r
383         }\r
384 \r
385         if( xAreHookCoRoutinesStillRunning() != pdTRUE )\r
386         {\r
387                 vDisplayMessage( "Error in tick hook to co-routine communications!\r\n" );\r
388                 sErrorHasOccurred = pdTRUE;\r
389         }\r
390 \r
391         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
392         {\r
393                 vDisplayMessage( "Error in block time test tasks!\r\n" );\r
394                 sErrorHasOccurred = pdTRUE;\r
395         }\r
396 \r
397         if( xAreAltBlockTimeTestTasksStillRunning() != pdTRUE )\r
398         {\r
399                 vDisplayMessage( "Error in fast block time test tasks!\r\n" );\r
400                 sErrorHasOccurred = pdTRUE;\r
401         }\r
402 \r
403         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
404         {\r
405                 vDisplayMessage( "Error in generic queue test task!\r\n" );\r
406                 sErrorHasOccurred = pdTRUE;             \r
407         }\r
408 \r
409         if( xAreAltGenericQueueTasksStillRunning() != pdTRUE )\r
410         {\r
411                 vDisplayMessage( "Error in fast generic queue test task!\r\n" );\r
412                 sErrorHasOccurred = pdTRUE;             \r
413         }\r
414 \r
415         if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
416         {\r
417                 vDisplayMessage( "Error in queue peek test task!\r\n" );\r
418                 sErrorHasOccurred = pdTRUE;\r
419         }\r
420 \r
421         if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
422         {\r
423                 vDisplayMessage( "Error in counting semaphore demo task!\r\n" );\r
424                 sErrorHasOccurred = pdTRUE;\r
425         }\r
426 \r
427         if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
428         {\r
429                 vDisplayMessage( "Error in recursive mutex tasks!\r\n" );\r
430                 sErrorHasOccurred = pdTRUE;\r
431         }\r
432 \r
433         if( sErrorHasOccurred == pdFALSE )\r
434         {\r
435                 vDisplayMessage( "OK " );\r
436         }\r
437 }\r
438 /*-----------------------------------------------------------*/\r
439 \r
440 static void prvStartMathTasks( void )\r
441 {\r
442         #ifdef BCC_INDUSTRIAL_PC_PORT\r
443                 /* The Borland project does not yet support floating point. */\r
444                 vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
445         #else\r
446                 vStartMathTasks( tskIDLE_PRIORITY );\r
447         #endif\r
448 }\r
449 /*-----------------------------------------------------------*/\r
450 \r
451 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void )\r
452 {\r
453         #ifdef BCC_INDUSTRIAL_PC_PORT\r
454                 /* The Borland project does not yet support floating point. */\r
455                 return xAreIntegerMathsTaskStillRunning();\r
456         #else\r
457                 return xAreMathsTaskStillRunning();\r
458         #endif\r
459 }\r
460 /*-----------------------------------------------------------*/\r
461 \r
462 void vApplicationIdleHook( void )\r
463 {\r
464         /* The co-routines are executed in the idle task using the idle task \r
465         hook. */\r
466         vCoRoutineSchedule();\r
467 }\r
468 /*-----------------------------------------------------------*/\r
469 \r