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