2 * FreeRTOS Kernel V10.1.1
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\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
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
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
45 ******************************************************************************
\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
52 * In addition to the standard demo tasks, the following tasks and tests are
\r
53 * defined and/or created within this file:
\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
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
68 /* Standard includes. */
\r
71 /* Kernel includes. */
\r
72 #include "FreeRTOS.h"
\r
77 /* Standard demo application includes. */
\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
94 /* Xilinx includes. */
\r
95 #include "xil_printf.h"
\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
107 /* A block time of zero simply means "don't block". */
\r
108 #define mainDONT_BLOCK ( ( TickType_t ) 0 )
\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
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
118 /* The base period used by the timer test tasks. */
\r
119 #define mainTIMER_TEST_PERIOD ( 50 )
\r
121 /*-----------------------------------------------------------*/
\r
125 * The check task, as described at the top of this file.
\r
127 static void prvCheckTask( void *pvParameters );
\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
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
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
145 extern void vRegisterSampleCLICommands( void );
\r
148 * The task that manages the FreeRTOS+CLI input and output.
\r
150 extern void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );
\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
157 static void prvPseudoRandomiser( void *pvParameters );
\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
164 void vFullDemoTickHook( void );
\r
166 /*-----------------------------------------------------------*/
\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 uint64_t ullRegTest1LoopCounter = 0ULL, ullRegTest2LoopCounter = 0ULL;
\r
174 /*-----------------------------------------------------------*/
\r
176 void main_full( void )
\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
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
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
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
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
208 /* Start the scheduler. */
\r
209 vTaskStartScheduler();
\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
221 /*-----------------------------------------------------------*/
\r
223 static void prvCheckTask( void *pvParameters )
\r
225 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;
\r
226 TickType_t xLastExecutionTime;
\r
227 static uint64_t ullLastRegTest1Value = 0, ullLastRegTest2Value = 0;
\r
228 uint64_t ullErrorFound = pdFALSE;
\r
229 const char *pcStatusString = "Pass";
\r
231 /* Just to stop compiler warnings. */
\r
232 ( void ) pvParameters;
\r
234 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
235 works correctly. */
\r
236 xLastExecutionTime = xTaskGetTickCount();
\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
243 /* Delay until it is time to execute again. */
\r
244 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );
\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
250 ullErrorFound |= 1ULL << 0ULL;
\r
251 pcStatusString = "Error: IntQ";
\r
254 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
256 ullErrorFound |= 1ULL << 1ULL;
\r
257 pcStatusString = "Error: Math";
\r
260 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
262 ullErrorFound |= 1ULL << 2ULL;
\r
263 pcStatusString = "Error: Dynamic";
\r
266 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
268 ullErrorFound |= 1ULL << 4ULL;
\r
269 pcStatusString = "Error: Block Time";
\r
272 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
274 ullErrorFound |= 1ULL << 5ULL;
\r
275 pcStatusString = "Error: Generic Queue";
\r
278 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
280 ullErrorFound |= 1ULL << 6ULL;
\r
281 pcStatusString = "Error: Recursive Mutex";
\r
284 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
286 ullErrorFound |= 1ULL << 8ULL;
\r
287 pcStatusString = "Error: Semaphore";
\r
290 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
292 ullErrorFound |= 1ULL << 10ULL;
\r
293 pcStatusString = "Error: Counting Semaphore";
\r
296 if( xAreEventGroupTasksStillRunning() != pdPASS )
\r
298 ullErrorFound |= 1ULL << 12ULL;
\r
299 pcStatusString = "Error: Event Group";
\r
302 if( xAreTaskNotificationTasksStillRunning() != pdTRUE )
\r
304 ullErrorFound |= 1ULL << 13ULL;
\r
305 pcStatusString = "Error: Task Notifications";
\r
308 if( xAreInterruptSemaphoreTasksStillRunning() != pdTRUE )
\r
310 ullErrorFound |= 1ULL << 14ULL;
\r
311 pcStatusString = "Error: Interrupt Semaphore";
\r
314 if( xAreStaticAllocationTasksStillRunning() != pdTRUE )
\r
316 ullErrorFound |= 1ULL << 15ULL;
\r
317 pcStatusString = "Error: Static Allocation";
\r
320 if( xAreAbortDelayTestTasksStillRunning() != pdTRUE )
\r
322 ullErrorFound |= 1ULL << 16ULL;
\r
323 pcStatusString = "Error: Abort Delay";
\r
326 if( xIsQueueOverwriteTaskStillRunning() != pdTRUE )
\r
328 ullErrorFound |= 1ULL << 17ULL;
\r
329 pcStatusString = "Error: Queue Overwrite";
\r
332 if( xAreTimerDemoTasksStillRunning( xDelayPeriod ) != pdTRUE )
\r
334 ullErrorFound |= 1ULL << 18ULL;
\r
335 pcStatusString = "Error: Timer Demo";
\r
338 /* Check that the register test 1 task is still running. */
\r
339 if( ullLastRegTest1Value == ullRegTest1LoopCounter )
\r
341 ullErrorFound |= 1ULL << 17ULL;
\r
342 pcStatusString = "Error: Reg Test 1";
\r
344 ullLastRegTest1Value = ullRegTest1LoopCounter;
\r
346 /* Check that the register test 2 task is still running. */
\r
347 if( ullLastRegTest2Value == ullRegTest2LoopCounter )
\r
349 ullErrorFound |= 1ULL << 18ULL;
\r
350 pcStatusString = "Error: Reg Test 2";
\r
352 ullLastRegTest2Value = ullRegTest2LoopCounter;
\r
354 /* Output the system status string. */
\r
355 xil_printf( "%s, status code = %lu, tick count = %lu\r\n", pcStatusString, ullErrorFound, xTaskGetTickCount() );
\r
357 configASSERT( ullErrorFound == pdFALSE );
\r
360 /*-----------------------------------------------------------*/
\r
362 static void prvRegTestTaskEntry1( void *pvParameters )
\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
367 if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )
\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
374 /* Start the part of the test that is written in assembler. */
\r
375 vRegTest1Implementation();
\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
383 /*-----------------------------------------------------------*/
\r
385 static void prvRegTestTaskEntry2( void *pvParameters )
\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
390 if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )
\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
397 /* Start the part of the test that is written in assembler. */
\r
398 vRegTest2Implementation();
\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
406 /*-----------------------------------------------------------*/
\r
408 static void prvPseudoRandomiser( void *pvParameters )
\r
410 const uint64_t ullMultiplier = 0x015a4e35ULL, ullIncrement = 1ULL, ullMinDelay = pdMS_TO_TICKS( 95 );
\r
411 volatile uint64_t ullNextRand = ( uint64_t ) &pvParameters, ullValue;
\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
418 ullNextRand = ( ullMultiplier * ullNextRand ) + ullIncrement;
\r
419 ullValue = ( ullNextRand >> 16ULL ) & 0xffULL;
\r
421 if( ullValue < ullMinDelay )
\r
423 ullValue = ullMinDelay;
\r
426 vTaskDelay( ullValue );
\r
428 while( ullValue > 0 )
\r
430 __asm volatile( "NOP" );
\r
431 __asm volatile( "NOP" );
\r
432 __asm volatile( "NOP" );
\r
433 __asm volatile( "NOP" );
\r
438 /*-----------------------------------------------------------*/
\r
440 void vFullDemoTickHook( void )
\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
446 /* Call the periodic queue overwrite from ISR demo. */
\r
447 vQueueOverwritePeriodicISRDemo();
\r
449 /* Call the periodic event group from ISR demo. */
\r
450 vPeriodicEventGroupsProcessing();
\r
452 /* Call the ISR component of the interrupt semaphore test. */
\r
453 vInterruptSemaphorePeriodicTest();
\r
455 /* Call the code that 'gives' a task notification from an ISR. */
\r
456 xNotifyTaskFromISR();
\r