]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0_Infineon_XMC1000_IAR_Keil_GCC/main-full.c
Continue work on XMC1000 demos.
[freertos] / FreeRTOS / Demo / CORTEX_M0_Infineon_XMC1000_IAR_Keil_GCC / main-full.c
1 /*\r
2     FreeRTOS V7.5.2 - 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 a set of standard demo tasks, some application specific\r
78  * tasks, and four timers.  It then starts the scheduler.  The web documentation\r
79  * provides more details of the standard demo application tasks, which provide\r
80  * no particular functionality, but do provide a good example of how to use the\r
81  * FreeRTOS API.\r
82  *\r
83  * In addition to the standard demo tasks, the following tasks and timer are\r
84  * defined and/or created within this file:\r
85  *\r
86  * "Reg test" tasks - These fill the registers with known values, then check\r
87  * that each register maintains its expected value for the lifetime of the\r
88  * task.  Each task uses a different set of values.  The reg test tasks execute\r
89  * with a very low priority, so get preempted very frequently.  A register\r
90  * containing an unexpected value is indicative of an error in the context\r
91  * switching mechanism.\r
92  *\r
93  * "Semaphore take task" - This task does nothing but block on a semaphore that\r
94  * is 'given' from the tick hook function (which is defined in main.c).  It\r
95  * toggles the fourth LED each time it receives the semaphore.  The Semahore is\r
96  * given every 50ms, so LED 4 toggles every 50ms.\r
97  *\r
98  * "Flash timers" - A software timer callback function is defined that does\r
99  * nothing but toggle an LED.  Three software timers are created that each\r
100  * use the same callback function, but each toggles a different LED at a\r
101  * different frequency.  The timers control the first three LEDs.\r
102  *\r
103  * "Check" software timer - The check timer period is initially set to three\r
104  * seconds.  Its callback function checks that all the standard demo tasks, and\r
105  * the register check tasks, are not only still executing, but are executing\r
106  * without reporting any errors.  If the check timer callback discovers that a\r
107  * task has either stalled, or reported an error, then it changes the period of\r
108  * the check timer from the initial three seconds, to just 200ms.  The callback\r
109  * function also toggles the fifth LED each time it is called.  This provides a\r
110  * visual indication of the system status:  If the LED toggles every three\r
111  * seconds then no issues have been discovered.  If the LED toggles every 200ms,\r
112  * then an issue has been discovered with at least one task.\r
113  */\r
114 \r
115 /* Kernel includes. */\r
116 #include "FreeRTOS.h"\r
117 #include "task.h"\r
118 #include "queue.h"\r
119 #include "semphr.h"\r
120 #include "timers.h"\r
121 \r
122 /* Common demo includes. */\r
123 #include "blocktim.h"\r
124 #include "countsem.h"\r
125 #include "recmutex.h"\r
126 #include "ParTest.h"\r
127 #include "dynamic.h"\r
128 #include "QueueOverwrite.h"\r
129 #include "QueueSet.h"\r
130 \r
131 /* The period after which the check timer will expire provided no errors have\r
132 been reported by any of the standard demo tasks.  ms are converted to the\r
133 equivalent in ticks using the portTICK_RATE_MS constant. */\r
134 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
135 \r
136 /* The period at which the check timer will expire if an error has been\r
137 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
138 in ticks using the portTICK_RATE_MS constant. */\r
139 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_RATE_MS )\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 base toggle rate used by the flash timers.  Each toggle rate is a\r
145 multiple of this. */\r
146 #define mainFLASH_TIMER_BASE_RATE                       ( 200UL / portTICK_RATE_MS )\r
147 \r
148 /* The LED toggle by the check timer. */\r
149 #define mainCHECK_LED                                           ( 4 )\r
150 \r
151 /* The LED toggled each time the task implemented by the prvSemaphoreTakeTask()\r
152 function takes the semaphore that is given by the tick hook function. */\r
153 #define mainSEMAPHORE_LED                                       ( 3 )\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 \r
157 /*\r
158  * Register check tasks, as described at the top of this file.  The nature of\r
159  * these files necessitates that they are written in an assembly.\r
160  */\r
161 extern void vRegTest1Task( void *pvParameters );\r
162 extern void vRegTest2Task( void *pvParameters );\r
163 \r
164 /*\r
165  * The hardware only has a single LED.  Simply toggle it.\r
166  */\r
167 extern void vMainToggleLED( void );\r
168 \r
169 /*\r
170  * The check timer callback function, as described at the top of this file.\r
171  */\r
172 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
173 \r
174 /*\r
175  * The flash timer callback function, as described at the top of this file.\r
176  * This callback function is assigned to three separate software timers.\r
177  */\r
178 static void prvFlashTimerCallback( xTimerHandle xTimer );\r
179 \r
180 /*\r
181  * The task that toggles an LED each time the semaphore 'given' by the tick\r
182  * hook function (which is defined in main.c) is 'taken' in the task.\r
183  */\r
184 static void prvSemaphoreTakeTask( void *pvParameters );\r
185 \r
186 /*\r
187  * Called by main() to create the comprehensive test/demo application if\r
188  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.\r
189  */\r
190 void main_full( void );\r
191 \r
192 /*-----------------------------------------------------------*/\r
193 \r
194 /* The following two variables are used to communicate the status of the\r
195 register check tasks to the check software timer.  If the variables keep\r
196 incrementing, then the register check tasks has not discovered any errors.  If\r
197 a variable stops incrementing, then an error has been found. */\r
198 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
199 \r
200 /* The semaphore that is given by the tick hook function (defined in main.c)\r
201 and taken by the task implemented by the prvSemaphoreTakeTask() function.  The\r
202 task toggles LED mainSEMAPHORE_LED each time the semaphore is taken. */\r
203 xSemaphoreHandle xLEDSemaphore = NULL;\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 void main_full( void )\r
207 {\r
208 xTimerHandle xTimer = NULL;\r
209 unsigned long ulTimer;\r
210 const unsigned long ulTimersToCreate = 3L;\r
211 /* The register test tasks are asm functions that don't use a stack.  The\r
212 stack allocated just has to be large enough to hold the task context, and\r
213 for the additional required for the stack overflow checking to work (if\r
214 configured). */\r
215 const size_t xRegTestStackSize = 25U;\r
216 \r
217         /* Create the standard demo tasks */\r
218         vCreateBlockTimeTasks();\r
219         vStartCountingSemaphoreTasks();\r
220         vStartRecursiveMutexTasks();\r
221         vStartDynamicPriorityTasks();\r
222         vStartQueueSetTasks();\r
223         vStartQueueOverwriteTask( tskIDLE_PRIORITY );\r
224 \r
225         /* Create that is given from the tick hook function, and the task that\r
226         toggles an LED each time the semaphore is given. */\r
227         vSemaphoreCreateBinary( xLEDSemaphore );\r
228         xTaskCreate(    prvSemaphoreTakeTask,           /* Function that implements the task. */\r
229                                         ( signed char * ) "Sem",        /* Text name of the task. */\r
230                                         configMINIMAL_STACK_SIZE,       /* Stack allocated to the task (in words). */\r
231                                         NULL,                                           /* The task parameter is not used. */\r
232                                         configMAX_PRIORITIES - 1,       /* The priority of the task. */\r
233                                         NULL );                                         /* Don't receive a handle back, it is not needed. */\r
234 \r
235         /* Create the register test tasks as described at the top of this file.\r
236         These are naked functions that don't use any stack.  A stack still has\r
237         to be allocated to hold the task context. */\r
238         xTaskCreate(    vRegTest1Task,                  /* Function that implements the task. */\r
239                                         ( signed char * ) "Reg1", /* Text name of the task. */\r
240                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
241                                         NULL,                                   /* The task parameter is not used. */\r
242                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
243                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
244 \r
245         xTaskCreate(    vRegTest2Task,                  /* Function that implements the task. */\r
246                                         ( signed char * ) "Reg2", /* Text name of the task. */\r
247                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
248                                         NULL,                                   /* The task parameter is not used. */\r
249                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
250                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
251 \r
252         /* Create the three flash timers. */\r
253         for( ulTimer = 0UL; ulTimer < ulTimersToCreate; ulTimer++ )\r
254         {\r
255                 xTimer = xTimerCreate(  ( const signed char * ) "FlashTimer",   /* A text name, purely to help debugging. */\r
256                                                                 ( mainFLASH_TIMER_BASE_RATE * ( ulTimer + 1UL ) ),      /* The timer period, in this case 3000ms (3s). */\r
257                                                                 pdTRUE,                                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
258                                                                 ( void * ) ulTimer,                                             /* The ID is used to hold the number of the LED that will be flashed. */\r
259                                                                 prvFlashTimerCallback                                   /* The callback function that inspects the status of all the other tasks. */\r
260                                                         );\r
261 \r
262                 if( xTimer != NULL )\r
263                 {\r
264                         xTimerStart( xTimer, mainDONT_BLOCK );\r
265                 }\r
266         }\r
267 \r
268         /* Create the software timer that performs the 'check' functionality,\r
269         as described at the top of this file. */\r
270         xTimer = xTimerCreate(  ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
271                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
272                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
273                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
274                                                         prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
275                                                 );\r
276 \r
277         /* If the software timer was created successfully, start it.  It won't\r
278         actually start running until the scheduler starts.  A block time of\r
279         zero is used in this call, although any value could be used as the block\r
280         time will be ignored because the scheduler has not started yet. */\r
281         if( xTimer != NULL )\r
282         {\r
283                 xTimerStart( xTimer, mainDONT_BLOCK );\r
284         }\r
285 \r
286         /* Start the kernel.  From here on, only tasks and interrupts will run. */\r
287         vTaskStartScheduler();\r
288 \r
289         /* If all is well, the scheduler will now be running, and the following\r
290         line will never be reached.  If the following line does execute, then there\r
291         was     insufficient FreeRTOS heap memory available for the idle and/or timer\r
292         tasks to be created.  See the memory management section on the FreeRTOS web\r
293         site, or the FreeRTOS tutorial books for more details. */\r
294         for( ;; );\r
295 }\r
296 /*-----------------------------------------------------------*/\r
297 \r
298 /* See the description at the top of this file. */\r
299 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
300 {\r
301 static long lChangedTimerPeriodAlready = pdFALSE;\r
302 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
303 unsigned long ulErrorFound = pdFALSE;\r
304 \r
305         /* Check all the demo and test tasks to ensure that they are all still\r
306         running, and that none have detected an error. */\r
307         if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
308         {\r
309                 ulErrorFound |= ( 0x01UL << 0UL );\r
310         }\r
311 \r
312         if( xAreBlockTimeTestTasksStillRunning() != pdPASS )\r
313         {\r
314                 ulErrorFound |= ( 0x01UL << 1UL );\r
315         }\r
316 \r
317         if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )\r
318         {\r
319                 ulErrorFound |= ( 0x01UL << 2UL );\r
320         }\r
321 \r
322         if( xAreRecursiveMutexTasksStillRunning() != pdPASS )\r
323         {\r
324                 ulErrorFound |= ( 0x01UL << 3UL );\r
325         }\r
326 \r
327         /* Check that the register test 1 task is still running. */\r
328         if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
329         {\r
330                 ulErrorFound |= ( 0x01UL << 4UL );\r
331         }\r
332         ulLastRegTest1Value = ulRegTest1LoopCounter;\r
333 \r
334         /* Check that the register test 2 task is still running. */\r
335         if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
336         {\r
337                 ulErrorFound |= ( 0x01UL << 5UL );\r
338         }\r
339         ulLastRegTest2Value = ulRegTest2LoopCounter;\r
340 \r
341         if( xAreQueueSetTasksStillRunning() != pdPASS )\r
342         {\r
343                 ulErrorFound |= ( 0x01UL << 6UL );\r
344         }\r
345 \r
346         if( xIsQueueOverwriteTaskStillRunning() != pdPASS )\r
347         {\r
348                 ulErrorFound |= ( 0x01UL << 7UL );\r
349         }\r
350 \r
351         /* Toggle the check LED to give an indication of the system status.  If\r
352         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
353         everything is ok.  A faster toggle indicates an error. */\r
354         vParTestToggleLED( mainCHECK_LED );\r
355 \r
356         /* Have any errors been latched in ulErrorFound?  If so, shorten the\r
357         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
358         This will result in an increase in the rate at which mainCHECK_LED\r
359         toggles. */\r
360         if( ulErrorFound != pdFALSE )\r
361         {\r
362                 if( lChangedTimerPeriodAlready == pdFALSE )\r
363                 {\r
364                         lChangedTimerPeriodAlready = pdTRUE;\r
365 \r
366                         /* This call to xTimerChangePeriod() uses a zero block time.\r
367                         Functions called from inside of a timer callback function must\r
368                         *never* attempt to block. */\r
369                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
370                 }\r
371         }\r
372 }\r
373 /*-----------------------------------------------------------*/\r
374 \r
375 static void prvSemaphoreTakeTask( void *pvParameters )\r
376 {\r
377         configASSERT( xLEDSemaphore );\r
378 \r
379         for( ;; )\r
380         {\r
381                 /* Wait to obtain the semaphore - which is given by the tick hook\r
382                 function every 50ms. */\r
383                 xSemaphoreTake( xLEDSemaphore, portMAX_DELAY );\r
384                 vParTestToggleLED( mainSEMAPHORE_LED );\r
385         }\r
386 }\r
387 /*-----------------------------------------------------------*/\r
388 \r
389 static void prvFlashTimerCallback( xTimerHandle xTimer )\r
390 {\r
391 unsigned long ulLED;\r
392 \r
393         /* This callback function is assigned to three separate software timers.\r
394         Each timer toggles a different LED.  Obtain the number of the LED that\r
395         this timer is toggling. */\r
396         ulLED = ( unsigned long ) pvTimerGetTimerID( xTimer );\r
397 \r
398         /* Toggle the LED. */\r
399         vParTestToggleLED( ulLED );\r
400 }\r
401 \r