]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_M0_LPC43xx_Keil/M4/main.c
815c4190a1309cf953cbfc615e5d6494441edd6e
[freertos] / FreeRTOS / Demo / CORTEX_M4F_M0_LPC43xx_Keil / M4 / main.c
1 /*\r
2     FreeRTOS V7.1.1 - 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  *\r
83  * main() creates all the demo application tasks and software timers, then starts\r
84  * the scheduler.  The web documentation provides more details of the standard\r
85  * demo application tasks, which provide no particular functionality, but do\r
86  * provide a good example of how to use the FreeRTOS API.\r
87  *\r
88  * In addition to the standard demo tasks, the following tasks and tests are\r
89  * defined and/or created within this file:\r
90  *\r
91  * "Reg test" tasks - These fill both the core and floating point registers with\r
92  * known values, then check that each register maintains its expected value for\r
93  * the lifetime of the task.  Each task uses a different set of values.  The reg\r
94  * test tasks execute with a very low priority, so get preempted very\r
95  * frequently.  A register containing an unexpected value is indicative of an\r
96  * error in the context switching mechanism.\r
97  *\r
98  * "Check" timer - The check software timer period is initially set to three\r
99  * seconds.  The callback function associated with the check software timer\r
100  * checks that all the standard demo tasks, and the register check tasks, are\r
101  * not only still executing, but are executing without reporting any errors.  If\r
102  * the check software timer discovers that a task has either stalled, or\r
103  * reported an error, then it changes its own execution period from the initial\r
104  * three seconds, to just 200ms.  The check software timer callback function\r
105  * also toggles an LED each time it is called.  This provides a visual\r
106  * indication of the system status:  If the LED toggles every three seconds,\r
107  * then no issues have been discovered.  If the LED toggles every 200ms, then\r
108  * an issue has been discovered with at least one task.\r
109  */\r
110 \r
111 /* Standard includes. */\r
112 #include <stdio.h>\r
113 \r
114 /* Kernel includes. */\r
115 #include "FreeRTOS.h"\r
116 #include "task.h"\r
117 #include "timers.h"\r
118 #include "semphr.h"\r
119 \r
120 /* Demo application includes. */\r
121 #include "partest.h"\r
122 #include "flash.h"\r
123 #include "flop.h"\r
124 #include "integer.h"\r
125 #include "PollQ.h"\r
126 #include "semtest.h"\r
127 #include "dynamic.h"\r
128 #include "BlockQ.h"\r
129 #include "blocktim.h"\r
130 #include "countsem.h"\r
131 #include "GenQTest.h"\r
132 #include "recmutex.h"\r
133 #include "death.h"\r
134 \r
135 /* Hardware includes. */\r
136 #include "platform_config.h"\r
137 \r
138 /* Priorities for the demo application tasks. */\r
139 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 1UL )\r
140 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )\r
141 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
142 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
143 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
144 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
145 \r
146 /* The LED used by the check timer. */\r
147 #define mainCHECK_LED                                           ( 3UL )\r
148 \r
149 /* A block time of zero simply means "don't block". */\r
150 #define mainDONT_BLOCK                                          ( 0UL )\r
151 \r
152 /* The period after which the check timer will expire, in ms, provided no errors\r
153 have been reported by any of the standard demo tasks.  ms are converted to the\r
154 equivalent in ticks using the portTICK_RATE_MS constant. */\r
155 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
156 \r
157 /* The period at which the check timer will expire, in ms, if an error has been\r
158 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
159 in ticks using the portTICK_RATE_MS constant. */\r
160 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_RATE_MS )\r
161 \r
162 /* Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 1 to create a simple demo.\r
163 Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 0 to create a much more\r
164 comprehensive test application.  See the comments at the top of this file, and\r
165 the documentation page on the http://www.FreeRTOS.org web site for more\r
166 information. */\r
167 #define mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY         0\r
168 \r
169 /*-----------------------------------------------------------*/\r
170 \r
171 /*\r
172  * Set up the hardware ready to run this demo.\r
173  */\r
174 static void prvSetupHardware( void );\r
175 \r
176 /*\r
177  * The check timer callback function, as described at the top of this file.\r
178  */\r
179 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
180 \r
181 /*\r
182  * Register check tasks, and the tasks used to write over and check the contents\r
183  * of the FPU registers, as described at the top of this file.  The nature of\r
184  * these files necessitates that they are written in an assembly file.\r
185  */\r
186 extern void vRegTest1Task( void *pvParameters );\r
187 extern void vRegTest2Task( void *pvParameters );\r
188 extern void vRegTestClearFlopRegistersToParameterValue( unsigned long ulValue );\r
189 extern unsigned long ulRegTestCheckFlopRegistersContainParameterValue( unsigned long ulValue );\r
190 \r
191 /*\r
192  * This file can be used to create either a simple LED flasher example, or a\r
193  * comprehensive test/demo application - depending on the setting of the\r
194  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY constant defined above.  If\r
195  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 1, then the following\r
196  * function will create a lot of additional tasks and a software timer.  If\r
197  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 0, then the following\r
198  * function will do nothing.\r
199  */\r
200 static void prvOptionallyCreateComprehensveTestApplication( void );\r
201 \r
202 /*-----------------------------------------------------------*/\r
203 \r
204 /* The following two variables are used to communicate the status of the\r
205 register check tasks to the check software timer.  If the variables keep\r
206 incrementing, then the register check tasks has not discovered any errors.  If\r
207 a variable stops incrementing, then an error has been found. */\r
208 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
209 \r
210 /*-----------------------------------------------------------*/\r
211 \r
212 int main( void )\r
213 {\r
214         /* Configure the hardware ready to run the test. */\r
215         prvSetupHardware();\r
216 \r
217         /* Start standard demo/test application flash tasks.  See the comments at\r
218         the top of this file.  The LED flash tasks are always created.  The other\r
219         tasks are only created if mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to\r
220         0 (at the top of this file).  See the comments at the top of this file for\r
221         more information. */\r
222         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
223 \r
224         /* The following function will only create more tasks and timers if\r
225         mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 0 (at the top of this\r
226         file).  See the comments at the top of this file for more information. */\r
227         prvOptionallyCreateComprehensveTestApplication();\r
228 \r
229         /* Start the scheduler. */\r
230         vTaskStartScheduler();\r
231         \r
232         /* Infinite loop */\r
233         for( ;; );      \r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
238 {\r
239 static long lChangedTimerPeriodAlready = pdFALSE;\r
240 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
241 unsigned long ulErrorFound = pdFALSE;\r
242 \r
243         /* Check all the demo tasks (other than the flash tasks) to ensure\r
244         that they are all still running, and that none have detected an error. */\r
245 \r
246         if( xAreMathsTaskStillRunning() != pdTRUE )\r
247         {\r
248                 ulErrorFound |= 0x01UL << 0UL;\r
249         }\r
250 \r
251         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
252         {\r
253                 ulErrorFound |= 0x01UL << 1UL;\r
254         }\r
255 \r
256         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
257         {\r
258                 ulErrorFound |= 0x01UL << 2UL;\r
259         }\r
260 \r
261         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
262         {\r
263                 ulErrorFound |= 0x01UL << 3UL;\r
264         }\r
265 \r
266         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
267         {\r
268                 ulErrorFound |= 0x01UL << 4UL;\r
269         }\r
270 \r
271         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
272         {\r
273                 ulErrorFound |= 0x01UL << 5UL;\r
274         }\r
275 \r
276         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
277         {\r
278                 ulErrorFound |= 0x01UL << 6UL;\r
279         }\r
280 \r
281         if( xIsCreateTaskStillRunning() != pdTRUE )\r
282         {\r
283                 ulErrorFound |= 0x01UL << 7UL;\r
284         }\r
285 \r
286         if( xArePollingQueuesStillRunning() != pdTRUE )\r
287         {\r
288                 ulErrorFound |= 0x01UL << 8UL;\r
289         }\r
290 \r
291         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
292         {\r
293                 ulErrorFound |= 0x01UL << 9UL;\r
294         }\r
295         \r
296         /* Check that the register test 1 task is still running. */\r
297         if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
298         {\r
299                 ulErrorFound |= 0x01UL << 10UL;\r
300         }\r
301         ulLastRegTest1Value = ulRegTest1LoopCounter;\r
302 \r
303         /* Check that the register test 2 task is still running. */\r
304         if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
305         {\r
306                 ulErrorFound |= 0x01UL << 11UL;\r
307         }\r
308         ulLastRegTest2Value = ulRegTest2LoopCounter;\r
309 \r
310         /* Toggle the check LED to give an indication of the system status.  If\r
311         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
312         everything is ok.  A faster toggle indicates an error. */\r
313         vParTestToggleLED( mainCHECK_LED );     \r
314         \r
315         /* Have any errors been latch in ulErrorFound?  If so, shorten the\r
316         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
317         This will result in an increase in the rate at which mainCHECK_LED\r
318         toggles. */\r
319         if( ulErrorFound != pdFALSE )\r
320         {\r
321                 if( lChangedTimerPeriodAlready == pdFALSE )\r
322                 {\r
323                         lChangedTimerPeriodAlready = pdTRUE;\r
324                         \r
325                         /* This call to xTimerChangePeriod() uses a zero block time.\r
326                         Functions called from inside of a timer callback function must\r
327                         *never* attempt to block. */\r
328                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
329                 }\r
330         }\r
331 }\r
332 /*-----------------------------------------------------------*/\r
333 \r
334 static void prvSetupHardware( void )\r
335 {\r
336 extern void Hitex_CGU_Init( void );\r
337 \r
338         /* Setup system (clock, PLL and Flash configuration) */\r
339         platformInit();\r
340 \r
341         /* Wind the clock speed up in steps to its maximum. */\r
342         Hitex_CGU_Init();\r
343         \r
344         /* Ensure all priority bits are assigned as preemption priority bits. */\r
345         NVIC_SetPriorityGrouping( 0 );\r
346         \r
347         /* Setup the LED outputs. */\r
348         vParTestInitialise();\r
349 }\r
350 /*-----------------------------------------------------------*/\r
351 \r
352 static void prvOptionallyCreateComprehensveTestApplication( void )\r
353 {\r
354         #if ( mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY == 0 )\r
355         {\r
356         xTimerHandle xCheckTimer = NULL;\r
357 \r
358                 /* Start all the other standard demo/test tasks. */\r
359                 vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
360                 vStartDynamicPriorityTasks();\r
361                 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
362                 vCreateBlockTimeTasks();\r
363                 vStartCountingSemaphoreTasks();\r
364                 vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
365                 vStartRecursiveMutexTasks();\r
366                 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
367                 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
368 \r
369                 /* Most importantly, start the tasks that use the FPU. */\r
370                 vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
371                 \r
372                 /* Create the register check tasks, as described at the top of this\r
373                 file */\r
374                 xTaskCreate( vRegTest1Task, ( signed char * ) "Reg1", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
375                 xTaskCreate( vRegTest2Task, ( signed char * ) "Reg2", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
376 \r
377                 /* Create the software timer that performs the 'check' functionality,\r
378                 as described at the top of this file. */\r
379                 xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
380                                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
381                                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
382                                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
383                                                                         prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
384                                                                   );    \r
385                 \r
386                 if( xCheckTimer != NULL )\r
387                 {\r
388                         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
389                 }\r
390 \r
391                 /* This task has to be created last as it keeps account of the number of\r
392                 tasks it expects to see running. */\r
393                 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
394         }\r
395         #else /* mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY */\r
396         {\r
397                 /* Just to prevent compiler warnings when the configuration options are\r
398                 set such that these static functions are not used. */\r
399                 ( void ) vRegTest1Task;\r
400                 ( void ) vRegTest2Task;\r
401                 ( void ) prvCheckTimerCallback;\r
402                 ( void ) prvSetupNestedFPUInterruptsTest;\r
403         }\r
404         #endif /* mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY */\r
405 }\r
406 /*-----------------------------------------------------------*/\r
407 \r
408 void vApplicationMallocFailedHook( void )\r
409 {\r
410         /* vApplicationMallocFailedHook() will only be called if\r
411         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
412         function that will get called if a call to pvPortMalloc() fails.\r
413         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
414         timer or semaphore is created.  It is also called by various parts of the\r
415         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
416         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
417         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
418         to query the size of free heap space that remains (although it does not\r
419         provide information on how the remaining heap might be fragmented). */\r
420         taskDISABLE_INTERRUPTS();\r
421         for( ;; );\r
422 }\r
423 /*-----------------------------------------------------------*/\r
424 \r
425 void vApplicationIdleHook( void )\r
426 {\r
427         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
428         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
429         task.  It is essential that code added to this hook function never attempts\r
430         to block in any way (for example, call xQueueReceive() with a block time\r
431         specified, or call vTaskDelay()).  If the application makes use of the\r
432         vTaskDelete() API function (as this demo application does) then it is also\r
433         important that vApplicationIdleHook() is permitted to return to its calling\r
434         function, because it is the responsibility of the idle task to clean up\r
435         memory allocated by the kernel to any task that has since been deleted. */\r
436 }\r
437 /*-----------------------------------------------------------*/\r
438 \r
439 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
440 {\r
441         ( void ) pcTaskName;\r
442         ( void ) pxTask;\r
443 \r
444         /* Run time stack overflow checking is performed if\r
445         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
446         function is called if a stack overflow is detected. */\r
447         taskDISABLE_INTERRUPTS();\r
448         for( ;; );\r
449 }\r
450 /*-----------------------------------------------------------*/\r
451 \r
452 void vApplicationTickHook( void )\r
453 {\r
454         /* This function will be called by each tick interrupt if \r
455         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
456         added here, but the tick hook is called from an interrupt context, so\r
457         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
458         functions can be used (those that end in FromISR()). */\r
459 }\r
460 /*-----------------------------------------------------------*/\r