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