]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0_Infineon_XMC1000_IAR_Keil_GCC/main-full.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / CORTEX_M0_Infineon_XMC1000_IAR_Keil_GCC / main-full.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /******************************************************************************\r
29  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
30  * project, and a more comprehensive test and demo application.  The\r
31  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
32  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
33  * in main.c.  This file implements the comprehensive test and demo version.\r
34  *\r
35  * NOTE 2:  This file only contains the source code that is specific to the\r
36  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
37  * required to configure the hardware, are defined in main.c.\r
38  ******************************************************************************\r
39  *\r
40  * main_full() creates a set of standard demo tasks, some application specific\r
41  * tasks, and four timers.  It then starts the scheduler.  The web documentation\r
42  * provides more details of the standard demo application tasks, which provide\r
43  * no particular functionality, but do provide a good example of how to use the\r
44  * FreeRTOS API.\r
45  *\r
46  * In addition to the standard demo tasks, the following tasks and timer are\r
47  * defined and/or created within this file:\r
48  *\r
49  * "Reg test" tasks - These fill the registers with known values, then check\r
50  * that each register maintains its expected value for the lifetime of the\r
51  * task.  Each task uses a different set of values.  The reg test tasks execute\r
52  * with a very low priority, so get preempted very frequently.  A register\r
53  * containing an unexpected value is indicative of an error in the context\r
54  * switching mechanism.\r
55  *\r
56  * "Interrupt semaphore take" task - This task does nothing but block on a\r
57  * semaphore that is 'given' from the tick hook function (which is defined in\r
58  * main.c).  It toggles the fourth LED each time it receives the semaphore.  The\r
59  * Semahore is given every 50ms, so LED 4 toggles every 50ms.\r
60  *\r
61  * "Flash timers" - A software timer callback function is defined that does\r
62  * nothing but toggle an LED.  Three software timers are created that each\r
63  * use the same callback function, but each toggles a different LED at a\r
64  * different frequency.  The timers control the first three LEDs.\r
65  *\r
66  * "Check" software timer - The check timer period is initially set to three\r
67  * seconds.  Its callback function checks that all the standard demo tasks, and\r
68  * the register check tasks, are not only still executing, but are executing\r
69  * without reporting any errors.  If the check timer callback discovers that a\r
70  * task has either stalled, or reported an error, then it changes the period of\r
71  * the check timer from the initial three seconds, to just 200ms.  The callback\r
72  * function also toggles the fifth LED each time it is called.  This provides a\r
73  * visual indication of the system status:  If the LED toggles every three\r
74  * seconds then no issues have been discovered.  If the LED toggles every 200ms,\r
75  * then an issue has been discovered with at least one task.\r
76  */\r
77 \r
78 /* Kernel includes. */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 #include "queue.h"\r
82 #include "semphr.h"\r
83 #include "timers.h"\r
84 \r
85 /* Common demo includes. */\r
86 #include "blocktim.h"\r
87 #include "countsem.h"\r
88 #include "recmutex.h"\r
89 #include "ParTest.h"\r
90 #include "dynamic.h"\r
91 #include "QueueOverwrite.h"\r
92 #include "QueueSet.h"\r
93 \r
94 /* The period after which the check timer will expire provided no errors have\r
95 been reported by any of the standard demo tasks.  ms are converted to the\r
96 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
97 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
98 \r
99 /* The period at which the check timer will expire if an error has been\r
100 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
101 in ticks using the portTICK_PERIOD_MS constant. */\r
102 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
103 \r
104 /* A block time of zero simply means "don't block". */\r
105 #define mainDONT_BLOCK                                          ( 0UL )\r
106 \r
107 /* The base toggle rate used by the flash timers.  Each toggle rate is a\r
108 multiple of this. */\r
109 #define mainFLASH_TIMER_BASE_RATE                       ( 200UL / portTICK_PERIOD_MS )\r
110 \r
111 /* The LED toggle by the check timer. */\r
112 #define mainCHECK_LED                                           ( 4 )\r
113 \r
114 /* The LED toggled each time the task implemented by the prvSemaphoreTakeTask()\r
115 function takes the semaphore that is given by the tick hook function. */\r
116 #define mainSEMAPHORE_LED                                       ( 3 )\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 /*\r
121  * Register check tasks, as described at the top of this file.  The nature of\r
122  * these files necessitates that they are written in an assembly.\r
123  */\r
124 extern void vRegTest1Task( void *pvParameters );\r
125 extern void vRegTest2Task( void *pvParameters );\r
126 \r
127 /*\r
128  * The hardware only has a single LED.  Simply toggle it.\r
129  */\r
130 extern void vMainToggleLED( void );\r
131 \r
132 /*\r
133  * The check timer callback function, as described at the top of this file.\r
134  */\r
135 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
136 \r
137 /*\r
138  * The flash timer callback function, as described at the top of this file.\r
139  * This callback function is assigned to three separate software timers.\r
140  */\r
141 static void prvFlashTimerCallback( TimerHandle_t xTimer );\r
142 \r
143 /*\r
144  * The task that toggles an LED each time the semaphore 'given' by the tick\r
145  * hook function (which is defined in main.c) is 'taken' in the task.\r
146  */\r
147 static void prvSemaphoreTakeTask( void *pvParameters );\r
148 \r
149 /*\r
150  * Called by main() to create the comprehensive test/demo application if\r
151  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.\r
152  */\r
153 void main_full( void );\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 \r
157 /* The following two variables are used to communicate the status of the\r
158 register check tasks to the check software timer.  If the variables keep\r
159 incrementing, then the register check tasks have not discovered any errors.  If\r
160 a variable stops incrementing, then an error has been found. */\r
161 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
162 \r
163 /* The semaphore that is given by the tick hook function (defined in main.c)\r
164 and taken by the task implemented by the prvSemaphoreTakeTask() function.  The\r
165 task toggles LED mainSEMAPHORE_LED each time the semaphore is taken. */\r
166 SemaphoreHandle_t xLEDSemaphore = NULL;\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 void main_full( void )\r
170 {\r
171 TimerHandle_t xTimer = NULL;\r
172 unsigned long ulTimer;\r
173 const unsigned long ulTimersToCreate = 3L;\r
174 /* The register test tasks are asm functions that don't use a stack.  The\r
175 stack allocated just has to be large enough to hold the task context, and\r
176 for the additional required for the stack overflow checking to work (if\r
177 configured). */\r
178 const size_t xRegTestStackSize = 25U;\r
179 \r
180         /* Create the standard demo tasks */\r
181         vCreateBlockTimeTasks();\r
182         vStartDynamicPriorityTasks();\r
183         vStartCountingSemaphoreTasks();\r
184         vStartRecursiveMutexTasks();\r
185         vStartQueueOverwriteTask( tskIDLE_PRIORITY );\r
186         vStartQueueSetTasks();\r
187 \r
188         /* Create that is given from the tick hook function, and the task that\r
189         toggles an LED each time the semaphore is given. */\r
190         vSemaphoreCreateBinary( xLEDSemaphore );\r
191         xTaskCreate(    prvSemaphoreTakeTask,           /* Function that implements the task. */\r
192                                         "Sem",                                          /* Text name of the task. */\r
193                                         configMINIMAL_STACK_SIZE,       /* Stack allocated to the task (in words). */\r
194                                         NULL,                                           /* The task parameter is not used. */\r
195                                         configMAX_PRIORITIES - 2,       /* The priority of the task. */\r
196                                         NULL );                                         /* Don't receive a handle back, it is not needed. */\r
197 \r
198         /* Create the register test tasks as described at the top of this file.\r
199         These are naked functions that don't use any stack.  A stack still has\r
200         to be allocated to hold the task context. */\r
201         xTaskCreate(    vRegTest1Task,                  /* Function that implements the task. */\r
202                                         "Reg1",                                 /* Text name of the task. */\r
203                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
204                                         NULL,                                   /* The task parameter is not used. */\r
205                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
206                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
207 \r
208         xTaskCreate(    vRegTest2Task,                  /* Function that implements the task. */\r
209                                         "Reg2",                                 /* Text name of the task. */\r
210                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
211                                         NULL,                                   /* The task parameter is not used. */\r
212                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
213                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
214 \r
215         /* Create the three flash timers. */\r
216         for( ulTimer = 0UL; ulTimer < ulTimersToCreate; ulTimer++ )\r
217         {\r
218                 xTimer = xTimerCreate(  "FlashTimer",                                                   /* A text name, purely to help debugging. */\r
219                                                                 ( mainFLASH_TIMER_BASE_RATE * ( ulTimer + 1UL ) ),      /* The timer period, in this case 3000ms (3s). */\r
220                                                                 pdTRUE,                                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
221                                                                 ( void * ) ulTimer,                                             /* The ID is used to hold the number of the LED that will be flashed. */\r
222                                                                 prvFlashTimerCallback                                   /* The callback function that inspects the status of all the other tasks. */\r
223                                                         );\r
224 \r
225                 if( xTimer != NULL )\r
226                 {\r
227                         xTimerStart( xTimer, mainDONT_BLOCK );\r
228                 }\r
229         }\r
230 \r
231         /* Create the software timer that performs the 'check' functionality,\r
232         as described at the top of this file. */\r
233         xTimer = xTimerCreate(  "CheckTimer",                                   /* A text name, purely to help debugging. */\r
234                                                         ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */\r
235                                                         pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
236                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
237                                                         prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */\r
238                                                 );\r
239 \r
240         /* If the software timer was created successfully, start it.  It won't\r
241         actually start running until the scheduler starts.  A block time of\r
242         zero is used in this call, although any value could be used as the block\r
243         time will be ignored because the scheduler has not started yet. */\r
244         if( xTimer != NULL )\r
245         {\r
246                 xTimerStart( xTimer, mainDONT_BLOCK );\r
247         }\r
248 \r
249         /* Start the kernel.  From here on, only tasks and interrupts will run. */\r
250         vTaskStartScheduler();\r
251 \r
252         /* If all is well, the scheduler will now be running, and the following\r
253         line will never be reached.  If the following line does execute, then there\r
254         was     insufficient FreeRTOS heap memory available for the idle and/or timer\r
255         tasks to be created.  See the memory management section on the FreeRTOS web\r
256         site, or the FreeRTOS tutorial books for more details. */\r
257         for( ;; );\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 /* See the description at the top of this file. */\r
262 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
263 {\r
264 static long lChangedTimerPeriodAlready = pdFALSE;\r
265 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
266 unsigned long ulErrorFound = pdFALSE;\r
267 \r
268         /* Check all the demo and test tasks to ensure that they are all still\r
269         running, and that none have detected an error. */\r
270         if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
271         {\r
272                 ulErrorFound |= ( 0x01UL << 0UL );\r
273         }\r
274 \r
275         if( xAreBlockTimeTestTasksStillRunning() != pdPASS )\r
276         {\r
277                 ulErrorFound |= ( 0x01UL << 1UL );\r
278         }\r
279 \r
280         if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )\r
281         {\r
282                 ulErrorFound |= ( 0x01UL << 2UL );\r
283         }\r
284 \r
285         if( xAreRecursiveMutexTasksStillRunning() != pdPASS )\r
286         {\r
287                 ulErrorFound |= ( 0x01UL << 3UL );\r
288         }\r
289 \r
290         /* Check that the register test 1 task is still running. */\r
291         if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
292         {\r
293                 ulErrorFound |= ( 0x01UL << 4UL );\r
294         }\r
295         ulLastRegTest1Value = ulRegTest1LoopCounter;\r
296 \r
297         /* Check that the register test 2 task is still running. */\r
298         if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
299         {\r
300                 ulErrorFound |= ( 0x01UL << 5UL );\r
301         }\r
302         ulLastRegTest2Value = ulRegTest2LoopCounter;\r
303 \r
304         if( xAreQueueSetTasksStillRunning() != pdPASS )\r
305         {\r
306                 ulErrorFound |= ( 0x01UL << 6UL );\r
307         }\r
308 \r
309         if( xIsQueueOverwriteTaskStillRunning() != pdPASS )\r
310         {\r
311                 ulErrorFound |= ( 0x01UL << 7UL );\r
312         }\r
313 \r
314         /* Toggle the check LED to give an indication of the system status.  If\r
315         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
316         everything is ok.  A faster toggle indicates an error. */\r
317         vParTestToggleLED( mainCHECK_LED );\r
318 \r
319         /* Have any errors been latched in ulErrorFound?  If so, shorten the\r
320         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
321         This will result in an increase in the rate at which mainCHECK_LED\r
322         toggles. */\r
323         if( ulErrorFound != pdFALSE )\r
324         {\r
325                 if( lChangedTimerPeriodAlready == pdFALSE )\r
326                 {\r
327                         lChangedTimerPeriodAlready = pdTRUE;\r
328 \r
329                         /* This call to xTimerChangePeriod() uses a zero block time.\r
330                         Functions called from inside of a timer callback function must\r
331                         *never* attempt to block. */\r
332                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
333                 }\r
334         }\r
335 }\r
336 /*-----------------------------------------------------------*/\r
337 \r
338 static void prvSemaphoreTakeTask( void *pvParameters )\r
339 {\r
340         configASSERT( xLEDSemaphore );\r
341 \r
342         for( ;; )\r
343         {\r
344                 /* Wait to obtain the semaphore - which is given by the tick hook\r
345                 function every 50ms. */\r
346                 xSemaphoreTake( xLEDSemaphore, portMAX_DELAY );\r
347                 vParTestToggleLED( mainSEMAPHORE_LED );\r
348         }\r
349 }\r
350 /*-----------------------------------------------------------*/\r
351 \r
352 static void prvFlashTimerCallback( TimerHandle_t xTimer )\r
353 {\r
354 unsigned long ulLED;\r
355 \r
356         /* This callback function is assigned to three separate software timers.\r
357         Each timer toggles a different LED.  Obtain the number of the LED that\r
358         this timer is toggling. */\r
359         ulLED = ( unsigned long ) pvTimerGetTimerID( xTimer );\r
360 \r
361         /* Toggle the LED. */\r
362         vParTestToggleLED( ulLED );\r
363 }\r
364 \r