]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_EFM32_Giant_Gecko_Simplicity_Studio/Full_Demo/main_full.c
218c9075bbe7c2ec18c62e8c3072db4585ee8032
[freertos] / FreeRTOS / Demo / CORTEX_EFM32_Giant_Gecko_Simplicity_Studio / Full_Demo / main_full.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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 that demonstrates the tickless low power features of FreeRTOS, and a\r
31  * more comprehensive test and demo application.  The configCREATE_LOW_POWER_DEMO\r
32  * setting in FreeRTOSConifg.h is used to select between the two, and to select\r
33  * the clock used when tickless mode is used.  See the notes on using\r
34  * conifgCREATE_LOW_POWER_DEMO in main.c.  This file implements the\r
35  * comprehensive test and demo version.\r
36  *\r
37  * NOTE 2:  This file only contains the source code that is specific to the\r
38  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
39  * required to configure the hardware, are defined in main.c.\r
40  *\r
41  ******************************************************************************\r
42  *\r
43  * main_full() creates all the demo application tasks and software timers, then\r
44  * starts the scheduler.  The web documentation provides more details of the\r
45  * standard demo application tasks, which provide no particular functionality,\r
46  * but do provide a good example of how to use the FreeRTOS API.\r
47  *\r
48  * In addition to the standard demo tasks, the following tasks and tests are\r
49  * defined and/or created within this file:\r
50  *\r
51  * "Reg test" tasks - These fill both the core and floating point registers with\r
52  * known values, then check that each register maintains its expected value for\r
53  * the lifetime of the task.  Each task uses a different set of values.  The reg\r
54  * test tasks execute with a very low priority, so get preempted very\r
55  * frequently.  A register containing an unexpected value is indicative of an\r
56  * error in the context switching mechanism.\r
57  *\r
58  * "Check" task - The check task period is initially set to three seconds.  The\r
59  * task checks that all the standard demo tasks, and the register check tasks,\r
60  * are not only still executing, but are executing without reporting any errors.\r
61  * If the check task discovers that a task has either stalled, or reported an\r
62  * error, then it changes its own execution period from the initial three\r
63  * seconds, to just 200ms.  The check task also toggles an LED each time it is\r
64  * called.  This provides a visual indication of the system status:  If the LED\r
65  * toggles every three seconds, then no issues have been discovered.  If the LED\r
66  * toggles every 200ms, then an issue has been discovered with at least one\r
67  * task.\r
68  */\r
69 \r
70 /* Standard includes. */\r
71 #include <stdio.h>\r
72 \r
73 /* Kernel includes. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 #include "timers.h"\r
77 #include "semphr.h"\r
78 \r
79 /* SiLabs includes. */\r
80 #include "bsp.h"\r
81 \r
82 /* Standard demo application includes. */\r
83 #include "flop.h"\r
84 #include "semtest.h"\r
85 #include "dynamic.h"\r
86 #include "blocktim.h"\r
87 #include "GenQTest.h"\r
88 #include "recmutex.h"\r
89 #include "TimerDemo.h"\r
90 #include "EventGroupsDemo.h"\r
91 #include "TaskNotify.h"\r
92 #include "StaticAllocation.h"\r
93 \r
94 /* Priorities for the demo application tasks. */\r
95 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
96 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
97 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
98 #define mainCHECK_TASK_PRIORITY                         ( configMAX_PRIORITIES - 1 )\r
99 \r
100 /* A block time of zero simply means "don't block". */\r
101 #define mainDONT_BLOCK                                          ( 0UL )\r
102 \r
103 /* The period of the check task, in ms, provided no errors have been reported by\r
104 any of the standard demo tasks.  ms are converted to the equivalent in ticks\r
105 using the portTICK_PERIOD_MS constant. */\r
106 #define mainNO_ERROR_CHECK_TASK_PERIOD          pdMS_TO_TICKS( 3000UL )\r
107 \r
108 /* The period of the check task, in ms, if an error has been reported in one of\r
109 the standard demo tasks.  ms are converted to the equivalent in ticks using the\r
110 portTICK_PERIOD_MS constant. */\r
111 #define mainERROR_CHECK_TASK_PERIOD             pdMS_TO_TICKS( 200UL )\r
112 \r
113 /* Parameters that are passed into the register check tasks solely for the\r
114 purpose of ensuring parameters are passed into tasks correctly. */\r
115 #define mainREG_TEST_TASK_1_PARAMETER           ( ( void * ) 0x12345678 )\r
116 #define mainREG_TEST_TASK_2_PARAMETER           ( ( void * ) 0x87654321 )\r
117 \r
118 /* The base period used by the timer test tasks. */\r
119 #define mainTIMER_TEST_PERIOD                           ( 50 )\r
120 \r
121 /* The LED toggled by the check task. */\r
122 #define mainTASK_LED                                            ( 0 )\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 /*\r
127  * Called by main() to run the full demo (as opposed to the blinky demo) when\r
128  * mainCREATE_LOW_POWER_DEMO is set to 0.\r
129  */\r
130 void main_full( void );\r
131 \r
132 /*\r
133  * The check task, as described at the top of this file.\r
134  */\r
135 static void prvCheckTask( void *pvParameters );\r
136 \r
137 /*\r
138  * Some of the tests and demo tasks executed by the full demo include\r
139  * interaction from an interrupt - for which the tick interrupt is used via the\r
140  * tick hook function.\r
141  */\r
142 void vFullDemoTickHook( void );\r
143 \r
144 /*\r
145  * Register check tasks, and the tasks used to write over and check the contents\r
146  * of the FPU registers, as described at the top of this file.  The nature of\r
147  * these files necessitates that they are written in an assembly file, but the\r
148  * entry points are kept in the C file for the convenience of checking the task\r
149  * parameter.\r
150  */\r
151 static void prvRegTestTaskEntry1( void *pvParameters );\r
152 extern void vRegTest1Implementation( void );\r
153 static void prvRegTestTaskEntry2( void *pvParameters );\r
154 extern void vRegTest2Implementation( void );\r
155 \r
156 /*-----------------------------------------------------------*/\r
157 \r
158 /* The following two variables are used to communicate the status of the\r
159 register check tasks to the check task.  If the variables keep incrementing,\r
160 then the register check tasks have not discovered any errors.  If a variable\r
161 stops incrementing, then an error has been found. */\r
162 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
163 \r
164 /* The variable incremented in lieu of having a proper LED outout. */\r
165 extern volatile uint32_t ulLED;\r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 void main_full( void )\r
170 {\r
171         /* Start all the other standard demo/test tasks.  They have no particular\r
172         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
173         kernel port. */\r
174         vStartDynamicPriorityTasks();\r
175         vCreateBlockTimeTasks();\r
176         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
177         vStartRecursiveMutexTasks();\r
178         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
179         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
180         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
181         vStartEventGroupTasks();\r
182         vStartTaskNotifyTask();\r
183         vStartStaticallyAllocatedTasks();\r
184 \r
185         /* Create the register check tasks, as described at the top of this     file */\r
186         xTaskCreate( prvRegTestTaskEntry1, "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
187         xTaskCreate( prvRegTestTaskEntry2, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
188 \r
189         /* Create the task that performs the 'check' functionality,     as described at\r
190         the top of this file. */\r
191         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
192 \r
193         /* Start the scheduler. */\r
194         vTaskStartScheduler();\r
195 \r
196         /* If all is well, the scheduler will now be running, and the following\r
197         line will never be reached.  If the following line does execute, then\r
198         there was insufficient FreeRTOS heap memory available for the Idle and/or\r
199         timer tasks to be created.  See the memory management section on the\r
200         FreeRTOS web site for more details on the FreeRTOS heap\r
201         http://www.freertos.org/a00111.html. */\r
202         for( ;; );\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 static void prvCheckTask( void *pvParameters )\r
207 {\r
208 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;\r
209 TickType_t xLastExecutionTime;\r
210 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
211 unsigned long ulErrorFound = pdFALSE;\r
212 \r
213         /* Just to stop compiler warnings. */\r
214         ( void ) pvParameters;\r
215 \r
216         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
217         works correctly. */\r
218         xLastExecutionTime = xTaskGetTickCount();\r
219 \r
220         /* Cycle for ever, delaying then checking all the other tasks are still\r
221         operating without error.  The onboard LED is toggled on each iteration.\r
222         If an error is detected then the delay period is decreased from\r
223         mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD.  This has the\r
224         effect of increasing the rate at which the onboard LED toggles, and in so\r
225         doing gives visual feedback of the system status. */\r
226         for( ;; )\r
227         {\r
228                 /* Delay until it is time to execute again. */\r
229                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );\r
230 \r
231                 /* Check all the demo tasks (other than the flash tasks) to ensure\r
232                 that they are all still running, and that none have detected an error. */\r
233                 if( xAreMathsTaskStillRunning() != pdTRUE )\r
234                 {\r
235                         ulErrorFound = 1UL << 1UL;\r
236                 }\r
237 \r
238                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
239                 {\r
240                         ulErrorFound = 1UL << 2UL;\r
241                 }\r
242 \r
243                 if( xAreStaticAllocationTasksStillRunning() != pdPASS )\r
244                 {\r
245                         ulErrorFound = 1UL << 3UL;\r
246                 }\r
247 \r
248                 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
249                 {\r
250                         ulErrorFound = 1UL << 4UL;\r
251                 }\r
252 \r
253                 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
254                 {\r
255                         ulErrorFound = 1UL << 5UL;\r
256                 }\r
257 \r
258                 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
259                 {\r
260                         ulErrorFound = 1UL << 6UL;\r
261                 }\r
262 \r
263                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
264                 {\r
265                         ulErrorFound = 1UL << 8UL;\r
266                 }\r
267 \r
268                 if( xAreTimerDemoTasksStillRunning( ( TickType_t ) xDelayPeriod ) != pdPASS )\r
269                 {\r
270                         ulErrorFound = 1UL << 9UL;\r
271                 }\r
272 \r
273                 if( xAreEventGroupTasksStillRunning() != pdPASS )\r
274                 {\r
275                         ulErrorFound = 1UL << 12UL;\r
276                 }\r
277 \r
278                 if( xAreTaskNotificationTasksStillRunning() != pdPASS )\r
279                 {\r
280                         ulErrorFound = 1UL << 14UL;\r
281                 }\r
282 \r
283                 /* Check that the register test 1 task is still running. */\r
284                 if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
285                 {\r
286                         ulErrorFound = 1UL << 15UL;\r
287                 }\r
288                 ulLastRegTest1Value = ulRegTest1LoopCounter;\r
289 \r
290                 /* Check that the register test 2 task is still running. */\r
291                 if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
292                 {\r
293                         ulErrorFound = 1UL << 16UL;\r
294                 }\r
295                 ulLastRegTest2Value = ulRegTest2LoopCounter;\r
296 \r
297                 /* Toggle the check LED to give an indication of the system status.  If\r
298                 the LED toggles every mainNO_ERROR_CHECK_TASK_PERIOD milliseconds then\r
299                 everything is ok.  A faster toggle indicates an error. */\r
300                 BSP_LedToggle( mainTASK_LED );\r
301 \r
302                 if( ulErrorFound != pdFALSE )\r
303                 {\r
304                         /* An error has been detected in one of the tasks - flash the LED\r
305                         at a higher frequency to give visible feedback that something has\r
306                         gone wrong. */\r
307                         xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;\r
308                 }\r
309 \r
310                 configASSERT( ulErrorFound == pdFALSE );\r
311         }\r
312 }\r
313 /*-----------------------------------------------------------*/\r
314 \r
315 static void prvRegTestTaskEntry1( void *pvParameters )\r
316 {\r
317         /* Although the regtest task is written in assembler, its entry point is\r
318         written in C for convenience of checking the task parameter is being passed\r
319         in correctly. */\r
320         if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )\r
321         {\r
322                 /* Start the part of the test that is written in assembler. */\r
323                 vRegTest1Implementation();\r
324         }\r
325 \r
326         /* The following line will only execute if the task parameter is found to\r
327         be incorrect.  The check task will detect that the regtest loop counter is\r
328         not being incremented and flag an error. */\r
329         vTaskDelete( NULL );\r
330 }\r
331 /*-----------------------------------------------------------*/\r
332 \r
333 static void prvRegTestTaskEntry2( void *pvParameters )\r
334 {\r
335         /* Although the regtest task is written in assembler, its entry point is\r
336         written in C for convenience of checking the task parameter is being passed\r
337         in correctly. */\r
338         if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )\r
339         {\r
340                 /* Start the part of the test that is written in assembler. */\r
341                 vRegTest2Implementation();\r
342         }\r
343 \r
344         /* The following line will only execute if the task parameter is found to\r
345         be incorrect.  The check task will detect that the regtest loop counter is\r
346         not being incremented and flag an error. */\r
347         vTaskDelete( NULL );\r
348 }\r
349 /*-----------------------------------------------------------*/\r
350 \r
351 void vFullDemoTickHook( void )\r
352 {\r
353         /* Some of the tests and demo tasks executed by the full demo include\r
354         interaction from an interrupt - for which the tick interrupt is used via\r
355         the tick hook function. */\r
356 \r
357         /* The full demo includes a software timer demo/test that requires\r
358         prodding periodically from the tick interrupt. */\r
359         vTimerPeriodicISRTests();\r
360 \r
361         /* Call the periodic event group from ISR demo. */\r
362         vPeriodicEventGroupsProcessing();\r
363 \r
364         /* Call the code that 'gives' a task notification from an ISR. */\r
365         xNotifyTaskFromISR();\r
366 }\r
367 \r
368 \r
369 \r
370 \r
371 \r
372 \r