]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_Atmel_SAMD20_XPlained/RTOSDemo/src/main-full.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / CORTEX_M0+_Atmel_SAMD20_XPlained / RTOSDemo / src / 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 a timer.  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  * "Command console task" - This uses the Atmel USART driver to provide the\r
118  * input and output to FreeRTOS+CLI - the FreeRTOS Command Line Interface.  To\r
119  * use the CLI:\r
120  *  - Power the SAMD20 XPlained board through the USB debugger connector.  This\r
121  *    will create a virtual COM port through the USB.\r
122  *  - Build and run the demo application.  \r
123  *  - Start a dumb terminal program such as TerraTerm or Hyper Terminal.\r
124  *  - In the dumb terminal select the UART port associated with the XPlained\r
125  *    debugger connection, using 19200 baud.\r
126  *  - Type 'help' in the terminal window to see a lit of command registered by\r
127  *    the demo.\r
128  *\r
129  * "Reg test" tasks - These fill the registers with known values, then check\r
130  * that each register maintains its expected value for the lifetime of the\r
131  * task.  Each task uses a different set of values.  The reg test tasks execute\r
132  * with a very low priority, so get preempted very frequently.  A register\r
133  * containing an unexpected value is indicative of an error in the context\r
134  * switching mechanism.\r
135  *\r
136  * "Check" software timer - The check timer period is initially set to three\r
137  * seconds.  Its callback function checks that all the standard demo tasks, and\r
138  * the register check tasks, are not only still executing, but are executing\r
139  * without reporting any errors.  If the check timer callback discovers that a\r
140  * task has either stalled, or reported an error, then it changes the period of\r
141  * the check timer from the initial three seconds, to just 200ms.  The callback\r
142  * function also toggles the LED each time it is called.  This provides a\r
143  * visual indication of the system status:  If the LED toggles every three\r
144  * seconds then no issues have been discovered.  If the LED toggles every 200ms,\r
145  * then an issue has been discovered with at least one task.\r
146  */\r
147 \r
148 /* Kernel includes. */\r
149 #include "FreeRTOS.h"\r
150 #include "task.h"\r
151 #include "queue.h"\r
152 #include "semphr.h"\r
153 #include "timers.h"\r
154 \r
155 /* Common demo includes. */\r
156 #include "blocktim.h"\r
157 #include "countsem.h"\r
158 #include "recmutex.h"\r
159 #include "ParTest.h"\r
160 #include "dynamic.h"\r
161 #include "QueueOverwrite.h"\r
162 #include "QueueSet.h"\r
163 #include "GenQTest.h"\r
164 #include "QPeek.h"\r
165 \r
166 /* The period after which the check timer will expire provided no errors have\r
167 been reported by any of the standard demo tasks.  ms are converted to the\r
168 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
169 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
170 \r
171 /* The period at which the check timer will expire if an error has been\r
172 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
173 in ticks using the portTICK_PERIOD_MS constant. */\r
174 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
175 \r
176 /* A block time of zero simply means "don't block". */\r
177 #define mainDONT_BLOCK                                          ( 0UL )\r
178 \r
179 \r
180 /*-----------------------------------------------------------*/\r
181 \r
182 /*\r
183  * Register check tasks, as described at the top of this file.  The nature of\r
184  * these files necessitates that they are written in an assembly.\r
185  */\r
186 extern void vRegTest1Task( void *pvParameters );\r
187 extern void vRegTest2Task( void *pvParameters );\r
188 \r
189 /*\r
190  * Function that starts the command console task.\r
191  */\r
192 extern void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority );\r
193 \r
194 /*\r
195  * The check timer callback function, as described at the top of this file.\r
196  */\r
197 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
198 \r
199 /*\r
200  * Called by main() to create the comprehensive test/demo application if\r
201  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.\r
202  */\r
203 void main_full( void );\r
204 \r
205 /*-----------------------------------------------------------*/\r
206 \r
207 /* The following two variables are used to communicate the status of the\r
208 register check tasks to the check software timer.  If the variables keep\r
209 incrementing, then the register check tasks has not discovered any errors.  If\r
210 a variable stops incrementing, then an error has been found. */\r
211 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
212 \r
213 /*-----------------------------------------------------------*/\r
214 \r
215 void main_full( void )\r
216 {\r
217 TimerHandle_t xTimer = NULL;\r
218 \r
219 /* The register test tasks are asm functions that don't use a stack.  The\r
220 stack allocated just has to be large enough to hold the task context, and\r
221 for the additional required for the stack overflow checking to work (if\r
222 configured). */\r
223 const size_t xRegTestStackSize = 25U;\r
224 \r
225         /* Create the standard demo tasks */\r
226         vCreateBlockTimeTasks();\r
227         vStartDynamicPriorityTasks();\r
228         vStartCountingSemaphoreTasks();\r
229         vStartRecursiveMutexTasks();\r
230         vStartQueueOverwriteTask( tskIDLE_PRIORITY );\r
231         vStartQueueSetTasks();\r
232         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
233         vStartQueuePeekTasks();\r
234         \r
235         /* Start the task that manages the command console for FreeRTOS+CLI. */\r
236         vUARTCommandConsoleStart( ( configMINIMAL_STACK_SIZE * 3 ), tskIDLE_PRIORITY ); \r
237 \r
238         /* Create the register test tasks as described at the top of this file.\r
239         These are naked functions that don't use any stack.  A stack still has\r
240         to be allocated to hold the task context. */\r
241         xTaskCreate(    vRegTest1Task,                  /* Function that implements the task. */\r
242                                         "Reg1",                                 /* Text name of the task. */\r
243                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
244                                         NULL,                                   /* The task parameter is not used. */\r
245                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
246                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
247 \r
248         xTaskCreate(    vRegTest2Task,                  /* Function that implements the task. */\r
249                                         "Reg2",                                 /* Text name of the task. */\r
250                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
251                                         NULL,                                   /* The task parameter is not used. */\r
252                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
253                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
254 \r
255         /* Create the software timer that performs the 'check' functionality,\r
256         as described at the top of this file. */\r
257         xTimer = xTimerCreate(  "CheckTimer",                                           /* A text name, purely to help debugging. */\r
258                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
259                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
260                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
261                                                         prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
262                                                 );\r
263 \r
264         /* If the software timer was created successfully, start it.  It won't\r
265         actually start running until the scheduler starts.  A block time of\r
266         zero is used in this call, although any value could be used as the block\r
267         time will be ignored because the scheduler has not started yet. */\r
268         configASSERT( xTimer );\r
269         if( xTimer != NULL )\r
270         {\r
271                 xTimerStart( xTimer, mainDONT_BLOCK );\r
272         }\r
273 \r
274         /* Start the kernel.  From here on, only tasks and interrupts will run. */\r
275         vTaskStartScheduler();\r
276 \r
277         /* If all is well, the scheduler will now be running, and the following\r
278         line will never be reached.  If the following line does execute, then there\r
279         was     insufficient FreeRTOS heap memory available for the idle and/or timer\r
280         tasks to be created.  See the memory management section on the FreeRTOS web\r
281         site, or the FreeRTOS tutorial books for more details. */\r
282         for( ;; );\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 /* See the description at the top of this file. */\r
287 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
288 {\r
289 static long lChangedTimerPeriodAlready = pdFALSE;\r
290 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
291 unsigned long ulErrorFound = pdFALSE;\r
292 \r
293         /* Check all the demo and test tasks to ensure that they are all still\r
294         running, and that none have detected an error. */\r
295         if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
296         {\r
297                 ulErrorFound = pdTRUE;\r
298         }\r
299 \r
300         if( xAreBlockTimeTestTasksStillRunning() != pdPASS )\r
301         {\r
302                 ulErrorFound = pdTRUE;\r
303         }\r
304 \r
305         if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )\r
306         {\r
307                 ulErrorFound = pdTRUE;\r
308         }\r
309 \r
310         if( xAreRecursiveMutexTasksStillRunning() != pdPASS )\r
311         {\r
312                 ulErrorFound = pdTRUE;\r
313         }\r
314 \r
315         /* Check that the register test 1 task is still running. */\r
316         if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
317         {\r
318                 ulErrorFound = pdTRUE;\r
319         }\r
320         ulLastRegTest1Value = ulRegTest1LoopCounter;\r
321 \r
322         /* Check that the register test 2 task is still running. */\r
323         if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
324         {\r
325                 ulErrorFound = pdTRUE;\r
326         }\r
327         ulLastRegTest2Value = ulRegTest2LoopCounter;\r
328 \r
329         \r
330         if( xAreQueueSetTasksStillRunning() != pdPASS )\r
331         {\r
332                 ulErrorFound = pdTRUE;\r
333         }\r
334 \r
335         if( xIsQueueOverwriteTaskStillRunning() != pdPASS )\r
336         {\r
337                 ulErrorFound = pdTRUE;\r
338         }\r
339         \r
340         if( xAreGenericQueueTasksStillRunning() != pdPASS )\r
341         {\r
342                 ulErrorFound = pdTRUE;\r
343         }\r
344         \r
345         if( xAreQueuePeekTasksStillRunning() != pdPASS )\r
346         {\r
347                 ulErrorFound = pdTRUE;\r
348         }\r
349 \r
350         /* Toggle the check LED to give an indication of the system status.  If\r
351         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
352         everything is ok.  A faster toggle indicates an error. */\r
353         port_pin_toggle_output_level( LED_0_PIN );\r
354 \r
355         /* Have any errors been latched in ulErrorFound?  If so, shorten the\r
356         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
357         This will result in an increase in the rate at which the LED toggles. */\r
358         if( ulErrorFound != pdFALSE )\r
359         {\r
360                 if( lChangedTimerPeriodAlready == pdFALSE )\r
361                 {\r
362                         lChangedTimerPeriodAlready = pdTRUE;\r
363 \r
364                         /* This call to xTimerChangePeriod() uses a zero block time.\r
365                         Functions called from inside of a timer callback function must\r
366                         *never* attempt to block. */\r
367                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
368                 }\r
369         }\r
370 }\r
371 /*-----------------------------------------------------------*/\r
372 \r