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