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