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