]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MicroBlaze_Kintex7_EthernetLite/RTOSDemo/src/Full_Demo/main_full.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / MicroBlaze_Kintex7_EthernetLite / RTOSDemo / 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 /******************************************************************************\r
31  * NOTE 1:  This project provides three demo applications.  A simple blinky\r
32  * style project, a more comprehensive test and demo application, and an\r
33  * lwIP example.  The mainSELECTED_APPLICATION setting in main.c is used to\r
34  * select between the three.  See the notes on using mainSELECTED_APPLICATION\r
35  * in main.c.  This file implements the comprehensive 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  * FreeRTOS+CLI command console.  The command console is access through the\r
52  * UART to USB connector on the ZC702 Zynq development board (marked J2).  For\r
53  * reasons of robustness testing the UART driver is deliberately written to be\r
54  * inefficient and should not be used as a template for a production driver.\r
55  * Type "help" to see a list of registered commands.  The FreeRTOS+CLI license\r
56  * is different to the FreeRTOS license, see http://www.FreeRTOS.org/cli for\r
57  * license and usage details.  The default baud rate is 115200.\r
58  *\r
59  * "Reg test" tasks - These fill both the core and floating point registers with\r
60  * known values, then check that each register maintains its expected value for\r
61  * the lifetime of the task.  Each task uses a different set of values.  The reg\r
62  * test tasks execute with a very low priority, so get preempted very\r
63  * frequently.  A register containing an unexpected value is indicative of an\r
64  * error in the context switching mechanism.\r
65  *\r
66  * "Check" task - The check task period is initially set to three seconds.  The\r
67  * task checks that all the standard demo tasks, and the register check tasks,\r
68  * are not only still executing, but are executing without reporting any errors.\r
69  * If the check task discovers that a task has either stalled, or reported an\r
70  * error, then it changes its own execution period from the initial three\r
71  * seconds, to just 200ms.  The check task also toggles an LED each time it is\r
72  * called.  This provides a visual indication of the system status:  If the LED\r
73  * toggles every three seconds, then no issues have been discovered.  If the LED\r
74  * toggles every 200ms, then an issue has been discovered with at least one\r
75  * task.\r
76  */\r
77 \r
78 /* Standard includes. */\r
79 #include <stdio.h>\r
80 \r
81 /* Kernel includes. */\r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 #include "timers.h"\r
85 #include "semphr.h"\r
86 \r
87 /* Standard demo application includes. */\r
88 #include "flash_timer.h"\r
89 #include "flop.h"\r
90 #include "semtest.h"\r
91 #include "dynamic.h"\r
92 #include "blocktim.h"\r
93 #include "countsem.h"\r
94 #include "GenQTest.h"\r
95 #include "recmutex.h"\r
96 #include "partest.h"\r
97 #include "serial.h"\r
98 #include "TimerDemo.h"\r
99 #include "EventGroupsDemo.h"\r
100 #include "TaskNotify.h"\r
101 #include "IntSemTest.h"\r
102 #include "AbortDelay.h"\r
103 \r
104 /* Priorities for the demo application tasks. */\r
105 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
106 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
107 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
108 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
109 #define mainUART_COMMAND_CONSOLE_STACK_SIZE     ( configMINIMAL_STACK_SIZE * 2UL )\r
110 #define mainCOM_TEST_TASK_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
111 #define mainCHECK_TASK_PRIORITY                         ( configMAX_PRIORITIES - 1 )\r
112 #define mainQUEUE_OVERWRITE_PRIORITY            ( tskIDLE_PRIORITY )\r
113 #define mainFLASH_PRIORITY                                      ( tskIDLE_PRIORITY )\r
114 \r
115 /* The priority used by the UART command console task. */\r
116 #define mainUART_COMMAND_CONSOLE_TASK_PRIORITY  ( configMAX_PRIORITIES - 2 )\r
117 \r
118 /* The LED used by the check task. */\r
119 #define mainCHECK_LED                                           ( 7 )\r
120 \r
121 \r
122 /* A block time of zero simply means "don't block". */\r
123 #define mainDONT_BLOCK                                          ( 0UL )\r
124 \r
125 /* The period of the check task, in ms, provided no errors have been reported by\r
126 any of the standard demo tasks.  ms are converted to the equivalent in ticks\r
127 using the pdMS_TO_TICKS() macro constant. */\r
128 #define mainNO_ERROR_CHECK_TASK_PERIOD          ( pdMS_TO_TICKS( 3000UL ) )\r
129 \r
130 /* The period of the check task, in ms, if an error has been reported in one of\r
131 the standard demo tasks.  ms are converted to the equivalent in ticks using the\r
132 pdMS_TO_TICKS() macro. */\r
133 #define mainERROR_CHECK_TASK_PERIOD             ( pdMS_TO_TICKS( 200UL ) )\r
134 \r
135 /* Parameters that are passed into the register check tasks solely for the\r
136 purpose of ensuring parameters are passed into tasks correctly. */\r
137 #define mainREG_TEST_TASK_1_PARAMETER           ( ( void * ) 0x12345678 )\r
138 #define mainREG_TEST_TASK_2_PARAMETER           ( ( void * ) 0x87654321 )\r
139 \r
140 /* The base period used by the timer test tasks. */\r
141 #define mainTIMER_TEST_PERIOD                           ( 50 )\r
142 \r
143 /* The number of timers to create that just flash LEDs. */\r
144 #define mainNUM_FLASH_TIMERS                            ( 3 )\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 \r
148 /*\r
149  * The check task, as described at the top of this file.\r
150  */\r
151 static void prvCheckTask( void *pvParameters );\r
152 \r
153 /*\r
154  * Register check tasks, and the tasks used to write over and check the contents\r
155  * of the FPU registers, as described at the top of this file.  The nature of\r
156  * these files necessitates that they are written in an assembly file, but the\r
157  * entry points are kept in the C file for the convenience of checking the task\r
158  * parameter.\r
159  */\r
160 static void prvRegTestTaskEntry1( void *pvParameters );\r
161 extern void vRegTest1Implementation( void );\r
162 static void prvRegTestTaskEntry2( void *pvParameters );\r
163 extern void vRegTest2Implementation( void );\r
164 \r
165 /*\r
166  * Register commands that can be used with FreeRTOS+CLI.  The commands are\r
167  * defined in CLI-Commands.c and File-Related-CLI-Command.c respectively.\r
168  */\r
169 extern void vRegisterSampleCLICommands( void );\r
170 \r
171 /*\r
172  * The task that manages the FreeRTOS+CLI input and output.\r
173  */\r
174 extern void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );\r
175 \r
176 /*\r
177  * When the full demo is build the tick hook it used to demonstrate API\r
178  * functions being called from an interrupt and to perform some tests.\r
179  */\r
180 void vFullDemoTickHook( void );\r
181 \r
182 /*\r
183  * When the full demo is build the idle hook is used to create some timers that\r
184  * cannot be created in main() because the timer demo tasks need the entire\r
185  * command queue.\r
186  */\r
187 void vFullDemoIdleHook( void );\r
188 \r
189 /*-----------------------------------------------------------*/\r
190 \r
191 /* The following two variables are used to communicate the status of the\r
192 register check tasks to the check task.  If the variables keep incrementing,\r
193 then the register check tasks have not discovered any errors.  If a variable\r
194 stops incrementing, then an error has been found. */\r
195 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
196 \r
197 /*-----------------------------------------------------------*/\r
198 \r
199 void main_full( void )\r
200 {\r
201         /* Start all the other standard demo/test tasks.  They have no particular\r
202         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
203         kernel port. */\r
204         vStartDynamicPriorityTasks();\r
205         vCreateBlockTimeTasks();\r
206         vStartCountingSemaphoreTasks();\r
207         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
208         vStartRecursiveMutexTasks();\r
209         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
210         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
211         vStartEventGroupTasks();\r
212         vStartTaskNotifyTask();\r
213         vStartInterruptSemaphoreTasks();\r
214         vCreateAbortDelayTasks();\r
215 \r
216         /* Note - the set of standard demo tasks contains two versions of\r
217         vStartMathTasks.c.  One is defined in flop.c, and uses double precision\r
218         floating point numbers and variables.  The other is defined in sp_flop.c,\r
219         and uses single precision floating point numbers and variables.  The\r
220         MicroBlaze floating point unit only handles single precision floating.\r
221         Therefore, to test the floating point hardware, sp_flop.c should be included\r
222         in this project. */\r
223         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
224 \r
225         /* Start the tasks that implements the command console on the UART, as\r
226         described above. */\r
227         vUARTCommandConsoleStart( mainUART_COMMAND_CONSOLE_STACK_SIZE, mainUART_COMMAND_CONSOLE_TASK_PRIORITY );\r
228 \r
229         /* Register the standard CLI commands. */\r
230         vRegisterSampleCLICommands();\r
231 \r
232         /* Create the register check tasks, as described at the top of this     file */\r
233         xTaskCreate( prvRegTestTaskEntry1, "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
234         xTaskCreate( prvRegTestTaskEntry2, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
235 \r
236         /* Create the task that performs the 'check' functionality,     as described at\r
237         the top of this file. */\r
238         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
239 \r
240         /* Start the scheduler. */\r
241         vTaskStartScheduler();\r
242 \r
243         /* If all is well, the scheduler will now be running, and the following\r
244         line will never be reached.  If the following line does execute, then\r
245         there was insufficient FreeRTOS heap memory available for the Idle and/or\r
246         timer tasks to be created.  See the memory management section on the\r
247         FreeRTOS web site for more details on the FreeRTOS heap\r
248         http://www.freertos.org/a00111.html. */\r
249         for( ;; );\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 static void prvCheckTask( void *pvParameters )\r
254 {\r
255 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;\r
256 TickType_t xLastExecutionTime;\r
257 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
258 unsigned long ulErrorFound = pdFALSE;\r
259 \r
260         /* Just to stop compiler warnings. */\r
261         ( void ) pvParameters;\r
262 \r
263         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
264         works correctly. */\r
265         xLastExecutionTime = xTaskGetTickCount();\r
266 \r
267         /* Cycle for ever, delaying then checking all the other tasks are still\r
268         operating without error.  The onboard LED is toggled on each iteration.\r
269         If an error is detected then the delay period is decreased from\r
270         mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD.  This has the\r
271         effect of increasing the rate at which the onboard LED toggles, and in so\r
272         doing gives visual feedback of the system status. */\r
273         for( ;; )\r
274         {\r
275                 /* Delay until it is time to execute again. */\r
276                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );\r
277 \r
278                 /* Check all the demo tasks (other than the flash tasks) to ensure\r
279                 that they are all still running, and that none have detected an error. */\r
280                 if( xAreMathsTaskStillRunning() != pdTRUE )\r
281                 {\r
282                         ulErrorFound |= 1UL << 1UL;\r
283                 }\r
284 \r
285                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
286                 {\r
287                         ulErrorFound |= 1UL << 2UL;\r
288                 }\r
289 \r
290                 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
291                 {\r
292                         ulErrorFound |= 1UL << 4UL;\r
293                 }\r
294 \r
295                 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
296                 {\r
297                         ulErrorFound |= 1UL << 5UL;\r
298                 }\r
299 \r
300                 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
301                 {\r
302                         ulErrorFound |= 1UL << 6UL;\r
303                 }\r
304 \r
305                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
306                 {\r
307                         ulErrorFound |= 1UL << 8UL;\r
308                 }\r
309 \r
310                 if( xAreTimerDemoTasksStillRunning( ( TickType_t ) mainNO_ERROR_CHECK_TASK_PERIOD ) != pdPASS )\r
311                 {\r
312                         ulErrorFound |= 1UL << 9UL;\r
313                 }\r
314 \r
315                 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
316                 {\r
317                         ulErrorFound |= 1UL << 10UL;\r
318                 }\r
319 \r
320                 if( xAreEventGroupTasksStillRunning() != pdPASS )\r
321                 {\r
322                         ulErrorFound |= 1UL << 12UL;\r
323                 }\r
324 \r
325                 if( xAreTaskNotificationTasksStillRunning() != pdTRUE )\r
326                 {\r
327                         ulErrorFound |= 1UL << 13UL;\r
328                 }\r
329 \r
330                 if( xAreInterruptSemaphoreTasksStillRunning() != pdTRUE )\r
331                 {\r
332                         ulErrorFound |= 1UL << 14UL;\r
333                 }\r
334 \r
335                 if( xAreAbortDelayTestTasksStillRunning() != pdTRUE )\r
336                 {\r
337                         ulErrorFound |= 1UL << 15UL;\r
338                 }\r
339 \r
340                 /* Check that the register test 1 task is still running. */\r
341                 if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
342                 {\r
343                         ulErrorFound |= 1UL << 16UL;\r
344                 }\r
345                 ulLastRegTest1Value = ulRegTest1LoopCounter;\r
346 \r
347                 /* Check that the register test 2 task is still running. */\r
348                 if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
349                 {\r
350                         ulErrorFound |= 1UL << 17UL;\r
351                 }\r
352                 ulLastRegTest2Value = ulRegTest2LoopCounter;\r
353 \r
354                 /* Toggle the check LED to give an indication of the system status.  If\r
355                 the LED toggles every mainNO_ERROR_CHECK_TASK_PERIOD milliseconds then\r
356                 everything is ok.  A faster toggle indicates an error. */\r
357                 vParTestToggleLED( mainCHECK_LED );\r
358 \r
359                 if( ulErrorFound != pdFALSE )\r
360                 {\r
361                         /* An error has been detected in one of the tasks - flash the LED\r
362                         at a higher frequency to give visible feedback that something has\r
363                         gone wrong. */\r
364                         xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;\r
365                 }\r
366         }\r
367 }\r
368 /*-----------------------------------------------------------*/\r
369 \r
370 static void prvRegTestTaskEntry1( void *pvParameters )\r
371 {\r
372         /* Although the regtest task is written in assembler, its entry point is\r
373         written in C for convenience of checking the task parameter is being passed\r
374         in correctly. */\r
375         if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )\r
376         {\r
377                 /* Start the part of the test that is written in assembler. */\r
378                 vRegTest1Implementation();\r
379         }\r
380 \r
381         /* The following line will only execute if the task parameter is found to\r
382         be incorrect.  The check task will detect that the regtest loop counter is\r
383         not being incremented and flag an error. */\r
384         vTaskDelete( NULL );\r
385 }\r
386 /*-----------------------------------------------------------*/\r
387 \r
388 static void prvRegTestTaskEntry2( void *pvParameters )\r
389 {\r
390         /* Although the regtest task is written in assembler, its entry point is\r
391         written in C for convenience of checking the task parameter is being passed\r
392         in correctly. */\r
393         if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )\r
394         {\r
395                 /* Start the part of the test that is written in assembler. */\r
396                 vRegTest2Implementation();\r
397         }\r
398 \r
399         /* The following line will only execute if the task parameter is found to\r
400         be incorrect.  The check task will detect that the regtest loop counter is\r
401         not being incremented and flag an error. */\r
402         vTaskDelete( NULL );\r
403 }\r
404 /*-----------------------------------------------------------*/\r
405 \r
406 void vFullDemoTickHook( void )\r
407 {\r
408         /* The full demo includes a software timer demo/test that requires\r
409         prodding periodically from the tick interrupt. */\r
410         vTimerPeriodicISRTests();\r
411 \r
412         /* Call the periodic event group from ISR demo. */\r
413         vPeriodicEventGroupsProcessing();\r
414 \r
415         /* Use task notifications from an interrupt. */\r
416         xNotifyTaskFromISR();\r
417 \r
418         /* Use mutexes from interrupts. */\r
419         vInterruptSemaphorePeriodicTest();\r
420 }\r
421 /*-----------------------------------------------------------*/\r
422 \r
423 void vFullDemoIdleHook( void )\r
424 {\r
425 static uint32_t ulStartedTimers = pdFALSE;\r
426 \r
427         if( ulStartedTimers == pdFALSE )\r
428         {\r
429                 /* The flash timers are not created from main() as the timer demo needs\r
430                 the entire timer command queue in order to perform some tests. */\r
431                 vStartLEDFlashTimers( mainNUM_FLASH_TIMERS );\r
432                 ulStartedTimers = pdTRUE;\r
433         }\r
434 }\r
435 \r
436 \r
437 \r
438 \r