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