]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_Atmel_SAMD20_XPlained/RTOSDemo/src/main-full.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_M0+_Atmel_SAMD20_XPlained / RTOSDemo / src / main-full.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /******************************************************************************\r
30  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
31  * project, and a more comprehensive test and demo application.  The\r
32  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
33  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
34  * in main.c.  This file implements the comprehensive test and demo version.\r
35  *\r
36  * NOTE 2:  This file only contains the source code that is specific to the\r
37  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
38  * required to configure the hardware, are defined in main.c.\r
39  ******************************************************************************\r
40  *\r
41  * main_full() creates a set of standard demo tasks, some application specific\r
42  * tasks, and a timer.  It then starts the scheduler.  The web documentation\r
43  * provides more details of the standard demo application tasks, which provide\r
44  * no particular functionality, but do provide a good example of how to use the\r
45  * FreeRTOS API.\r
46  *\r
47  * In addition to the standard demo tasks, the following tasks and timer are\r
48  * defined and/or created within this file:\r
49  *\r
50  * "Command console task" - This uses the Atmel USART driver to provide the\r
51  * input and output to FreeRTOS+CLI - the FreeRTOS Command Line Interface.  To\r
52  * use the CLI:\r
53  *  - Power the SAMD20 XPlained board through the USB debugger connector.  This\r
54  *    will create a virtual COM port through the USB.\r
55  *  - Build and run the demo application.  \r
56  *  - Start a dumb terminal program such as TerraTerm or Hyper Terminal.\r
57  *  - In the dumb terminal select the UART port associated with the XPlained\r
58  *    debugger connection, using 19200 baud.\r
59  *  - Type 'help' in the terminal window to see a lit of command registered by\r
60  *    the demo.\r
61  *\r
62  * "Reg test" tasks - These fill the registers with known values, then check\r
63  * that each register maintains its expected value for the lifetime of the\r
64  * task.  Each task uses a different set of values.  The reg test tasks execute\r
65  * with a very low priority, so get preempted very frequently.  A register\r
66  * containing an unexpected value is indicative of an error in the context\r
67  * switching mechanism.\r
68  *\r
69  * "Check" software timer - The check timer period is initially set to three\r
70  * seconds.  Its callback function checks that all the standard demo tasks, and\r
71  * the register check tasks, are not only still executing, but are executing\r
72  * without reporting any errors.  If the check timer callback discovers that a\r
73  * task has either stalled, or reported an error, then it changes the period of\r
74  * the check timer from the initial three seconds, to just 200ms.  The callback\r
75  * function also toggles the LED each time it is called.  This provides a\r
76  * visual indication of the system status:  If the LED toggles every three\r
77  * seconds then no issues have been discovered.  If the LED toggles every 200ms,\r
78  * then an issue has been discovered with at least one task.\r
79  */\r
80 \r
81 /* Kernel includes. */\r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 #include "queue.h"\r
85 #include "semphr.h"\r
86 #include "timers.h"\r
87 \r
88 /* Common demo includes. */\r
89 #include "blocktim.h"\r
90 #include "countsem.h"\r
91 #include "recmutex.h"\r
92 #include "ParTest.h"\r
93 #include "dynamic.h"\r
94 #include "QueueOverwrite.h"\r
95 #include "QueueSet.h"\r
96 #include "GenQTest.h"\r
97 #include "QPeek.h"\r
98 \r
99 /* The period after which the check timer will expire provided no errors have\r
100 been reported by any of the standard demo tasks.  ms are converted to the\r
101 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
102 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
103 \r
104 /* The period at which the check timer will expire if an error has been\r
105 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
106 in ticks using the portTICK_PERIOD_MS constant. */\r
107 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
108 \r
109 /* A block time of zero simply means "don't block". */\r
110 #define mainDONT_BLOCK                                          ( 0UL )\r
111 \r
112 \r
113 /*-----------------------------------------------------------*/\r
114 \r
115 /*\r
116  * Register check tasks, as described at the top of this file.  The nature of\r
117  * these files necessitates that they are written in an assembly.\r
118  */\r
119 extern void vRegTest1Task( void *pvParameters );\r
120 extern void vRegTest2Task( void *pvParameters );\r
121 \r
122 /*\r
123  * Function that starts the command console task.\r
124  */\r
125 extern void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority );\r
126 \r
127 /*\r
128  * The check timer callback function, as described at the top of this file.\r
129  */\r
130 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
131 \r
132 /*\r
133  * Called by main() to create the comprehensive test/demo application if\r
134  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.\r
135  */\r
136 void main_full( void );\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 \r
140 /* The following two variables are used to communicate the status of the\r
141 register check tasks to the check software timer.  If the variables keep\r
142 incrementing, then the register check tasks have not discovered any errors.  If\r
143 a variable stops incrementing, then an error has been found. */\r
144 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
145 \r
146 /*-----------------------------------------------------------*/\r
147 \r
148 void main_full( void )\r
149 {\r
150 TimerHandle_t xTimer = NULL;\r
151 \r
152 /* The register test tasks are asm functions that don't use a stack.  The\r
153 stack allocated just has to be large enough to hold the task context, and\r
154 for the additional required for the stack overflow checking to work (if\r
155 configured). */\r
156 const size_t xRegTestStackSize = 25U;\r
157 \r
158         /* Create the standard demo tasks */\r
159         vCreateBlockTimeTasks();\r
160         vStartDynamicPriorityTasks();\r
161         vStartCountingSemaphoreTasks();\r
162         vStartRecursiveMutexTasks();\r
163         vStartQueueOverwriteTask( tskIDLE_PRIORITY );\r
164         vStartQueueSetTasks();\r
165         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
166         vStartQueuePeekTasks();\r
167         \r
168         /* Start the task that manages the command console for FreeRTOS+CLI. */\r
169         vUARTCommandConsoleStart( ( configMINIMAL_STACK_SIZE * 3 ), tskIDLE_PRIORITY ); \r
170 \r
171         /* Create the register test tasks as described at the top of this file.\r
172         These are naked functions that don't use any stack.  A stack still has\r
173         to be allocated to hold the task context. */\r
174         xTaskCreate(    vRegTest1Task,                  /* Function that implements the task. */\r
175                                         "Reg1",                                 /* Text name of the task. */\r
176                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
177                                         NULL,                                   /* The task parameter is not used. */\r
178                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
179                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
180 \r
181         xTaskCreate(    vRegTest2Task,                  /* Function that implements the task. */\r
182                                         "Reg2",                                 /* Text name of the task. */\r
183                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
184                                         NULL,                                   /* The task parameter is not used. */\r
185                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
186                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
187 \r
188         /* Create the software timer that performs the 'check' functionality,\r
189         as described at the top of this file. */\r
190         xTimer = xTimerCreate(  "CheckTimer",                                           /* A text name, purely to help debugging. */\r
191                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
192                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
193                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
194                                                         prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
195                                                 );\r
196 \r
197         /* If the software timer was created successfully, start it.  It won't\r
198         actually start running until the scheduler starts.  A block time of\r
199         zero is used in this call, although any value could be used as the block\r
200         time will be ignored because the scheduler has not started yet. */\r
201         configASSERT( xTimer );\r
202         if( xTimer != NULL )\r
203         {\r
204                 xTimerStart( xTimer, mainDONT_BLOCK );\r
205         }\r
206 \r
207         /* Start the kernel.  From here on, only tasks and interrupts will run. */\r
208         vTaskStartScheduler();\r
209 \r
210         /* If all is well, the scheduler will now be running, and the following\r
211         line will never be reached.  If the following line does execute, then there\r
212         was     insufficient FreeRTOS heap memory available for the idle and/or timer\r
213         tasks to be created.  See the memory management section on the FreeRTOS web\r
214         site, or the FreeRTOS tutorial books for more details. */\r
215         for( ;; );\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 /* See the description at the top of this file. */\r
220 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
221 {\r
222 static long lChangedTimerPeriodAlready = pdFALSE;\r
223 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
224 unsigned long ulErrorFound = pdFALSE;\r
225 \r
226         /* Check all the demo and test tasks to ensure that they are all still\r
227         running, and that none have detected an error. */\r
228         if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
229         {\r
230                 ulErrorFound = pdTRUE;\r
231         }\r
232 \r
233         if( xAreBlockTimeTestTasksStillRunning() != pdPASS )\r
234         {\r
235                 ulErrorFound = pdTRUE;\r
236         }\r
237 \r
238         if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )\r
239         {\r
240                 ulErrorFound = pdTRUE;\r
241         }\r
242 \r
243         if( xAreRecursiveMutexTasksStillRunning() != pdPASS )\r
244         {\r
245                 ulErrorFound = pdTRUE;\r
246         }\r
247 \r
248         /* Check that the register test 1 task is still running. */\r
249         if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
250         {\r
251                 ulErrorFound = pdTRUE;\r
252         }\r
253         ulLastRegTest1Value = ulRegTest1LoopCounter;\r
254 \r
255         /* Check that the register test 2 task is still running. */\r
256         if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
257         {\r
258                 ulErrorFound = pdTRUE;\r
259         }\r
260         ulLastRegTest2Value = ulRegTest2LoopCounter;\r
261 \r
262         \r
263         if( xAreQueueSetTasksStillRunning() != pdPASS )\r
264         {\r
265                 ulErrorFound = pdTRUE;\r
266         }\r
267 \r
268         if( xIsQueueOverwriteTaskStillRunning() != pdPASS )\r
269         {\r
270                 ulErrorFound = pdTRUE;\r
271         }\r
272         \r
273         if( xAreGenericQueueTasksStillRunning() != pdPASS )\r
274         {\r
275                 ulErrorFound = pdTRUE;\r
276         }\r
277         \r
278         if( xAreQueuePeekTasksStillRunning() != pdPASS )\r
279         {\r
280                 ulErrorFound = pdTRUE;\r
281         }\r
282 \r
283         /* Toggle the check LED to give an indication of the system status.  If\r
284         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
285         everything is ok.  A faster toggle indicates an error. */\r
286         port_pin_toggle_output_level( LED_0_PIN );\r
287 \r
288         /* Have any errors been latched in ulErrorFound?  If so, shorten the\r
289         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
290         This will result in an increase in the rate at which the LED toggles. */\r
291         if( ulErrorFound != pdFALSE )\r
292         {\r
293                 if( lChangedTimerPeriodAlready == pdFALSE )\r
294                 {\r
295                         lChangedTimerPeriodAlready = pdTRUE;\r
296 \r
297                         /* This call to xTimerChangePeriod() uses a zero block time.\r
298                         Functions called from inside of a timer callback function must\r
299                         *never* attempt to block. */\r
300                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
301                 }\r
302         }\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r