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