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