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