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