]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_STM32F407ZG-SK/main.c
89b0cfc346130e7153e4eb787a69654d68909a0a
[freertos] / FreeRTOS / Demo / CORTEX_M4F_STM32F407ZG-SK / main.c
1 /*\r
2     FreeRTOS V8.0.0:rc1 - 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /******************************************************************************\r
67  * >>>>>> NOTE 1: <<<<<<\r
68  *\r
69  * main() can be configured to create either a very simple LED flasher demo, or\r
70  * a more comprehensive test/demo application.\r
71  *\r
72  * To create a very simple LED flasher example, set the\r
73  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY constant (defined below) to 1.  When\r
74  * this is done, only the standard demo flash tasks are created.  The standard\r
75  * demo flash example creates three tasks, each of which toggle an LED at a\r
76  * fixed but different frequency.\r
77  *\r
78  * To create a more comprehensive test and demo application, set\r
79  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 0.\r
80  *\r
81  * >>>>>> NOTE 2: <<<<<<\r
82  *\r
83  * In addition to the normal set of standard demo tasks, the comprehensive test\r
84  * makes heavy use of the floating point unit, and forces floating point\r
85  * instructions to be used from interrupts that nest three deep.  The nesting\r
86  * starts from the tick hook function, resulting is an abnormally long context\r
87  * switch time.  This is done purely to stress test the FPU context switching\r
88  * implementation, and that part of the test can be removed by setting\r
89  * configUSE_TICK_HOOK to 0 in FreeRTOSConfig.h.\r
90  ******************************************************************************\r
91  *\r
92  * main() creates all the demo application tasks and software timers, then starts\r
93  * the scheduler.  The web documentation provides more details of the standard\r
94  * demo application tasks, which provide no particular functionality, but do\r
95  * provide a good example of how to use the FreeRTOS API.\r
96  *\r
97  * In addition to the standard demo tasks, the following tasks and tests are\r
98  * defined and/or created within this file:\r
99  *\r
100  * "Reg test" tasks - These fill both the core and floating point registers with\r
101  * known values, then check that each register maintains its expected value for\r
102  * the lifetime of the task.  Each task uses a different set of values.  The reg\r
103  * test tasks execute with a very low priority, so get preempted very\r
104  * frequently.  A register containing an unexpected value is indicative of an\r
105  * error in the context switching mechanism.\r
106  *\r
107  * "Check" timer - The check software timer period is initially set to three\r
108  * seconds.  The callback function associated with the check software timer\r
109  * checks that all the standard demo tasks, and the register check tasks, are\r
110  * not only still executing, but are executing without reporting any errors.  If\r
111  * the check software timer discovers that a task has either stalled, or\r
112  * reported an error, then it changes its own execution period from the initial\r
113  * three seconds, to just 200ms.  The check software timer callback function\r
114  * also toggles an LED each time it is called.  This provides a visual\r
115  * indication of the system status:  If the LED toggles every three seconds,\r
116  * then no issues have been discovered.  If the LED toggles every 200ms, then\r
117  * an issue has been discovered with at least one task.\r
118  *\r
119  * Tick hook - The application tick hook is called from the schedulers tick\r
120  * interrupt service routine when configUSE_TICK_HOOK is set to 1 in\r
121  * FreeRTOSConfig.h.  In this example, the tick hook is used to test the kernels\r
122  * handling of the floating point units (FPU) context, both at the task level\r
123  * and when nesting interrupts access the floating point unit registers.  The\r
124  * tick hook function first fills the FPU registers with a known value, it\r
125  * then triggers a medium priority interrupt.  The medium priority interrupt\r
126  * fills the FPU registers with a different value, and triggers a high priority\r
127  * interrupt.  The high priority interrupt once again fills the the FPU\r
128  * registers with a known value before returning to the medium priority\r
129  * interrupt.  The medium priority interrupt checks that the FPU registers\r
130  * contain the values that it wrote to them, then returns to the tick hook\r
131  * function.  Finally, the tick hook function checks that the FPU registers\r
132  * contain the values that it wrote to them, before it too returns.\r
133  *\r
134  * Button interrupt - The button marked "USER" on the starter kit is used to\r
135  * demonstrate how to write an interrupt service routine, and how to synchronise\r
136  * a task with an interrupt.  A task is created that blocks on a test semaphore.\r
137  * When the USER button is pressed, the button interrupt handler gives the\r
138  * semaphore, causing the task to unblock.  When the task unblocks, it simply\r
139  * increments an execution count variable, then returns to block on the\r
140  * semaphore again.\r
141  */\r
142 \r
143 /* Kernel includes. */\r
144 #include "FreeRTOS.h"\r
145 #include "task.h"\r
146 #include "timers.h"\r
147 #include "semphr.h"\r
148 \r
149 /* Demo application includes. */\r
150 #include "partest.h"\r
151 #include "flash.h"\r
152 #include "flop.h"\r
153 #include "integer.h"\r
154 #include "PollQ.h"\r
155 #include "semtest.h"\r
156 #include "dynamic.h"\r
157 #include "BlockQ.h"\r
158 #include "blocktim.h"\r
159 #include "countsem.h"\r
160 #include "GenQTest.h"\r
161 #include "recmutex.h"\r
162 #include "death.h"\r
163 \r
164 /* Hardware and starter kit includes. */\r
165 #include "arm_comm.h"\r
166 #include "iar_stm32f407zg_sk.h"\r
167 #include "stm32f4xx.h"\r
168 #include "stm32f4xx_conf.h"\r
169 \r
170 /* Priorities for the demo application tasks. */\r
171 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 1UL )\r
172 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )\r
173 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
174 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
175 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
176 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
177 \r
178 /* The LED used by the check timer. */\r
179 #define mainCHECK_LED                                           ( 3UL )\r
180 \r
181 /* A block time of zero simply means "don't block". */\r
182 #define mainDONT_BLOCK                                          ( 0UL )\r
183 \r
184 /* The period after which the check timer will expire, in ms, provided no errors\r
185 have been reported by any of the standard demo tasks.  ms are converted to the\r
186 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
187 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
188 \r
189 /* The period at which the check timer will expire, in ms, if an error has been\r
190 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
191 in ticks using the portTICK_PERIOD_MS constant. */\r
192 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
193 \r
194 /* Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 1 to create a simple demo.\r
195 Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 0 to create a much more\r
196 comprehensive test application.  See the comments at the top of this file, and\r
197 the documentation page on the http://www.FreeRTOS.org web site for more\r
198 information. */\r
199 #define mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY         0\r
200 \r
201 /*-----------------------------------------------------------*/\r
202 \r
203 /*\r
204  * Set up the hardware ready to run this demo.\r
205  */\r
206 static void prvSetupHardware( void );\r
207 \r
208 /*\r
209  * The check timer callback function, as described at the top of this file.\r
210  */\r
211 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
212 \r
213 /*\r
214  * Configure the interrupts used to test the interrupt nesting depth as\r
215  * described at the top of this file.\r
216  */\r
217 static void prvSetupNestedFPUInterruptsTest( void );\r
218 \r
219 /*\r
220  * Register check tasks, and the tasks used to write over and check the contents\r
221  * of the FPU registers, as described at the top of this file.  The nature of\r
222  * these files necessitates that they are written in an assembly file.\r
223  */\r
224 extern void vRegTest1Task( void *pvParameters );\r
225 extern void vRegTest2Task( void *pvParameters );\r
226 extern void vRegTestClearFlopRegistersToParameterValue( unsigned long ulValue );\r
227 extern unsigned long ulRegTestCheckFlopRegistersContainParameterValue( unsigned long ulValue );\r
228 \r
229 /*\r
230  * The task that is synchronised with the button interrupt.  This is done just\r
231  * to demonstrate how to write interrupt service routines, and how to\r
232  * synchronise a task with an interrupt.\r
233  */\r
234 static void prvButtonTestTask( void *pvParameters );\r
235 \r
236 /*\r
237  * This file can be used to create either a simple LED flasher example, or a\r
238  * comprehensive test/demo application - depending on the setting of the\r
239  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY constant defined above.  If\r
240  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 1, then the following\r
241  * function will create a lot of additional tasks and a software timer.  If\r
242  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 0, then the following\r
243  * function will do nothing.\r
244  */\r
245 static void prvOptionallyCreateComprehensveTestApplication( void );\r
246 \r
247 /*-----------------------------------------------------------*/\r
248 \r
249 /* The following two variables are used to communicate the status of the\r
250 register check tasks to the check software timer.  If the variables keep\r
251 incrementing, then the register check tasks has not discovered any errors.  If\r
252 a variable stops incrementing, then an error has been found. */\r
253 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
254 \r
255 /* The following variables are used to verify that the interrupt nesting depth\r
256 is as intended.  ulFPUInterruptNesting is incremented on entry to an interrupt\r
257 that uses the FPU, and decremented on exit of the same interrupt.\r
258 ulMaxFPUInterruptNesting latches the highest value reached by\r
259 ulFPUInterruptNesting.  These variables have no other purpose. */\r
260 volatile unsigned long ulFPUInterruptNesting = 0UL, ulMaxFPUInterruptNesting = 0UL;\r
261 \r
262 /* The semaphore used to demonstrate a task being synchronised with an\r
263 interrupt. */\r
264 static SemaphoreHandle_t xTestSemaphore = NULL;\r
265 \r
266 /* The variable that is incremented by the task synchronised with the button\r
267 interrupt. */\r
268 volatile unsigned long ulButtonPressCounts = 0UL;\r
269 \r
270 /*-----------------------------------------------------------*/\r
271 \r
272 int main(void)\r
273 {\r
274         /* Configure the hardware ready to run the test. */\r
275         prvSetupHardware();\r
276 \r
277         /* Start standard demo/test application flash tasks.  See the comments at\r
278         the top of this file.  The LED flash tasks are always created.  The other\r
279         tasks are only created if mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to\r
280         0 (at the top of this file).  See the comments at the top of this file for\r
281         more information. */\r
282         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
283 \r
284         /* The following function will only create more tasks and timers if\r
285         mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 0 (at the top of this\r
286         file).  See the comments at the top of this file for more information. */\r
287         prvOptionallyCreateComprehensveTestApplication();\r
288 \r
289         /* Start the scheduler. */\r
290         vTaskStartScheduler();\r
291 \r
292         /* If all is well, the scheduler will now be running, and the following line\r
293         will never be reached.  If the following line does execute, then there was\r
294         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
295         to be created.  See the memory management section on the FreeRTOS web site\r
296         for more details. */\r
297         for( ;; );\r
298 }\r
299 /*-----------------------------------------------------------*/\r
300 \r
301 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
302 {\r
303 static long lChangedTimerPeriodAlready = pdFALSE;\r
304 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
305 long lErrorFound = pdFALSE;\r
306 \r
307         /* Check all the demo tasks (other than the flash tasks) to ensure\r
308         that they are all still running, and that none have detected an error. */\r
309 \r
310         if( xAreMathsTaskStillRunning() != pdTRUE )\r
311         {\r
312                 lErrorFound = pdTRUE;\r
313         }\r
314 \r
315         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
316         {\r
317                 lErrorFound = pdTRUE;\r
318         }\r
319 \r
320         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
321         {\r
322                 lErrorFound = pdTRUE;\r
323         }\r
324 \r
325         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
326         {\r
327                 lErrorFound = pdTRUE;\r
328         }\r
329 \r
330         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
331         {\r
332                 lErrorFound = pdTRUE;\r
333         }\r
334 \r
335         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
336         {\r
337                 lErrorFound = pdTRUE;\r
338         }\r
339 \r
340         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
341         {\r
342                 lErrorFound = pdTRUE;\r
343         }\r
344 \r
345         if( xIsCreateTaskStillRunning() != pdTRUE )\r
346         {\r
347                 lErrorFound = pdTRUE;\r
348         }\r
349 \r
350         if( xArePollingQueuesStillRunning() != pdTRUE )\r
351         {\r
352                 lErrorFound = pdTRUE;\r
353         }\r
354 \r
355         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
356         {\r
357                 lErrorFound = pdTRUE;\r
358         }\r
359 \r
360         /* Check that the register test 1 task is still running. */\r
361         if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
362         {\r
363                 lErrorFound = pdTRUE;\r
364         }\r
365         ulLastRegTest1Value = ulRegTest1LoopCounter;\r
366 \r
367         /* Check that the register test 2 task is still running. */\r
368         if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
369         {\r
370                 lErrorFound = pdTRUE;\r
371         }\r
372         ulLastRegTest2Value = ulRegTest2LoopCounter;\r
373 \r
374         /* Toggle the check LED to give an indication of the system status.  If\r
375         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
376         everything is ok.  A faster toggle indicates an error. */\r
377         vParTestToggleLED( mainCHECK_LED );\r
378 \r
379         /* Have any errors been latch in lErrorFound?  If so, shorten the\r
380         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
381         This will result in an increase in the rate at which mainCHECK_LED\r
382         toggles. */\r
383         if( lErrorFound != pdFALSE )\r
384         {\r
385                 if( lChangedTimerPeriodAlready == pdFALSE )\r
386                 {\r
387                         lChangedTimerPeriodAlready = pdTRUE;\r
388 \r
389                         /* This call to xTimerChangePeriod() uses a zero block time.\r
390                         Functions called from inside of a timer callback function must\r
391                         *never* attempt to block. */\r
392                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
393                 }\r
394         }\r
395 }\r
396 /*-----------------------------------------------------------*/\r
397 \r
398 static void prvButtonTestTask( void *pvParameters )\r
399 {\r
400         configASSERT( xTestSemaphore );\r
401 \r
402         /* This is the task used as an example of how to synchronise a task with\r
403         an interrupt.  Each time the button interrupt gives the semaphore, this task\r
404         will unblock, increment its execution counter, then return to block\r
405         again. */\r
406 \r
407         /* Take the semaphore before started to ensure it is in the correct\r
408         state. */\r
409         xSemaphoreTake( xTestSemaphore, mainDONT_BLOCK );\r
410 \r
411         for( ;; )\r
412         {\r
413                 xSemaphoreTake( xTestSemaphore, portMAX_DELAY );\r
414                 ulButtonPressCounts++;\r
415         }\r
416 }\r
417 /*-----------------------------------------------------------*/\r
418 \r
419 static void prvSetupHardware( void )\r
420 {\r
421         /* Setup STM32 system (clock, PLL and Flash configuration) */\r
422         SystemInit();\r
423 \r
424         /* Ensure all priority bits are assigned as preemption priority bits. */\r
425         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
426 \r
427         /* Setup the LED outputs. */\r
428         vParTestInitialise();\r
429 \r
430         /* Configure the button input.  This configures the interrupt to use the\r
431         lowest interrupt priority, so it is ok to use the ISR safe FreeRTOS API\r
432         from the button interrupt handler. */\r
433         STM_EVAL_PBInit( BUTTON_USER, BUTTON_MODE_EXTI );\r
434 }\r
435 /*-----------------------------------------------------------*/\r
436 \r
437 void vApplicationTickHook( void )\r
438 {\r
439         #if ( mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY == 0 )\r
440         {\r
441                 /* Just to verify that the interrupt nesting behaves as expected,\r
442                 increment ulFPUInterruptNesting on entry, and decrement it on exit. */\r
443                 ulFPUInterruptNesting++;\r
444 \r
445                 /* Fill the FPU registers with 0. */\r
446                 vRegTestClearFlopRegistersToParameterValue( 0UL );\r
447 \r
448                 /* Trigger a timer 2 interrupt, which will fill the registers with a\r
449                 different value and itself trigger a timer 3 interrupt.  Note that the\r
450                 timers are not actually used.  The timer 2 and 3 interrupt vectors are\r
451                 just used for convenience. */\r
452                 NVIC_SetPendingIRQ( TIM2_IRQn );\r
453 \r
454                 /* Ensure that, after returning from the nested interrupts, all the FPU\r
455                 registers contain the value to which they were set by the tick hook\r
456                 function. */\r
457                 configASSERT( ulRegTestCheckFlopRegistersContainParameterValue( 0UL ) );\r
458 \r
459                 ulFPUInterruptNesting--;\r
460         }\r
461         #endif\r
462 }\r
463 /*-----------------------------------------------------------*/\r
464 \r
465 static void prvSetupNestedFPUInterruptsTest( void )\r
466 {\r
467 NVIC_InitTypeDef NVIC_InitStructure;\r
468 \r
469         /* Enable the TIM2 interrupt in the NVIC.  The timer itself is not used,\r
470         just its interrupt vector to force nesting from software.  TIM2 must have\r
471         a lower priority than TIM3, and both must have priorities above\r
472         configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
473         NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;\r
474         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY - 1;\r
475         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;\r
476         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;\r
477         NVIC_Init( &NVIC_InitStructure );\r
478 \r
479         /* Enable the TIM3 interrupt in the NVIC.  The timer itself is not used,\r
480         just its interrupt vector to force nesting from software.  TIM2 must have\r
481         a lower priority than TIM3, and both must have priorities above\r
482         configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
483         NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;\r
484         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY - 2;\r
485         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;\r
486         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;\r
487         NVIC_Init( &NVIC_InitStructure );\r
488 }\r
489 /*-----------------------------------------------------------*/\r
490 \r
491 void TIM3_IRQHandler( void )\r
492 {\r
493         /* Just to verify that the interrupt nesting behaves as expected, increment\r
494         ulFPUInterruptNesting on entry, and decrement it on exit. */\r
495         ulFPUInterruptNesting++;\r
496 \r
497         /* This is the highest priority interrupt in the chain of forced nesting\r
498         interrupts, so latch the maximum value reached by ulFPUInterruptNesting.\r
499         This is done purely to allow verification that the nesting depth reaches\r
500         that intended. */\r
501         if( ulFPUInterruptNesting > ulMaxFPUInterruptNesting )\r
502         {\r
503                 ulMaxFPUInterruptNesting = ulFPUInterruptNesting;\r
504         }\r
505 \r
506         /* Fill the FPU registers with 99 to overwrite the values written by\r
507         TIM2_IRQHandler(). */\r
508         vRegTestClearFlopRegistersToParameterValue( 99UL );\r
509 \r
510         ulFPUInterruptNesting--;\r
511 }\r
512 /*-----------------------------------------------------------*/\r
513 \r
514 void TIM2_IRQHandler( void )\r
515 {\r
516         /* Just to verify that the interrupt nesting behaves as expected, increment\r
517         ulFPUInterruptNesting on entry, and decrement it on exit. */\r
518         ulFPUInterruptNesting++;\r
519 \r
520         /* Fill the FPU registers with 1. */\r
521         vRegTestClearFlopRegistersToParameterValue( 1UL );\r
522 \r
523         /* Trigger a timer 3 interrupt, which will fill the registers with a\r
524         different value. */\r
525         NVIC_SetPendingIRQ( TIM3_IRQn );\r
526 \r
527         /* Ensure that, after returning from the nesting interrupt, all the FPU\r
528         registers contain the value to which they were set by this interrupt\r
529         function. */\r
530         configASSERT( ulRegTestCheckFlopRegistersContainParameterValue( 1UL ) );\r
531 \r
532         ulFPUInterruptNesting--;\r
533 }\r
534 /*-----------------------------------------------------------*/\r
535 \r
536 static void prvOptionallyCreateComprehensveTestApplication( void )\r
537 {\r
538         #if ( mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY == 0 )\r
539         {\r
540         TimerHandle_t xCheckTimer = NULL;\r
541 \r
542                 /* Configure the interrupts used to test FPU registers being used from\r
543                 nested interrupts. */\r
544                 prvSetupNestedFPUInterruptsTest();\r
545 \r
546                 /* Start all the other standard demo/test tasks. */\r
547                 vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
548                 vStartDynamicPriorityTasks();\r
549                 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
550                 vCreateBlockTimeTasks();\r
551                 vStartCountingSemaphoreTasks();\r
552                 vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
553                 vStartRecursiveMutexTasks();\r
554                 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
555                 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
556 \r
557                 /* Most importantly, start the tasks that use the FPU. */\r
558                 vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
559 \r
560                 /* Create the register check tasks, as described at the top of this\r
561                 file */\r
562                 xTaskCreate( vRegTest1Task, "Reg1", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
563                 xTaskCreate( vRegTest2Task, "Reg2", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
564 \r
565                 /* Create the semaphore that is used to demonstrate a task being\r
566                 synchronised with an interrupt. */\r
567                 vSemaphoreCreateBinary( xTestSemaphore );\r
568 \r
569                 /* Create the task that is unblocked by the demonstration interrupt. */\r
570                 xTaskCreate( prvButtonTestTask, "BtnTest", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
571 \r
572                 /* Create the software timer that performs the 'check' functionality,\r
573                 as described at the top of this file. */\r
574                 xCheckTimer = xTimerCreate( "CheckTimer",                                       /* A text name, purely to help debugging. */\r
575                                                                         ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */\r
576                                                                         pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
577                                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
578                                                                         prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */\r
579                                                                   );\r
580 \r
581                 if( xCheckTimer != NULL )\r
582                 {\r
583                         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
584                 }\r
585 \r
586                 /* This task has to be created last as it keeps account of the number of\r
587                 tasks it expects to see running. */\r
588                 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
589         }\r
590         #else /* mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY */\r
591         {\r
592                 /* Just to prevent compiler warnings when the configuration options are\r
593                 set such that these static functions are not used. */\r
594                 ( void ) vRegTest1Task;\r
595                 ( void ) vRegTest2Task;\r
596                 ( void ) prvCheckTimerCallback;\r
597                 ( void ) prvSetupNestedFPUInterruptsTest;\r
598         }\r
599         #endif /* mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY */\r
600 }\r
601 /*-----------------------------------------------------------*/\r
602 \r
603 void EXTI9_5_IRQHandler(void)\r
604 {\r
605 long lHigherPriorityTaskWoken = pdFALSE;\r
606 \r
607         /* Only line 6 is enabled, so there is no need to test which line generated\r
608         the interrupt. */\r
609         EXTI_ClearITPendingBit( EXTI_Line6 );\r
610 \r
611         /* This interrupt does nothing more than demonstrate how to synchronise a\r
612         task with an interrupt.  First the handler releases a semaphore.\r
613         lHigherPriorityTaskWoken has been initialised to zero. */\r
614         xSemaphoreGiveFromISR( xTestSemaphore, &lHigherPriorityTaskWoken );\r
615 \r
616         /* If there was a task that was blocked on the semaphore, and giving the\r
617         semaphore caused the task to unblock, and the unblocked task has a priority\r
618         higher than the currently executing task (the task that this interrupt\r
619         interrupted), then lHigherPriorityTaskWoken will have been set to pdTRUE.\r
620         Passing pdTRUE into the following macro call will cause this interrupt to\r
621         return directly to the unblocked, higher priority, task. */\r
622         portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );\r
623 }\r
624 /*-----------------------------------------------------------*/\r
625 \r
626 void vApplicationMallocFailedHook( void )\r
627 {\r
628         /* vApplicationMallocFailedHook() will only be called if\r
629         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
630         function that will get called if a call to pvPortMalloc() fails.\r
631         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
632         timer or semaphore is created.  It is also called by various parts of the\r
633         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
634         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
635         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
636         to query the size of free heap space that remains (although it does not\r
637         provide information on how the remaining heap might be fragmented). */\r
638         taskDISABLE_INTERRUPTS();\r
639         for( ;; );\r
640 }\r
641 /*-----------------------------------------------------------*/\r
642 \r
643 void vApplicationIdleHook( void )\r
644 {\r
645         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
646         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
647         task.  It is essential that code added to this hook function never attempts\r
648         to block in any way (for example, call xQueueReceive() with a block time\r
649         specified, or call vTaskDelay()).  If the application makes use of the\r
650         vTaskDelete() API function (as this demo application does) then it is also\r
651         important that vApplicationIdleHook() is permitted to return to its calling\r
652         function, because it is the responsibility of the idle task to clean up\r
653         memory allocated by the kernel to any task that has since been deleted. */\r
654 }\r
655 /*-----------------------------------------------------------*/\r
656 \r
657 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
658 {\r
659         ( void ) pcTaskName;\r
660         ( void ) pxTask;\r
661 \r
662         /* Run time stack overflow checking is performed if\r
663         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
664         function is called if a stack overflow is detected. */\r
665         taskDISABLE_INTERRUPTS();\r
666         for( ;; );\r
667 }\r
668 /*-----------------------------------------------------------*/\r