]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_Infineon_XMC4500_Keil/main_full.c
7a7a2eb7d8fb7fa4762a22f432f90e1cbcaaded9
[freertos] / FreeRTOS / Demo / CORTEX_M4F_Infineon_XMC4500_Keil / main_full.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /******************************************************************************\r
68  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
69  * project, and a more comprehensive test and demo application.  The\r
70  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
71  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
72  * in main.c.  This file implements the comprehensive test and demo version.\r
73  *\r
74  * NOTE 2:  This file only contains the source code that is specific to the\r
75  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
76  * required to configure the hardware, are defined in main.c.\r
77  ******************************************************************************\r
78  *\r
79  * main_full() creates all the demo application tasks and a software timer, then\r
80  * starts the scheduler.  The web documentation provides more details of the \r
81  * standard demo application tasks, which provide no particular functionality, \r
82  * but do provide a good example of how to use the FreeRTOS API.\r
83  *\r
84  * In addition to the standard demo tasks, the following tasks and tests are\r
85  * defined and/or created within this file:\r
86  *\r
87  * "Reg test" tasks - These fill both the core and floating point registers with\r
88  * known values, then check that each register maintains its expected value for\r
89  * the lifetime of the task.  Each task uses a different set of values.  The reg\r
90  * test tasks execute with a very low priority, so get preempted very\r
91  * frequently.  A register containing an unexpected value is indicative of an\r
92  * error in the context switching mechanism.\r
93  *\r
94  * "Check" timer - The check software timer period is initially set to three\r
95  * seconds.  The callback function associated with the check software timer\r
96  * checks that all the standard demo tasks, and the register check tasks, are\r
97  * not only still executing, but are executing without reporting any errors.  If\r
98  * the check software timer discovers that a task has either stalled, or\r
99  * reported an error, then it changes its own execution period from the initial\r
100  * three seconds, to just 200ms.  The check software timer callback function\r
101  * also toggles the single LED each time it is called.  This provides a visual\r
102  * indication of the system status:  If the LED toggles every three seconds,\r
103  * then no issues have been discovered.  If the LED toggles every 200ms, then\r
104  * an issue has been discovered with at least one task.\r
105  */\r
106 \r
107 /* Standard includes. */\r
108 #include <stdio.h>\r
109 \r
110 /* Kernel includes. */\r
111 #include "FreeRTOS.h"\r
112 #include "task.h"\r
113 #include "timers.h"\r
114 #include "semphr.h"\r
115 \r
116 /* Standard demo application includes. */\r
117 #include "flop.h"\r
118 #include "integer.h"\r
119 #include "PollQ.h"\r
120 #include "semtest.h"\r
121 #include "dynamic.h"\r
122 #include "BlockQ.h"\r
123 #include "blocktim.h"\r
124 #include "countsem.h"\r
125 #include "GenQTest.h"\r
126 #include "recmutex.h"\r
127 #include "death.h"\r
128 \r
129 /* Hardware includes. */\r
130 #include "XMC4500.h"\r
131 #include "System_XMC4500.h"\r
132 \r
133 /* Priorities for the demo application tasks. */\r
134 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )\r
135 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
136 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
137 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
138 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
139 \r
140 /* To toggle the single LED */\r
141 #define mainTOGGLE_LED()                                        ( PORT3->OMR =  0x02000200 )\r
142 \r
143 /* A block time of zero simply means "don't block". */\r
144 #define mainDONT_BLOCK                                          ( 0UL )\r
145 \r
146 /* The period after which the check timer will expire, in ms, provided no errors\r
147 have been reported by any of the standard demo tasks.  ms are converted to the\r
148 equivalent in ticks using the portTICK_RATE_MS constant. */\r
149 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
150 \r
151 /* The period at which the check timer will expire, in ms, if an error has been\r
152 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
153 in ticks using the portTICK_RATE_MS constant. */\r
154 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_RATE_MS )\r
155 \r
156 /*-----------------------------------------------------------*/\r
157 \r
158 /*\r
159  * The check timer callback function, as described at the top of this file.\r
160  */\r
161 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
162 \r
163 /*\r
164  * Register check tasks, and the tasks used to write over and check the contents\r
165  * of the FPU registers, as described at the top of this file.  The nature of\r
166  * these files necessitates that they are written in an assembly file.\r
167  */\r
168 extern void vRegTest1Task( void *pvParameters );\r
169 extern void vRegTest2Task( void *pvParameters );\r
170 \r
171 /*-----------------------------------------------------------*/\r
172 \r
173 /* The following two variables are used to communicate the status of the\r
174 register check tasks to the check software timer.  If the variables keep\r
175 incrementing, then the register check tasks has not discovered any errors.  If\r
176 a variable stops incrementing, then an error has been found. */\r
177 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
178 \r
179 /*-----------------------------------------------------------*/\r
180 \r
181 void main_full( void )\r
182 {\r
183 xTimerHandle xCheckTimer = NULL;\r
184 \r
185         /* Start all the other standard demo/test tasks.  The have not particular\r
186         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
187         kernel port. */\r
188         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
189         vStartDynamicPriorityTasks();\r
190         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
191         vCreateBlockTimeTasks();\r
192         vStartCountingSemaphoreTasks();\r
193         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
194         vStartRecursiveMutexTasks();\r
195         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
196         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
197         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
198         \r
199         /* Create the register check tasks, as described at the top of this\r
200         file */\r
201         xTaskCreate( vRegTest1Task, ( signed char * ) "Reg1", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
202         xTaskCreate( vRegTest2Task, ( signed char * ) "Reg2", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
203 \r
204         /* Create the software timer that performs the 'check' functionality,\r
205         as described at the top of this file. */\r
206         xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
207                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
208                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
209                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
210                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
211                                                           );    \r
212         \r
213         if( xCheckTimer != NULL )\r
214         {\r
215                 xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
216         }\r
217 \r
218         /* The set of tasks created by the following function call have to be \r
219         created last as they keep account of the number of tasks they expect to see \r
220         running. */\r
221         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
222 \r
223         /* Start the scheduler. */\r
224         vTaskStartScheduler();\r
225         \r
226         /* If all is well, the scheduler will now be running, and the following line\r
227         will never be reached.  If the following line does execute, then there was\r
228         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
229         to be created.  See the memory management section on the FreeRTOS web site\r
230         for more details. */\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 = pdTRUE;\r
247         }\r
248 \r
249         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
250         {\r
251                 ulErrorFound = pdTRUE;\r
252         }\r
253 \r
254         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
255         {\r
256                 ulErrorFound = pdTRUE;\r
257         }\r
258 \r
259         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
260         {\r
261                 ulErrorFound = pdTRUE;\r
262         }\r
263 \r
264         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
265         {\r
266                 ulErrorFound = pdTRUE;\r
267         }\r
268 \r
269         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
270         {\r
271                 ulErrorFound = pdTRUE;\r
272         }\r
273 \r
274         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
275         {\r
276                 ulErrorFound = pdTRUE;\r
277         }\r
278 \r
279         if( xIsCreateTaskStillRunning() != pdTRUE )\r
280         {\r
281                 ulErrorFound = pdTRUE;\r
282         }\r
283 \r
284         if( xArePollingQueuesStillRunning() != pdTRUE )\r
285         {\r
286                 ulErrorFound = pdTRUE;\r
287         }\r
288 \r
289         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
290         {\r
291                 ulErrorFound = pdTRUE;\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 = pdTRUE;\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 = pdTRUE;\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         mainTOGGLE_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