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