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