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