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