]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_M0_LPC43xx_Keil/M4/main.c
9871fb4b45b818fdb413ba62dc88882f9feca8af
[freertos] / FreeRTOS / Demo / CORTEX_M4F_M0_LPC43xx_Keil / M4 / main.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /******************************************************************************\r
66  * >>>>>> NOTE 1: <<<<<<\r
67  *\r
68  * main() can be configured to create either a very simple LED flasher demo, or\r
69  * a more comprehensive test/demo application.\r
70  *\r
71  * To create a very simple LED flasher example, set the\r
72  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY constant (defined below) to 1.  When\r
73  * this is done, only the standard demo flash tasks are created.  The standard\r
74  * demo flash example creates three tasks, each of which toggle an LED at a\r
75  * fixed but different frequency.\r
76  *\r
77  * To create a more comprehensive test and demo application, set\r
78  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 0.\r
79  ******************************************************************************\r
80  *\r
81  * main() creates all the demo application tasks and software timers, then starts\r
82  * the scheduler.  The web documentation provides more details of the standard\r
83  * demo application tasks, which provide no particular functionality, but do\r
84  * provide a good example of how to use the FreeRTOS API.\r
85  *\r
86  * In addition to the standard demo tasks, the following tasks and tests are\r
87  * defined and/or created within this file:\r
88  *\r
89  * "Reg test" tasks - These fill both the core and floating point registers with\r
90  * known values, then check that each register maintains its expected value for\r
91  * the lifetime of the task.  Each task uses a different set of values.  The reg\r
92  * test tasks execute with a very low priority, so get preempted very\r
93  * frequently.  A register containing an unexpected value is indicative of an\r
94  * error in the context switching mechanism.\r
95  *\r
96  * "Check" timer - The check software timer period is initially set to three\r
97  * seconds.  The callback function associated with the check software timer\r
98  * checks that all the standard demo tasks, and the register check tasks, are\r
99  * not only still executing, but are executing without reporting any errors.  If\r
100  * the check software timer discovers that a task has either stalled, or\r
101  * reported an error, then it changes its own execution period from the initial\r
102  * three seconds, to just 200ms.  The check software timer callback function\r
103  * also toggles an LED each time it is called.  This provides a visual\r
104  * indication of the system status:  If the LED toggles every three seconds,\r
105  * then no issues have been discovered.  If the LED toggles every 200ms, then\r
106  * an issue has been discovered with at least one task.\r
107  */\r
108 \r
109 /* Standard includes. */\r
110 #include <stdio.h>\r
111 \r
112 /* Kernel includes. */\r
113 #include "FreeRTOS.h"\r
114 #include "task.h"\r
115 #include "timers.h"\r
116 #include "semphr.h"\r
117 \r
118 /* Demo application includes. */\r
119 #include "partest.h"\r
120 #include "flash.h"\r
121 #include "flop.h"\r
122 #include "integer.h"\r
123 #include "PollQ.h"\r
124 #include "semtest.h"\r
125 #include "dynamic.h"\r
126 #include "BlockQ.h"\r
127 #include "blocktim.h"\r
128 #include "countsem.h"\r
129 #include "GenQTest.h"\r
130 #include "recmutex.h"\r
131 #include "death.h"\r
132 \r
133 /* Hardware includes. */\r
134 #include "platform_config.h"\r
135 \r
136 /* Priorities for the demo application tasks. */\r
137 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 1UL )\r
138 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )\r
139 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
140 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
141 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
142 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
143 \r
144 /* The LED used by the check timer. */\r
145 #define mainCHECK_LED                                           ( 3UL )\r
146 \r
147 /* A block time of zero simply means "don't block". */\r
148 #define mainDONT_BLOCK                                          ( 0UL )\r
149 \r
150 /* The period after which the check timer will expire, in ms, provided no errors\r
151 have been reported by any of the standard demo tasks.  ms are converted to the\r
152 equivalent in ticks using the portTICK_RATE_MS constant. */\r
153 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
154 \r
155 /* The period at which the check timer will expire, in ms, if an error has been\r
156 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
157 in ticks using the portTICK_RATE_MS constant. */\r
158 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_RATE_MS )\r
159 \r
160 /* Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 1 to create a simple demo.\r
161 Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 0 to create a much more\r
162 comprehensive test application.  See the comments at the top of this file, and\r
163 the documentation page on the http://www.FreeRTOS.org web site for more\r
164 information. */\r
165 #define mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY         0\r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 /*\r
170  * Set up the hardware ready to run this demo.\r
171  */\r
172 static void prvSetupHardware( void );\r
173 \r
174 /*\r
175  * The check timer callback function, as described at the top of this file.\r
176  */\r
177 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
178 \r
179 /*\r
180  * Register check tasks, and the tasks used to write over and check the contents\r
181  * of the FPU registers, as described at the top of this file.  The nature of\r
182  * these files necessitates that they are written in an assembly file.\r
183  */\r
184 extern void vRegTest1Task( void *pvParameters );\r
185 extern void vRegTest2Task( void *pvParameters );\r
186 extern void vRegTestClearFlopRegistersToParameterValue( unsigned long ulValue );\r
187 extern unsigned long ulRegTestCheckFlopRegistersContainParameterValue( unsigned long ulValue );\r
188 \r
189 /*\r
190  * This file can be used to create either a simple LED flasher example, or a\r
191  * comprehensive test/demo application - depending on the setting of the\r
192  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY constant defined above.  If\r
193  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 1, then the following\r
194  * function will create a lot of additional tasks and a software timer.  If\r
195  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 0, then the following\r
196  * function will do nothing.\r
197  */\r
198 static void prvOptionallyCreateComprehensveTestApplication( void );\r
199 \r
200 /*-----------------------------------------------------------*/\r
201 \r
202 /* The following two variables are used to communicate the status of the\r
203 register check tasks to the check software timer.  If the variables keep\r
204 incrementing, then the register check tasks has not discovered any errors.  If\r
205 a variable stops incrementing, then an error has been found. */\r
206 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
207 \r
208 /*-----------------------------------------------------------*/\r
209 \r
210 int main( void )\r
211 {\r
212         /* Configure the hardware ready to run the test. */\r
213         prvSetupHardware();\r
214 \r
215         /* Start standard demo/test application flash tasks.  See the comments at\r
216         the top of this file.  The LED flash tasks are always created.  The other\r
217         tasks are only created if mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to\r
218         0 (at the top of this file).  See the comments at the top of this file for\r
219         more information. */\r
220         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
221 \r
222         /* The following function will only create more tasks and timers if\r
223         mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 0 (at the top of this\r
224         file).  See the comments at the top of this file for more information. */\r
225         prvOptionallyCreateComprehensveTestApplication();\r
226 \r
227         /* Start the scheduler. */\r
228         vTaskStartScheduler();\r
229         \r
230         /* Infinite loop */\r
231         for( ;; );      \r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
236 {\r
237 static long lChangedTimerPeriodAlready = pdFALSE;\r
238 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
239 unsigned long ulErrorFound = pdFALSE;\r
240 \r
241         /* Check all the demo tasks (other than the flash tasks) to ensure\r
242         that they are all still running, and that none have detected an error. */\r
243 \r
244         if( xAreMathsTaskStillRunning() != pdTRUE )\r
245         {\r
246                 ulErrorFound |= 0x01UL << 0UL;\r
247         }\r
248 \r
249         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
250         {\r
251                 ulErrorFound |= 0x01UL << 1UL;\r
252         }\r
253 \r
254         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
255         {\r
256                 ulErrorFound |= 0x01UL << 2UL;\r
257         }\r
258 \r
259         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
260         {\r
261                 ulErrorFound |= 0x01UL << 3UL;\r
262         }\r
263 \r
264         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
265         {\r
266                 ulErrorFound |= 0x01UL << 4UL;\r
267         }\r
268 \r
269         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
270         {\r
271                 ulErrorFound |= 0x01UL << 5UL;\r
272         }\r
273 \r
274         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
275         {\r
276                 ulErrorFound |= 0x01UL << 6UL;\r
277         }\r
278 \r
279         if( xIsCreateTaskStillRunning() != pdTRUE )\r
280         {\r
281                 ulErrorFound |= 0x01UL << 7UL;\r
282         }\r
283 \r
284         if( xArePollingQueuesStillRunning() != pdTRUE )\r
285         {\r
286                 ulErrorFound |= 0x01UL << 8UL;\r
287         }\r
288 \r
289         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
290         {\r
291                 ulErrorFound |= 0x01UL << 9UL;\r
292         }\r
293         \r
294         /* Check that the register test 1 task is still running. */\r
295         if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
296         {\r
297                 ulErrorFound |= 0x01UL << 10UL;\r
298         }\r
299         ulLastRegTest1Value = ulRegTest1LoopCounter;\r
300 \r
301         /* Check that the register test 2 task is still running. */\r
302         if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
303         {\r
304                 ulErrorFound |= 0x01UL << 11UL;\r
305         }\r
306         ulLastRegTest2Value = ulRegTest2LoopCounter;\r
307 \r
308         /* Toggle the check LED to give an indication of the system status.  If\r
309         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
310         everything is ok.  A faster toggle indicates an error. */\r
311         vParTestToggleLED( mainCHECK_LED );     \r
312         \r
313         /* Have any errors been latch in ulErrorFound?  If so, shorten the\r
314         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
315         This will result in an increase in the rate at which mainCHECK_LED\r
316         toggles. */\r
317         if( ulErrorFound != pdFALSE )\r
318         {\r
319                 if( lChangedTimerPeriodAlready == pdFALSE )\r
320                 {\r
321                         lChangedTimerPeriodAlready = pdTRUE;\r
322                         \r
323                         /* This call to xTimerChangePeriod() uses a zero block time.\r
324                         Functions called from inside of a timer callback function must\r
325                         *never* attempt to block. */\r
326                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
327                 }\r
328         }\r
329 }\r
330 /*-----------------------------------------------------------*/\r
331 \r
332 static void prvSetupHardware( void )\r
333 {\r
334 extern void Hitex_CGU_Init( void );\r
335 \r
336         /* Setup system (clock, PLL and Flash configuration) */\r
337         platformInit();\r
338 \r
339         /* Wind the clock speed up in steps to its maximum. */\r
340         Hitex_CGU_Init();\r
341         \r
342         /* Ensure all priority bits are assigned as preemption priority bits. */\r
343         NVIC_SetPriorityGrouping( 0 );\r
344         \r
345         /* Setup the LED outputs. */\r
346         vParTestInitialise();\r
347 }\r
348 /*-----------------------------------------------------------*/\r
349 \r
350 static void prvOptionallyCreateComprehensveTestApplication( void )\r
351 {\r
352         #if ( mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY == 0 )\r
353         {\r
354         xTimerHandle xCheckTimer = NULL;\r
355 \r
356                 /* Start all the other standard demo/test tasks. */\r
357                 vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
358                 vStartDynamicPriorityTasks();\r
359                 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
360                 vCreateBlockTimeTasks();\r
361                 vStartCountingSemaphoreTasks();\r
362                 vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
363                 vStartRecursiveMutexTasks();\r
364                 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
365                 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
366 \r
367                 /* Most importantly, start the tasks that use the FPU. */\r
368                 vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
369                 \r
370                 /* Create the register check tasks, as described at the top of this\r
371                 file */\r
372                 xTaskCreate( vRegTest1Task, ( signed char * ) "Reg1", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
373                 xTaskCreate( vRegTest2Task, ( signed char * ) "Reg2", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
374 \r
375                 /* Create the software timer that performs the 'check' functionality,\r
376                 as described at the top of this file. */\r
377                 xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
378                                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
379                                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
380                                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
381                                                                         prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
382                                                                   );    \r
383                 \r
384                 if( xCheckTimer != NULL )\r
385                 {\r
386                         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
387                 }\r
388 \r
389                 /* This task has to be created last as it keeps account of the number of\r
390                 tasks it expects to see running. */\r
391                 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
392         }\r
393         #else /* mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY */\r
394         {\r
395                 /* Just to prevent compiler warnings when the configuration options are\r
396                 set such that these static functions are not used. */\r
397                 ( void ) vRegTest1Task;\r
398                 ( void ) vRegTest2Task;\r
399                 ( void ) prvCheckTimerCallback;\r
400                 ( void ) prvSetupNestedFPUInterruptsTest;\r
401         }\r
402         #endif /* mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY */\r
403 }\r
404 /*-----------------------------------------------------------*/\r
405 \r
406 void vApplicationMallocFailedHook( void )\r
407 {\r
408         /* vApplicationMallocFailedHook() will only be called if\r
409         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
410         function that will get called if a call to pvPortMalloc() fails.\r
411         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
412         timer or semaphore is created.  It is also called by various parts of the\r
413         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
414         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
415         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
416         to query the size of free heap space that remains (although it does not\r
417         provide information on how the remaining heap might be fragmented). */\r
418         taskDISABLE_INTERRUPTS();\r
419         for( ;; );\r
420 }\r
421 /*-----------------------------------------------------------*/\r
422 \r
423 void vApplicationIdleHook( void )\r
424 {\r
425         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
426         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
427         task.  It is essential that code added to this hook function never attempts\r
428         to block in any way (for example, call xQueueReceive() with a block time\r
429         specified, or call vTaskDelay()).  If the application makes use of the\r
430         vTaskDelete() API function (as this demo application does) then it is also\r
431         important that vApplicationIdleHook() is permitted to return to its calling\r
432         function, because it is the responsibility of the idle task to clean up\r
433         memory allocated by the kernel to any task that has since been deleted. */\r
434 }\r
435 /*-----------------------------------------------------------*/\r
436 \r
437 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
438 {\r
439         ( void ) pcTaskName;\r
440         ( void ) pxTask;\r
441 \r
442         /* Run time stack overflow checking is performed if\r
443         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
444         function is called if a stack overflow is detected. */\r
445         taskDISABLE_INTERRUPTS();\r
446         for( ;; );\r
447 }\r
448 /*-----------------------------------------------------------*/\r
449 \r
450 void vApplicationTickHook( void )\r
451 {\r
452         /* This function will be called by each tick interrupt if \r
453         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
454         added here, but the tick hook is called from an interrupt context, so\r
455         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
456         functions can be used (those that end in FromISR()). */\r
457 }\r
458 /*-----------------------------------------------------------*/\r