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