]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_A9_Cyclone_V_SoC_DK/main_full.c
Updated pack to FreeRTOS 10.4.6
[cmsis-freertos] / Demo / CORTEX_A9_Cyclone_V_SoC_DK / main_full.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /******************************************************************************
29  * NOTE 1:  This project provides two demo applications.  A simple blinky
30  * style project, and a more comprehensive test and demo application.  The
31  * mainCREATE_SIMPLY_BLINKY_DEMO_ONLY setting in main.c is used to select
32  * between the two.  See the notes on using mainCREATE_SIMPLY_BLINKY_DEMO_ONLY
33  * in main.c.  This file implements the comprehensive version.
34  *
35  * NOTE 2:  This file only contains the source code that is specific to the
36  * full demo.  Generic functions, such FreeRTOS hook functions, and functions
37  * required to configure the hardware, are defined in main.c.
38  *
39  ******************************************************************************
40  *
41  * main_full() creates all the demo application tasks and software timers, then
42  * starts the scheduler.  The web documentation provides more details of the
43  * standard demo application tasks, which provide no particular functionality,
44  * but do provide a good example of how to use the FreeRTOS API.
45  *
46  * In addition to the standard demo tasks, the following tasks and tests are
47  * defined and/or created within this file:
48  *
49  * "Reg test" tasks - These fill both the core and floating point registers with
50  * known values, then check that each register maintains its expected value for
51  * the lifetime of the task.  Each task uses a different set of values.  The reg
52  * test tasks execute with a very low priority, so get preempted very
53  * frequently.  A register containing an unexpected value is indicative of an
54  * error in the context switching mechanism.
55  *
56  * "Check" task - The check task period is initially set to three seconds.  The
57  * task checks that all the standard demo tasks, and the register check tasks,
58  * are not only still executing, but are executing without reporting any errors.
59  * If the check task discovers that a task has either stalled, or reported an
60  * error, then it changes its own execution period from the initial three
61  * seconds, to just 200ms.  The check task also toggles an LED each time it is
62  * called.  This provides a visual indication of the system status:  If the LED
63  * toggles every three seconds, then no issues have been discovered.  If the LED
64  * toggles every 200ms, then an issue has been discovered with at least one
65  * task.
66  */
67
68 /* Standard includes. */
69 #include <stdio.h>
70
71 /* Kernel includes. */
72 #include "FreeRTOS.h"
73 #include "task.h"
74 #include "timers.h"
75 #include "semphr.h"
76
77 /* Standard demo application includes. */
78 #include "flop.h"
79 #include "semtest.h"
80 #include "dynamic.h"
81 #include "BlockQ.h"
82 #include "blocktim.h"
83 #include "countsem.h"
84 #include "GenQTest.h"
85 #include "recmutex.h"
86 #include "death.h"
87 #include "partest.h"
88 #include "comtest2.h"
89 #include "serial.h"
90 #include "TimerDemo.h"
91 #include "QueueOverwrite.h"
92 #include "EventGroupsDemo.h"
93 #include "IntSemTest.h"
94
95 /* Priorities for the demo application tasks. */
96 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )
97 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )
98 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )
99 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )
100 #define mainUART_COMMAND_CONSOLE_STACK_SIZE     ( configMINIMAL_STACK_SIZE * 3UL )
101 #define mainCOM_TEST_TASK_PRIORITY                      ( tskIDLE_PRIORITY + 2 )
102 #define mainCHECK_TASK_PRIORITY                         ( configMAX_PRIORITIES - 1 )
103 #define mainQUEUE_OVERWRITE_PRIORITY            ( tskIDLE_PRIORITY )
104
105 /* The priority used by the UART command console task.  This is very basic and
106 uses the Altera polling UART driver - so *must* run at the idle priority. */
107 #define mainUART_COMMAND_CONSOLE_TASK_PRIORITY  ( tskIDLE_PRIORITY )
108
109 /* The LED used by the check task. */
110 #define mainCHECK_LED                                           ( 0 )
111
112 /* A block time of zero simply means "don't block". */
113 #define mainDONT_BLOCK                                          ( 0UL )
114
115 /* The period of the check task, in ms, provided no errors have been reported by
116 any of the standard demo tasks.  ms are converted to the equivalent in ticks
117 using the pdMS_TO_TICKS() macro constant. */
118 #define mainNO_ERROR_CHECK_TASK_PERIOD          pdMS_TO_TICKS( 3000UL )
119
120 /* The period of the check task, in ms, if an error has been reported in one of
121 the standard demo tasks.  ms are converted to the equivalent in ticks using the
122 pdMS_TO_TICKS() macro. */
123 #define mainERROR_CHECK_TASK_PERIOD             pdMS_TO_TICKS( 200UL )
124
125 /* Parameters that are passed into the register check tasks solely for the
126 purpose of ensuring parameters are passed into tasks correctly. */
127 #define mainREG_TEST_TASK_1_PARAMETER           ( ( void * ) 0x12345678 )
128 #define mainREG_TEST_TASK_2_PARAMETER           ( ( void * ) 0x87654321 )
129
130 /* The base period used by the timer test tasks. */
131 #define mainTIMER_TEST_PERIOD                           ( 50 )
132
133 /*-----------------------------------------------------------*/
134
135
136 /*
137  * The check task, as described at the top of this file.
138  */
139 static void prvCheckTask( void *pvParameters );
140
141 /*
142  * Register check tasks, and the tasks used to write over and check the contents
143  * of the FPU registers, as described at the top of this file.  The nature of
144  * these files necessitates that they are written in an assembly file, but the
145  * entry points are kept in the C file for the convenience of checking the task
146  * parameter.
147  */
148 static void prvRegTestTaskEntry1( void *pvParameters );
149 extern void vRegTest1Implementation( void );
150 static void prvRegTestTaskEntry2( void *pvParameters );
151 extern void vRegTest2Implementation( void );
152
153 /*
154  * Register commands that can be used with FreeRTOS+CLI.  The commands are
155  * defined in CLI-Commands.c and File-Related-CLI-Command.c respectively.
156  */
157 extern void vRegisterSampleCLICommands( void );
158
159 /*
160  * The task that manages the FreeRTOS+CLI input and output.
161  */
162 extern void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );
163
164 /*
165  * A high priority task that does nothing other than execute at a pseudo random
166  * time to ensure the other test tasks don't just execute in a repeating
167  * pattern.
168  */
169 static void prvPseudoRandomiser( void *pvParameters );
170
171 /*-----------------------------------------------------------*/
172
173 /* The following two variables are used to communicate the status of the
174 register check tasks to the check task.  If the variables keep incrementing,
175 then the register check tasks have not discovered any errors.  If a variable
176 stops incrementing, then an error has been found. */
177 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
178
179 /*-----------------------------------------------------------*/
180
181 void main_full( void )
182 {
183         /* Start all the other standard demo/test tasks.  They have no particular
184         functionality, but do demonstrate how to use the FreeRTOS API and test the
185         kernel port. */
186         vStartDynamicPriorityTasks();
187         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
188         vCreateBlockTimeTasks();
189         vStartCountingSemaphoreTasks();
190         vStartGenericQueueTasks( tskIDLE_PRIORITY );
191         vStartRecursiveMutexTasks();
192         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
193         vStartMathTasks( mainFLOP_TASK_PRIORITY );
194         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );
195         vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );
196         vStartEventGroupTasks();
197         vStartInterruptSemaphoreTasks();
198
199         /* Start the tasks that implements the command console on the UART, as
200         described above. */
201         vUARTCommandConsoleStart( mainUART_COMMAND_CONSOLE_STACK_SIZE, mainUART_COMMAND_CONSOLE_TASK_PRIORITY );
202
203         /* Register the standard CLI commands. */
204         vRegisterSampleCLICommands();
205
206         /* Create the register check tasks, as described at the top of this     file */
207         xTaskCreate( prvRegTestTaskEntry1, "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_1_PARAMETER, tskIDLE_PRIORITY, NULL );
208         xTaskCreate( prvRegTestTaskEntry2, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );
209
210         /* Create the task that just adds a little random behaviour. */
211         xTaskCreate( prvPseudoRandomiser, "Rnd", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
212
213         /* Create the task that performs the 'check' functionality,     as described at
214         the top of this file. */
215         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
216
217         /* The set of tasks created by the following function call have to be
218         created last as they keep account of the number of tasks they expect to see
219         running. */
220         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
221
222         /* Start the scheduler. */
223         vTaskStartScheduler();
224
225         /* If all is well, the scheduler will now be running, and the following
226         line will never be reached.  If the following line does execute, then
227         there was either insufficient FreeRTOS heap memory available for the idle
228         and/or timer tasks to be created, or vTaskStartScheduler() was called from
229         User mode.  See the memory management section on the FreeRTOS web site for
230         more details on the FreeRTOS heap http://www.freertos.org/a00111.html.  The
231         mode from which main() is called is set in the C start up code and must be
232         a privileged mode (not user mode). */
233         for( ;; );
234 }
235 /*-----------------------------------------------------------*/
236
237 static void prvCheckTask( void *pvParameters )
238 {
239 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;
240 TickType_t xLastExecutionTime;
241 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
242 unsigned long ulErrorFound = pdFALSE;
243
244         /* Just to stop compiler warnings. */
245         ( void ) pvParameters;
246
247         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
248         works correctly. */
249         xLastExecutionTime = xTaskGetTickCount();
250
251         /* Cycle for ever, delaying then checking all the other tasks are still
252         operating without error.  The onboard LED is toggled on each iteration.
253         If an error is detected then the delay period is decreased from
254         mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD.  This has the
255         effect of increasing the rate at which the onboard LED toggles, and in so
256         doing gives visual feedback of the system status. */
257         for( ;; )
258         {
259                 /* Delay until it is time to execute again. */
260                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );
261
262                 /* Check all the demo tasks (other than the flash tasks) to ensure
263                 that they are all still running, and that none have detected an error. */
264                 if( xAreMathsTaskStillRunning() != pdTRUE )
265                 {
266                         ulErrorFound = 1 << 1;
267                 }
268
269                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
270                 {
271                         ulErrorFound = 1 << 2;
272                 }
273
274                 if( xAreBlockingQueuesStillRunning() != pdTRUE )
275                 {
276                         ulErrorFound = 1 << 3;
277                 }
278
279                 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
280                 {
281                         ulErrorFound = 1 << 4;
282                 }
283
284                 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
285                 {
286                         ulErrorFound = 1 << 5;
287                 }
288
289                 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
290                 {
291                         ulErrorFound = 1 << 6;
292                 }
293
294                 if( xIsCreateTaskStillRunning() != pdTRUE )
295                 {
296                         ulErrorFound = 1 << 7;
297                 }
298
299                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
300                 {
301                         ulErrorFound = 1 << 8;
302                 }
303
304                 if( xAreTimerDemoTasksStillRunning( ( TickType_t ) mainNO_ERROR_CHECK_TASK_PERIOD ) != pdPASS )
305                 {
306                         ulErrorFound = 1 << 9;
307                 }
308
309                 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
310                 {
311                         ulErrorFound = 1 << 10;
312                 }
313
314                 if( xIsQueueOverwriteTaskStillRunning() != pdPASS )
315                 {
316                         ulErrorFound = 1 << 11;
317                 }
318
319                 if( xAreEventGroupTasksStillRunning() != pdPASS )
320                 {
321                         ulErrorFound = 1 << 12;
322                 }
323
324                 if( xAreInterruptSemaphoreTasksStillRunning() != pdPASS )
325                 {
326                         ulErrorFound = 1 << 13;
327                 }
328
329                 /* Check that the register test 1 task is still running. */
330                 if( ulLastRegTest1Value == ulRegTest1LoopCounter )
331                 {
332                         ulErrorFound = 1 << 14;
333                 }
334                 ulLastRegTest1Value = ulRegTest1LoopCounter;
335
336                 /* Check that the register test 2 task is still running. */
337                 if( ulLastRegTest2Value == ulRegTest2LoopCounter )
338                 {
339                         ulErrorFound = 1 << 15;
340                 }
341                 ulLastRegTest2Value = ulRegTest2LoopCounter;
342
343                 /* Toggle the check LED to give an indication of the system status.  If
344                 the LED toggles every mainNO_ERROR_CHECK_TASK_PERIOD milliseconds then
345                 everything is ok.  A faster toggle indicates an error. */
346                 vParTestToggleLED( mainCHECK_LED );
347
348                 if( ulErrorFound != pdFALSE )
349                 {
350                         /* An error has been detected in one of the tasks - flash the LED
351                         at a higher frequency to give visible feedback that something has
352                         gone wrong (it might just be that the loop back connector required
353                         by the comtest tasks has not been fitted). */
354                         xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;
355                 }
356         }
357 }
358 /*-----------------------------------------------------------*/
359
360 static void prvRegTestTaskEntry1( void *pvParameters )
361 {
362         /* Although the regtest task is written in assembler, its entry point is
363         written in C for convenience of checking the task parameter is being passed
364         in correctly. */
365         if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )
366         {
367                 /* The reg test task also tests the floating point registers.  Tasks
368                 that use the floating point unit must call vPortTaskUsesFPU() before
369                 any floating point instructions are executed. */
370                 vPortTaskUsesFPU();
371
372                 /* Start the part of the test that is written in assembler. */
373                 vRegTest1Implementation();
374         }
375
376         /* The following line will only execute if the task parameter is found to
377         be incorrect.  The check task will detect that the regtest loop counter is
378         not being incremented and flag an error. */
379         vTaskDelete( NULL );
380 }
381 /*-----------------------------------------------------------*/
382
383 static void prvRegTestTaskEntry2( void *pvParameters )
384 {
385         /* Although the regtest task is written in assembler, its entry point is
386         written in C for convenience of checking the task parameter is being passed
387         in correctly. */
388         if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )
389         {
390                 /* The reg test task also tests the floating point registers.  Tasks
391                 that use the floating point unit must call vPortTaskUsesFPU() before
392                 any floating point instructions are executed. */
393                 vPortTaskUsesFPU();
394
395                 /* Start the part of the test that is written in assembler. */
396                 vRegTest2Implementation();
397         }
398
399         /* The following line will only execute if the task parameter is found to
400         be incorrect.  The check task will detect that the regtest loop counter is
401         not being incremented and flag an error. */
402         vTaskDelete( NULL );
403 }
404 /*-----------------------------------------------------------*/
405
406 static void prvPseudoRandomiser( void *pvParameters )
407 {
408 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL, ulMinDelay = ( 35 / portTICK_PERIOD_MS );
409 volatile uint32_t ulNextRand = ( uint32_t ) &pvParameters, ulValue;
410
411
412         /* This task does nothing other than ensure there is a little bit of
413         disruption in the scheduling pattern of the other tasks.  Normally this is
414         done by generating interrupts at pseudo random times. */
415         for( ;; )
416         {
417                 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
418                 ulValue = ( ulNextRand >> 16UL ) & 0xffUL;
419
420                 if( ulValue < ulMinDelay )
421                 {
422                         ulValue = ulMinDelay;
423                 }
424
425                 vTaskDelay( ulValue );
426
427                 while( ulValue > 0 )
428                 {
429                         __asm volatile( "NOP" );
430                         __asm volatile( "NOP" );
431                         __asm volatile( "NOP" );
432                         __asm volatile( "NOP" );
433
434                         ulValue--;
435                 }
436         }
437 }
438
439
440
441
442
443