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