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