]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4S_Atmel_Studio/src/main_full.c
Add the queue set test to the SAM4S-EK Atmel Studio code.
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4S_Atmel_Studio / src / main_full.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45 \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55 \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license\r
57     and contact details.\r
58 \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell\r
63     the code with commercial support, indemnification, and middleware, under\r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under\r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /******************************************************************************\r
70  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
71  * project, and a more comprehensive test and demo application.  The\r
72  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
73  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
74  * in main.c.  This file implements the comprehensive test and demo version.\r
75  *\r
76  * NOTE 2:  This file only contains the source code that is specific to the\r
77  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
78  * required to configure the hardware, are defined in main.c.\r
79  ******************************************************************************\r
80  *\r
81  * main_full() creates all the demo application tasks and a software timer, then\r
82  * starts the scheduler.  The web documentation provides more details of the\r
83  * standard demo application tasks, which provide no particular functionality,\r
84  * but do provide a good example of how to use the FreeRTOS API.\r
85  *\r
86  * In addition to the standard demo tasks, the following tasks and tests are\r
87  * defined and/or created within this file:\r
88  *\r
89  * "Check" timer - The check software timer period is initially set to three\r
90  * seconds.  The callback function associated with the check software timer\r
91  * checks that all the standard demo tasks are not only still executing, but\r
92  * are executing without reporting any errors.  If the check software timer\r
93  * discovers that a task has either stalled, or reported an error, then it\r
94  * changes its own execution period from the initial three seconds, to just\r
95  * 200ms.  The check software timer callback function also toggles the green\r
96  * LED each time it is called.  This provides a visual indication of the system\r
97  * status:  If the green LED toggles every three seconds, then no issues have\r
98  * been discovered.  If the green LED toggles every 200ms, then an issue has\r
99  * been discovered with at least one task.\r
100  *\r
101  * See the documentation page for this demo on the FreeRTOS.org web site for\r
102  * full information, including hardware setup requirements.\r
103  */\r
104 \r
105 /* Standard includes. */\r
106 #include <stdio.h>\r
107 \r
108 /* Kernel includes. */\r
109 #include "FreeRTOS.h"\r
110 #include "task.h"\r
111 #include "timers.h"\r
112 #include "semphr.h"\r
113 \r
114 /* Standard demo application includes. */\r
115 #include "integer.h"\r
116 #include "PollQ.h"\r
117 #include "semtest.h"\r
118 #include "dynamic.h"\r
119 #include "BlockQ.h"\r
120 #include "blocktim.h"\r
121 #include "countsem.h"\r
122 #include "GenQTest.h"\r
123 #include "recmutex.h"\r
124 #include "death.h"\r
125 #include "flash_timer.h"\r
126 #include "partest.h"\r
127 #include "comtest2.h"\r
128 #include "QueueSet.h"\r
129 \r
130 /* Atmel library includes. */\r
131 #include "asf.h"\r
132 \r
133 /* Priorities for the demo application tasks. */\r
134 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )\r
135 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
136 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
137 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
138 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
139 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
140 \r
141 /* A block time of zero simply means "don't block". */\r
142 #define mainDONT_BLOCK                                          ( 0UL )\r
143 \r
144 /* The period after which the check timer will expire, in ms, provided no errors\r
145 have been reported by any of the standard demo tasks.  ms are converted to the\r
146 equivalent in ticks using the portTICK_RATE_MS constant. */\r
147 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
148 \r
149 /* The period at which the check timer will expire, in ms, if an error has been\r
150 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
151 in ticks using the portTICK_RATE_MS constant. */\r
152 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_RATE_MS )\r
153 \r
154 /* The standard demo flash timers can be used to flash any number of LEDs.  In\r
155 this case, because only three LEDs are available, and one is in use by the\r
156 check timer, only two are used by the flash timers. */\r
157 #define mainNUMBER_OF_FLASH_TIMERS_LEDS         ( 2 )\r
158 \r
159 /* The LED toggled by the check timer.  The first two LEDs are toggle by the\r
160 standard demo flash timers. */\r
161 #define mainCHECK_LED                                           ( 2 )\r
162 \r
163 /* Baud rate used by the comtest tasks. */\r
164 #define mainCOM_TEST_BAUD_RATE                          ( 115200 )\r
165 \r
166 /* The LED used by the comtest tasks. In this case, there are no LEDs available\r
167 for the comtest, so the LED number is deliberately out of range. */\r
168 #define mainCOM_TEST_LED                                        ( 3 )\r
169 \r
170 /*-----------------------------------------------------------*/\r
171 \r
172 /*\r
173  * The check timer callback function, as described at the top of this file.\r
174  */\r
175 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
176 \r
177 /*-----------------------------------------------------------*/\r
178 \r
179 void main_full( void )\r
180 {\r
181 xTimerHandle xCheckTimer = NULL;\r
182 \r
183         /* Start all the other standard demo/test tasks.  The have not particular\r
184         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
185         kernel port. */\r
186         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
187         vStartDynamicPriorityTasks();\r
188         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
189         vCreateBlockTimeTasks();\r
190         vStartCountingSemaphoreTasks();\r
191         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
192         vStartRecursiveMutexTasks();\r
193         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
194         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
195         vStartLEDFlashTimers( mainNUMBER_OF_FLASH_TIMERS_LEDS );\r
196         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
197         vStartQueueSetTasks();\r
198 \r
199         /* Create the software timer that performs the 'check' functionality,\r
200         as described at the top of this file. */\r
201         xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
202                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
203                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
204                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
205                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
206                                                           );\r
207 \r
208         if( xCheckTimer != NULL )\r
209         {\r
210                 xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
211         }\r
212 \r
213         /* The set of tasks created by the following function call have to be\r
214         created last as they keep account of the number of tasks they expect to see\r
215         running. */\r
216         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
217 \r
218         /* Start the scheduler. */\r
219         vTaskStartScheduler();\r
220 \r
221         /* If all is well, the scheduler will now be running, and the following line\r
222         will never be reached.  If the following line does execute, then there was\r
223         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
224         to be created.  See the memory management section on the FreeRTOS web site\r
225         for more details. */\r
226         for( ;; );\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
231 {\r
232 static long lChangedTimerPeriodAlready = pdFALSE;\r
233 unsigned long ulErrorFound = pdFALSE;\r
234 \r
235         /* Check all the demo tasks (other than the flash tasks) to ensure\r
236         they are all still running, and that none have detected an error. */\r
237 \r
238         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
239         {\r
240                 ulErrorFound = pdTRUE;\r
241         }\r
242 \r
243         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
244         {\r
245                 ulErrorFound = pdTRUE;\r
246         }\r
247 \r
248         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
249         {\r
250                 ulErrorFound = pdTRUE;\r
251         }\r
252 \r
253         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
254         {\r
255                 ulErrorFound = pdTRUE;\r
256         }\r
257 \r
258         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
259         {\r
260                 ulErrorFound = pdTRUE;\r
261         }\r
262 \r
263         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
264         {\r
265                 ulErrorFound = pdTRUE;\r
266         }\r
267 \r
268         if( xIsCreateTaskStillRunning() != pdTRUE )\r
269         {\r
270                 ulErrorFound = pdTRUE;\r
271         }\r
272 \r
273         if( xArePollingQueuesStillRunning() != pdTRUE )\r
274         {\r
275                 ulErrorFound = pdTRUE;\r
276         }\r
277 \r
278         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
279         {\r
280                 ulErrorFound = pdTRUE;\r
281         }\r
282 \r
283         if( xAreComTestTasksStillRunning() != pdTRUE )\r
284         {\r
285                 ulErrorFound = pdTRUE;\r
286         }\r
287 \r
288         if( xAreQueueSetTasksStillRunning() != pdPASS )\r
289         {\r
290                 ulErrorFound = pdTRUE;\r
291         }\r
292 \r
293         /* Toggle the check LED to give an indication of the system status.  If\r
294         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
295         everything is ok.  A faster toggle indicates an error. */\r
296         vParTestToggleLED( mainCHECK_LED );\r
297 \r
298         /* Have any errors been latch in ulErrorFound?  If so, shorten the\r
299         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
300         This will result in an increase in the rate at which mainCHECK_LED\r
301         toggles. */\r
302         if( ulErrorFound != pdFALSE )\r
303         {\r
304                 if( lChangedTimerPeriodAlready == pdFALSE )\r
305                 {\r
306                         lChangedTimerPeriodAlready = pdTRUE;\r
307 \r
308                         /* This call to xTimerChangePeriod() uses a zero block time.\r
309                         Functions called from inside of a timer callback function must\r
310                         *never* attempt to block. */\r
311                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
312                 }\r
313         }\r
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 void vApplicationTickHook( void )\r
318 {\r
319         /* This function will be called by each tick interrupt if\r
320         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
321         added here, but the tick hook is called from an interrupt context, so\r
322         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
323         functions can be used (those that end in FromISR()).  In this case the tick\r
324         hook is used as part of the queue set test. */\r
325         vQueueSetWriteToQueueFromISR();\r
326 }\r
327 /*-----------------------------------------------------------*/\r
328 \r