]> git.sur5r.net Git - freertos/blob - Demo/PC/main.c
Add recursive mutexes to PC demo.
[freertos] / Demo / PC / main.c
1 /*\r
2         FreeRTOS.org V4.7.0 - 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 #include "RecMutex.h"\r
99 \r
100 /* Priority definitions for the tasks in the demo application. */\r
101 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
102 #define mainCREATOR_TASK_PRIORITY       ( tskIDLE_PRIORITY + 3 )\r
103 #define mainPRINT_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
104 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
105 #define mainQUEUE_BLOCK_PRIORITY        ( tskIDLE_PRIORITY + 3 )\r
106 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
107 #define mainSEMAPHORE_TASK_PRIORITY     ( tskIDLE_PRIORITY + 1 )\r
108 #define mainGENERIC_QUEUE_PRIORITY      ( tskIDLE_PRIORITY )\r
109 \r
110 #define mainPRINT_STACK_SIZE            ( ( unsigned portSHORT ) 512 )\r
111 #define mainDEBUG_LOG_BUFFER_SIZE       ( ( unsigned portSHORT ) 20480 )\r
112 \r
113 /* The number of flash co-routines to create. */\r
114 #define mainNUM_FLASH_CO_ROUTINES       ( 8 )\r
115 \r
116 /* Task function for the "Print" task as described at the top of the file. */\r
117 static void vErrorChecks( void *pvParameters );\r
118 \r
119 /* Function that checks the unique count of all the other tasks as described at\r
120 the top of the file. */\r
121 static void prvCheckOtherTasksAreStillRunning( void );\r
122 \r
123 /* Key presses can be used to start/stop the trace visualisation utility or stop\r
124 the scheduler. */\r
125 static void     prvCheckForKeyPresses( void );\r
126 \r
127 /* Buffer used by the trace visualisation utility so only needed if the trace\r
128 being used. */\r
129 #if configUSE_TRACE_FACILITY == 1\r
130         static portCHAR pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];\r
131 #endif\r
132 \r
133 /* Constant definition used to turn on/off the pre-emptive scheduler. */\r
134 static const portSHORT sUsingPreemption = configUSE_PREEMPTION;\r
135 \r
136 /* Start the math tasks appropriate to the build.  The Borland port does\r
137 not yet support floating point so uses the integer equivalent. */\r
138 static void prvStartMathTasks( void );\r
139 \r
140 /* Check which ever tasks are relevant to this build. */\r
141 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void );\r
142 \r
143 /*-----------------------------------------------------------*/\r
144 \r
145 portSHORT main( void )\r
146 {\r
147         /* Initialise hardware and utilities. */\r
148         vParTestInitialise();\r
149         vPrintInitialise();\r
150         \r
151         /* CREATE ALL THE DEMO APPLICATION TASKS. */\r
152         prvStartMathTasks();\r
153         vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM1, ser115200 );\r
154         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
155         vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );\r
156         vCreateBlockTimeTasks();\r
157         vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );\r
158         vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );\r
159         vStartDynamicPriorityTasks();\r
160         vStartMultiEventTasks();\r
161         vStartQueuePeekTasks();\r
162         vStartCountingSemaphoreTasks();\r
163         vStartAltGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );\r
164         vCreateAltBlockTimeTasks();\r
165         vStartAltBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );        \r
166         vStartAltPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
167         vStartRecursiveMutexTasks();\r
168                 \r
169         /* Create the "Print" task as described at the top of the file. */\r
170         xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );\r
171 \r
172         /* This task has to be created last as it keeps account of the number of tasks\r
173         it expects to see running. */\r
174         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
175 \r
176         /* Create the co-routines that flash the LED's. */\r
177         vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );\r
178 \r
179         /* Create the co-routines that communicate with the tick hook. */\r
180         vStartHookCoRoutines();\r
181 \r
182         /* Set the scheduler running.  This function will not return unless a task\r
183         calls vTaskEndScheduler(). */\r
184         vTaskStartScheduler();\r
185 \r
186         return 1;\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 static void vErrorChecks( void *pvParameters )\r
191 {\r
192 portTickType xExpectedWakeTime;\r
193 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;\r
194 const portLONG lMaxAllowableTimeDifference = ( portLONG ) 0;\r
195 portTickType xWakeTime;\r
196 portLONG lTimeDifference;\r
197 const portCHAR *pcReceivedMessage;\r
198 const portCHAR * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";\r
199 \r
200         ( void ) pvParameters;\r
201 \r
202         /* Loop continuously, blocking, then checking all the other tasks are still\r
203         running, before blocking once again.  This task blocks on the queue of\r
204         messages that require displaying so will wake either by its time out expiring,\r
205         or a message becoming available. */\r
206         for( ;; )\r
207         {\r
208                 /* Calculate the time we will unblock if no messages are received\r
209                 on the queue.  This is used to check that we have not blocked for too long. */\r
210                 xExpectedWakeTime = xTaskGetTickCount();\r
211                 xExpectedWakeTime += xPrintRate;\r
212 \r
213                 /* Block waiting for either a time out or a message to be posted that\r
214                 required displaying. */\r
215                 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );\r
216 \r
217                 /* Was a message received? */\r
218                 if( pcReceivedMessage == NULL )\r
219                 {\r
220                         /* A message was not received so we timed out, did we unblock at the\r
221                         expected time? */\r
222                         xWakeTime = xTaskGetTickCount();\r
223 \r
224                         /* Calculate the difference between the time we unblocked and the\r
225                         time we should have unblocked. */\r
226                         if( xWakeTime > xExpectedWakeTime )\r
227                         {\r
228                                 lTimeDifference = ( portLONG ) ( xWakeTime - xExpectedWakeTime );\r
229                         }\r
230                         else\r
231                         {\r
232                                 lTimeDifference = ( portLONG ) ( xExpectedWakeTime - xWakeTime );\r
233                         }\r
234 \r
235                         if( lTimeDifference > lMaxAllowableTimeDifference )\r
236                         {\r
237                                 /* We blocked too long - create a message that will get\r
238                                 printed out the next time around.  If we are not using\r
239                                 preemption then we won't expect the timing to be so\r
240                                 accurate. */\r
241                                 if( sUsingPreemption == pdTRUE )\r
242                                 {\r
243                                         vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );\r
244                                 }\r
245                         }\r
246 \r
247                         /* Check the other tasks are still running, just in case. */\r
248                         prvCheckOtherTasksAreStillRunning();\r
249                 }\r
250                 else\r
251                 {\r
252                         /* We unblocked due to a message becoming available.  Send the message\r
253                         for printing. */\r
254                         vDisplayMessage( pcReceivedMessage );\r
255                 }\r
256 \r
257                 /* Key presses are used to invoke the trace visualisation utility, or end\r
258                 the program. */\r
259                 prvCheckForKeyPresses();\r
260         }\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 static void     prvCheckForKeyPresses( void )\r
265 {\r
266 portSHORT sIn;\r
267 \r
268         taskENTER_CRITICAL();\r
269                 #ifdef DEBUG_BUILD\r
270                         /* kbhit can be used in .exe's that are executed from the command\r
271                         line, but not if executed through the debugger. */\r
272                         sIn = 0;\r
273                 #else\r
274                         sIn = kbhit();\r
275                 #endif\r
276         taskEXIT_CRITICAL();\r
277 \r
278         if( sIn )\r
279         {\r
280                 /* Key presses can be used to start/stop the trace utility, or end the \r
281                 program. */\r
282                 sIn = getch();\r
283                 switch( sIn )\r
284                 {\r
285                         /* Only define keys for turning on and off the trace if the trace\r
286                         is being used. */\r
287                         #if configUSE_TRACE_FACILITY == 1\r
288                                 case 't' :      vTaskList( pcWriteBuffer );\r
289                                                         vWriteMessageToDisk( pcWriteBuffer );\r
290                                                         break;                  \r
291                                 case 's' :      vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );\r
292                                                         break;\r
293 \r
294                                 case 'e' :      {\r
295                                                                 unsigned portLONG ulBufferLength;\r
296                                                                 ulBufferLength = ulTaskEndTrace();\r
297                                                                 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );\r
298                                                         }\r
299                                                         break;\r
300                         #endif\r
301 \r
302                         default  :      vTaskEndScheduler();\r
303                                                 break;\r
304                 }\r
305         }\r
306 }\r
307 /*-----------------------------------------------------------*/\r
308 \r
309 static void prvCheckOtherTasksAreStillRunning( void )\r
310 {\r
311 static portSHORT sErrorHasOccurred = pdFALSE;\r
312 \r
313         if( prvCheckMathTasksAreStillRunning() != pdTRUE )\r
314         {\r
315                 vDisplayMessage( "Maths task count unchanged!\r\n" );\r
316                 sErrorHasOccurred = pdTRUE;\r
317         }\r
318 \r
319         if( xAreComTestTasksStillRunning() != pdTRUE )\r
320         {\r
321                 vDisplayMessage( "Com test count unchanged!\r\n" );\r
322                 sErrorHasOccurred = pdTRUE;\r
323         }\r
324 \r
325         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
326         {\r
327                 vDisplayMessage( "Blocking queues count unchanged!\r\n" );\r
328                 sErrorHasOccurred = pdTRUE;\r
329         }\r
330 \r
331         if( xAreAltBlockingQueuesStillRunning() != pdTRUE )\r
332         {\r
333                 vDisplayMessage( "Alt blocking queues count unchanged!\r\n" );\r
334                 sErrorHasOccurred = pdTRUE;\r
335         }\r
336 \r
337         if( xArePollingQueuesStillRunning() != pdTRUE )\r
338         {\r
339                 vDisplayMessage( "Polling queue count unchanged!\r\n" );\r
340                 sErrorHasOccurred = pdTRUE;\r
341         }\r
342 \r
343         if( xAreAltPollingQueuesStillRunning() != pdTRUE )\r
344         {\r
345                 vDisplayMessage( "Alt polling queue count unchanged!\r\n" );\r
346                 sErrorHasOccurred = pdTRUE;\r
347         }\r
348 \r
349         if( xIsCreateTaskStillRunning() != pdTRUE )\r
350         {\r
351                 vDisplayMessage( "Incorrect number of tasks running!\r\n" );\r
352                 sErrorHasOccurred = pdTRUE;\r
353         }\r
354 \r
355         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
356         {\r
357                 vDisplayMessage( "Semaphore take count unchanged!\r\n" );\r
358                 sErrorHasOccurred = pdTRUE;\r
359         }\r
360 \r
361         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
362         {\r
363                 vDisplayMessage( "Dynamic priority count unchanged!\r\n" );\r
364                 sErrorHasOccurred = pdTRUE;\r
365         }\r
366         \r
367         if( xAreMultiEventTasksStillRunning() != pdTRUE )\r
368         {\r
369                 vDisplayMessage( "Error in multi events tasks!\r\n" );\r
370                 sErrorHasOccurred = pdTRUE;\r
371         }\r
372 \r
373         if( xAreFlashCoRoutinesStillRunning() != pdTRUE )\r
374         {\r
375                 vDisplayMessage( "Error in co-routine flash tasks!\r\n" );\r
376                 sErrorHasOccurred = pdTRUE;\r
377         }\r
378 \r
379         if( xAreHookCoRoutinesStillRunning() != pdTRUE )\r
380         {\r
381                 vDisplayMessage( "Error in tick hook to co-routine communications!\r\n" );\r
382                 sErrorHasOccurred = pdTRUE;\r
383         }\r
384 \r
385         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
386         {\r
387                 vDisplayMessage( "Error in block time test tasks!\r\n" );\r
388                 sErrorHasOccurred = pdTRUE;\r
389         }\r
390 \r
391         if( xAreAltBlockTimeTestTasksStillRunning() != pdTRUE )\r
392         {\r
393                 vDisplayMessage( "Error in fast block time test tasks!\r\n" );\r
394                 sErrorHasOccurred = pdTRUE;\r
395         }\r
396 \r
397         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
398         {\r
399                 vDisplayMessage( "Error in generic queue test task!\r\n" );\r
400                 sErrorHasOccurred = pdTRUE;             \r
401         }\r
402 \r
403         if( xAreAltGenericQueueTasksStillRunning() != pdTRUE )\r
404         {\r
405                 vDisplayMessage( "Error in fast generic queue test task!\r\n" );\r
406                 sErrorHasOccurred = pdTRUE;             \r
407         }\r
408 \r
409         if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
410         {\r
411                 vDisplayMessage( "Error in queue peek test task!\r\n" );\r
412                 sErrorHasOccurred = pdTRUE;\r
413         }\r
414 \r
415         if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
416         {\r
417                 vDisplayMessage( "Error in counting semaphore demo task!\r\n" );\r
418                 sErrorHasOccurred = pdTRUE;\r
419         }\r
420 \r
421         if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
422         {\r
423                 vDisplayMessage( "Error in recursive mutex tasks!\r\n" );\r
424                 sErrorHasOccurred = pdTRUE;\r
425         }\r
426 \r
427         if( sErrorHasOccurred == pdFALSE )\r
428         {\r
429                 vDisplayMessage( "OK " );\r
430         }\r
431 }\r
432 /*-----------------------------------------------------------*/\r
433 \r
434 static void prvStartMathTasks( void )\r
435 {\r
436         #ifdef BCC_INDUSTRIAL_PC_PORT\r
437                 /* The Borland project does not yet support floating point. */\r
438                 vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
439         #else\r
440                 vStartMathTasks( tskIDLE_PRIORITY );\r
441         #endif\r
442 }\r
443 /*-----------------------------------------------------------*/\r
444 \r
445 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void )\r
446 {\r
447         #ifdef BCC_INDUSTRIAL_PC_PORT\r
448                 /* The Borland project does not yet support floating point. */\r
449                 return xAreIntegerMathsTaskStillRunning();\r
450         #else\r
451                 return xAreMathsTaskStillRunning();\r
452         #endif\r
453 }\r
454 /*-----------------------------------------------------------*/\r
455 \r
456 void vApplicationIdleHook( void )\r
457 {\r
458         /* The co-routines are executed in the idle task using the idle task \r
459         hook. */\r
460         vCoRoutineSchedule();\r
461 }\r
462 /*-----------------------------------------------------------*/\r
463 \r