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