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