]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_M0_LPC43xx_Keil/M4/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_M4F_M0_LPC43xx_Keil / M4 / main.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /******************************************************************************\r
30  * >>>>>> NOTE 1: <<<<<<\r
31  *\r
32  * main() can be configured to create either a very simple LED flasher demo, or\r
33  * a more comprehensive test/demo application.\r
34  *\r
35  * To create a very simple LED flasher example, set the\r
36  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY constant (defined below) to 1.  When\r
37  * this is done, only the standard demo flash tasks are created.  The standard\r
38  * demo flash example creates three tasks, each of which toggle an LED at a\r
39  * fixed but different frequency.\r
40  *\r
41  * To create a more comprehensive test and demo application, set\r
42  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 0.\r
43  ******************************************************************************\r
44  *\r
45  * main() creates all the demo application tasks and software timers, then starts\r
46  * the scheduler.  The web documentation provides more details of the standard\r
47  * demo application tasks, which provide no particular functionality, but do\r
48  * provide a good example of how to use the FreeRTOS API.\r
49  *\r
50  * In addition to the standard demo tasks, the following tasks and tests are\r
51  * defined and/or created within this file:\r
52  *\r
53  * "Reg test" tasks - These fill both the core and floating point registers with\r
54  * known values, then check that each register maintains its expected value for\r
55  * the lifetime of the task.  Each task uses a different set of values.  The reg\r
56  * test tasks execute with a very low priority, so get preempted very\r
57  * frequently.  A register containing an unexpected value is indicative of an\r
58  * error in the context switching mechanism.\r
59  *\r
60  * "Check" timer - The check software timer period is initially set to three\r
61  * seconds.  The callback function associated with the check software timer\r
62  * checks that all the standard demo tasks, and the register check tasks, are\r
63  * not only still executing, but are executing without reporting any errors.  If\r
64  * the check software timer discovers that a task has either stalled, or\r
65  * reported an error, then it changes its own execution period from the initial\r
66  * three seconds, to just 200ms.  The check software timer callback function\r
67  * also toggles an LED each time it is called.  This provides a visual\r
68  * indication of the system status:  If the LED toggles every three seconds,\r
69  * then no issues have been discovered.  If the LED toggles every 200ms, then\r
70  * an issue has been discovered with at least one task.\r
71  */\r
72 \r
73 /* Standard includes. */\r
74 #include <stdio.h>\r
75 \r
76 /* Kernel includes. */\r
77 #include "FreeRTOS.h"\r
78 #include "task.h"\r
79 #include "timers.h"\r
80 #include "semphr.h"\r
81 \r
82 /* Demo application includes. */\r
83 #include "partest.h"\r
84 #include "flash.h"\r
85 #include "flop.h"\r
86 #include "integer.h"\r
87 #include "PollQ.h"\r
88 #include "semtest.h"\r
89 #include "dynamic.h"\r
90 #include "BlockQ.h"\r
91 #include "blocktim.h"\r
92 #include "countsem.h"\r
93 #include "GenQTest.h"\r
94 #include "recmutex.h"\r
95 #include "death.h"\r
96 \r
97 /* Hardware includes. */\r
98 #include "platform_config.h"\r
99 \r
100 /* Priorities for the demo application tasks. */\r
101 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 1UL )\r
102 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )\r
103 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
104 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
105 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
106 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
107 \r
108 /* The LED used by the check timer. */\r
109 #define mainCHECK_LED                                           ( 3UL )\r
110 \r
111 /* A block time of zero simply means "don't block". */\r
112 #define mainDONT_BLOCK                                          ( 0UL )\r
113 \r
114 /* The period after which the check timer will expire, in ms, provided no errors\r
115 have been reported by any of the standard demo tasks.  ms are converted to the\r
116 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
117 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
118 \r
119 /* The period at which the check timer will expire, in ms, if an error has been\r
120 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
121 in ticks using the portTICK_PERIOD_MS constant. */\r
122 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
123 \r
124 /* Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 1 to create a simple demo.\r
125 Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 0 to create a much more\r
126 comprehensive test application.  See the comments at the top of this file, and\r
127 the documentation page on the http://www.FreeRTOS.org web site for more\r
128 information. */\r
129 #define mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY         0\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /*\r
134  * Set up the hardware ready to run this demo.\r
135  */\r
136 static void prvSetupHardware( void );\r
137 \r
138 /*\r
139  * The check timer callback function, as described at the top of this file.\r
140  */\r
141 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
142 \r
143 /*\r
144  * Register check tasks, and the tasks used to write over and check the contents\r
145  * of the FPU registers, as described at the top of this file.  The nature of\r
146  * these files necessitates that they are written in an assembly file.\r
147  */\r
148 extern void vRegTest1Task( void *pvParameters );\r
149 extern void vRegTest2Task( void *pvParameters );\r
150 extern void vRegTestClearFlopRegistersToParameterValue( unsigned long ulValue );\r
151 extern unsigned long ulRegTestCheckFlopRegistersContainParameterValue( unsigned long ulValue );\r
152 \r
153 /*\r
154  * This file can be used to create either a simple LED flasher example, or a\r
155  * comprehensive test/demo application - depending on the setting of the\r
156  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY constant defined above.  If\r
157  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 1, then the following\r
158  * function will create a lot of additional tasks and a software timer.  If\r
159  * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 0, then the following\r
160  * function will do nothing.\r
161  */\r
162 static void prvOptionallyCreateComprehensveTestApplication( void );\r
163 \r
164 /*-----------------------------------------------------------*/\r
165 \r
166 /* The following two variables are used to communicate the status of the\r
167 register check tasks to the check software timer.  If the variables keep\r
168 incrementing, then the register check tasks have not discovered any errors.  If\r
169 a variable stops incrementing, then an error has been found. */\r
170 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
171 \r
172 /*-----------------------------------------------------------*/\r
173 \r
174 int main( void )\r
175 {\r
176         /* Configure the hardware ready to run the test. */\r
177         prvSetupHardware();\r
178 \r
179         /* Start standard demo/test application flash tasks.  See the comments at\r
180         the top of this file.  The LED flash tasks are always created.  The other\r
181         tasks are only created if mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to\r
182         0 (at the top of this file).  See the comments at the top of this file for\r
183         more information. */\r
184         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
185 \r
186         /* The following function will only create more tasks and timers if\r
187         mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 0 (at the top of this\r
188         file).  See the comments at the top of this file for more information. */\r
189         prvOptionallyCreateComprehensveTestApplication();\r
190 \r
191         /* Start the scheduler. */\r
192         vTaskStartScheduler();\r
193 \r
194         /* Infinite loop */\r
195         for( ;; );\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
200 {\r
201 static long lChangedTimerPeriodAlready = pdFALSE;\r
202 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
203 unsigned long ulErrorFound = pdFALSE;\r
204 \r
205         /* Check all the demo tasks (other than the flash tasks) to ensure\r
206         that they are all still running, and that none have detected an error. */\r
207 \r
208         if( xAreMathsTaskStillRunning() != pdTRUE )\r
209         {\r
210                 ulErrorFound |= 0x01UL << 0UL;\r
211         }\r
212 \r
213         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
214         {\r
215                 ulErrorFound |= 0x01UL << 1UL;\r
216         }\r
217 \r
218         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
219         {\r
220                 ulErrorFound |= 0x01UL << 2UL;\r
221         }\r
222 \r
223         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
224         {\r
225                 ulErrorFound |= 0x01UL << 3UL;\r
226         }\r
227 \r
228         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
229         {\r
230                 ulErrorFound |= 0x01UL << 4UL;\r
231         }\r
232 \r
233         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
234         {\r
235                 ulErrorFound |= 0x01UL << 5UL;\r
236         }\r
237 \r
238         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
239         {\r
240                 ulErrorFound |= 0x01UL << 6UL;\r
241         }\r
242 \r
243         if( xIsCreateTaskStillRunning() != pdTRUE )\r
244         {\r
245                 ulErrorFound |= 0x01UL << 7UL;\r
246         }\r
247 \r
248         if( xArePollingQueuesStillRunning() != pdTRUE )\r
249         {\r
250                 ulErrorFound |= 0x01UL << 8UL;\r
251         }\r
252 \r
253         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
254         {\r
255                 ulErrorFound |= 0x01UL << 9UL;\r
256         }\r
257 \r
258         /* Check that the register test 1 task is still running. */\r
259         if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
260         {\r
261                 ulErrorFound |= 0x01UL << 10UL;\r
262         }\r
263         ulLastRegTest1Value = ulRegTest1LoopCounter;\r
264 \r
265         /* Check that the register test 2 task is still running. */\r
266         if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
267         {\r
268                 ulErrorFound |= 0x01UL << 11UL;\r
269         }\r
270         ulLastRegTest2Value = ulRegTest2LoopCounter;\r
271 \r
272         /* Toggle the check LED to give an indication of the system status.  If\r
273         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
274         everything is ok.  A faster toggle indicates an error. */\r
275         vParTestToggleLED( mainCHECK_LED );\r
276 \r
277         /* Have any errors been latch in ulErrorFound?  If so, shorten the\r
278         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
279         This will result in an increase in the rate at which mainCHECK_LED\r
280         toggles. */\r
281         if( ulErrorFound != pdFALSE )\r
282         {\r
283                 if( lChangedTimerPeriodAlready == pdFALSE )\r
284                 {\r
285                         lChangedTimerPeriodAlready = pdTRUE;\r
286 \r
287                         /* This call to xTimerChangePeriod() uses a zero block time.\r
288                         Functions called from inside of a timer callback function must\r
289                         *never* attempt to block. */\r
290                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
291                 }\r
292         }\r
293 }\r
294 /*-----------------------------------------------------------*/\r
295 \r
296 static void prvSetupHardware( void )\r
297 {\r
298 extern void Hitex_CGU_Init( void );\r
299 \r
300         /* Setup system (clock, PLL and Flash configuration) */\r
301         platformInit();\r
302 \r
303         /* Wind the clock speed up in steps to its maximum. */\r
304         Hitex_CGU_Init();\r
305 \r
306         /* Ensure all priority bits are assigned as preemption priority bits. */\r
307         NVIC_SetPriorityGrouping( 0 );\r
308 \r
309         /* Setup the LED outputs. */\r
310         vParTestInitialise();\r
311 }\r
312 /*-----------------------------------------------------------*/\r
313 \r
314 static void prvOptionallyCreateComprehensveTestApplication( void )\r
315 {\r
316         #if ( mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY == 0 )\r
317         {\r
318         TimerHandle_t xCheckTimer = NULL;\r
319 \r
320                 /* Start all the other standard demo/test tasks. */\r
321                 vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
322                 vStartDynamicPriorityTasks();\r
323                 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
324                 vCreateBlockTimeTasks();\r
325                 vStartCountingSemaphoreTasks();\r
326                 vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
327                 vStartRecursiveMutexTasks();\r
328                 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
329                 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
330 \r
331                 /* Most importantly, start the tasks that use the FPU. */\r
332                 vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
333 \r
334                 /* Create the register check tasks, as described at the top of this\r
335                 file */\r
336                 xTaskCreate( vRegTest1Task, "Reg1", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
337                 xTaskCreate( vRegTest2Task, "Reg2", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
338 \r
339                 /* Create the software timer that performs the 'check' functionality,\r
340                 as described at the top of this file. */\r
341                 xCheckTimer = xTimerCreate( "CheckTimer",                                       /* A text name, purely to help debugging. */\r
342                                                                         ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */\r
343                                                                         pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
344                                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
345                                                                         prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */\r
346                                                                   );\r
347 \r
348                 if( xCheckTimer != NULL )\r
349                 {\r
350                         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
351                 }\r
352 \r
353                 /* This task has to be created last as it keeps account of the number of\r
354                 tasks it expects to see running. */\r
355                 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
356         }\r
357         #else /* mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY */\r
358         {\r
359                 /* Just to prevent compiler warnings when the configuration options are\r
360                 set such that these static functions are not used. */\r
361                 ( void ) vRegTest1Task;\r
362                 ( void ) vRegTest2Task;\r
363                 ( void ) prvCheckTimerCallback;\r
364                 ( void ) prvSetupNestedFPUInterruptsTest;\r
365         }\r
366         #endif /* mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY */\r
367 }\r
368 /*-----------------------------------------------------------*/\r
369 \r
370 void vApplicationMallocFailedHook( void )\r
371 {\r
372         /* vApplicationMallocFailedHook() will only be called if\r
373         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
374         function that will get called if a call to pvPortMalloc() fails.\r
375         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
376         timer or semaphore is created.  It is also called by various parts of the\r
377         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
378         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
379         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
380         to query the size of free heap space that remains (although it does not\r
381         provide information on how the remaining heap might be fragmented). */\r
382         taskDISABLE_INTERRUPTS();\r
383         for( ;; );\r
384 }\r
385 /*-----------------------------------------------------------*/\r
386 \r
387 void vApplicationIdleHook( void )\r
388 {\r
389         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
390         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
391         task.  It is essential that code added to this hook function never attempts\r
392         to block in any way (for example, call xQueueReceive() with a block time\r
393         specified, or call vTaskDelay()).  If the application makes use of the\r
394         vTaskDelete() API function (as this demo application does) then it is also\r
395         important that vApplicationIdleHook() is permitted to return to its calling\r
396         function, because it is the responsibility of the idle task to clean up\r
397         memory allocated by the kernel to any task that has since been deleted. */\r
398 }\r
399 /*-----------------------------------------------------------*/\r
400 \r
401 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
402 {\r
403         ( void ) pcTaskName;\r
404         ( void ) pxTask;\r
405 \r
406         /* Run time stack overflow checking is performed if\r
407         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
408         function is called if a stack overflow is detected. */\r
409         taskDISABLE_INTERRUPTS();\r
410         for( ;; );\r
411 }\r
412 /*-----------------------------------------------------------*/\r
413 \r
414 void vApplicationTickHook( void )\r
415 {\r
416         /* This function will be called by each tick interrupt if\r
417         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
418         added here, but the tick hook is called from an interrupt context, so\r
419         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
420         functions can be used (those that end in FromISR()). */\r
421 }\r
422 /*-----------------------------------------------------------*/\r