]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4S_Atmel_Studio/src/main_full.c
Kernel changes:
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4S_Atmel_Studio / src / main_full.c
1 /*\r
2     FreeRTOS V8.1.2 - 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 all the demo application tasks and a software timer, then\r
79  * starts the scheduler.  The web documentation provides more details of the\r
80  * standard demo application tasks, which provide no particular functionality,\r
81  * but do provide a good example of how to use the FreeRTOS API.\r
82  *\r
83  * In addition to the standard demo tasks, the following tasks and tests are\r
84  * defined and/or created within this file:\r
85  *\r
86  * "Check" timer - The check software timer period is initially set to three\r
87  * seconds.  The callback function associated with the check software timer\r
88  * checks that all the standard demo tasks are not only still executing, but\r
89  * are executing without reporting any errors.  If the check software timer\r
90  * discovers that a task has either stalled, or reported an error, then it\r
91  * changes its own execution period from the initial three seconds, to just\r
92  * 200ms.  The check software timer callback function also toggles the green\r
93  * LED each time it is called.  This provides a visual indication of the system\r
94  * status:  If the green LED toggles every three seconds, then no issues have\r
95  * been discovered.  If the green LED toggles every 200ms, then an issue has\r
96  * been discovered with at least one task.\r
97  *\r
98  * See the documentation page for this demo on the FreeRTOS.org web site for\r
99  * full information, including hardware setup requirements.\r
100  */\r
101 \r
102 /* Standard includes. */\r
103 #include <stdio.h>\r
104 \r
105 /* Kernel includes. */\r
106 #include "FreeRTOS.h"\r
107 #include "task.h"\r
108 #include "timers.h"\r
109 #include "semphr.h"\r
110 \r
111 /* Standard demo application includes. */\r
112 #include "integer.h"\r
113 #include "PollQ.h"\r
114 #include "semtest.h"\r
115 #include "dynamic.h"\r
116 #include "BlockQ.h"\r
117 #include "blocktim.h"\r
118 #include "countsem.h"\r
119 #include "GenQTest.h"\r
120 #include "recmutex.h"\r
121 #include "death.h"\r
122 #include "flash_timer.h"\r
123 #include "partest.h"\r
124 #include "comtest2.h"\r
125 #include "QueueSet.h"\r
126 #include "IntQueue.h"\r
127 #include "TaskNotify.h"\r
128 #include "TimerDemo.h"\r
129 #include "EventGroupsDemo.h"\r
130 #include "IntSemTest.h"\r
131 \r
132 /* Atmel library includes. */\r
133 #include "asf.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 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
142 \r
143 /* A block time of zero simply means "don't block". */\r
144 #define mainDONT_BLOCK                                          ( 0UL )\r
145 \r
146 /* The period after which the check timer will expire, in ms, provided no errors\r
147 have been reported by any of the standard demo tasks.  ms are converted to the\r
148 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
149 #define mainCHECK_TIMER_PERIOD_MS                       ( pdMS_TO_TICKS( 3000UL ) )\r
150 \r
151 /* The period at which the check timer will expire, in ms, if an error has been\r
152 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
153 in ticks using the portTICK_PERIOD_MS constant. */\r
154 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( pdMS_TO_TICKS( 200UL ) )\r
155 \r
156 /* The standard demo flash timers can be used to flash any number of LEDs.  In\r
157 this case, because only three LEDs are available, and one is in use by the\r
158 check timer, only two are used by the flash timers. */\r
159 #define mainNUMBER_OF_FLASH_TIMERS_LEDS         ( 2 )\r
160 \r
161 /* The LED toggled by the check timer.  The first two LEDs are toggle by the\r
162 standard demo flash timers. */\r
163 #define mainCHECK_LED                                           ( 2 )\r
164 \r
165 /* Baud rate used by the comtest tasks. */\r
166 #define mainCOM_TEST_BAUD_RATE                          ( 115200 )\r
167 \r
168 /* The LED used by the comtest tasks. In this case, there are no LEDs available\r
169 for the comtest, so the LED number is deliberately out of range. */\r
170 #define mainCOM_TEST_LED                                        ( 3 )\r
171 \r
172 /* Used by the standard demo timer tasks. */\r
173 #define mainTIMER_TEST_PERIOD                           ( 50 )\r
174 \r
175 /*-----------------------------------------------------------*/\r
176 \r
177 /*\r
178  * Called by the idle hook function when the project is configured to run the\r
179  * full (as opposed to the blinky) demo.\r
180  */\r
181 void vFullDemoIdleHook( void );\r
182 \r
183 /*\r
184  * Called by the tick hook function when the project is configured to run the\r
185  * full (as opposed to the blinky) demo.\r
186  */\r
187 void vFullDemoTickHook( void );\r
188 \r
189 /*\r
190  * The check timer callback function, as described at the top of this file.\r
191  */\r
192 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
193 \r
194 /*-----------------------------------------------------------*/\r
195 \r
196 void main_full( void )\r
197 {\r
198         /* Start all the other standard demo/test tasks.  The have not particular\r
199         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
200         kernel port. */\r
201         vStartInterruptQueueTasks();\r
202         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
203         vStartDynamicPriorityTasks();\r
204         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
205         vCreateBlockTimeTasks();\r
206         vStartCountingSemaphoreTasks();\r
207         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
208         vStartRecursiveMutexTasks();\r
209         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
210         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
211         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
212         vStartQueueSetTasks();\r
213         vStartTaskNotifyTask();\r
214         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
215         vStartEventGroupTasks();\r
216         vStartInterruptSemaphoreTasks();\r
217 \r
218         /* The set of tasks created by the following function call have to be\r
219         created last as they keep account of the number of tasks they expect to see\r
220         running. */\r
221         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
222 \r
223         /* Start the scheduler. */\r
224         vTaskStartScheduler();\r
225 \r
226         /* If all is well, the scheduler will now be running, and the following line\r
227         will never be reached.  If the following line does execute, then there was\r
228         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
229         to be created.  See the memory management section on the FreeRTOS web site\r
230         for more details. */\r
231         for( ;; );\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
236 {\r
237 static long lChangedTimerPeriodAlready = pdFALSE;\r
238 unsigned long ulErrorFound = pdFALSE;\r
239 \r
240         /* Check all the demo tasks (other than the flash tasks) to ensure\r
241         they are all still running, and that none have detected an error. */\r
242 \r
243         if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
244         {\r
245                 ulErrorFound |= 1UL << 0UL;\r
246         }\r
247 \r
248         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
249         {\r
250                 ulErrorFound |= 1UL << 1UL;\r
251         }\r
252 \r
253         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
254         {\r
255                 ulErrorFound |= 1UL << 2UL;\r
256         }\r
257 \r
258         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
259         {\r
260                 ulErrorFound |= 1UL << 3UL;\r
261         }\r
262 \r
263         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
264         {\r
265                 ulErrorFound |= 1UL << 4UL;\r
266         }\r
267 \r
268         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
269         {\r
270                 ulErrorFound |= 1UL << 5UL;\r
271         }\r
272 \r
273         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
274         {\r
275                 ulErrorFound |= 1UL << 6UL;\r
276         }\r
277 \r
278         if( xIsCreateTaskStillRunning() != pdTRUE )\r
279         {\r
280                 ulErrorFound |= 1UL << 7UL;\r
281         }\r
282 \r
283         if( xArePollingQueuesStillRunning() != pdTRUE )\r
284         {\r
285                 ulErrorFound |= 1UL << 8UL;\r
286         }\r
287 \r
288         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
289         {\r
290                 ulErrorFound |= 1UL << 9UL;\r
291         }\r
292 \r
293         if( xAreComTestTasksStillRunning() != pdTRUE )\r
294         {\r
295                 ulErrorFound |= 1UL << 10UL;\r
296         }\r
297 \r
298         if( xAreQueueSetTasksStillRunning() != pdTRUE )\r
299         {\r
300                 ulErrorFound |= 1UL << 11UL;\r
301         }\r
302         \r
303         if( xAreTaskNotificationTasksStillRunning() != pdTRUE )\r
304         {\r
305                 ulErrorFound |= 1UL << 12UL;\r
306         }\r
307         \r
308         if( xAreTimerDemoTasksStillRunning( mainCHECK_TIMER_PERIOD_MS ) != pdTRUE )\r
309         {\r
310                 ulErrorFound |= 1UL << 13UL;\r
311         }\r
312         \r
313         if( xAreEventGroupTasksStillRunning() != pdTRUE )\r
314         {\r
315                 ulErrorFound |= 1UL << 14UL;\r
316         }\r
317         \r
318         if( xAreInterruptSemaphoreTasksStillRunning() != pdTRUE )\r
319         {\r
320                 ulErrorFound |= 1UL << 15UL;\r
321         }\r
322         \r
323 \r
324         /* Toggle the check LED to give an indication of the system status.  If\r
325         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
326         everything is ok.  A faster toggle indicates an error. */\r
327         vParTestToggleLED( mainCHECK_LED );\r
328 \r
329         /* Have any errors been latch in ulErrorFound?  If so, shorten the\r
330         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
331         This will result in an increase in the rate at which mainCHECK_LED\r
332         toggles. */\r
333         if( ulErrorFound != pdFALSE )\r
334         {\r
335                 if( lChangedTimerPeriodAlready == pdFALSE )\r
336                 {\r
337                         lChangedTimerPeriodAlready = pdTRUE;\r
338 \r
339                         /* This call to xTimerChangePeriod() uses a zero block time.\r
340                         Functions called from inside of a timer callback function must\r
341                         *never* attempt to block. */\r
342                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
343                 }\r
344         }\r
345 }\r
346 /*-----------------------------------------------------------*/\r
347 \r
348 void vFullDemoIdleHook( void )\r
349 {\r
350 static TimerHandle_t xCheckTimer = NULL;\r
351                 \r
352         if( xCheckTimer == NULL )\r
353         {\r
354                 /* Create the software timer that performs the 'check' \r
355                 functionality, in the full demo.  This is not done before the\r
356                 scheduler is started as to do so would prevent the standard demo\r
357                 timer tasks from passing their tests (they expect the timer\r
358                 command queue to be empty. */\r
359                 xCheckTimer = xTimerCreate( "CheckTimer",                               /* A text name, purely to help debugging. */\r
360                                                                         mainCHECK_TIMER_PERIOD_MS,      /* The timer period, in this case 3000ms (3s). */\r
361                                                                         pdTRUE,                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
362                                                                         ( void * ) 0,                           /* The ID is not used, so can be set to anything. */\r
363                                                                         prvCheckTimerCallback           /* The callback function that inspects the status of all the other tasks. */\r
364                                                                         );\r
365 \r
366                 if( xCheckTimer != NULL )\r
367                 {\r
368                         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
369                 }\r
370                 \r
371                 /* Also start some timers that just flash LEDs. */\r
372                 vStartLEDFlashTimers( mainNUMBER_OF_FLASH_TIMERS_LEDS );\r
373         }\r
374 }\r
375 /*-----------------------------------------------------------*/\r
376 \r
377 void vFullDemoTickHook( void )\r
378 {\r
379         /* In this case the tick hook is used as part of the queue set test. */\r
380         vQueueSetAccessQueueSetFromISR();\r
381                 \r
382         /* Use task notifications from an interrupt. */\r
383         xNotifyTaskFromISR();\r
384                 \r
385         /* Use timers from an interrupt. */\r
386         vTimerPeriodicISRTests();\r
387         \r
388         /* Use event groups from an interrupt. */\r
389         vPeriodicEventGroupsProcessing();\r
390         \r
391         /* Use mutexes from interrupts. */\r
392         vInterruptSemaphorePeriodicTest();\r
393 }\r