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