]> git.sur5r.net Git - freertos/blob - Demo/PC/main.c
Ensure LPC1768 demos are correct prior to V5.4.0 release.
[freertos] / Demo / PC / main.c
1 /*\r
2         FreeRTOS V5.4.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /**\r
49  * Creates all the demo application tasks and co-routines, then starts the \r
50  * scheduler.\r
51  *\r
52  * Main. c also creates a task called "Print".  This only executes every \r
53  * five seconds but has the highest priority so is guaranteed to get \r
54  * processor time.  Its main function is to check that all the other tasks \r
55  * are still operational.  Nearly all the tasks in the demo application \r
56  * maintain a unique count that is incremented each time the task successfully \r
57  * completes its function.  Should any error occur within the task the count is \r
58  * permanently halted.  The print task checks the count of each task to ensure \r
59  * it has changed since the last time the print task executed.  If any count is \r
60  * found not to have changed the print task displays an appropriate message.  \r
61  * If all the tasks are still incrementing their unique counts the print task \r
62  * displays an "OK" message.\r
63  *\r
64  * The LED flash tasks do not maintain a count as they already provide visual \r
65  * feedback of their status.\r
66  *\r
67  * The print task blocks on the queue into which messages that require \r
68  * displaying are posted.  It will therefore only block for the full 5 seconds\r
69  * if no messages are posted onto the queue.\r
70  *\r
71  * Main. c also provides a demonstration of how the trace visualisation utility\r
72  * can be used, and how the scheduler can be stopped.\r
73  *\r
74  * \page MainC main.c\r
75  * \ingroup DemoFiles\r
76  * <HR>\r
77  */\r
78 \r
79 #include <stdlib.h>\r
80 #include <conio.h>\r
81 #include "FreeRTOS.h"\r
82 #include "task.h"\r
83 #include "croutine.h"\r
84 #include "partest.h"\r
85 #include "serial.h"\r
86 \r
87 /* Demo file headers. */\r
88 #include "BlockQ.h"\r
89 #include "PollQ.h"\r
90 #include "death.h"\r
91 #include "crflash.h"\r
92 #include "flop.h"\r
93 #include "print.h"\r
94 #include "comtest.h"\r
95 #include "fileio.h"\r
96 #include "semtest.h"\r
97 #include "integer.h"\r
98 #include "dynamic.h"\r
99 #include "mevents.h"\r
100 #include "crhook.h"\r
101 #include "blocktim.h"\r
102 #include "AltBlock.h"\r
103 #include "GenQTest.h"\r
104 #include "QPeek.h"\r
105 #include "countsem.h"\r
106 #include "AltQTest.h"\r
107 #include "AltPollQ.h"\r
108 #include "AltBlckQ.h"\r
109 #include "RecMutex.h"\r
110 \r
111 /* Priority definitions for the tasks in the demo application. */\r
112 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
113 #define mainCREATOR_TASK_PRIORITY       ( tskIDLE_PRIORITY + 3 )\r
114 #define mainPRINT_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
115 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
116 #define mainQUEUE_BLOCK_PRIORITY        ( tskIDLE_PRIORITY + 3 )\r
117 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
118 #define mainSEMAPHORE_TASK_PRIORITY     ( tskIDLE_PRIORITY + 1 )\r
119 #define mainGENERIC_QUEUE_PRIORITY      ( tskIDLE_PRIORITY )\r
120 \r
121 #define mainPRINT_STACK_SIZE            ( ( unsigned portSHORT ) 512 )\r
122 #define mainDEBUG_LOG_BUFFER_SIZE       ( ( unsigned portSHORT ) 20480 )\r
123 \r
124 /* The number of flash co-routines to create. */\r
125 #define mainNUM_FLASH_CO_ROUTINES       ( 8 )\r
126 \r
127 /* Task function for the "Print" task as described at the top of the file. */\r
128 static void vErrorChecks( void *pvParameters );\r
129 \r
130 /* Function that checks the unique count of all the other tasks as described at\r
131 the top of the file. */\r
132 static void prvCheckOtherTasksAreStillRunning( void );\r
133 \r
134 /* Key presses can be used to start/stop the trace visualisation utility or stop\r
135 the scheduler. */\r
136 static void     prvCheckForKeyPresses( void );\r
137 \r
138 /* Buffer used by the trace visualisation utility so only needed if the trace\r
139 being used. */\r
140 #if configUSE_TRACE_FACILITY == 1\r
141         static portCHAR pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];\r
142 #endif\r
143 \r
144 /* Constant definition used to turn on/off the pre-emptive scheduler. */\r
145 static const portSHORT sUsingPreemption = configUSE_PREEMPTION;\r
146 \r
147 /* Start the math tasks appropriate to the build.  The Borland port does\r
148 not yet support floating point so uses the integer equivalent. */\r
149 static void prvStartMathTasks( void );\r
150 \r
151 /* Check which ever tasks are relevant to this build. */\r
152 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void );\r
153 \r
154 /* Used to demonstrate the "task switched in" callback function. */\r
155 static portBASE_TYPE prvExampleTaskHook( void * pvParameter );\r
156 \r
157 /* Just used to count the number of times the example task callback function is\r
158 called, and the number of times a queue send passes. */\r
159 static unsigned portLONG portLONG uxCheckTaskHookCallCount = 0;\r
160 static unsigned portLONG portLONG uxQueueSendPassedCount = 0;\r
161 \r
162 /*-----------------------------------------------------------*/\r
163 \r
164 portSHORT main( void )\r
165 {\r
166         /* Initialise hardware and utilities. */\r
167         vParTestInitialise();\r
168         vPrintInitialise();\r
169         \r
170         /* CREATE ALL THE DEMO APPLICATION TASKS. */\r
171         prvStartMathTasks();\r
172         vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM1, ser115200 );\r
173         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
174         vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );\r
175         vCreateBlockTimeTasks();\r
176         vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );\r
177         vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );\r
178         vStartDynamicPriorityTasks();\r
179         vStartMultiEventTasks();\r
180         vStartQueuePeekTasks();\r
181         vStartCountingSemaphoreTasks();\r
182         vStartAltGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );\r
183         vCreateAltBlockTimeTasks();\r
184         vStartAltBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );        \r
185         vStartAltPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
186         vStartRecursiveMutexTasks();\r
187                 \r
188         /* Create the "Print" task as described at the top of the file. */\r
189         xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );\r
190 \r
191         /* This task has to be created last as it keeps account of the number of tasks\r
192         it expects to see running. */\r
193         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
194 \r
195         /* Create the co-routines that flash the LED's. */\r
196         vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );\r
197 \r
198         /* Create the co-routines that communicate with the tick hook. */\r
199         vStartHookCoRoutines();\r
200 \r
201         /* Set the scheduler running.  This function will not return unless a task\r
202         calls vTaskEndScheduler(). */\r
203         vTaskStartScheduler();\r
204 \r
205         return 1;\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 static portBASE_TYPE prvExampleTaskHook( void * pvParameter )\r
210 {\r
211         if( pvParameter != ( void * ) 0xabcd )\r
212         {\r
213                 /* The parameter did not contain the value we expected, so cause an\r
214                 error to be detected by setting the call count back to zero. */\r
215                 uxCheckTaskHookCallCount = 0;\r
216         }\r
217         else\r
218         {\r
219                 /* Simply increment a number so we know the callback has been executed. */\r
220                 uxCheckTaskHookCallCount++;\r
221         }\r
222 \r
223         return 0;\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 void vMainQueueSendPassed( void )\r
228 {\r
229         /* This is just an example implementation of the "queue send" trace hook. */\r
230         uxQueueSendPassedCount++;\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 static void vErrorChecks( void *pvParameters )\r
235 {\r
236 portTickType xExpectedWakeTime;\r
237 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;\r
238 const portLONG lMaxAllowableTimeDifference = ( portLONG ) 0;\r
239 portTickType xWakeTime;\r
240 portLONG lTimeDifference;\r
241 const portCHAR *pcReceivedMessage;\r
242 const portCHAR * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";\r
243 const portCHAR * const pcUnexpectedHookValueMsg = "Task hook has unexpected value!\r\n";\r
244 \r
245         ( void ) pvParameters;\r
246 \r
247         /* Register our callback function. */\r
248         vTaskSetApplicationTaskTag( NULL, prvExampleTaskHook );\r
249         \r
250         /* Just for test purposes. */\r
251         if( xTaskGetApplicationTaskTag( NULL ) != prvExampleTaskHook )\r
252         {\r
253                 vPrintDisplayMessage( &pcUnexpectedHookValueMsg );\r
254         }\r
255 \r
256         /* Loop continuously, blocking, then checking all the other tasks are still\r
257         running, before blocking once again.  This task blocks on the queue of\r
258         messages that require displaying so will wake either by its time out expiring,\r
259         or a message becoming available. */\r
260         for( ;; )\r
261         {\r
262                 /* Calculate the time we will unblock if no messages are received\r
263                 on the queue.  This is used to check that we have not blocked for too long. */\r
264                 xExpectedWakeTime = xTaskGetTickCount();\r
265                 xExpectedWakeTime += xPrintRate;\r
266 \r
267                 /* Block waiting for either a time out or a message to be posted that\r
268                 required displaying. */\r
269                 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );\r
270 \r
271                 /* Was a message received? */\r
272                 if( pcReceivedMessage == NULL )\r
273                 {\r
274                         /* A message was not received so we timed out, did we unblock at the\r
275                         expected time? */\r
276                         xWakeTime = xTaskGetTickCount();\r
277 \r
278                         /* Calculate the difference between the time we unblocked and the\r
279                         time we should have unblocked. */\r
280                         if( xWakeTime > xExpectedWakeTime )\r
281                         {\r
282                                 lTimeDifference = ( portLONG ) ( xWakeTime - xExpectedWakeTime );\r
283                         }\r
284                         else\r
285                         {\r
286                                 lTimeDifference = ( portLONG ) ( xExpectedWakeTime - xWakeTime );\r
287                         }\r
288 \r
289                         if( lTimeDifference > lMaxAllowableTimeDifference )\r
290                         {\r
291                                 /* We blocked too long - create a message that will get\r
292                                 printed out the next time around.  If we are not using\r
293                                 preemption then we won't expect the timing to be so\r
294                                 accurate. */\r
295                                 if( sUsingPreemption == pdTRUE )\r
296                                 {\r
297                                         vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );\r
298                                 }\r
299                         }\r
300 \r
301                         /* Check the other tasks are still running, just in case. */\r
302                         prvCheckOtherTasksAreStillRunning();\r
303                 }\r
304                 else\r
305                 {\r
306                         /* We unblocked due to a message becoming available.  Send the message\r
307                         for printing. */\r
308                         vDisplayMessage( pcReceivedMessage );\r
309                 }\r
310 \r
311                 /* Key presses are used to invoke the trace visualisation utility, or end\r
312                 the program. */\r
313                 prvCheckForKeyPresses();\r
314         }\r
315 }\r
316 /*-----------------------------------------------------------*/\r
317 \r
318 static void     prvCheckForKeyPresses( void )\r
319 {\r
320 portSHORT sIn;\r
321 \r
322         taskENTER_CRITICAL();\r
323                 #ifdef DEBUG_BUILD\r
324                         /* kbhit can be used in .exe's that are executed from the command\r
325                         line, but not if executed through the debugger. */\r
326                         sIn = 0;\r
327                 #else\r
328                         sIn = kbhit();\r
329                 #endif\r
330         taskEXIT_CRITICAL();\r
331 \r
332         if( sIn )\r
333         {\r
334                 /* Key presses can be used to start/stop the trace utility, or end the \r
335                 program. */\r
336                 sIn = getch();\r
337                 switch( sIn )\r
338                 {\r
339                         /* Only define keys for turning on and off the trace if the trace\r
340                         is being used. */\r
341                         #if configUSE_TRACE_FACILITY == 1\r
342                                 case 't' :      vTaskList( pcWriteBuffer );\r
343                                                         vWriteMessageToDisk( pcWriteBuffer );\r
344                                                         break;                  \r
345                                 case 's' :      vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );\r
346                                                         break;\r
347 \r
348                                 case 'e' :      {\r
349                                                                 unsigned portLONG ulBufferLength;\r
350                                                                 ulBufferLength = ulTaskEndTrace();\r
351                                                                 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );\r
352                                                         }\r
353                                                         break;\r
354                         #endif\r
355 \r
356                         default  :      vTaskEndScheduler();\r
357                                                 break;\r
358                 }\r
359         }\r
360 }\r
361 /*-----------------------------------------------------------*/\r
362 \r
363 static void prvCheckOtherTasksAreStillRunning( void )\r
364 {\r
365 static portSHORT sErrorHasOccurred = pdFALSE;\r
366 static unsigned portLONG portLONG uxLastHookCallCount = 0, uxLastQueueSendCount = 0;\r
367 \r
368         if( prvCheckMathTasksAreStillRunning() != pdTRUE )\r
369         {\r
370                 vDisplayMessage( "Maths task count unchanged!\r\n" );\r
371                 sErrorHasOccurred = pdTRUE;\r
372         }\r
373 \r
374         if( xAreComTestTasksStillRunning() != pdTRUE )\r
375         {\r
376                 vDisplayMessage( "Com test count unchanged!\r\n" );\r
377                 sErrorHasOccurred = pdTRUE;\r
378         }\r
379 \r
380         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
381         {\r
382                 vDisplayMessage( "Blocking queues count unchanged!\r\n" );\r
383                 sErrorHasOccurred = pdTRUE;\r
384         }\r
385 \r
386         if( xAreAltBlockingQueuesStillRunning() != pdTRUE )\r
387         {\r
388                 vDisplayMessage( "Alt blocking queues count unchanged!\r\n" );\r
389                 sErrorHasOccurred = pdTRUE;\r
390         }\r
391 \r
392         if( xArePollingQueuesStillRunning() != pdTRUE )\r
393         {\r
394                 vDisplayMessage( "Polling queue count unchanged!\r\n" );\r
395                 sErrorHasOccurred = pdTRUE;\r
396         }\r
397 \r
398         if( xAreAltPollingQueuesStillRunning() != pdTRUE )\r
399         {\r
400                 vDisplayMessage( "Alt polling queue count unchanged!\r\n" );\r
401                 sErrorHasOccurred = pdTRUE;\r
402         }\r
403 \r
404         if( xIsCreateTaskStillRunning() != pdTRUE )\r
405         {\r
406                 vDisplayMessage( "Incorrect number of tasks running!\r\n" );\r
407                 sErrorHasOccurred = pdTRUE;\r
408         }\r
409 \r
410         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
411         {\r
412                 vDisplayMessage( "Semaphore take count unchanged!\r\n" );\r
413                 sErrorHasOccurred = pdTRUE;\r
414         }\r
415 \r
416         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
417         {\r
418                 vDisplayMessage( "Dynamic priority count unchanged!\r\n" );\r
419                 sErrorHasOccurred = pdTRUE;\r
420         }\r
421         \r
422         if( xAreMultiEventTasksStillRunning() != pdTRUE )\r
423         {\r
424                 vDisplayMessage( "Error in multi events tasks!\r\n" );\r
425                 sErrorHasOccurred = pdTRUE;\r
426         }\r
427 \r
428         if( xAreFlashCoRoutinesStillRunning() != pdTRUE )\r
429         {\r
430                 vDisplayMessage( "Error in co-routine flash tasks!\r\n" );\r
431                 sErrorHasOccurred = pdTRUE;\r
432         }\r
433 \r
434         if( xAreHookCoRoutinesStillRunning() != pdTRUE )\r
435         {\r
436                 vDisplayMessage( "Error in tick hook to co-routine communications!\r\n" );\r
437                 sErrorHasOccurred = pdTRUE;\r
438         }\r
439 \r
440         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
441         {\r
442                 vDisplayMessage( "Error in block time test tasks!\r\n" );\r
443                 sErrorHasOccurred = pdTRUE;\r
444         }\r
445 \r
446         if( xAreAltBlockTimeTestTasksStillRunning() != pdTRUE )\r
447         {\r
448                 vDisplayMessage( "Error in fast block time test tasks!\r\n" );\r
449                 sErrorHasOccurred = pdTRUE;\r
450         }\r
451 \r
452         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
453         {\r
454                 vDisplayMessage( "Error in generic queue test task!\r\n" );\r
455                 sErrorHasOccurred = pdTRUE;             \r
456         }\r
457 \r
458         if( xAreAltGenericQueueTasksStillRunning() != pdTRUE )\r
459         {\r
460                 vDisplayMessage( "Error in fast generic queue test task!\r\n" );\r
461                 sErrorHasOccurred = pdTRUE;             \r
462         }\r
463 \r
464         if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
465         {\r
466                 vDisplayMessage( "Error in queue peek test task!\r\n" );\r
467                 sErrorHasOccurred = pdTRUE;\r
468         }\r
469 \r
470         if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
471         {\r
472                 vDisplayMessage( "Error in counting semaphore demo task!\r\n" );\r
473                 sErrorHasOccurred = pdTRUE;\r
474         }\r
475 \r
476         if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
477         {\r
478                 vDisplayMessage( "Error in recursive mutex tasks!\r\n" );\r
479                 sErrorHasOccurred = pdTRUE;\r
480         }\r
481 \r
482         /* The hook function associated with this task is called each time the task\r
483         is switched in.  We therefore expect the number of times the callback \r
484         function has been executed to have increrment since the last time this \r
485         function executed. */\r
486         if( uxCheckTaskHookCallCount <= uxLastHookCallCount )\r
487         {\r
488                 vDisplayMessage( "Error in task hook call count!\r\n" );\r
489                 sErrorHasOccurred = pdTRUE;\r
490         }\r
491         else\r
492         {\r
493                 uxLastHookCallCount = uxCheckTaskHookCallCount;\r
494         }\r
495 \r
496         /* We would expect some queue sending to occur between calls of this \r
497         function. */\r
498         if( uxQueueSendPassedCount <= uxLastQueueSendCount )\r
499         {\r
500                 vDisplayMessage( "Error in queue send hook call count!\r\n" );\r
501                 sErrorHasOccurred = pdTRUE;\r
502         }\r
503         else\r
504         {\r
505                 uxLastQueueSendCount = uxQueueSendPassedCount;\r
506         }\r
507 \r
508         if( sErrorHasOccurred == pdFALSE )\r
509         {\r
510                 vDisplayMessage( "OK " );\r
511         }\r
512 }\r
513 /*-----------------------------------------------------------*/\r
514 \r
515 static void prvStartMathTasks( void )\r
516 {\r
517         #ifdef BCC_INDUSTRIAL_PC_PORT\r
518                 /* The Borland project does not yet support floating point. */\r
519                 vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
520         #else\r
521                 vStartMathTasks( tskIDLE_PRIORITY );\r
522         #endif\r
523 }\r
524 /*-----------------------------------------------------------*/\r
525 \r
526 static portBASE_TYPE prvCheckMathTasksAreStillRunning( void )\r
527 {\r
528         #ifdef BCC_INDUSTRIAL_PC_PORT\r
529                 /* The Borland project does not yet support floating point. */\r
530                 return xAreIntegerMathsTaskStillRunning();\r
531         #else\r
532                 return xAreMathsTaskStillRunning();\r
533         #endif\r
534 }\r
535 /*-----------------------------------------------------------*/\r
536 \r
537 void vApplicationIdleHook( void )\r
538 {\r
539         /* The co-routines are executed in the idle task using the idle task \r
540         hook. */\r
541         vCoRoutineSchedule();\r
542 }\r
543 /*-----------------------------------------------------------*/\r
544 \r