]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_Infineon_XMC4500_Keil/main_full.c
Update version number.
[freertos] / FreeRTOS / Demo / CORTEX_M4F_Infineon_XMC4500_Keil / main_full.c
1 /*\r
2     FreeRTOS V7.5.1 - 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:  This project provides two demo applications.  A simple blinky style\r
67  * project, and a more comprehensive test and demo application.  The\r
68  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
69  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
70  * in main.c.  This file implements the comprehensive test and demo version.\r
71  *\r
72  * NOTE 2:  This file only contains the source code that is specific to the\r
73  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
74  * required to configure the hardware, are defined in main.c.\r
75  ******************************************************************************\r
76  *\r
77  * main_full() creates all the demo application tasks and a software timer, then\r
78  * starts the scheduler.  The web documentation provides more details of the \r
79  * standard demo application tasks, which provide no particular functionality, \r
80  * but do provide a good example of how to use the FreeRTOS API.\r
81  *\r
82  * In addition to the standard demo tasks, the following tasks and tests are\r
83  * defined and/or created within this file:\r
84  *\r
85  * "Reg test" tasks - These fill both the core and floating point registers with\r
86  * known values, then check that each register maintains its expected value for\r
87  * the lifetime of the task.  Each task uses a different set of values.  The reg\r
88  * test tasks execute with a very low priority, so get preempted very\r
89  * frequently.  A register containing an unexpected value is indicative of an\r
90  * error in the context switching mechanism.\r
91  *\r
92  * "Check" timer - The check software timer period is initially set to three\r
93  * seconds.  The callback function associated with the check software timer\r
94  * checks that all the standard demo tasks, and the register check tasks, are\r
95  * not only still executing, but are executing without reporting any errors.  If\r
96  * the check software timer discovers that a task has either stalled, or\r
97  * reported an error, then it changes its own execution period from the initial\r
98  * three seconds, to just 200ms.  The check software timer callback function\r
99  * also toggles the single LED each time it is called.  This provides a visual\r
100  * indication of the system status:  If the LED toggles every three seconds,\r
101  * then no issues have been discovered.  If the LED toggles every 200ms, then\r
102  * an issue has been discovered with at least one task.\r
103  */\r
104 \r
105 /* Standard includes. */\r
106 #include <stdio.h>\r
107 \r
108 /* Kernel includes. */\r
109 #include "FreeRTOS.h"\r
110 #include "task.h"\r
111 #include "timers.h"\r
112 #include "semphr.h"\r
113 \r
114 /* Standard demo application includes. */\r
115 #include "flop.h"\r
116 #include "integer.h"\r
117 #include "PollQ.h"\r
118 #include "semtest.h"\r
119 #include "dynamic.h"\r
120 #include "BlockQ.h"\r
121 #include "blocktim.h"\r
122 #include "countsem.h"\r
123 #include "GenQTest.h"\r
124 #include "recmutex.h"\r
125 #include "death.h"\r
126 \r
127 /* Hardware includes. */\r
128 #include "XMC4500.h"\r
129 #include "System_XMC4500.h"\r
130 \r
131 /* Priorities for the demo application tasks. */\r
132 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )\r
133 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
134 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
135 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
136 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
137 \r
138 /* To toggle the single LED */\r
139 #define mainTOGGLE_LED()                                        ( PORT3->OMR =  0x02000200 )\r
140 \r
141 /* A block time of zero simply means "don't block". */\r
142 #define mainDONT_BLOCK                                          ( 0UL )\r
143 \r
144 /* The period after which the check timer will expire, in ms, provided no errors\r
145 have been reported by any of the standard demo tasks.  ms are converted to the\r
146 equivalent in ticks using the portTICK_RATE_MS constant. */\r
147 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
148 \r
149 /* The period at which the check timer will expire, in ms, if an error has been\r
150 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
151 in ticks using the portTICK_RATE_MS constant. */\r
152 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_RATE_MS )\r
153 \r
154 /*-----------------------------------------------------------*/\r
155 \r
156 /*\r
157  * The check timer callback function, as described at the top of this file.\r
158  */\r
159 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
160 \r
161 /*\r
162  * Register check tasks, and the tasks used to write over and check the contents\r
163  * of the FPU registers, as described at the top of this file.  The nature of\r
164  * these files necessitates that they are written in an assembly file.\r
165  */\r
166 extern void vRegTest1Task( void *pvParameters );\r
167 extern void vRegTest2Task( void *pvParameters );\r
168 \r
169 /*-----------------------------------------------------------*/\r
170 \r
171 /* The following two variables are used to communicate the status of the\r
172 register check tasks to the check software timer.  If the variables keep\r
173 incrementing, then the register check tasks has not discovered any errors.  If\r
174 a variable stops incrementing, then an error has been found. */\r
175 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
176 \r
177 /*-----------------------------------------------------------*/\r
178 \r
179 void main_full( void )\r
180 {\r
181 xTimerHandle xCheckTimer = NULL;\r
182 \r
183         /* Start all the other standard demo/test tasks.  The have not particular\r
184         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
185         kernel port. */\r
186         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
187         vStartDynamicPriorityTasks();\r
188         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
189         vCreateBlockTimeTasks();\r
190         vStartCountingSemaphoreTasks();\r
191         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
192         vStartRecursiveMutexTasks();\r
193         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
194         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
195         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
196         \r
197         /* Create the register check tasks, as described at the top of this\r
198         file */\r
199         xTaskCreate( vRegTest1Task, ( signed char * ) "Reg1", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
200         xTaskCreate( vRegTest2Task, ( signed char * ) "Reg2", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
201 \r
202         /* Create the software timer that performs the 'check' functionality,\r
203         as described at the top of this file. */\r
204         xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
205                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
206                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
207                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
208                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
209                                                           );    \r
210         \r
211         if( xCheckTimer != NULL )\r
212         {\r
213                 xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
214         }\r
215 \r
216         /* The set of tasks created by the following function call have to be \r
217         created last as they keep account of the number of tasks they expect to see \r
218         running. */\r
219         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
220 \r
221         /* Start the scheduler. */\r
222         vTaskStartScheduler();\r
223         \r
224         /* If all is well, the scheduler will now be running, and the following line\r
225         will never be reached.  If the following line does execute, then there was\r
226         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
227         to be created.  See the memory management section on the FreeRTOS web site\r
228         for more details. */\r
229         for( ;; );      \r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
234 {\r
235 static long lChangedTimerPeriodAlready = pdFALSE;\r
236 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
237 unsigned long ulErrorFound = pdFALSE;\r
238 \r
239         /* Check all the demo tasks (other than the flash tasks) to ensure\r
240         that they are all still running, and that none have detected an error. */\r
241 \r
242         if( xAreMathsTaskStillRunning() != pdTRUE )\r
243         {\r
244                 ulErrorFound = pdTRUE;\r
245         }\r
246 \r
247         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
248         {\r
249                 ulErrorFound = pdTRUE;\r
250         }\r
251 \r
252         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
253         {\r
254                 ulErrorFound = pdTRUE;\r
255         }\r
256 \r
257         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
258         {\r
259                 ulErrorFound = pdTRUE;\r
260         }\r
261 \r
262         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
263         {\r
264                 ulErrorFound = pdTRUE;\r
265         }\r
266 \r
267         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
268         {\r
269                 ulErrorFound = pdTRUE;\r
270         }\r
271 \r
272         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
273         {\r
274                 ulErrorFound = pdTRUE;\r
275         }\r
276 \r
277         if( xIsCreateTaskStillRunning() != pdTRUE )\r
278         {\r
279                 ulErrorFound = pdTRUE;\r
280         }\r
281 \r
282         if( xArePollingQueuesStillRunning() != pdTRUE )\r
283         {\r
284                 ulErrorFound = pdTRUE;\r
285         }\r
286 \r
287         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
288         {\r
289                 ulErrorFound = pdTRUE;\r
290         }\r
291         \r
292         /* Check that the register test 1 task is still running. */\r
293         if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
294         {\r
295                 ulErrorFound = pdTRUE;\r
296         }\r
297         ulLastRegTest1Value = ulRegTest1LoopCounter;\r
298 \r
299         /* Check that the register test 2 task is still running. */\r
300         if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
301         {\r
302                 ulErrorFound = pdTRUE;\r
303         }\r
304         ulLastRegTest2Value = ulRegTest2LoopCounter;\r
305 \r
306         /* Toggle the check LED to give an indication of the system status.  If\r
307         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
308         everything is ok.  A faster toggle indicates an error. */\r
309         mainTOGGLE_LED();       \r
310         \r
311         /* Have any errors been latch in ulErrorFound?  If so, shorten the\r
312         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
313         This will result in an increase in the rate at which mainCHECK_LED\r
314         toggles. */\r
315         if( ulErrorFound != pdFALSE )\r
316         {\r
317                 if( lChangedTimerPeriodAlready == pdFALSE )\r
318                 {\r
319                         lChangedTimerPeriodAlready = pdTRUE;\r
320                         \r
321                         /* This call to xTimerChangePeriod() uses a zero block time.\r
322                         Functions called from inside of a timer callback function must\r
323                         *never* attempt to block. */\r
324                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
325                 }\r
326         }\r
327 }\r
328 /*-----------------------------------------------------------*/\r
329 \r