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