]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D3x_Xplained_IAR/Full_Demo/main_full.c
8d0786709a3ab3b4ee6470907f04c961ffe429b8
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D3x_Xplained_IAR / Full_Demo / main_full.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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  * "FreeRTOS+CLI command console" -  The command console is access using the USB\r
50  * CDC driver provided by Atmel.  It is accessed through the USB connector\r
51  * marked J6 SAMA5D3 Xplained board.  Type "help" to see a list of registered\r
52  * commands.  The FreeRTOS+CLI license is different to the FreeRTOS license, see\r
53  * http://www.FreeRTOS.org/cli for license and usage details.  The default baud\r
54  * rate is 115200.\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 initially set to three seconds.  The\r
64  * task checks that 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  * If the check task discovers that a task has either stalled, or reported an\r
67  * error, then it changes its own execution period from the initial three\r
68  * seconds, to just 200ms.  The check task also toggles an LED each time it is\r
69  * called.  This provides a visual indication of the system status:  If the LED\r
70  * toggles every three seconds, then no issues have been discovered.  If the LED\r
71  * toggles every 200ms, then an issue has been discovered with at least one\r
72  * task.\r
73  */\r
74 \r
75 /* Standard includes. */\r
76 #include <stdio.h>\r
77 \r
78 /* Kernel includes. */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 #include "timers.h"\r
82 #include "semphr.h"\r
83 \r
84 /* Standard demo application includes. */\r
85 #include "flop.h"\r
86 #include "semtest.h"\r
87 #include "dynamic.h"\r
88 #include "BlockQ.h"\r
89 #include "blocktim.h"\r
90 #include "countsem.h"\r
91 #include "GenQTest.h"\r
92 #include "recmutex.h"\r
93 #include "death.h"\r
94 #include "partest.h"\r
95 #include "comtest2.h"\r
96 #include "serial.h"\r
97 #include "TimerDemo.h"\r
98 #include "QueueOverwrite.h"\r
99 #include "IntQueue.h"\r
100 #include "EventGroupsDemo.h"\r
101 \r
102 /* Priorities for the demo application tasks. */\r
103 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
104 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
105 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
106 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
107 #define mainCDC_COMMAND_CONSOLE_STACK_SIZE      ( configMINIMAL_STACK_SIZE * 2UL )\r
108 #define mainCOM_TEST_TASK_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
109 #define mainCHECK_TASK_PRIORITY                         ( configMAX_PRIORITIES - 1 )\r
110 #define mainQUEUE_OVERWRITE_PRIORITY            ( tskIDLE_PRIORITY )\r
111 \r
112 /* The initial priority used by the UART command console task. */\r
113 #define mainUART_COMMAND_CONSOLE_TASK_PRIORITY  ( configMAX_PRIORITIES - 2 )\r
114 \r
115 /* The LED used by the check task. */\r
116 #define mainCHECK_LED                                           ( 0 )\r
117 \r
118 /* A block time of zero simply means "don't block". */\r
119 #define mainDONT_BLOCK                                          ( 0UL )\r
120 \r
121 /* The period of the check task, in ms, provided no errors have been reported by\r
122 any of the standard demo tasks.  ms are converted to the equivalent in ticks\r
123 using the pdMS_TO_TICKS() macro constant. */\r
124 #define mainNO_ERROR_CHECK_TASK_PERIOD          pdMS_TO_TICKS( 3000UL )\r
125 \r
126 /* The period of the check task, in ms, if an error has been reported in one of\r
127 the standard demo tasks.  ms are converted to the equivalent in ticks using the\r
128 pdMS_TO_TICKS() macro. */\r
129 #define mainERROR_CHECK_TASK_PERIOD             pdMS_TO_TICKS( 200UL )\r
130 \r
131 /* Parameters that are passed into the register check tasks solely for the\r
132 purpose of ensuring parameters are passed into tasks correctly. */\r
133 #define mainREG_TEST_TASK_1_PARAMETER           ( ( void * ) 0x12345678 )\r
134 #define mainREG_TEST_TASK_2_PARAMETER           ( ( void * ) 0x87654321 )\r
135 \r
136 /* The base period used by the timer test tasks. */\r
137 #define mainTIMER_TEST_PERIOD                           ( 50 )\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 /*\r
142  * Called by main() to run the full demo (as opposed to the blinky demo) when\r
143  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
144  */\r
145 void main_full( void );\r
146 \r
147 /*\r
148  * The check task, as described at the top of this file.\r
149  */\r
150 static void prvCheckTask( void *pvParameters );\r
151 \r
152 /*\r
153  * Register check tasks, and the tasks used to write over and check the contents\r
154  * of the FPU registers, as described at the top of this file.  The nature of\r
155  * these files necessitates that they are written in an assembly file, but the\r
156  * entry points are kept in the C file for the convenience of checking the task\r
157  * parameter.\r
158  */\r
159 static void prvRegTestTaskEntry1( void *pvParameters );\r
160 extern void vRegTest1Implementation( void );\r
161 static void prvRegTestTaskEntry2( void *pvParameters );\r
162 extern void vRegTest2Implementation( void );\r
163 \r
164 /*\r
165  * Register commands that can be used with FreeRTOS+CLI.  The commands are\r
166  * defined in CLI-Commands.c and File-Related-CLI-Command.c respectively.\r
167  */\r
168 extern void vRegisterSampleCLICommands( void );\r
169 \r
170 /*\r
171  * The task that manages the FreeRTOS+CLI input and output.\r
172  */\r
173 extern void vUSBCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );\r
174 \r
175 /*\r
176  * A high priority task that does nothing other than execute at a pseudo random\r
177  * time to ensure the other test tasks don't just execute in a repeating\r
178  * pattern.\r
179  */\r
180 static void prvPseudoRandomiser( void *pvParameters );\r
181 \r
182 /*-----------------------------------------------------------*/\r
183 \r
184 /* The following two variables are used to communicate the status of the\r
185 register check tasks to the check task.  If the variables keep incrementing,\r
186 then the register check tasks have not discovered any errors.  If a variable\r
187 stops incrementing, then an error has been found. */\r
188 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
189 \r
190 /*-----------------------------------------------------------*/\r
191 \r
192 void main_full( void )\r
193 {\r
194         /* Start all the other standard demo/test tasks.  They have no particular\r
195         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
196         kernel port. */\r
197         vStartInterruptQueueTasks();\r
198         vStartDynamicPriorityTasks();\r
199         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
200         vCreateBlockTimeTasks();\r
201         vStartCountingSemaphoreTasks();\r
202         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
203         vStartRecursiveMutexTasks();\r
204         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
205         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
206         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
207         vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );\r
208         vStartEventGroupTasks();\r
209 \r
210         /* Start the tasks that implements the command console on the UART, as\r
211         described above. */\r
212         vUSBCommandConsoleStart( mainCDC_COMMAND_CONSOLE_STACK_SIZE, mainUART_COMMAND_CONSOLE_TASK_PRIORITY );\r
213 \r
214         /* Register the standard CLI commands. */\r
215         vRegisterSampleCLICommands();\r
216 \r
217         /* Create the register check tasks, as described at the top of this     file */\r
218         xTaskCreate( prvRegTestTaskEntry1, "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
219         xTaskCreate( prvRegTestTaskEntry2, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
220 \r
221         /* Create the task that just adds a little random behaviour. */\r
222         xTaskCreate( prvPseudoRandomiser, "Rnd", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
223 \r
224         /* Create the task that performs the 'check' functionality,     as described at\r
225         the top of this file. */\r
226         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
227 \r
228         /* The set of tasks created by the following function call have to be\r
229         created last as they keep account of the number of tasks they expect to see\r
230         running. */\r
231         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
232 \r
233         /* Start the scheduler. */\r
234         vTaskStartScheduler();\r
235 \r
236         /* If all is well, the scheduler will now be running, and the following\r
237         line will never be reached.  If the following line does execute, then\r
238         there was either insufficient FreeRTOS heap memory available for the idle\r
239         and/or timer tasks to be created, or vTaskStartScheduler() was called from\r
240         User mode.  See the memory management section on the FreeRTOS web site for\r
241         more details on the FreeRTOS heap http://www.freertos.org/a00111.html.  The\r
242         mode from which main() is called is set in the C start up code and must be\r
243         a privileged mode (not user mode). */\r
244         for( ;; );\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 static void prvCheckTask( void *pvParameters )\r
249 {\r
250 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;\r
251 TickType_t xLastExecutionTime;\r
252 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
253 unsigned long ulErrorFound = pdFALSE;\r
254 \r
255         /* Just to stop compiler warnings. */\r
256         ( void ) pvParameters;\r
257 \r
258         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
259         works correctly. */\r
260         xLastExecutionTime = xTaskGetTickCount();\r
261 \r
262         /* Cycle for ever, delaying then checking all the other tasks are still\r
263         operating without error.  The onboard LED is toggled on each iteration.\r
264         If an error is detected then the delay period is decreased from\r
265         mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD.  This has the\r
266         effect of increasing the rate at which the onboard LED toggles, and in so\r
267         doing gives visual feedback of the system status. */\r
268         for( ;; )\r
269         {\r
270                 /* Delay until it is time to execute again. */\r
271                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );\r
272 \r
273                 /* Check all the demo tasks (other than the flash tasks) to ensure\r
274                 that they are all still running, and that none have detected an error. */\r
275                 if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
276                 {\r
277                         ulErrorFound = pdTRUE;\r
278                 }\r
279 \r
280                 if( xAreMathsTaskStillRunning() != pdTRUE )\r
281                 {\r
282                         ulErrorFound = pdTRUE;\r
283                 }\r
284 \r
285                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
286                 {\r
287                         ulErrorFound = pdTRUE;\r
288                 }\r
289 \r
290                 if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
291                 {\r
292                         ulErrorFound = pdTRUE;\r
293                 }\r
294 \r
295                 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
296                 {\r
297                         ulErrorFound = pdTRUE;\r
298                 }\r
299 \r
300                 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
301                 {\r
302                         ulErrorFound = pdTRUE;\r
303                 }\r
304 \r
305                 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
306                 {\r
307                         ulErrorFound = pdTRUE;\r
308                 }\r
309 \r
310                 if( xIsCreateTaskStillRunning() != pdTRUE )\r
311                 {\r
312                         ulErrorFound = pdTRUE;\r
313                 }\r
314 \r
315                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
316                 {\r
317                         ulErrorFound = pdTRUE;\r
318                 }\r
319 \r
320                 if( xAreTimerDemoTasksStillRunning( ( TickType_t ) mainNO_ERROR_CHECK_TASK_PERIOD ) != pdPASS )\r
321                 {\r
322                         ulErrorFound = pdTRUE;\r
323                 }\r
324 \r
325                 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
326                 {\r
327                         ulErrorFound = pdTRUE;\r
328                 }\r
329 \r
330                 if( xIsQueueOverwriteTaskStillRunning() != pdPASS )\r
331                 {\r
332                         ulErrorFound = pdTRUE;\r
333                 }\r
334 \r
335                 if( xAreEventGroupTasksStillRunning() != pdPASS )\r
336                 {\r
337                         ulErrorFound = pdTRUE;\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 = pdTRUE;\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 = pdTRUE;\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 (it might just be that the loop back connector required\r
364                         by the comtest tasks has not been fitted). */\r
365                         xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;\r
366                 }\r
367         }\r
368 }\r
369 /*-----------------------------------------------------------*/\r
370 \r
371 static void prvRegTestTaskEntry1( void *pvParameters )\r
372 {\r
373         /* Although the regtest task is written in assembler, its entry point is\r
374         written in C for convenience of checking the task parameter is being passed\r
375         in correctly. */\r
376         if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )\r
377         {\r
378                 /* The reg test task also tests the floating point registers.  Tasks\r
379                 that use the floating point unit must call vPortTaskUsesFPU() before\r
380                 any floating point instructions are executed. */\r
381                 vPortTaskUsesFPU();\r
382 \r
383                 /* Start the part of the test that is written in assembler. */\r
384                 vRegTest1Implementation();\r
385         }\r
386 \r
387         /* The following line will only execute if the task parameter is found to\r
388         be incorrect.  The check task will detect that the regtest loop counter is\r
389         not being incremented and flag an error. */\r
390         vTaskDelete( NULL );\r
391 }\r
392 /*-----------------------------------------------------------*/\r
393 \r
394 static void prvRegTestTaskEntry2( void *pvParameters )\r
395 {\r
396         /* Although the regtest task is written in assembler, its entry point is\r
397         written in C for convenience of checking the task parameter is being passed\r
398         in correctly. */\r
399         if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )\r
400         {\r
401                 /* The reg test task also tests the floating point registers.  Tasks\r
402                 that use the floating point unit must call vPortTaskUsesFPU() before\r
403                 any floating point instructions are executed. */\r
404                 vPortTaskUsesFPU();\r
405 \r
406                 /* Start the part of the test that is written in assembler. */\r
407                 vRegTest2Implementation();\r
408         }\r
409 \r
410         /* The following line will only execute if the task parameter is found to\r
411         be incorrect.  The check task will detect that the regtest loop counter is\r
412         not being incremented and flag an error. */\r
413         vTaskDelete( NULL );\r
414 }\r
415 /*-----------------------------------------------------------*/\r
416 \r
417 static void prvPseudoRandomiser( void *pvParameters )\r
418 {\r
419 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL, ulMinDelay = ( 35 / portTICK_PERIOD_MS ), ulIBit = ( 1UL << 7UL );\r
420 volatile uint32_t ulNextRand = ( uint32_t ) &pvParameters, ulValue;\r
421 \r
422         /* A few minor port tests before entering the randomiser loop.\r
423 \r
424         At this point interrupts should be enabled. */\r
425         configASSERT( ( __get_CPSR() & ulIBit ) == 0 );\r
426 \r
427         /* The CPU does not have an interrupt mask register, so critical sections\r
428         have to globally disable interrupts.  Therefore entering a critical section\r
429         should leave the I bit set. */\r
430         taskENTER_CRITICAL();\r
431         configASSERT( ( __get_CPSR() & ulIBit ) == ulIBit );\r
432 \r
433         /* Nest the critical sections. */\r
434         taskENTER_CRITICAL();\r
435         configASSERT( ( __get_CPSR() & ulIBit ) == ulIBit );\r
436 \r
437         /* After yielding the I bit should still be set.  Note yielding is possible\r
438         in a critical section as each task maintains its own critical section\r
439         nesting count so some tasks are in critical sections and others are not -\r
440         however this is *not* something task code should do! */\r
441         taskYIELD();\r
442         configASSERT( ( __get_CPSR() & ulIBit ) == ulIBit );\r
443 \r
444         /* The I bit should not be cleared again until both critical sections have\r
445         been exited. */\r
446         taskEXIT_CRITICAL();\r
447         taskYIELD();\r
448         configASSERT( ( __get_CPSR() & ulIBit ) == ulIBit );\r
449         taskEXIT_CRITICAL();\r
450         configASSERT( ( __get_CPSR() & ulIBit ) == 0 );\r
451         taskYIELD();\r
452         configASSERT( ( __get_CPSR() & ulIBit ) == 0 );\r
453 \r
454         /* This task does nothing other than ensure there is a little bit of\r
455         disruption in the scheduling pattern of the other tasks.  Normally this is\r
456         done by generating interrupts at pseudo random times. */\r
457         for( ;; )\r
458         {\r
459                 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;\r
460                 ulValue = ( ulNextRand >> 16UL ) & 0xffUL;\r
461 \r
462                 if( ulValue < ulMinDelay )\r
463                 {\r
464                         ulValue = ulMinDelay;\r
465                 }\r
466 \r
467                 vTaskDelay( ulValue );\r
468 \r
469                 while( ulValue > 0 )\r
470                 {\r
471                         __asm volatile( "NOP" );\r
472                         __asm volatile( "NOP" );\r
473                         __asm volatile( "NOP" );\r
474                         __asm volatile( "NOP" );\r
475 \r
476                         ulValue--;\r
477                 }\r
478         }\r
479 }\r
480 \r
481 \r
482 \r
483 \r
484 \r
485 \r