]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_Kinetis_K60_Tower_IAR/main-full.c
Comment K60 demo code, ready for release.
[freertos] / Demo / CORTEX_Kinetis_K60_Tower_IAR / main-full.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3 \r
4 \r
5         FreeRTOS supports many tools and architectures. V7.0.0 is sponsored by:\r
6         Atollic AB - Atollic provides professional embedded systems development\r
7         tools for C/C++ development, code analysis and test automation.\r
8         See http://www.atollic.com\r
9 \r
10 \r
11     ***************************************************************************\r
12      *                                                                       *\r
13      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
14      *    Complete, revised, and edited pdf reference manuals are also       *\r
15      *    available.                                                         *\r
16      *                                                                       *\r
17      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
18      *    ensuring you get running as quickly as possible and with an        *\r
19      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
20      *    the FreeRTOS project to continue with its mission of providing     *\r
21      *    professional grade, cross platform, de facto standard solutions    *\r
22      *    for microcontrollers - completely free of charge!                  *\r
23      *                                                                       *\r
24      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
25      *                                                                       *\r
26      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
27      *                                                                       *\r
28     ***************************************************************************\r
29 \r
30 \r
31     This file is part of the FreeRTOS distribution.\r
32 \r
33     FreeRTOS is free software; you can redistribute it and/or modify it under\r
34     the terms of the GNU General Public License (version 2) as published by the\r
35     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
36     >>>NOTE<<< The modification to the GPL is included to allow you to\r
37     distribute a combined work that includes FreeRTOS without being obliged to\r
38     provide the source code for proprietary components outside of the FreeRTOS\r
39     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
40     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
41     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
42     more details. You should have received a copy of the GNU General Public\r
43     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
44     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
45     by writing to Richard Barry, contact details for whom are available on the\r
46     FreeRTOS WEB site.\r
47 \r
48     1 tab == 4 spaces!\r
49 \r
50     http://www.FreeRTOS.org - Documentation, latest information, license and\r
51     contact details.\r
52 \r
53     http://www.SafeRTOS.com - A version that is certified for use in safety\r
54     critical systems.\r
55 \r
56     http://www.OpenRTOS.com - Commercial support, development, porting,\r
57     licensing and training services.\r
58 */\r
59 \r
60 /*\r
61  * main-blinky.c is included when the "Blinky" build configuration is used.\r
62  * main-full.c is included when the "Full" build configuration is used.\r
63  *\r
64  * main-full.c (this file) defines a comprehensive demo that creates many\r
65  * tasks, queues, semaphores and timers.  It also demonstrates how Cortex-M3\r
66  * interrupts can interact with FreeRTOS tasks/timers, a simple web server, and\r
67  * run time statistics gathering functionality.  ***IF YOU ARE LOOKING FOR A \r
68  * SIMPLER STARTING POINT THEN USE THE "BLINKY" BUILD CONFIGURATION FIRST.***\r
69  *\r
70  * If the Ethernet functionality is excluded, then this demo will run 'stand \r
71  * alone' (without the rest of the tower system) on the TWR-K60N512 tower \r
72  * module.  If the Ethernet functionality is included, then the full Freescale\r
73  * K60 tower kit, including both the TWR-K60N512 and TWR-SER modules, is \r
74  * required (as the Ethernet connector is on the TWR-SER).  The TWR-K60N512 is \r
75  * populated with a K60N512 Cortex-M4 microcontroller.\r
76  *\r
77  * The main() Function:\r
78  * main() creates four demo specific software timers, and one demo specific\r
79  * task (the web server task).  It also creates a whole host of 'standard\r
80  * demo' tasks/queues/semaphores/timers, before starting the scheduler.  The \r
81  * demo specific tasks and timers are described in the comments here.  The \r
82  * standard demo tasks are described on the FreeRTOS.org web site.\r
83  *\r
84  * The standard demo tasks provide no specific functionality.  They are\r
85  * included to both test the FreeRTOS port, and provide examples of how the\r
86  * various FreeRTOS API functions can be used.\r
87  *\r
88  * This demo creates 37 persistent tasks, then dynamically creates and destroys\r
89  * another two tasks as the demo executes.\r
90  *\r
91  *\r
92  * The Demo Specific "LED" Timers and Callback Function:\r
93  * Two very simple LED timers are created.  All they do is toggle and LED timer\r
94  * callback function is executed.  The two timers share a callback function, so \r
95  * the callback function parameter is used to determine which timer actually \r
96  * expired, and therefore, which LED to toggle.  Both timers use a different \r
97  * frequency, one toggles the blue LED and the other the green LED.\r
98  *\r
99  * The LED/Button Software Timer and the Button Interrupt:\r
100  * The user button SW2 is configured to generate an interrupt each time it is\r
101  * pressed.  The interrupt service routine switches the orange/yellow LED on, \r
102  * and resets the LED software timer.  The LED timer has a 5000 millisecond (5 \r
103  * second) period, and uses a callback function that is defined to just turn the \r
104  * LED off again.  Therefore, pressing the user button will turn the LED on, and \r
105  * the LED will remain on until a full five seconds pass without the button \r
106  * being pressed.\r
107  *\r
108  * The Demo Specific "Check" Timer and Callback Function:\r
109  * The check timer period is initially set to three seconds.  The check timer \r
110  * callback function checks that all the standard demo tasks are not only still \r
111  * executing, but are executing without reporting any errors.  If the check \r
112  * timer discovers that a task has either stalled, or reported an error, then it \r
113  * changes its own period from the initial three seconds, to just 200ms.  The \r
114  * check timer callback function also toggles the orange/red LED each time it is \r
115  * called.  This provides a visual indication of the system status:  If the LED \r
116  * toggles every three seconds, then no issues have been discovered.  If the LED \r
117  * toggles every 200ms, then an issue has been discovered with at least one \r
118  * task.  The last reported issue is latched into the pcStatusMessage variable, \r
119  * and displayed at the bottom of the "task stats" web page served by the \r
120  * embedded web server task.\r
121  *\r
122  * The web server task:\r
123  * The web server task implements a simple embedded web server that includes\r
124  * CGI scripting.  Pages are provided that allow task statistics, network\r
125  * statistics and run time statistics to be viewed.  In addition, an IO page is\r
126  * served that allows the orange/yellow LED to be turned on and off.  Finally,\r
127  * a page is included that serves a large jpg file.  See the documentation page\r
128  * for this demo on the http://www.FreeRTOS.org web site for web server\r
129  * configuration and usage instructions.\r
130  *\r
131  * The Demo Specific Idle Hook Function:\r
132  * The idle hook function demonstrates how to query the amount of FreeRTOS heap\r
133  * space that is remaining (see vApplicationIdleHook() defined in this file).\r
134  *\r
135  * The Demo Specific Tick Hook Function:\r
136  * The tick hook function is used to test the interrupt safe software timer\r
137  * functionality.\r
138  * \r
139  */\r
140 \r
141 /* Kernel includes. */\r
142 #include "FreeRTOS.h"\r
143 #include "task.h"\r
144 #include "queue.h"\r
145 #include "timers.h"\r
146 \r
147 /* Freescale includes. */\r
148 #include "common.h"\r
149 \r
150 /* Common demo includes. */\r
151 #include "partest.h"\r
152 #include "flash.h"\r
153 #include "BlockQ.h"\r
154 #include "death.h"\r
155 #include "blocktim.h"\r
156 #include "semtest.h"\r
157 #include "GenQTest.h"\r
158 #include "QPeek.h"\r
159 #include "recmutex.h"\r
160 #include "TimerDemo.h"\r
161 #include "PollQ.h"\r
162 #include "countsem.h"\r
163 #include "dynamic.h"\r
164 \r
165 /* The LED toggled by the check timer callback function.  */\r
166 #define mainCHECK_LED                           3UL\r
167 \r
168 /* The LED turned on by the button interrupt, and turned off by the LED timer. */\r
169 #define mainTIMER_CONTROLLED_LED        2UL\r
170 \r
171 /* The LEDs toggled by the two simple flash LED timers. */\r
172 #define mainLED0                                        0UL\r
173 #define mainLED1                                        1UL\r
174 \r
175 /* Constant used by the standard timer test functions. */\r
176 #define mainTIMER_TEST_PERIOD           ( 50 )\r
177 \r
178 /* Priorities used by the various different standard demo tasks. */\r
179 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
180 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
181 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
182 #define mainCREATOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 3 )\r
183 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )\r
184 #define mainuIP_TASK_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
185 \r
186 /* The WEB server uses string handling functions, which in turn use a bit more\r
187 stack than most of the other tasks. */\r
188 #define mainuIP_STACK_SIZE                      ( configMINIMAL_STACK_SIZE * 3 )\r
189 \r
190 /* The period at which the check timer will expire, in ms, provided no errors\r
191 have been reported by any of the standard demo tasks.  ms are converted to the\r
192 equivalent in ticks using the portTICK_RATE_MS constant. */\r
193 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
194 \r
195 /* The period at which the check timer will expire, in ms, if an error has been\r
196 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
197 in ticks using the portTICK_RATE_MS constant. */\r
198 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_RATE_MS )\r
199 \r
200 /* The LED that is turned on by pressing SW2 remains on until the button has not \r
201 been pushed for a full 5000ms. */\r
202 #define mainBUTTON_LED_TIMER_PERIOD_MS          ( 5000UL / portTICK_RATE_MS )\r
203 \r
204 /* The period at which the two simple LED flash timers will execute their\r
205 callback functions. */\r
206 #define mainLED1_TIMER_PERIOD_MS                        ( 200UL / portTICK_RATE_MS )\r
207 #define mainLED2_TIMER_PERIOD_MS                        ( 600UL / portTICK_RATE_MS )\r
208 \r
209 /* A block time of zero simply means "don't block". */\r
210 #define mainDONT_BLOCK                                          ( 0UL )\r
211 \r
212 /* The vector used by the GPIO port E.  Button SW2 is configured to generate\r
213 an interrupt on this port. */\r
214 #define mainGPIO_E_VECTOR                                       ( 91 )\r
215 \r
216 /*-----------------------------------------------------------*/\r
217 \r
218 /*\r
219  * Setup the NVIC, LED outputs, and button inputs.\r
220  */\r
221 static void prvSetupHardware( void );\r
222 \r
223 /*\r
224  * Creates the timers that are specific to this demo - namely, the check timer\r
225  * the button LED timer, and the two simple LED flash timers.\r
226  */\r
227 static void prvCreateDemoSpecificTimers( void );\r
228 \r
229 /*\r
230  * The LED/button timer callback function.  This does nothing but switch an LED \r
231  * off.\r
232  */\r
233 static void prvButtonLEDTimerCallback( xTimerHandle xTimer );\r
234 \r
235 /*\r
236  * The callback function used by both simple LED flash timers.  Both timers use\r
237  * the same callback, so the function parameter is used to determine which LED\r
238  * should be flashed (effectively to determine which timer has expired).\r
239  */\r
240 static void prvLEDTimerCallback( xTimerHandle xTimer );\r
241 \r
242 /*\r
243  * The check timer callback function, as described at the top of this file.\r
244  */\r
245 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
246 \r
247 /*\r
248  * Contains the implementation of the web server.\r
249  */\r
250 extern void vuIP_Task( void *pvParameters );\r
251 \r
252 /*-----------------------------------------------------------*/\r
253 \r
254 /* The LED/Button software timer.  This uses prvButtonLEDTimerCallback() as it's \r
255 callback function. */\r
256 static xTimerHandle xLEDButtonTimer = NULL;\r
257 \r
258 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
259 function. */\r
260 static xTimerHandle xCheckTimer = NULL;\r
261 \r
262 /* LED timers - these simply flash LEDs, each using a different frequency.  Both\r
263 use the same prvLEDTimerCallback() callback function. */\r
264 static xTimerHandle xLED1Timer = NULL, xLED2Timer = NULL;\r
265 \r
266 /* If an error is detected in a standard demo task, then pcStatusMessage will\r
267 be set to point to a string that identifies the offending task.  This is just\r
268 to make debugging easier. */\r
269 static const char *pcStatusMessage = NULL;\r
270 \r
271 /* Used in the run time stats calculations. */\r
272 static unsigned long ulClocksPer10thOfAMilliSecond = 0UL;\r
273 \r
274 /*-----------------------------------------------------------*/\r
275 \r
276 void main( void )\r
277 {\r
278         /* Configure the NVIC, LED outputs and button inputs. */\r
279         prvSetupHardware();\r
280 \r
281         /* Create the timers that are specific to this demo - other timers are\r
282         created as part of the standard demo within vStartTimerDemoTask. */\r
283         prvCreateDemoSpecificTimers();\r
284 \r
285         /* Create a lot of 'standard demo' tasks.  Nearly 40 tasks are created in\r
286         this demo.  For a much simpler demo, select the 'blinky' build\r
287         configuration. */\r
288         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
289         vCreateBlockTimeTasks();\r
290         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
291         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
292         vStartQueuePeekTasks();\r
293         vStartRecursiveMutexTasks();\r
294         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
295         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
296         vStartCountingSemaphoreTasks();\r
297         vStartDynamicPriorityTasks();\r
298         \r
299         /* The web server task. */\r
300         xTaskCreate( vuIP_Task, "uIP", mainuIP_STACK_SIZE, NULL, mainuIP_TASK_PRIORITY, NULL );\r
301 \r
302         /* The suicide tasks must be created last, as they need to know how many\r
303         tasks were running prior to their creation in order to ascertain whether\r
304         or not the correct/expected number of tasks are running at any given\r
305         time. */\r
306         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
307 \r
308         /* Start the tasks and timers running. */\r
309         vTaskStartScheduler();\r
310 \r
311         /* If all is well, the scheduler will now be running, and the following line\r
312         will never be reached.  If the following line does execute, then there was\r
313         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
314         to be created.  See the memory management section on the FreeRTOS web site\r
315         for more details. */\r
316         for( ;; );\r
317 }\r
318 /*-----------------------------------------------------------*/\r
319 \r
320 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
321 {\r
322 static long lChangedTimerPeriodAlready = pdFALSE;\r
323 \r
324         /* Check the standard demo tasks are running without error.   Latch the\r
325         latest reported error in the pcStatusMessage character pointer.  The latched\r
326         string can be viewed using the embedded web server - it is displayed at\r
327         the bottom of the served "task stats" page. */\r
328         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
329         {\r
330                 pcStatusMessage = "Error: GenQueue";\r
331         }\r
332 \r
333         if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
334         {\r
335                 pcStatusMessage = "Error: QueuePeek\n";\r
336         }\r
337 \r
338         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
339         {\r
340                 pcStatusMessage = "Error: BlockQueue\n";\r
341         }\r
342 \r
343         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
344         {\r
345                 pcStatusMessage = "Error: BlockTime\n";\r
346         }\r
347 \r
348         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
349         {\r
350                 pcStatusMessage = "Error: SemTest\n";\r
351         }\r
352 \r
353         if( xIsCreateTaskStillRunning() != pdTRUE )\r
354         {\r
355                 pcStatusMessage = "Error: Death\n";\r
356         }\r
357 \r
358         if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
359         {\r
360                 pcStatusMessage = "Error: RecMutex\n";\r
361         }\r
362 \r
363         if( xAreTimerDemoTasksStillRunning( ( mainCHECK_TIMER_PERIOD_MS ) ) != pdTRUE )\r
364         {\r
365                 pcStatusMessage = "Error: TimerDemo\n";\r
366         }\r
367 \r
368         if( xArePollingQueuesStillRunning() != pdTRUE )\r
369         {\r
370                 pcStatusMessage = "Error: PollQueue\n";\r
371         }\r
372 \r
373         if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
374         {\r
375                 pcStatusMessage = "Error: CountSem\n";\r
376         }\r
377         \r
378         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
379         {\r
380                 pcStatusMessage = "Error: DynamicPriority\n";\r
381         }\r
382         \r
383         /* Toggle the check LED to give an indication of the system status.  If\r
384         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
385         everything is ok.  A faster toggle indicates an error. */\r
386         vParTestToggleLED( mainCHECK_LED );\r
387 \r
388         /* Have any errors been latch in pcStatusMessage?  If so, shorten the\r
389         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
390         This will result in an increase in the rate at which mainCHECK_LED\r
391         toggles. */\r
392         if( pcStatusMessage != NULL )\r
393         {\r
394                 if( lChangedTimerPeriodAlready == pdFALSE )\r
395                 {\r
396                         lChangedTimerPeriodAlready = pdTRUE;\r
397                         printf( "%s", pcStatusMessage );\r
398                         \r
399                         /* This call to xTimerChangePeriod() uses a zero block time.  \r
400                         Functions called from inside of a timer callback function must \r
401                         *never* attempt to block. */\r
402                         xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
403                 }\r
404         }\r
405 }\r
406 /*-----------------------------------------------------------*/\r
407 \r
408 static void prvButtonLEDTimerCallback( xTimerHandle xTimer )\r
409 {\r
410         /* The timer has expired - so no button pushes have occurred in the last\r
411         five seconds - turn the LED off. */\r
412         vParTestSetLED( mainTIMER_CONTROLLED_LED, pdFALSE );\r
413 }\r
414 /*-----------------------------------------------------------*/\r
415 \r
416 static void prvLEDTimerCallback( xTimerHandle xTimer )\r
417 {\r
418 unsigned long ulLED;\r
419 \r
420         /* This callback is shared by two timers, so the parameter is used to\r
421         determine which LED to toggle.  The LED number is stored in the ID of the\r
422         timer. */\r
423         ulLED = ( unsigned long ) pvTimerGetTimerID( xTimer );\r
424         vParTestToggleLED( ulLED );\r
425 }\r
426 /*-----------------------------------------------------------*/\r
427 \r
428 /* The ISR executed when the user button is pushed. */\r
429 void vPort_E_ISRHandler( void )\r
430 {\r
431 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
432 \r
433         /* The button was pushed, so ensure the LED is on before resetting the\r
434         LED timer.  The LED timer will turn the LED off if the button is not\r
435         pushed within 5000ms. */\r
436         vParTestSetLED( mainTIMER_CONTROLLED_LED, pdTRUE );\r
437 \r
438         /* This interrupt safe FreeRTOS function can be called from this interrupt\r
439         because the interrupt priority is equal to or below the\r
440         configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
441         xTimerResetFromISR( xLEDButtonTimer, &xHigherPriorityTaskWoken );\r
442 \r
443         /* Clear the interrupt before leaving.  */\r
444         PORTE_ISFR = 0xFFFFFFFFUL;\r
445 \r
446         /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
447         service/daemon task) to unblock, and the unblocked task has a priority\r
448         higher than or equal to the task that was interrupted, then\r
449         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
450         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
451         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
452 }\r
453 /*-----------------------------------------------------------*/\r
454 \r
455 static void prvSetupHardware( void )\r
456 {\r
457         /* Enable the interrupt on SW1. */\r
458         taskDISABLE_INTERRUPTS();\r
459         PORTE_PCR26 = PORT_PCR_MUX( 1 ) | PORT_PCR_IRQC( 0xA ) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;\r
460         enable_irq( mainGPIO_E_VECTOR );\r
461         \r
462         /* The interrupt calls an interrupt safe API function - so its priority must\r
463         be equal to or lower than configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY. */\r
464         set_irq_priority( mainGPIO_E_VECTOR, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
465         \r
466         /* Configure the LED outputs. */\r
467         vParTestInitialise();\r
468 }\r
469 /*-----------------------------------------------------------*/\r
470 \r
471 static void prvCreateDemoSpecificTimers( void )\r
472 {\r
473         /* This function creates the timers, but does not start them.  This is\r
474         because the standard demo timer test is started from main(), after this \r
475         function is     called.  The standard demo timer test will deliberately fill the \r
476         timer command queue - and will fail the test if the command queue already \r
477         holds start commands for the timers created here.  Instead, the timers \r
478         created in this function are started from the idle task, at which time, the \r
479         timer service/daemon task will be running, and will have drained the timer \r
480         command queue. */\r
481         \r
482         /* Create the software timer that is responsible for turning off the LED\r
483         if the button is not pushed within 5000ms, as described at the top of\r
484         this file. */\r
485         xLEDButtonTimer = xTimerCreate(         ( const signed char * ) "ButtonLEDTimer",       /* A text name, purely to help debugging. */\r
486                                                                 ( mainBUTTON_LED_TIMER_PERIOD_MS ),                     /* The timer period, in this case 5000ms (5s). */\r
487                                                                 pdFALSE,                                                                        /* This is a one shot timer, so xAutoReload is set to pdFALSE. */\r
488                                                                 ( void * ) 0,                                                           /* The ID is not used, so can be set to anything. */\r
489                                                                 prvButtonLEDTimerCallback                                       /* The callback function that switches the LED off. */\r
490                                                         );\r
491 \r
492         /* Create the software timer that performs the 'check' functionality,\r
493         as described at the top of this file. */\r
494         xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
495                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
496                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
497                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
498                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
499                                                           );\r
500         \r
501         /* Create the software timers used to simply flash LEDs.  These two timers\r
502         share a callback function, so the callback parameter is used to pass in the\r
503         LED that should be toggled. */\r
504         xLED1Timer = xTimerCreate( ( const signed char * ) "LED1Timer",/* A text name, purely to help debugging. */\r
505                                                                 ( mainLED1_TIMER_PERIOD_MS ),           /* The timer period, in this case 3000ms (3s). */\r
506                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
507                                                                 ( void * ) mainLED0,                            /* The ID is used to pass in the number of the LED to be toggled. */\r
508                                                                 prvLEDTimerCallback                                     /* The callback function simply toggles the LED specified by its parameter. */\r
509                                                           );\r
510 \r
511         xLED2Timer = xTimerCreate( ( const signed char * ) "LED2Timer",/* A text name, purely to help debugging. */\r
512                                                                 ( mainLED2_TIMER_PERIOD_MS ),           /* The timer period, in this case 3000ms (3s). */\r
513                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
514                                                                 ( void * ) mainLED1,                            /* The ID is used to pass in the number of the LED to be toggled. */\r
515                                                                 prvLEDTimerCallback                                     /* The callback function simply toggles the LED specified by its parameter. */\r
516                                                           );\r
517 }\r
518 /*-----------------------------------------------------------*/\r
519 \r
520 void vApplicationMallocFailedHook( void )\r
521 {\r
522         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
523         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
524         internally by FreeRTOS API functions that create tasks, queues, software\r
525         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
526         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
527         taskDISABLE_INTERRUPTS();\r
528         for( ;; );\r
529 }\r
530 /*-----------------------------------------------------------*/\r
531 \r
532 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
533 {\r
534         ( void ) pcTaskName;\r
535         ( void ) pxTask;\r
536 \r
537         /* Run time stack overflow checking is performed if\r
538         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
539         function is called if a stack overflow is detected. */\r
540         taskDISABLE_INTERRUPTS();\r
541         for( ;; );\r
542 }\r
543 /*-----------------------------------------------------------*/\r
544 \r
545 void vApplicationIdleHook( void )\r
546 {\r
547 static long lPrintedOut = pdFALSE;\r
548 volatile size_t xFreeHeapSpace;\r
549 \r
550         if( lPrintedOut == pdFALSE )\r
551         {\r
552                 lPrintedOut = pdTRUE;\r
553 \r
554                 /* The timer command queue will have been filled when the timer test\r
555                 tasks were created in main() (this is part of the test they perform).\r
556                 Therefore, while the check and LED timers can be created in main(), they\r
557                 cannot be started from main().  Once the scheduler has started, the timer\r
558                 service task will drain the command queue, and now the check and LED\r
559                 timers can be started successfully.  Normally the idle task must not\r
560                 call a function that could cause it to block in case there are no tasks\r
561                 that are able to run.  In this case, however, it is ok as posting to the\r
562                 timer command queue guarantees that at least the timer service/daemon\r
563                 task will be able to execute. */\r
564                 xTimerStart( xCheckTimer, portMAX_DELAY );\r
565                 xTimerStart( xLED1Timer, portMAX_DELAY );\r
566                 xTimerStart( xLED2Timer, portMAX_DELAY );\r
567                 \r
568                 xFreeHeapSpace = xPortGetFreeHeapSize();\r
569                 printf( "%d bytes of FreeRTOS heap remain unused\nconfigTOTAL_HEAP_SIZE can be reduced\n", xFreeHeapSpace );\r
570                 \r
571                 if( xFreeHeapSpace > 100 )\r
572                 {\r
573                         /* By now, the kernel has allocated everything it is going to, so\r
574                         if there is a lot of heap remaining unallocated then\r
575                         the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
576                         reduced accordingly. */\r
577                 }\r
578         }\r
579 }\r
580 /*-----------------------------------------------------------*/\r
581 \r
582 void vApplicationTickHook( void )\r
583 {\r
584         /* Call the periodic timer test, which tests the timer API functions that\r
585         can be called from an ISR. */\r
586         vTimerPeriodicISRTests();\r
587 }       \r
588 /*-----------------------------------------------------------*/\r
589 \r
590 char *pcGetTaskStatusMessage( void )\r
591 {\r
592         /* A simple GET function used by a CGI script so it can display the \r
593         execution status at the bottom of the task stats web page served by the\r
594         embedded web server. */\r
595         if( pcStatusMessage == NULL )\r
596         {\r
597                 return "All tasks running without error";\r
598         }\r
599         else\r
600         {\r
601                 return ( char * ) pcStatusMessage;\r
602         }\r
603 }\r
604 /*-----------------------------------------------------------*/\r
605 \r
606 void vMainConfigureTimerForRunTimeStats( void )\r
607 {\r
608         /* How many clocks are there per tenth of a millisecond? */\r
609         ulClocksPer10thOfAMilliSecond = configCPU_CLOCK_HZ / 10000UL;\r
610 }\r
611 /*-----------------------------------------------------------*/\r
612 \r
613 unsigned long ulMainGetRunTimeCounterValue( void )\r
614 {\r
615 unsigned long ulSysTickCounts, ulTickCount, ulReturn;\r
616 const unsigned long ulSysTickReloadValue = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
617 volatile unsigned long * const pulCurrentSysTickCount = ( ( volatile unsigned long *) 0xe000e018 );\r
618 volatile unsigned long * const pulInterruptCTRLState = ( ( volatile unsigned long *) 0xe000ed04 );\r
619 const unsigned long ulSysTickPendingBit = 0x04000000UL;\r
620 \r
621         /* NOTE: There are potentially race conditions here.  However, it is used\r
622         anyway to keep the examples simple, and to avoid reliance on a separate\r
623         timer peripheral. */\r
624 \r
625 \r
626         /* The SysTick is a down counter.  How many clocks have passed since it was\r
627         last reloaded? */\r
628         ulSysTickCounts = ulSysTickReloadValue - *pulCurrentSysTickCount;\r
629         \r
630         /* How many times has it overflowed? */\r
631         ulTickCount = xTaskGetTickCountFromISR();\r
632         \r
633         /* Is there a SysTick interrupt pending? */\r
634         if( ( *pulInterruptCTRLState & ulSysTickPendingBit ) != 0UL )\r
635         {\r
636                 /* There is a SysTick interrupt pending, so the SysTick has overflowed\r
637                 but the tick count not yet incremented. */\r
638                 ulTickCount++;\r
639                 \r
640                 /* Read the SysTick again, as the overflow might have occurred since\r
641                 it was read last. */\r
642                 ulSysTickCounts = ulSysTickReloadValue - *pulCurrentSysTickCount;\r
643         }       \r
644         \r
645         /* Convert the tick count into tenths of a millisecond.  THIS ASSUMES\r
646         configTICK_RATE_HZ is 1000! */\r
647         ulReturn = ( ulTickCount * 10UL ) ;\r
648                 \r
649         /* Add on the number of tenths of a millisecond that have passed since the\r
650         tick count last got updated. */\r
651         ulReturn += ( ulSysTickCounts / ulClocksPer10thOfAMilliSecond );\r
652         \r
653         return ulReturn;        \r
654 }\r
655 /*-----------------------------------------------------------*/\r
656 \r