]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R5_UltraScale_MPSoC/RTOSDemo_R5/src/Full_Demo/main_full.c
1ef692852c778fc2a2014dd4e251863905086bb8
[freertos] / FreeRTOS / Demo / CORTEX_R5_UltraScale_MPSoC / RTOSDemo_R5 / 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\r
31  * style project, and a more comprehensive test and demo application.  The\r
32  * mainSELECTED_APPLICATION setting in main.c is used to select between the two.\r
33  * See the notes on using mainSELECTED_APPLICATION in main.c.  This file\r
34  * implements the comprehensive 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  * NOTE 3:  The full demo includes a test that checks the floating point context\r
41  * is maintained correctly across task switches.  The standard GCC libraries can\r
42  * use floating point registers and made this test fail (unless the tasks that\r
43  * use the library are given a floating point context as described on the\r
44  * documentation page for this demo).\r
45  *\r
46  ******************************************************************************\r
47  *\r
48  * main_full() creates all the demo application tasks and software timers, then\r
49  * starts the scheduler.  The web documentation provides more details of the\r
50  * standard demo application tasks, which provide no particular functionality,\r
51  * but do provide a good example of how to use the FreeRTOS API.\r
52  *\r
53  * In addition to the standard demo tasks, the following tasks and tests are\r
54  * defined and/or created within this file:\r
55  *\r
56  * "Reg test" tasks - These fill both the core and floating point registers with\r
57  * known values, then check that each register maintains its expected value for\r
58  * the lifetime of the task.  Each task uses a different set of values.  The reg\r
59  * test tasks execute with a very low priority, so get preempted very\r
60  * frequently.  A register containing an unexpected value is indicative of an\r
61  * error in the context switching mechanism.\r
62  *\r
63  * "Check" task - The check task period is set to five seconds.  Each time it\r
64  * executes it checks all the standard demo tasks, and the register check tasks,\r
65  * are not only still executing, but are executing without reporting any errors,\r
66  * then outputs the system status to the UART.\r
67  */\r
68 \r
69 /* Standard includes. */\r
70 #include <stdio.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 "blocktim.h"\r
83 #include "countsem.h"\r
84 #include "GenQTest.h"\r
85 #include "recmutex.h"\r
86 #include "IntQueue.h"\r
87 #include "EventGroupsDemo.h"\r
88 #include "TaskNotify.h"\r
89 #include "IntSemTest.h"\r
90 #include "StaticAllocation.h"\r
91 #include "AbortDelay.h"\r
92 #include "QueueOverwrite.h"\r
93 #include "TimerDemo.h"\r
94 \r
95 /* Xilinx includes. */\r
96 #include "xil_printf.h"\r
97 \r
98 /* Priorities for the demo application tasks. */\r
99 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + ( UBaseType_t ) 1 )\r
100 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + ( UBaseType_t ) 2 )\r
101 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + ( UBaseType_t ) 3 )\r
102 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
103 #define mainUART_COMMAND_CONSOLE_STACK_SIZE     ( configMINIMAL_STACK_SIZE * ( UBaseType_t ) 3 )\r
104 #define mainCOM_TEST_TASK_PRIORITY                      ( tskIDLE_PRIORITY + ( UBaseType_t ) 2 )\r
105 #define mainCHECK_TASK_PRIORITY                         ( configMAX_PRIORITIES - ( UBaseType_t ) 1 )\r
106 #define mainQUEUE_OVERWRITE_PRIORITY            ( tskIDLE_PRIORITY )\r
107 \r
108 /* A block time of zero simply means "don't block". */\r
109 #define mainDONT_BLOCK                                          ( ( TickType_t ) 0 )\r
110 \r
111 /* The period of the check task, in ms. */\r
112 #define mainNO_ERROR_CHECK_TASK_PERIOD          pdMS_TO_TICKS( ( TickType_t ) 5000 )\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 /*-----------------------------------------------------------*/\r
123 \r
124 \r
125 /*\r
126  * The check task, as described at the top of this file.\r
127  */\r
128 static void prvCheckTask( void *pvParameters );\r
129 \r
130 /*\r
131  * Register check tasks, and the tasks used to write over and check the contents\r
132  * of the FPU registers, as described at the top of this file.  The nature of\r
133  * these files necessitates that they are written in an assembly file, but the\r
134  * entry points are kept in the C file for the convenience of checking the task\r
135  * parameter.\r
136  */\r
137 static void prvRegTestTaskEntry1( void *pvParameters );\r
138 extern void vRegTest1Implementation( void );\r
139 static void prvRegTestTaskEntry2( void *pvParameters );\r
140 extern void vRegTest2Implementation( void );\r
141 \r
142 /*\r
143  * Register commands that can be used with FreeRTOS+CLI.  The commands are\r
144  * defined in CLI-Commands.c and File-Related-CLI-Command.c respectively.\r
145  */\r
146 extern void vRegisterSampleCLICommands( void );\r
147 \r
148 /*\r
149  * The task that manages the FreeRTOS+CLI input and output.\r
150  */\r
151 extern void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );\r
152 \r
153 /*\r
154  * A high priority task that does nothing other than execute at a pseudo random\r
155  * time to ensure the other test tasks don't just execute in a repeating\r
156  * pattern.\r
157  */\r
158 static void prvPseudoRandomiser( void *pvParameters );\r
159 \r
160 /*\r
161  *  The full demo uses the tick hook function to include test code in the tick\r
162  *  interrupt.  vFullDemoTickHook() is called by vApplicationTickHook(), which\r
163  *  is defined in main.c.\r
164  */\r
165 void vFullDemoTickHook( void );\r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 /* The following two variables are used to communicate the status of the\r
170 register check tasks to the check task.  If the variables keep incrementing,\r
171 then the register check tasks have not discovered any errors.  If a variable\r
172 stops incrementing, then an error has been found. */\r
173 volatile uint32_t ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
174 \r
175 /*-----------------------------------------------------------*/\r
176 \r
177 void main_full( void )\r
178 {\r
179         /* Start all the other standard demo/test tasks.  They have no particular\r
180         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
181         kernel port. */\r
182         vStartInterruptQueueTasks();\r
183         vStartDynamicPriorityTasks();\r
184         vCreateBlockTimeTasks();\r
185         vStartCountingSemaphoreTasks();\r
186         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
187         vStartRecursiveMutexTasks();\r
188         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
189         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
190         vStartEventGroupTasks();\r
191         vStartTaskNotifyTask();\r
192         vStartInterruptSemaphoreTasks();\r
193         vStartStaticallyAllocatedTasks();\r
194         vCreateAbortDelayTasks();\r
195         vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );\r
196         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
197 \r
198         /* Create the register check tasks, as described at the top of this     file */\r
199         xTaskCreate( prvRegTestTaskEntry1, "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
200         xTaskCreate( prvRegTestTaskEntry2, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
201 \r
202         /* Create the task that just adds a little random behaviour. */\r
203         xTaskCreate( prvPseudoRandomiser, "Rnd", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
204 \r
205         /* Create the task that performs the 'check' functionality,     as described at\r
206         the top of this file. */\r
207         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
208 \r
209         /* Start the scheduler. */\r
210         vTaskStartScheduler();\r
211 \r
212         /* If all is well, the scheduler will now be running, and the following\r
213         line will never be reached.  If the following line does execute, then\r
214         there was either insufficient FreeRTOS heap memory available for the idle\r
215         and/or timer tasks to be created, or vTaskStartScheduler() was called from\r
216         User mode.  See the memory management section on the FreeRTOS web site for\r
217         more details on the FreeRTOS heap http://www.freertos.org/a00111.html.  The\r
218         mode from which main() is called is set in the C start up code and must be\r
219         a privileged mode (not user mode). */\r
220         for( ;; );\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 static void prvCheckTask( void *pvParameters )\r
225 {\r
226 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;\r
227 TickType_t xLastExecutionTime;\r
228 static uint32_t ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
229 uint32_t ulErrorFound = pdFALSE;\r
230 const char *pcStatusString = "Pass";\r
231 \r
232         /* Just to stop compiler warnings. */\r
233         ( void ) pvParameters;\r
234 \r
235         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
236         works correctly. */\r
237         xLastExecutionTime = xTaskGetTickCount();\r
238 \r
239         /* Cycle for ever, delaying then checking all the other tasks are still\r
240         operating without error.  The system status is written to the UART on each\r
241         iteration. */\r
242         for( ;; )\r
243         {\r
244                 /* Delay until it is time to execute again. */\r
245                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );\r
246 \r
247                 /* Check all the demo tasks (other than the flash tasks) to ensure\r
248                 that they are all still running, and that none have detected an error. */\r
249                 if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
250                 {\r
251                         ulErrorFound |= 1UL << 0UL;\r
252                         pcStatusString = "Error: IntQ";\r
253                 }\r
254 \r
255                 if( xAreMathsTaskStillRunning() != pdTRUE )\r
256                 {\r
257                         ulErrorFound |= 1UL << 1UL;\r
258                         pcStatusString = "Error: Math";\r
259                 }\r
260 \r
261                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
262                 {\r
263                         ulErrorFound |= 1UL << 2UL;\r
264                         pcStatusString = "Error: Dynamic";\r
265                 }\r
266 \r
267                 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
268                 {\r
269                         ulErrorFound |= 1UL << 4UL;\r
270                         pcStatusString = "Error: Block Time";\r
271                 }\r
272 \r
273                 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
274                 {\r
275                         ulErrorFound |= 1UL << 5UL;\r
276                         pcStatusString = "Error: Generic Queue";\r
277                 }\r
278 \r
279                 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
280                 {\r
281                         ulErrorFound |= 1UL << 6UL;\r
282                         pcStatusString = "Error: Recursive Mutex";\r
283                 }\r
284 \r
285                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
286                 {\r
287                         ulErrorFound |= 1UL << 8UL;\r
288                         pcStatusString = "Error: Semaphore";\r
289                 }\r
290 \r
291                 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
292                 {\r
293                         ulErrorFound |= 1UL << 10UL;\r
294                         pcStatusString = "Error: Counting Semaphore";\r
295                 }\r
296 \r
297                 if( xAreEventGroupTasksStillRunning() != pdPASS )\r
298                 {\r
299                         ulErrorFound |= 1UL << 12UL;\r
300                         pcStatusString = "Error: Event Group";\r
301                 }\r
302 \r
303                 if( xAreTaskNotificationTasksStillRunning() != pdTRUE )\r
304                 {\r
305                         ulErrorFound |= 1UL << 13UL;\r
306                         pcStatusString = "Error: Task Notifications";\r
307                 }\r
308 \r
309                 if( xAreInterruptSemaphoreTasksStillRunning() != pdTRUE )\r
310                 {\r
311                         ulErrorFound |= 1UL << 14UL;\r
312                         pcStatusString = "Error: Interrupt Semaphore";\r
313                 }\r
314 \r
315                 if( xAreStaticAllocationTasksStillRunning() != pdTRUE )\r
316                 {\r
317                         ulErrorFound |= 1UL << 15UL;\r
318                         pcStatusString = "Error: Static Allocation";\r
319                 }\r
320 \r
321                 if( xAreAbortDelayTestTasksStillRunning() != pdTRUE )\r
322                 {\r
323                         ulErrorFound |= 1UL << 16UL;\r
324                         pcStatusString = "Error: Abort Delay";\r
325                 }\r
326 \r
327                 if( xIsQueueOverwriteTaskStillRunning() != pdTRUE )\r
328                 {\r
329                         ulErrorFound |= 1UL << 17UL;\r
330                         pcStatusString = "Error: Queue Overwrite";\r
331                 }\r
332 \r
333                 if( xAreTimerDemoTasksStillRunning( xDelayPeriod ) != pdTRUE )\r
334                 {\r
335                         ulErrorFound |= 1UL << 18UL;\r
336                         pcStatusString = "Error: Timer Demo";\r
337                 }\r
338 \r
339                 /* Check that the register test 1 task is still running. */\r
340                 if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
341                 {\r
342                         ulErrorFound |= 1UL << 19UL;\r
343                         pcStatusString = "Error: Reg Test 1";\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 << 20UL;\r
351                         pcStatusString = "Error: Reg Test 2";\r
352                 }\r
353                 ulLastRegTest2Value = ulRegTest2LoopCounter;\r
354 \r
355                 /* Output the system status string. */\r
356                 xil_printf( "%s, status code = %lu, tick count = %lu\r\n", pcStatusString, ulErrorFound, xTaskGetTickCount() );\r
357 \r
358                 configASSERT( ulErrorFound == pdFALSE );\r
359         }\r
360 }\r
361 /*-----------------------------------------------------------*/\r
362 \r
363 static void prvRegTestTaskEntry1( 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_1_PARAMETER )\r
369         {\r
370                 /* The reg test task also tests the floating point registers.  Tasks\r
371                 that use the floating point unit must call vPortTaskUsesFPU() before\r
372                 any floating point instructions are executed. */\r
373                 vPortTaskUsesFPU();\r
374 \r
375                 /* Start the part of the test that is written in assembler. */\r
376                 vRegTest1Implementation();\r
377         }\r
378 \r
379         /* The following line will only execute if the task parameter is found to\r
380         be incorrect.  The check task will detect that the regtest loop counter is\r
381         not being incremented and flag an error. */\r
382         vTaskDelete( NULL );\r
383 }\r
384 /*-----------------------------------------------------------*/\r
385 \r
386 static void prvRegTestTaskEntry2( void *pvParameters )\r
387 {\r
388         /* Although the regtest task is written in assembler, its entry point is\r
389         written in C for convenience of checking the task parameter is being passed\r
390         in correctly. */\r
391         if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )\r
392         {\r
393                 /* The reg test task also tests the floating point registers.  Tasks\r
394                 that use the floating point unit must call vPortTaskUsesFPU() before\r
395                 any floating point instructions are executed. */\r
396                 vPortTaskUsesFPU();\r
397 \r
398                 /* Start the part of the test that is written in assembler. */\r
399                 vRegTest2Implementation();\r
400         }\r
401 \r
402         /* The following line will only execute if the task parameter is found to\r
403         be incorrect.  The check task will detect that the regtest loop counter is\r
404         not being incremented and flag an error. */\r
405         vTaskDelete( NULL );\r
406 }\r
407 /*-----------------------------------------------------------*/\r
408 \r
409 static void prvPseudoRandomiser( void *pvParameters )\r
410 {\r
411 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL, ulMinDelay = pdMS_TO_TICKS( 95 );\r
412 volatile uint32_t ulNextRand = ( uint32_t ) &pvParameters, ulValue;\r
413 \r
414         /* This task does nothing other than ensure there is a little bit of\r
415         disruption in the scheduling pattern of the other tasks.  Normally this is\r
416         done by generating interrupts at pseudo random times. */\r
417         for( ;; )\r
418         {\r
419                 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;\r
420                 ulValue = ( ulNextRand >> 16UL ) & 0xffUL;\r
421 \r
422                 if( ulValue < ulMinDelay )\r
423                 {\r
424                         ulValue = ulMinDelay;\r
425                 }\r
426 \r
427                 vTaskDelay( ulValue );\r
428 \r
429                 while( ulValue > 0 )\r
430                 {\r
431                         __asm volatile( "NOP" );\r
432                         __asm volatile( "NOP" );\r
433                         __asm volatile( "NOP" );\r
434                         __asm volatile( "NOP" );\r
435                         ulValue--;\r
436                 }\r
437         }\r
438 }\r
439 /*-----------------------------------------------------------*/\r
440 \r
441 void vFullDemoTickHook( void )\r
442 {\r
443         /* The full demo includes a software timer demo/test that requires\r
444         prodding periodically from the tick interrupt. */\r
445         vTimerPeriodicISRTests();\r
446 \r
447         /* Call the periodic queue overwrite from ISR demo. */\r
448         vQueueOverwritePeriodicISRDemo();\r
449 \r
450         /* Call the periodic event group from ISR demo. */\r
451         vPeriodicEventGroupsProcessing();\r
452 \r
453         /* Call the ISR component of the interrupt semaphore test. */\r
454         vInterruptSemaphorePeriodicTest();\r
455 \r
456         /* Call the code that 'gives' a task notification from an ISR. */\r
457         xNotifyTaskFromISR();\r
458 \r
459         /* Test flop alignment in interrupts - calling printf from an interrupt\r
460         is BAD! */\r
461         #if( configASSERT_DEFINED == 1 )\r
462         {\r
463         char cBuf[ 20 ];\r
464         UBaseType_t uxSavedInterruptStatus;\r
465 \r
466                 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
467                 {\r
468                         sprintf( cBuf, "%1.3f", 1.234 );\r
469                 }\r
470                 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
471 \r
472                 configASSERT( strcmp( cBuf, "1.234" ) == 0 );\r
473         }\r
474         #endif /* configASSERT_DEFINED */\r
475 }\r
476 \r
477 \r
478 \r