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