]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D2x_Xplained_IAR/Full_Demo/main_full.c
174281fb141b7bbc1627ac24a9943f07ac496f31
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D2x_Xplained_IAR / 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 style\r
31  * project, and a more comprehensive test and demo application.  The\r
32  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
33  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
34  * in main.c.  This file implements the comprehensive test and demo 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  ******************************************************************************\r
41  *\r
42  * main_full() creates all the demo application tasks and software timers, then\r
43  * starts the scheduler.  The web documentation provides more details of the\r
44  * standard demo application tasks, which provide no particular functionality,\r
45  * but do provide a good example of how to use the FreeRTOS API.\r
46  *\r
47  * In addition to the standard demo tasks, the following tasks and tests are\r
48  * defined and/or created within this file:\r
49  *\r
50  * "Reg test" tasks - These fill both the core and floating point registers with\r
51  * known values, then check that each register maintains its expected value for\r
52  * the lifetime of the task.  Each task uses a different set of values.  The reg\r
53  * test tasks execute with a very low priority, so get preempted very\r
54  * frequently.  A register containing an unexpected value is indicative of an\r
55  * error in the context switching mechanism.\r
56  *\r
57  * "Check" task - The check task period is initially set to three seconds.  The\r
58  * task checks that all the standard demo tasks, and the register check tasks,\r
59  * are not only still executing, but are executing without reporting any errors.\r
60  * If the check task discovers that a task has either stalled, or reported an\r
61  * error, then it changes its own execution period from the initial three\r
62  * seconds, to just 200ms.  The check task also toggles an LED each time it is\r
63  * called.  This provides a visual indication of the system status:  If the LED\r
64  * toggles every three seconds, then no issues have been discovered.  If the LED\r
65  * toggles every 200ms, then an issue has been discovered with at least one\r
66  * task. The LED used is defined in mainCHECK_LED, below.\r
67  *\r
68  * NOTE on LEDS:\r
69  *\r
70  *     This demo is NOT configured to use the LED built onto the SAMA6D2\r
71  *     XPLained board!\r
72  *\r
73  *     The LED driver PIN_LED definitions have been altered in\r
74  *     board_sama5d2-xplained.h to remap them to GPIOs terminating on pins 30,\r
75  *     32 and 34 of J17. (This change is conditional on the preprocessor\r
76  *     #define "LEDS_ON_J17".) These GPIOs are configured to be "high drive"\r
77  *     push-pull outputs; they can source up to 18mA at 1.8v. Low\r
78  *     forward-voltage LEDs may be connected via 100 ohm resistors to pins\r
79  *     30, 32 and 34 with their cathodes to pin 35/36 (GND).\r
80  */\r
81 \r
82 /* Standard includes. */\r
83 #include <stdio.h>\r
84 \r
85 /* Kernel includes. */\r
86 #include "FreeRTOS.h"\r
87 #include "task.h"\r
88 #include "timers.h"\r
89 #include "semphr.h"\r
90 \r
91 /* Standard demo application includes. */\r
92 #include "flop.h"\r
93 #include "semtest.h"\r
94 #include "dynamic.h"\r
95 #include "BlockQ.h"\r
96 #include "blocktim.h"\r
97 #include "countsem.h"\r
98 #include "GenQTest.h"\r
99 #include "recmutex.h"\r
100 #include "death.h"\r
101 #include "partest.h"\r
102 #include "comtest2.h"\r
103 #include "serial.h"\r
104 #include "TimerDemo.h"\r
105 #include "QueueOverwrite.h"\r
106 #include "IntQueue.h"\r
107 #include "EventGroupsDemo.h"\r
108 #include "flash.h"\r
109 \r
110 /* Priorities for the demo application tasks. */\r
111 #define mainLED_FLASH_TASK_PRIORITY                     ( tskIDLE_PRIORITY + 1UL )\r
112 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
113 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
114 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
115 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
116 #define mainCDC_COMMAND_CONSOLE_STACK_SIZE      ( configMINIMAL_STACK_SIZE * 2UL )\r
117 #define mainCOM_TEST_TASK_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
118 #define mainCHECK_TASK_PRIORITY                         ( configMAX_PRIORITIES - 1 )\r
119 #define mainQUEUE_OVERWRITE_PRIORITY            ( tskIDLE_PRIORITY )\r
120 \r
121 /* The initial priority used by the UART command console task. */\r
122 #define mainUART_COMMAND_CONSOLE_TASK_PRIORITY  ( configMAX_PRIORITIES - 2 )\r
123 \r
124 /* The LED used by the check task. */\r
125 #define mainCHECK_LED                                           ( 2 )\r
126 \r
127 /* A block time of zero simply means "don't block". */\r
128 #define mainDONT_BLOCK                                          ( 0UL )\r
129 \r
130 /* The period of the check task, in ms, provided no errors have been reported by\r
131 any of the standard demo tasks.  ms are converted to the equivalent in ticks\r
132 using the pdMS_TO_TICKS() macro constant. */\r
133 #define mainNO_ERROR_CHECK_TASK_PERIOD          pdMS_TO_TICKS( 3000UL )\r
134 \r
135 /* The period of the check task, in ms, if an error has been reported in one of\r
136 the standard demo tasks.  ms are converted to the equivalent in ticks using the\r
137 pdMS_TO_TICKS() macro. */\r
138 #define mainERROR_CHECK_TASK_PERIOD             pdMS_TO_TICKS( 200UL )\r
139 \r
140 /* Parameters that are passed into the register check tasks solely for the\r
141 purpose of ensuring parameters are passed into tasks correctly. */\r
142 #define mainREG_TEST_TASK_1_PARAMETER           ( ( void * ) 0x12345678 )\r
143 #define mainREG_TEST_TASK_2_PARAMETER           ( ( void * ) 0x87654321 )\r
144 \r
145 /* The base period used by the timer test tasks. */\r
146 #define mainTIMER_TEST_PERIOD                           ( 50 )\r
147 \r
148 /*-----------------------------------------------------------*/\r
149 \r
150 /*\r
151  * Called by main() to run the full demo (as opposed to the blinky demo) when\r
152  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
153  */\r
154 void main_full( void );\r
155 \r
156 /*\r
157  * The check task, as described at the top of this file.\r
158  */\r
159 static void prvCheckTask( void *pvParameters );\r
160 \r
161 /*\r
162  * Register check tasks, and the tasks used to write over and check the contents\r
163  * of the FPU registers, as described at the top of this file.  The nature of\r
164  * these files necessitates that they are written in an assembly file, but the\r
165  * entry points are kept in the C file for the convenience of checking the task\r
166  * parameter.\r
167  */\r
168 static void prvRegTestTaskEntry1( void *pvParameters );\r
169 extern void vRegTest1Implementation( void );\r
170 static void prvRegTestTaskEntry2( void *pvParameters );\r
171 extern void vRegTest2Implementation( void );\r
172 \r
173 /*\r
174  * A high priority task that does nothing other than execute at a pseudo random\r
175  * time to ensure the other test tasks don't just execute in a repeating\r
176  * pattern.\r
177  */\r
178 static void prvPseudoRandomiser( void *pvParameters );\r
179 \r
180 /*-----------------------------------------------------------*/\r
181 \r
182 /* The following two variables are used to communicate the status of the\r
183 register check tasks to the check task.  If the variables keep incrementing,\r
184 then the register check tasks have not discovered any errors.  If a variable\r
185 stops incrementing, then an error has been found. */\r
186 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
187 \r
188 /*-----------------------------------------------------------*/\r
189 \r
190 void main_full( void )\r
191 {\r
192         /* Start all the other standard demo/test tasks.  They have no particular\r
193         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
194         kernel port. */\r
195         vStartInterruptQueueTasks();\r
196         vStartDynamicPriorityTasks();\r
197         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
198         vCreateBlockTimeTasks();\r
199         vStartCountingSemaphoreTasks();\r
200         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
201         vStartRecursiveMutexTasks();\r
202         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
203         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
204         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
205         vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );\r
206         vStartEventGroupTasks();\r
207 \r
208         /* Create the register check tasks, as described at the top of this     file */\r
209         xTaskCreate( prvRegTestTaskEntry1, "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
210         xTaskCreate( prvRegTestTaskEntry2, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
211 \r
212         /* Create the task that just adds a little random behaviour. */\r
213         xTaskCreate( prvPseudoRandomiser, "Rnd", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
214 \r
215         /* Create the task that performs the 'check' functionality,     as described at\r
216         the top of this file. */\r
217         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
218 \r
219         /* The set of tasks created by the following function call have to be\r
220         created last as they keep account of the number of tasks they expect to see\r
221         running. */\r
222         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
223 \r
224         /* Start the scheduler. */\r
225         vTaskStartScheduler();\r
226 \r
227         /* If all is well, the scheduler will now be running, and the following\r
228         line will never be reached.  If the following line does execute, then\r
229         there was either insufficient FreeRTOS heap memory available for the idle\r
230         and/or timer tasks to be created, or vTaskStartScheduler() was called from\r
231         User mode.  See the memory management section on the FreeRTOS web site for\r
232         more details on the FreeRTOS heap http://www.freertos.org/a00111.html.  The\r
233         mode from which main() is called is set in the C start up code and must be\r
234         a privileged mode (not user mode). */\r
235         for( ;; );\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 static void prvCheckTask( void *pvParameters )\r
240 {\r
241 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;\r
242 TickType_t xLastExecutionTime;\r
243 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
244 unsigned long ulErrorFound = pdFALSE;\r
245 \r
246         /* Just to stop compiler warnings. */\r
247         ( void ) pvParameters;\r
248 \r
249         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
250         works correctly. */\r
251         xLastExecutionTime = xTaskGetTickCount();\r
252 \r
253         /* Cycle for ever, delaying then checking all the other tasks are still\r
254         operating without error.  The onboard LED is toggled on each iteration.\r
255         If an error is detected then the delay period is decreased from\r
256         mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD.  This has the\r
257         effect of increasing the rate at which the onboard LED toggles, and in so\r
258         doing gives visual feedback of the system status. */\r
259         for( ;; )\r
260         {\r
261                 /* Delay until it is time to execute again. */\r
262                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );\r
263 \r
264                 /* Check all the demo tasks (other than the flash tasks) to ensure\r
265                 that they are all still running, and that none have detected an error. */\r
266                 if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
267                 {\r
268                         ulErrorFound = pdTRUE;\r
269                 }\r
270 \r
271                 if( xAreMathsTaskStillRunning() != pdTRUE )\r
272                 {\r
273                         ulErrorFound = pdTRUE;\r
274                 }\r
275 \r
276                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
277                 {\r
278                         ulErrorFound = pdTRUE;\r
279                 }\r
280 \r
281                 if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
282                 {\r
283                         ulErrorFound = pdTRUE;\r
284                 }\r
285 \r
286                 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
287                 {\r
288                         ulErrorFound = pdTRUE;\r
289                 }\r
290 \r
291                 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
292                 {\r
293                         ulErrorFound = pdTRUE;\r
294                 }\r
295 \r
296                 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
297                 {\r
298                         ulErrorFound = pdTRUE;\r
299                 }\r
300 \r
301                 if( xIsCreateTaskStillRunning() != pdTRUE )\r
302                 {\r
303                         ulErrorFound = pdTRUE;\r
304                 }\r
305 \r
306                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
307                 {\r
308                         ulErrorFound = pdTRUE;\r
309                 }\r
310 \r
311                 if( xAreTimerDemoTasksStillRunning( ( TickType_t ) mainNO_ERROR_CHECK_TASK_PERIOD ) != pdPASS )\r
312                 {\r
313                         ulErrorFound = pdTRUE;\r
314                 }\r
315 \r
316                 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
317                 {\r
318                         ulErrorFound = pdTRUE;\r
319                 }\r
320 \r
321                 if( xIsQueueOverwriteTaskStillRunning() != pdPASS )\r
322                 {\r
323                         ulErrorFound = pdTRUE;\r
324                 }\r
325 \r
326                 if( xAreEventGroupTasksStillRunning() != pdPASS )\r
327                 {\r
328                         ulErrorFound = pdTRUE;\r
329                 }\r
330 \r
331                 /* Check that the register test 1 task is still running. */\r
332                 if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
333                 {\r
334                         ulErrorFound = pdTRUE;\r
335                 }\r
336                 ulLastRegTest1Value = ulRegTest1LoopCounter;\r
337 \r
338                 /* Check that the register test 2 task is still running. */\r
339                 if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
340                 {\r
341                         ulErrorFound = pdTRUE;\r
342                 }\r
343                 ulLastRegTest2Value = ulRegTest2LoopCounter;\r
344 \r
345                 /* Toggle the check LED to give an indication of the system status.  If\r
346                 the LED toggles every mainNO_ERROR_CHECK_TASK_PERIOD milliseconds then\r
347                 everything is ok.  A faster toggle indicates an error. */\r
348                 vParTestToggleLED( mainCHECK_LED );\r
349 \r
350                 if( ulErrorFound != pdFALSE )\r
351                 {\r
352                         /* An error has been detected in one of the tasks - flash the LED\r
353                         at a higher frequency to give visible feedback that something has\r
354                         gone wrong (it might just be that the loop back connector required\r
355                         by the comtest tasks has not been fitted). */\r
356                         xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;\r
357                 }\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 = ( 35 / portTICK_PERIOD_MS ), ulIBit = ( 1UL << 7UL );\r
411 volatile uint32_t ulNextRand = ( uint32_t ) &pvParameters, ulValue;\r
412 \r
413         /* A few minor port tests before entering the randomiser loop.\r
414 \r
415         At this point interrupts should be enabled. */\r
416         configASSERT( ( __get_CPSR() & ulIBit ) == 0 );\r
417 \r
418         /* The CPU does not have an interrupt mask register, so critical sections\r
419         have to globally disable interrupts.  Therefore entering a critical section\r
420         should leave the I bit set. */\r
421         taskENTER_CRITICAL();\r
422         configASSERT( ( __get_CPSR() & ulIBit ) == ulIBit );\r
423 \r
424         /* Nest the critical sections. */\r
425         taskENTER_CRITICAL();\r
426         configASSERT( ( __get_CPSR() & ulIBit ) == ulIBit );\r
427 \r
428         /* After yielding the I bit should still be set.  Note yielding is possible\r
429         in a critical section as each task maintains its own critical section\r
430         nesting count so some tasks are in critical sections and others are not -\r
431         however this is *not* something task code should do! */\r
432         taskYIELD();\r
433         configASSERT( ( __get_CPSR() & ulIBit ) == ulIBit );\r
434 \r
435         /* The I bit should not be cleared again until both critical sections have\r
436         been exited. */\r
437         taskEXIT_CRITICAL();\r
438         taskYIELD();\r
439         configASSERT( ( __get_CPSR() & ulIBit ) == ulIBit );\r
440         taskEXIT_CRITICAL();\r
441         configASSERT( ( __get_CPSR() & ulIBit ) == 0 );\r
442         taskYIELD();\r
443         configASSERT( ( __get_CPSR() & ulIBit ) == 0 );\r
444 \r
445         /* This task does nothing other than ensure there is a little bit of\r
446         disruption in the scheduling pattern of the other tasks.  Normally this is\r
447         done by generating interrupts at pseudo random times. */\r
448         for( ;; )\r
449         {\r
450                 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;\r
451                 ulValue = ( ulNextRand >> 16UL ) & 0xffUL;\r
452 \r
453                 if( ulValue < ulMinDelay )\r
454                 {\r
455                         ulValue = ulMinDelay;\r
456                 }\r
457 \r
458                 vTaskDelay( ulValue );\r
459 \r
460                 while( ulValue > 0 )\r
461                 {\r
462                         __asm volatile( "NOP" );\r
463                         __asm volatile( "NOP" );\r
464                         __asm volatile( "NOP" );\r
465                         __asm volatile( "NOP" );\r
466 \r
467                         ulValue--;\r
468                 }\r
469         }\r
470 }\r
471 \r
472 \r
473 \r
474 \r
475 \r
476 \r