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