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