2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 * main-blinky.c is included when the "Blinky" build configuration is used.
\r
71 * main-full.c is included when the "Full" build configuration is used.
\r
73 * main-full.c (this file) defines a comprehensive demo that creates many
\r
74 * tasks, queues, semaphores and timers. It also demonstrates how Cortex-M3
\r
75 * interrupts can interact with FreeRTOS tasks/timers, a simple web server, and
\r
76 * run time statistics gathering functionality. ***IF YOU ARE LOOKING FOR A
\r
77 * SIMPLER STARTING POINT THEN USE THE "BLINKY" BUILD CONFIGURATION FIRST.***
\r
79 * If the Ethernet functionality is excluded, then this demo will run 'stand
\r
80 * alone' (without the rest of the tower system) on the TWR-K60N512 tower
\r
81 * module. If the Ethernet functionality is included, then the full Freescale
\r
82 * K60 tower kit, including both the TWR-K60N512 and TWR-SER modules, is
\r
83 * required (as the Ethernet connector is on the TWR-SER). The TWR-K60N512 is
\r
84 * populated with a K60N512 Cortex-M4 microcontroller.
\r
86 * The main() Function:
\r
87 * main() creates four demo specific software timers, and one demo specific
\r
88 * task (the web server task). It also creates a whole host of 'standard
\r
89 * demo' tasks/queues/semaphores/timers, before starting the scheduler. The
\r
90 * demo specific tasks and timers are described in the comments here. The
\r
91 * standard demo tasks are described on the FreeRTOS.org web site.
\r
93 * The standard demo tasks provide no specific functionality. They are
\r
94 * included to both test the FreeRTOS port, and provide examples of how the
\r
95 * various FreeRTOS API functions can be used.
\r
97 * This demo creates 37 persistent tasks, then dynamically creates and destroys
\r
98 * another two tasks as the demo executes.
\r
101 * The Demo Specific "LED" Timers and Callback Function:
\r
102 * Two very simple LED timers are created. All they do is toggle an LED each
\r
103 * when the timer callback function is executed. The two timers share a
\r
104 * callback function, so the callback function parameter is used to determine
\r
105 * which timer actually expired, and therefore, which LED to toggle. Both
\r
106 * timers use a different frequency, one toggles the blue LED and the other the
\r
109 * The LED/Button Software Timer and the Button Interrupt:
\r
110 * The user button SW2 is configured to generate an interrupt each time it is
\r
111 * pressed. The interrupt service routine switches the orange/yellow LED on,
\r
112 * and resets the LED software timer. The LED timer has a 5000 millisecond (5
\r
113 * second) period, and uses a callback function that is defined to just turn the
\r
114 * LED off again. Therefore, pressing the user button will turn the LED on, and
\r
115 * the LED will remain on until a full five seconds pass without the button
\r
118 * The Demo Specific "Check" Timer and Callback Function:
\r
119 * The check timer period is initially set to three seconds. The check timer
\r
120 * callback function checks that all the standard demo tasks are not only still
\r
121 * executing, but are executing without reporting any errors. If the check
\r
122 * timer discovers that a task has either stalled, or reported an error, then it
\r
123 * changes its own period from the initial three seconds, to just 200ms. The
\r
124 * check timer callback function also toggles the orange/red LED each time it is
\r
125 * called. This provides a visual indication of the system status: If the LED
\r
126 * toggles every three seconds, then no issues have been discovered. If the LED
\r
127 * toggles every 200ms, then an issue has been discovered with at least one
\r
128 * task. The last reported issue is latched into the pcStatusMessage variable,
\r
129 * and displayed at the bottom of the "task stats" web page served by the
\r
130 * embedded web server task.
\r
132 * The web server task:
\r
133 * The web server task implements a simple embedded web server that includes
\r
134 * CGI scripting. Pages are provided that allow task statistics, network
\r
135 * statistics and run time statistics to be viewed. In addition, an IO page is
\r
136 * served that allows the orange/yellow LED to be turned on and off. Finally,
\r
137 * a page is included that serves a large jpg file. See the documentation page
\r
138 * for this demo on the http://www.FreeRTOS.org web site for web server
\r
139 * configuration and usage instructions.
\r
141 * The Demo Specific Idle Hook Function:
\r
142 * The idle hook function demonstrates how to query the amount of FreeRTOS heap
\r
143 * space that is remaining (see vApplicationIdleHook() defined in this file).
\r
145 * The Demo Specific Tick Hook Function:
\r
146 * The tick hook function is used to test the interrupt safe software timer
\r
151 /* Kernel includes. */
\r
152 #include "FreeRTOS.h"
\r
155 #include "timers.h"
\r
157 /* Freescale includes. */
\r
158 #include "common.h"
\r
160 /* Common demo includes. */
\r
161 #include "partest.h"
\r
163 #include "BlockQ.h"
\r
165 #include "blocktim.h"
\r
166 #include "semtest.h"
\r
167 #include "GenQTest.h"
\r
169 #include "recmutex.h"
\r
170 #include "TimerDemo.h"
\r
172 #include "countsem.h"
\r
173 #include "dynamic.h"
\r
175 /* The LED toggled by the check timer callback function. */
\r
176 #define mainCHECK_LED 3UL
\r
178 /* The LED turned on by the button interrupt, and turned off by the LED timer. */
\r
179 #define mainTIMER_CONTROLLED_LED 2UL
\r
181 /* The LEDs toggled by the two simple flash LED timers. */
\r
182 #define mainLED0 0UL
\r
183 #define mainLED1 1UL
\r
185 /* Constant used by the standard timer test functions. */
\r
186 #define mainTIMER_TEST_PERIOD ( 50 )
\r
188 /* Priorities used by the various different standard demo tasks. */
\r
189 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
190 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
191 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
192 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
193 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
194 #define mainuIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
196 /* The WEB server uses string handling functions, which in turn use a bit more
\r
197 stack than most of the other tasks. */
\r
198 #define mainuIP_STACK_SIZE ( configMINIMAL_STACK_SIZE * 3 )
\r
200 /* The period at which the check timer will expire, in ms, provided no errors
\r
201 have been reported by any of the standard demo tasks. ms are converted to the
\r
202 equivalent in ticks using the portTICK_RATE_MS constant. */
\r
203 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_RATE_MS )
\r
205 /* The period at which the check timer will expire, in ms, if an error has been
\r
206 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
207 in ticks using the portTICK_RATE_MS constant. */
\r
208 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_RATE_MS )
\r
210 /* The LED that is turned on by pressing SW2 remains on until the button has not
\r
211 been pushed for a full 5000ms. */
\r
212 #define mainBUTTON_LED_TIMER_PERIOD_MS ( 5000UL / portTICK_RATE_MS )
\r
214 /* The period at which the two simple LED flash timers will execute their
\r
215 callback functions. */
\r
216 #define mainLED1_TIMER_PERIOD_MS ( 200UL / portTICK_RATE_MS )
\r
217 #define mainLED2_TIMER_PERIOD_MS ( 600UL / portTICK_RATE_MS )
\r
219 /* A block time of zero simply means "don't block". */
\r
220 #define mainDONT_BLOCK ( 0UL )
\r
222 /* The vector used by the GPIO port E. Button SW2 is configured to generate
\r
223 an interrupt on this port. */
\r
224 #define mainGPIO_E_VECTOR ( 91 )
\r
226 /*-----------------------------------------------------------*/
\r
229 * Setup the NVIC, LED outputs, and button inputs.
\r
231 static void prvSetupHardware( void );
\r
234 * Creates the timers that are specific to this demo - namely, the check timer
\r
235 * the button LED timer, and the two simple LED flash timers.
\r
237 static void prvCreateDemoSpecificTimers( void );
\r
240 * The LED/button timer callback function. This does nothing but switch an LED
\r
243 static void prvButtonLEDTimerCallback( xTimerHandle xTimer );
\r
246 * The callback function used by both simple LED flash timers. Both timers use
\r
247 * the same callback, so the function parameter is used to determine which LED
\r
248 * should be flashed (effectively to determine which timer has expired).
\r
250 static void prvLEDTimerCallback( xTimerHandle xTimer );
\r
253 * The check timer callback function, as described at the top of this file.
\r
255 static void prvCheckTimerCallback( xTimerHandle xTimer );
\r
258 * Contains the implementation of the web server.
\r
260 extern void vuIP_Task( void *pvParameters );
\r
262 /*-----------------------------------------------------------*/
\r
264 /* The LED/Button software timer. This uses prvButtonLEDTimerCallback() as it's
\r
265 callback function. */
\r
266 static xTimerHandle xLEDButtonTimer = NULL;
\r
268 /* The check timer. This uses prvCheckTimerCallback() as its callback
\r
270 static xTimerHandle xCheckTimer = NULL;
\r
272 /* LED timers - these simply flash LEDs, each using a different frequency. Both
\r
273 use the same prvLEDTimerCallback() callback function. */
\r
274 static xTimerHandle xLED1Timer = NULL, xLED2Timer = NULL;
\r
276 /* If an error is detected in a standard demo task, then pcStatusMessage will
\r
277 be set to point to a string that identifies the offending task. This is just
\r
278 to make debugging easier. */
\r
279 static const char *pcStatusMessage = NULL;
\r
281 /* Used in the run time stats calculations. */
\r
282 static unsigned long ulClocksPer10thOfAMilliSecond = 0UL;
\r
284 /*-----------------------------------------------------------*/
\r
288 /* Configure the NVIC, LED outputs and button inputs. */
\r
289 prvSetupHardware();
\r
291 /* Create the timers that are specific to this demo - other timers are
\r
292 created as part of the standard demo within vStartTimerDemoTask. */
\r
293 prvCreateDemoSpecificTimers();
\r
295 /* Create a lot of 'standard demo' tasks. Nearly 40 tasks are created in
\r
296 this demo. For a much simpler demo, select the 'blinky' build
\r
298 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
299 vCreateBlockTimeTasks();
\r
300 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
301 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
302 vStartQueuePeekTasks();
\r
303 vStartRecursiveMutexTasks();
\r
304 vStartTimerDemoTask( mainTIMER_TEST_PERIOD );
\r
305 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
306 vStartCountingSemaphoreTasks();
\r
307 vStartDynamicPriorityTasks();
\r
309 /* The web server task. */
\r
310 xTaskCreate( vuIP_Task, "uIP", mainuIP_STACK_SIZE, NULL, mainuIP_TASK_PRIORITY, NULL );
\r
312 /* The suicide tasks must be created last, as they need to know how many
\r
313 tasks were running prior to their creation in order to ascertain whether
\r
314 or not the correct/expected number of tasks are running at any given
\r
316 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
318 /* Start the tasks and timers running. */
\r
319 vTaskStartScheduler();
\r
321 /* If all is well, the scheduler will now be running, and the following line
\r
322 will never be reached. If the following line does execute, then there was
\r
323 insufficient FreeRTOS heap memory available for the idle and/or timer tasks
\r
324 to be created. See the memory management section on the FreeRTOS web site
\r
325 for more details. */
\r
328 /*-----------------------------------------------------------*/
\r
330 static void prvCheckTimerCallback( xTimerHandle xTimer )
\r
332 static long lChangedTimerPeriodAlready = pdFALSE;
\r
334 /* Check the standard demo tasks are running without error. Latch the
\r
335 latest reported error in the pcStatusMessage character pointer. The latched
\r
336 string can be viewed using the embedded web server - it is displayed at
\r
337 the bottom of the served "task stats" page. */
\r
338 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
340 pcStatusMessage = "Error: GenQueue";
\r
343 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
345 pcStatusMessage = "Error: QueuePeek\n";
\r
348 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
350 pcStatusMessage = "Error: BlockQueue\n";
\r
353 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
355 pcStatusMessage = "Error: BlockTime\n";
\r
358 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
360 pcStatusMessage = "Error: SemTest\n";
\r
363 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
365 pcStatusMessage = "Error: Death\n";
\r
368 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
370 pcStatusMessage = "Error: RecMutex\n";
\r
373 if( xAreTimerDemoTasksStillRunning( ( mainCHECK_TIMER_PERIOD_MS ) ) != pdTRUE )
\r
375 pcStatusMessage = "Error: TimerDemo\n";
\r
378 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
380 pcStatusMessage = "Error: PollQueue\n";
\r
383 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
385 pcStatusMessage = "Error: CountSem\n";
\r
388 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
390 pcStatusMessage = "Error: DynamicPriority\n";
\r
393 /* Toggle the check LED to give an indication of the system status. If
\r
394 the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then
\r
395 everything is ok. A faster toggle indicates an error. */
\r
396 vParTestToggleLED( mainCHECK_LED );
\r
398 /* Have any errors been latch in pcStatusMessage? If so, shorten the
\r
399 period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.
\r
400 This will result in an increase in the rate at which mainCHECK_LED
\r
402 if( pcStatusMessage != NULL )
\r
404 if( lChangedTimerPeriodAlready == pdFALSE )
\r
406 lChangedTimerPeriodAlready = pdTRUE;
\r
408 /* This call to xTimerChangePeriod() uses a zero block time.
\r
409 Functions called from inside of a timer callback function must
\r
410 *never* attempt to block. */
\r
411 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
415 /*-----------------------------------------------------------*/
\r
417 static void prvButtonLEDTimerCallback( xTimerHandle xTimer )
\r
419 /* The timer has expired - so no button pushes have occurred in the last
\r
420 five seconds - turn the LED off. */
\r
421 vParTestSetLED( mainTIMER_CONTROLLED_LED, pdFALSE );
\r
423 /*-----------------------------------------------------------*/
\r
425 static void prvLEDTimerCallback( xTimerHandle xTimer )
\r
427 unsigned long ulLED;
\r
429 /* This callback is shared by two timers, so the parameter is used to
\r
430 determine which LED to toggle. The LED number is stored in the ID of the
\r
432 ulLED = ( unsigned long ) pvTimerGetTimerID( xTimer );
\r
433 vParTestToggleLED( ulLED );
\r
435 /*-----------------------------------------------------------*/
\r
437 /* The ISR executed when the user button is pushed. */
\r
438 void vPort_E_ISRHandler( void )
\r
440 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
442 /* The button was pushed, so ensure the LED is on before resetting the
\r
443 LED timer. The LED timer will turn the LED off if the button is not
\r
444 pushed within 5000ms. */
\r
445 vParTestSetLED( mainTIMER_CONTROLLED_LED, pdTRUE );
\r
447 /* This interrupt safe FreeRTOS function can be called from this interrupt
\r
448 because the interrupt priority is equal to or below the
\r
449 configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */
\r
450 xTimerResetFromISR( xLEDButtonTimer, &xHigherPriorityTaskWoken );
\r
452 /* Clear the interrupt before leaving. */
\r
453 PORTE_ISFR = 0xFFFFFFFFUL;
\r
455 /* If calling xTimerResetFromISR() caused a task (in this case the timer
\r
456 service/daemon task) to unblock, and the unblocked task has a priority
\r
457 higher than or equal to the task that was interrupted, then
\r
458 xHigherPriorityTaskWoken will now be set to pdTRUE, and calling
\r
459 portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */
\r
460 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
462 /*-----------------------------------------------------------*/
\r
464 static void prvSetupHardware( void )
\r
466 /* Enable the interrupt on SW1. */
\r
467 taskDISABLE_INTERRUPTS();
\r
468 PORTE_PCR26 = PORT_PCR_MUX( 1 ) | PORT_PCR_IRQC( 0xA ) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
\r
469 enable_irq( mainGPIO_E_VECTOR );
\r
471 /* The interrupt calls an interrupt safe API function - so its priority must
\r
472 be equal to or lower than configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY. */
\r
473 set_irq_priority( mainGPIO_E_VECTOR, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
475 /* Configure the LED outputs. */
\r
476 vParTestInitialise();
\r
478 /*-----------------------------------------------------------*/
\r
480 static void prvCreateDemoSpecificTimers( void )
\r
482 /* This function creates the timers, but does not start them. This is
\r
483 because the standard demo timer test is started from main(), after this
\r
484 function is called. The standard demo timer test will deliberately fill the
\r
485 timer command queue - and will fail the test if the command queue already
\r
486 holds start commands for the timers created here. Instead, the timers
\r
487 created in this function are started from the idle task, at which time, the
\r
488 timer service/daemon task will be running, and will have drained the timer
\r
491 /* Create the software timer that is responsible for turning off the LED
\r
492 if the button is not pushed within 5000ms, as described at the top of
\r
494 xLEDButtonTimer = xTimerCreate( ( const signed char * ) "ButtonLEDTimer", /* A text name, purely to help debugging. */
\r
495 ( mainBUTTON_LED_TIMER_PERIOD_MS ), /* The timer period, in this case 5000ms (5s). */
\r
496 pdFALSE, /* This is a one shot timer, so xAutoReload is set to pdFALSE. */
\r
497 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
498 prvButtonLEDTimerCallback /* The callback function that switches the LED off. */
\r
501 /* Create the software timer that performs the 'check' functionality,
\r
502 as described at the top of this file. */
\r
503 xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */
\r
504 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
505 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
506 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
507 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
510 /* Create the software timers used to simply flash LEDs. These two timers
\r
511 share a callback function, so the callback parameter is used to pass in the
\r
512 LED that should be toggled. */
\r
513 xLED1Timer = xTimerCreate( ( const signed char * ) "LED1Timer",/* A text name, purely to help debugging. */
\r
514 ( mainLED1_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
515 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
516 ( void * ) mainLED0, /* The ID is used to pass in the number of the LED to be toggled. */
\r
517 prvLEDTimerCallback /* The callback function simply toggles the LED specified by its parameter. */
\r
520 xLED2Timer = xTimerCreate( ( const signed char * ) "LED2Timer",/* A text name, purely to help debugging. */
\r
521 ( mainLED2_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
522 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
523 ( void * ) mainLED1, /* The ID is used to pass in the number of the LED to be toggled. */
\r
524 prvLEDTimerCallback /* The callback function simply toggles the LED specified by its parameter. */
\r
527 /*-----------------------------------------------------------*/
\r
529 void vApplicationMallocFailedHook( void )
\r
531 /* Called if a call to pvPortMalloc() fails because there is insufficient
\r
532 free memory available in the FreeRTOS heap. pvPortMalloc() is called
\r
533 internally by FreeRTOS API functions that create tasks, queues, software
\r
534 timers, and semaphores. The size of the FreeRTOS heap is set by the
\r
535 configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
\r
536 taskDISABLE_INTERRUPTS();
\r
539 /*-----------------------------------------------------------*/
\r
541 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
543 ( void ) pcTaskName;
\r
546 /* Run time stack overflow checking is performed if
\r
547 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
548 function is called if a stack overflow is detected. */
\r
549 taskDISABLE_INTERRUPTS();
\r
552 /*-----------------------------------------------------------*/
\r
554 void vApplicationIdleHook( void )
\r
556 static long lPrintedOut = pdFALSE;
\r
557 volatile size_t xFreeHeapSpace;
\r
559 if( lPrintedOut == pdFALSE )
\r
561 lPrintedOut = pdTRUE;
\r
563 /* The timer command queue will have been filled when the timer test
\r
564 tasks were created in main() (this is part of the test they perform).
\r
565 Therefore, while the check and LED timers can be created in main(), they
\r
566 cannot be started from main(). Once the scheduler has started, the timer
\r
567 service task will drain the command queue, and now the check and LED
\r
568 timers can be started successfully. Normally the idle task must not
\r
569 call a function that could cause it to block in case there are no tasks
\r
570 that are able to run. In this case, however, it is ok as posting to the
\r
571 timer command queue guarantees that at least the timer service/daemon
\r
572 task will be able to execute. */
\r
573 xTimerStart( xCheckTimer, portMAX_DELAY );
\r
574 xTimerStart( xLED1Timer, portMAX_DELAY );
\r
575 xTimerStart( xLED2Timer, portMAX_DELAY );
\r
577 xFreeHeapSpace = xPortGetFreeHeapSize();
\r
579 if( xFreeHeapSpace > 100 )
\r
581 /* By now, the kernel has allocated everything it is going to, so
\r
582 if there is a lot of heap remaining unallocated then
\r
583 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be
\r
584 reduced accordingly. */
\r
588 /*-----------------------------------------------------------*/
\r
590 void vApplicationTickHook( void )
\r
592 /* Call the periodic timer test, which tests the timer API functions that
\r
593 can be called from an ISR. */
\r
594 vTimerPeriodicISRTests();
\r
596 /*-----------------------------------------------------------*/
\r
598 char *pcGetTaskStatusMessage( void )
\r
600 /* A simple GET function used by a CGI script so it can display the
\r
601 execution status at the bottom of the task stats web page served by the
\r
602 embedded web server. */
\r
603 if( pcStatusMessage == NULL )
\r
605 return "All tasks running without error";
\r
609 return ( char * ) pcStatusMessage;
\r
612 /*-----------------------------------------------------------*/
\r
614 void vMainConfigureTimerForRunTimeStats( void )
\r
616 /* How many clocks are there per tenth of a millisecond? */
\r
617 ulClocksPer10thOfAMilliSecond = configCPU_CLOCK_HZ / 10000UL;
\r
619 /*-----------------------------------------------------------*/
\r
621 unsigned long ulMainGetRunTimeCounterValue( void )
\r
623 unsigned long ulSysTickCounts, ulTickCount, ulReturn;
\r
624 const unsigned long ulSysTickReloadValue = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
\r
625 volatile unsigned long * const pulCurrentSysTickCount = ( ( volatile unsigned long *) 0xe000e018 );
\r
626 volatile unsigned long * const pulInterruptCTRLState = ( ( volatile unsigned long *) 0xe000ed04 );
\r
627 const unsigned long ulSysTickPendingBit = 0x04000000UL;
\r
629 /* NOTE: There are potentially race conditions here. However, it is used
\r
630 anyway to keep the examples simple, and to avoid reliance on a separate
\r
631 timer peripheral. */
\r
634 /* The SysTick is a down counter. How many clocks have passed since it was
\r
636 ulSysTickCounts = ulSysTickReloadValue - *pulCurrentSysTickCount;
\r
638 /* How many times has it overflowed? */
\r
639 ulTickCount = xTaskGetTickCountFromISR();
\r
641 /* This is called from the context switch, so will be called from a
\r
642 critical section. xTaskGetTickCountFromISR() contains its own critical
\r
643 section, and the ISR safe critical sections are not designed to nest,
\r
644 so reset the critical section. */
\r
645 portSET_INTERRUPT_MASK_FROM_ISR();
\r
647 /* Is there a SysTick interrupt pending? */
\r
648 if( ( *pulInterruptCTRLState & ulSysTickPendingBit ) != 0UL )
\r
650 /* There is a SysTick interrupt pending, so the SysTick has overflowed
\r
651 but the tick count not yet incremented. */
\r
654 /* Read the SysTick again, as the overflow might have occurred since
\r
655 it was read last. */
\r
656 ulSysTickCounts = ulSysTickReloadValue - *pulCurrentSysTickCount;
\r
659 /* Convert the tick count into tenths of a millisecond. THIS ASSUMES
\r
660 configTICK_RATE_HZ is 1000! */
\r
661 ulReturn = ( ulTickCount * 10UL ) ;
\r
663 /* Add on the number of tenths of a millisecond that have passed since the
\r
664 tick count last got updated. */
\r
665 ulReturn += ( ulSysTickCounts / ulClocksPer10thOfAMilliSecond );
\r
669 /*-----------------------------------------------------------*/
\r