]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4S_Atmel_Studio/src/main_full.c
d65e11c67e17a11ce7111301500131d539a7f820
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4S_Atmel_Studio / src / main_full.c
1 /*\r
2     FreeRTOS V7.5.1 - 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 all the demo application tasks and a software timer, then\r
78  * starts the scheduler.  The web documentation provides more details of the\r
79  * standard demo application tasks, which provide no particular functionality,\r
80  * but do provide a good example of how to use the FreeRTOS API.\r
81  *\r
82  * In addition to the standard demo tasks, the following tasks and tests are\r
83  * defined and/or created within this file:\r
84  *\r
85  * "Check" timer - The check software timer period is initially set to three\r
86  * seconds.  The callback function associated with the check software timer\r
87  * checks that all the standard demo tasks are not only still executing, but\r
88  * are executing without reporting any errors.  If the check software timer\r
89  * discovers that a task has either stalled, or reported an error, then it\r
90  * changes its own execution period from the initial three seconds, to just\r
91  * 200ms.  The check software timer callback function also toggles the green\r
92  * LED each time it is called.  This provides a visual indication of the system\r
93  * status:  If the green LED toggles every three seconds, then no issues have\r
94  * been discovered.  If the green LED toggles every 200ms, then an issue has\r
95  * been discovered with at least one task.\r
96  *\r
97  * See the documentation page for this demo on the FreeRTOS.org web site for\r
98  * full information, including hardware setup requirements.\r
99  */\r
100 \r
101 /* Standard includes. */\r
102 #include <stdio.h>\r
103 \r
104 /* Kernel includes. */\r
105 #include "FreeRTOS.h"\r
106 #include "task.h"\r
107 #include "timers.h"\r
108 #include "semphr.h"\r
109 \r
110 /* Standard demo application includes. */\r
111 #include "integer.h"\r
112 #include "PollQ.h"\r
113 #include "semtest.h"\r
114 #include "dynamic.h"\r
115 #include "BlockQ.h"\r
116 #include "blocktim.h"\r
117 #include "countsem.h"\r
118 #include "GenQTest.h"\r
119 #include "recmutex.h"\r
120 #include "death.h"\r
121 #include "flash_timer.h"\r
122 #include "partest.h"\r
123 #include "comtest2.h"\r
124 #include "QueueSet.h"\r
125 \r
126 /* Atmel library includes. */\r
127 #include "asf.h"\r
128 \r
129 /* Priorities for the demo application tasks. */\r
130 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )\r
131 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
132 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
133 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
134 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
135 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
136 \r
137 /* A block time of zero simply means "don't block". */\r
138 #define mainDONT_BLOCK                                          ( 0UL )\r
139 \r
140 /* The period after which the check timer will expire, in ms, provided no errors\r
141 have been reported by any of the standard demo tasks.  ms are converted to the\r
142 equivalent in ticks using the portTICK_RATE_MS constant. */\r
143 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
144 \r
145 /* The period at which the check timer will expire, in ms, if an error has been\r
146 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
147 in ticks using the portTICK_RATE_MS constant. */\r
148 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_RATE_MS )\r
149 \r
150 /* The standard demo flash timers can be used to flash any number of LEDs.  In\r
151 this case, because only three LEDs are available, and one is in use by the\r
152 check timer, only two are used by the flash timers. */\r
153 #define mainNUMBER_OF_FLASH_TIMERS_LEDS         ( 2 )\r
154 \r
155 /* The LED toggled by the check timer.  The first two LEDs are toggle by the\r
156 standard demo flash timers. */\r
157 #define mainCHECK_LED                                           ( 2 )\r
158 \r
159 /* Baud rate used by the comtest tasks. */\r
160 #define mainCOM_TEST_BAUD_RATE                          ( 115200 )\r
161 \r
162 /* The LED used by the comtest tasks. In this case, there are no LEDs available\r
163 for the comtest, so the LED number is deliberately out of range. */\r
164 #define mainCOM_TEST_LED                                        ( 3 )\r
165 \r
166 /*-----------------------------------------------------------*/\r
167 \r
168 /*\r
169  * The check timer callback function, as described at the top of this file.\r
170  */\r
171 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
172 \r
173 /*-----------------------------------------------------------*/\r
174 \r
175 void main_full( void )\r
176 {\r
177 xTimerHandle xCheckTimer = NULL;\r
178 \r
179         /* Start all the other standard demo/test tasks.  The have not particular\r
180         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
181         kernel port. */\r
182         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
183         vStartDynamicPriorityTasks();\r
184         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
185         vCreateBlockTimeTasks();\r
186         vStartCountingSemaphoreTasks();\r
187         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
188         vStartRecursiveMutexTasks();\r
189         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
190         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
191         vStartLEDFlashTimers( mainNUMBER_OF_FLASH_TIMERS_LEDS );\r
192         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
193         vStartQueueSetTasks();\r
194 \r
195         /* Create the software timer that performs the 'check' functionality,\r
196         as described at the top of this file. */\r
197         xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
198                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
199                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
200                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
201                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
202                                                           );\r
203 \r
204         if( xCheckTimer != NULL )\r
205         {\r
206                 xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
207         }\r
208 \r
209         /* The set of tasks created by the following function call have to be\r
210         created last as they keep account of the number of tasks they expect to see\r
211         running. */\r
212         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
213 \r
214         /* Start the scheduler. */\r
215         vTaskStartScheduler();\r
216 \r
217         /* If all is well, the scheduler will now be running, and the following line\r
218         will never be reached.  If the following line does execute, then there was\r
219         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
220         to be created.  See the memory management section on the FreeRTOS web site\r
221         for more details. */\r
222         for( ;; );\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
227 {\r
228 static long lChangedTimerPeriodAlready = pdFALSE;\r
229 unsigned long ulErrorFound = pdFALSE;\r
230 \r
231         /* Check all the demo tasks (other than the flash tasks) to ensure\r
232         they are all still running, and that none have detected an error. */\r
233 \r
234         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
235         {\r
236                 ulErrorFound = pdTRUE;\r
237         }\r
238 \r
239         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
240         {\r
241                 ulErrorFound = pdTRUE;\r
242         }\r
243 \r
244         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
245         {\r
246                 ulErrorFound = pdTRUE;\r
247         }\r
248 \r
249         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
250         {\r
251                 ulErrorFound = pdTRUE;\r
252         }\r
253 \r
254         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
255         {\r
256                 ulErrorFound = pdTRUE;\r
257         }\r
258 \r
259         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
260         {\r
261                 ulErrorFound = pdTRUE;\r
262         }\r
263 \r
264         if( xIsCreateTaskStillRunning() != pdTRUE )\r
265         {\r
266                 ulErrorFound = pdTRUE;\r
267         }\r
268 \r
269         if( xArePollingQueuesStillRunning() != pdTRUE )\r
270         {\r
271                 ulErrorFound = pdTRUE;\r
272         }\r
273 \r
274         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
275         {\r
276                 ulErrorFound = pdTRUE;\r
277         }\r
278 \r
279         if( xAreComTestTasksStillRunning() != pdTRUE )\r
280         {\r
281                 ulErrorFound = pdTRUE;\r
282         }\r
283 \r
284         if( xAreQueueSetTasksStillRunning() != pdPASS )\r
285         {\r
286                 ulErrorFound = pdTRUE;\r
287         }\r
288 \r
289         /* Toggle the check LED to give an indication of the system status.  If\r
290         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
291         everything is ok.  A faster toggle indicates an error. */\r
292         vParTestToggleLED( mainCHECK_LED );\r
293 \r
294         /* Have any errors been latch in ulErrorFound?  If so, shorten the\r
295         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
296         This will result in an increase in the rate at which mainCHECK_LED\r
297         toggles. */\r
298         if( ulErrorFound != pdFALSE )\r
299         {\r
300                 if( lChangedTimerPeriodAlready == pdFALSE )\r
301                 {\r
302                         lChangedTimerPeriodAlready = pdTRUE;\r
303 \r
304                         /* This call to xTimerChangePeriod() uses a zero block time.\r
305                         Functions called from inside of a timer callback function must\r
306                         *never* attempt to block. */\r
307                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
308                 }\r
309         }\r
310 }\r
311 /*-----------------------------------------------------------*/\r
312 \r