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