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