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