]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0_Infineon_XMC1000_IAR_Keil_GCC/main-full.c
dfbba8e9899c5eb0cf4e8ec548b4d6acb88475f9
[freertos] / FreeRTOS / Demo / CORTEX_M0_Infineon_XMC1000_IAR_Keil_GCC / main-full.c
1 /*\r
2     FreeRTOS V8.1.0 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /******************************************************************************\r
67  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
68  * project, and a more comprehensive test and demo application.  The\r
69  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
70  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
71  * in main.c.  This file implements the comprehensive test and demo version.\r
72  *\r
73  * NOTE 2:  This file only contains the source code that is specific to the\r
74  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
75  * required to configure the hardware, are defined in main.c.\r
76  ******************************************************************************\r
77  *\r
78  * main_full() creates a set of standard demo tasks, some application specific\r
79  * tasks, and four timers.  It then starts the scheduler.  The web documentation\r
80  * provides more details of the standard demo application tasks, which provide\r
81  * no particular functionality, but do provide a good example of how to use the\r
82  * FreeRTOS API.\r
83  *\r
84  * In addition to the standard demo tasks, the following tasks and timer are\r
85  * defined and/or created within this file:\r
86  *\r
87  * "Reg test" tasks - These fill the registers with known values, then check\r
88  * that each register maintains its expected value for the lifetime of the\r
89  * task.  Each task uses a different set of values.  The reg test tasks execute\r
90  * with a very low priority, so get preempted very frequently.  A register\r
91  * containing an unexpected value is indicative of an error in the context\r
92  * switching mechanism.\r
93  *\r
94  * "Interrupt semaphore take" task - This task does nothing but block on a\r
95  * semaphore that is 'given' from the tick hook function (which is defined in\r
96  * main.c).  It toggles the fourth LED each time it receives the semaphore.  The\r
97  * Semahore is given every 50ms, so LED 4 toggles every 50ms.\r
98  *\r
99  * "Flash timers" - A software timer callback function is defined that does\r
100  * nothing but toggle an LED.  Three software timers are created that each\r
101  * use the same callback function, but each toggles a different LED at a\r
102  * different frequency.  The timers control the first three LEDs.\r
103  *\r
104  * "Check" software timer - The check timer period is initially set to three\r
105  * seconds.  Its callback function checks that all the standard demo tasks, and\r
106  * the register check tasks, are not only still executing, but are executing\r
107  * without reporting any errors.  If the check timer callback discovers that a\r
108  * task has either stalled, or reported an error, then it changes the period of\r
109  * the check timer from the initial three seconds, to just 200ms.  The callback\r
110  * function also toggles the fifth LED each time it is called.  This provides a\r
111  * visual indication of the system status:  If the LED toggles every three\r
112  * seconds then no issues have been discovered.  If the LED toggles every 200ms,\r
113  * then an issue has been discovered with at least one task.\r
114  */\r
115 \r
116 /* Kernel includes. */\r
117 #include "FreeRTOS.h"\r
118 #include "task.h"\r
119 #include "queue.h"\r
120 #include "semphr.h"\r
121 #include "timers.h"\r
122 \r
123 /* Common demo includes. */\r
124 #include "blocktim.h"\r
125 #include "countsem.h"\r
126 #include "recmutex.h"\r
127 #include "ParTest.h"\r
128 #include "dynamic.h"\r
129 #include "QueueOverwrite.h"\r
130 #include "QueueSet.h"\r
131 \r
132 /* The period after which the check timer will expire provided no errors have\r
133 been reported by any of the standard demo tasks.  ms are converted to the\r
134 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
135 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
136 \r
137 /* The period at which the check timer will expire if an error has been\r
138 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
139 in ticks using the portTICK_PERIOD_MS constant. */\r
140 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
141 \r
142 /* A block time of zero simply means "don't block". */\r
143 #define mainDONT_BLOCK                                          ( 0UL )\r
144 \r
145 /* The base toggle rate used by the flash timers.  Each toggle rate is a\r
146 multiple of this. */\r
147 #define mainFLASH_TIMER_BASE_RATE                       ( 200UL / portTICK_PERIOD_MS )\r
148 \r
149 /* The LED toggle by the check timer. */\r
150 #define mainCHECK_LED                                           ( 4 )\r
151 \r
152 /* The LED toggled each time the task implemented by the prvSemaphoreTakeTask()\r
153 function takes the semaphore that is given by the tick hook function. */\r
154 #define mainSEMAPHORE_LED                                       ( 3 )\r
155 \r
156 /*-----------------------------------------------------------*/\r
157 \r
158 /*\r
159  * Register check tasks, as described at the top of this file.  The nature of\r
160  * these files necessitates that they are written in an assembly.\r
161  */\r
162 extern void vRegTest1Task( void *pvParameters );\r
163 extern void vRegTest2Task( void *pvParameters );\r
164 \r
165 /*\r
166  * The hardware only has a single LED.  Simply toggle it.\r
167  */\r
168 extern void vMainToggleLED( void );\r
169 \r
170 /*\r
171  * The check timer callback function, as described at the top of this file.\r
172  */\r
173 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
174 \r
175 /*\r
176  * The flash timer callback function, as described at the top of this file.\r
177  * This callback function is assigned to three separate software timers.\r
178  */\r
179 static void prvFlashTimerCallback( TimerHandle_t xTimer );\r
180 \r
181 /*\r
182  * The task that toggles an LED each time the semaphore 'given' by the tick\r
183  * hook function (which is defined in main.c) is 'taken' in the task.\r
184  */\r
185 static void prvSemaphoreTakeTask( void *pvParameters );\r
186 \r
187 /*\r
188  * Called by main() to create the comprehensive test/demo application if\r
189  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.\r
190  */\r
191 void main_full( void );\r
192 \r
193 /*-----------------------------------------------------------*/\r
194 \r
195 /* The following two variables are used to communicate the status of the\r
196 register check tasks to the check software timer.  If the variables keep\r
197 incrementing, then the register check tasks has not discovered any errors.  If\r
198 a variable stops incrementing, then an error has been found. */\r
199 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
200 \r
201 /* The semaphore that is given by the tick hook function (defined in main.c)\r
202 and taken by the task implemented by the prvSemaphoreTakeTask() function.  The\r
203 task toggles LED mainSEMAPHORE_LED each time the semaphore is taken. */\r
204 SemaphoreHandle_t xLEDSemaphore = NULL;\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 void main_full( void )\r
208 {\r
209 TimerHandle_t xTimer = NULL;\r
210 unsigned long ulTimer;\r
211 const unsigned long ulTimersToCreate = 3L;\r
212 /* The register test tasks are asm functions that don't use a stack.  The\r
213 stack allocated just has to be large enough to hold the task context, and\r
214 for the additional required for the stack overflow checking to work (if\r
215 configured). */\r
216 const size_t xRegTestStackSize = 25U;\r
217 \r
218         /* Create the standard demo tasks */\r
219         vCreateBlockTimeTasks();\r
220         vStartDynamicPriorityTasks();\r
221         vStartCountingSemaphoreTasks();\r
222         vStartRecursiveMutexTasks();\r
223         vStartQueueOverwriteTask( tskIDLE_PRIORITY );\r
224         vStartQueueSetTasks();\r
225 \r
226         /* Create that is given from the tick hook function, and the task that\r
227         toggles an LED each time the semaphore is given. */\r
228         vSemaphoreCreateBinary( xLEDSemaphore );\r
229         xTaskCreate(    prvSemaphoreTakeTask,           /* Function that implements the task. */\r
230                                         "Sem",                                          /* Text name of the task. */\r
231                                         configMINIMAL_STACK_SIZE,       /* Stack allocated to the task (in words). */\r
232                                         NULL,                                           /* The task parameter is not used. */\r
233                                         configMAX_PRIORITIES - 2,       /* The priority of the task. */\r
234                                         NULL );                                         /* Don't receive a handle back, it is not needed. */\r
235 \r
236         /* Create the register test tasks as described at the top of this file.\r
237         These are naked functions that don't use any stack.  A stack still has\r
238         to be allocated to hold the task context. */\r
239         xTaskCreate(    vRegTest1Task,                  /* Function that implements the task. */\r
240                                         "Reg1",                                 /* Text name of the task. */\r
241                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
242                                         NULL,                                   /* The task parameter is not used. */\r
243                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
244                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
245 \r
246         xTaskCreate(    vRegTest2Task,                  /* Function that implements the task. */\r
247                                         "Reg2",                                 /* Text name of the task. */\r
248                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
249                                         NULL,                                   /* The task parameter is not used. */\r
250                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
251                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
252 \r
253         /* Create the three flash timers. */\r
254         for( ulTimer = 0UL; ulTimer < ulTimersToCreate; ulTimer++ )\r
255         {\r
256                 xTimer = xTimerCreate(  "FlashTimer",                                                   /* A text name, purely to help debugging. */\r
257                                                                 ( mainFLASH_TIMER_BASE_RATE * ( ulTimer + 1UL ) ),      /* The timer period, in this case 3000ms (3s). */\r
258                                                                 pdTRUE,                                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
259                                                                 ( void * ) ulTimer,                                             /* The ID is used to hold the number of the LED that will be flashed. */\r
260                                                                 prvFlashTimerCallback                                   /* The callback function that inspects the status of all the other tasks. */\r
261                                                         );\r
262 \r
263                 if( xTimer != NULL )\r
264                 {\r
265                         xTimerStart( xTimer, mainDONT_BLOCK );\r
266                 }\r
267         }\r
268 \r
269         /* Create the software timer that performs the 'check' functionality,\r
270         as described at the top of this file. */\r
271         xTimer = xTimerCreate(  "CheckTimer",                                   /* A text name, purely to help debugging. */\r
272                                                         ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */\r
273                                                         pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
274                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
275                                                         prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */\r
276                                                 );\r
277 \r
278         /* If the software timer was created successfully, start it.  It won't\r
279         actually start running until the scheduler starts.  A block time of\r
280         zero is used in this call, although any value could be used as the block\r
281         time will be ignored because the scheduler has not started yet. */\r
282         if( xTimer != NULL )\r
283         {\r
284                 xTimerStart( xTimer, mainDONT_BLOCK );\r
285         }\r
286 \r
287         /* Start the kernel.  From here on, only tasks and interrupts will run. */\r
288         vTaskStartScheduler();\r
289 \r
290         /* If all is well, the scheduler will now be running, and the following\r
291         line will never be reached.  If the following line does execute, then there\r
292         was     insufficient FreeRTOS heap memory available for the idle and/or timer\r
293         tasks to be created.  See the memory management section on the FreeRTOS web\r
294         site, or the FreeRTOS tutorial books for more details. */\r
295         for( ;; );\r
296 }\r
297 /*-----------------------------------------------------------*/\r
298 \r
299 /* See the description at the top of this file. */\r
300 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
301 {\r
302 static long lChangedTimerPeriodAlready = pdFALSE;\r
303 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
304 unsigned long ulErrorFound = pdFALSE;\r
305 \r
306         /* Check all the demo and test tasks to ensure that they are all still\r
307         running, and that none have detected an error. */\r
308         if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
309         {\r
310                 ulErrorFound |= ( 0x01UL << 0UL );\r
311         }\r
312 \r
313         if( xAreBlockTimeTestTasksStillRunning() != pdPASS )\r
314         {\r
315                 ulErrorFound |= ( 0x01UL << 1UL );\r
316         }\r
317 \r
318         if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )\r
319         {\r
320                 ulErrorFound |= ( 0x01UL << 2UL );\r
321         }\r
322 \r
323         if( xAreRecursiveMutexTasksStillRunning() != pdPASS )\r
324         {\r
325                 ulErrorFound |= ( 0x01UL << 3UL );\r
326         }\r
327 \r
328         /* Check that the register test 1 task is still running. */\r
329         if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
330         {\r
331                 ulErrorFound |= ( 0x01UL << 4UL );\r
332         }\r
333         ulLastRegTest1Value = ulRegTest1LoopCounter;\r
334 \r
335         /* Check that the register test 2 task is still running. */\r
336         if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
337         {\r
338                 ulErrorFound |= ( 0x01UL << 5UL );\r
339         }\r
340         ulLastRegTest2Value = ulRegTest2LoopCounter;\r
341 \r
342         if( xAreQueueSetTasksStillRunning() != pdPASS )\r
343         {\r
344                 ulErrorFound |= ( 0x01UL << 6UL );\r
345         }\r
346 \r
347         if( xIsQueueOverwriteTaskStillRunning() != pdPASS )\r
348         {\r
349                 ulErrorFound |= ( 0x01UL << 7UL );\r
350         }\r
351 \r
352         /* Toggle the check LED to give an indication of the system status.  If\r
353         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
354         everything is ok.  A faster toggle indicates an error. */\r
355         vParTestToggleLED( mainCHECK_LED );\r
356 \r
357         /* Have any errors been latched in ulErrorFound?  If so, shorten the\r
358         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
359         This will result in an increase in the rate at which mainCHECK_LED\r
360         toggles. */\r
361         if( ulErrorFound != pdFALSE )\r
362         {\r
363                 if( lChangedTimerPeriodAlready == pdFALSE )\r
364                 {\r
365                         lChangedTimerPeriodAlready = pdTRUE;\r
366 \r
367                         /* This call to xTimerChangePeriod() uses a zero block time.\r
368                         Functions called from inside of a timer callback function must\r
369                         *never* attempt to block. */\r
370                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
371                 }\r
372         }\r
373 }\r
374 /*-----------------------------------------------------------*/\r
375 \r
376 static void prvSemaphoreTakeTask( void *pvParameters )\r
377 {\r
378         configASSERT( xLEDSemaphore );\r
379 \r
380         for( ;; )\r
381         {\r
382                 /* Wait to obtain the semaphore - which is given by the tick hook\r
383                 function every 50ms. */\r
384                 xSemaphoreTake( xLEDSemaphore, portMAX_DELAY );\r
385                 vParTestToggleLED( mainSEMAPHORE_LED );\r
386         }\r
387 }\r
388 /*-----------------------------------------------------------*/\r
389 \r
390 static void prvFlashTimerCallback( TimerHandle_t xTimer )\r
391 {\r
392 unsigned long ulLED;\r
393 \r
394         /* This callback function is assigned to three separate software timers.\r
395         Each timer toggles a different LED.  Obtain the number of the LED that\r
396         this timer is toggling. */\r
397         ulLED = ( unsigned long ) pvTimerGetTimerID( xTimer );\r
398 \r
399         /* Toggle the LED. */\r
400         vParTestToggleLED( ulLED );\r
401 }\r
402 \r