2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. 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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\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
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
40 ******************************************************************************
\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
47 * In addition to the standard demo tasks, the following tasks and tests are
\r
48 * defined and/or created within this file:
\r
50 * "FreeRTOS+CLI command console" - The command console is access using the USB
\r
51 * CDC driver provided by Atmel. It is accessed through the USB connector
\r
52 * marked J6 SAMA5D3 Xplained board. Type "help" to see a list of registered
\r
53 * commands. The FreeRTOS+CLI license is different to the FreeRTOS license, see
\r
54 * http://www.FreeRTOS.org/cli for license and usage details. The default baud
\r
57 * "Reg test" tasks - These fill both the core and floating point registers with
\r
58 * known values, then check that each register maintains its expected value for
\r
59 * the lifetime of the task. Each task uses a different set of values. The reg
\r
60 * test tasks execute with a very low priority, so get preempted very
\r
61 * frequently. A register containing an unexpected value is indicative of an
\r
62 * error in the context switching mechanism.
\r
64 * "Check" task - The check task period is initially set to three seconds. The
\r
65 * task checks that all the standard demo tasks, and the register check tasks,
\r
66 * are not only still executing, but are executing without reporting any errors.
\r
67 * If the check task discovers that a task has either stalled, or reported an
\r
68 * error, then it changes its own execution period from the initial three
\r
69 * seconds, to just 200ms. The check task also toggles an LED each time it is
\r
70 * called. This provides a visual indication of the system status: If the LED
\r
71 * toggles every three seconds, then no issues have been discovered. If the LED
\r
72 * toggles every 200ms, then an issue has been discovered with at least one
\r
76 /* Standard includes. */
\r
79 /* Kernel includes. */
\r
80 #include "FreeRTOS.h"
\r
85 /* Standard demo application includes. */
\r
87 #include "semtest.h"
\r
88 #include "dynamic.h"
\r
90 #include "blocktim.h"
\r
91 #include "countsem.h"
\r
92 #include "GenQTest.h"
\r
93 #include "recmutex.h"
\r
95 #include "partest.h"
\r
96 #include "comtest2.h"
\r
98 #include "TimerDemo.h"
\r
99 #include "QueueOverwrite.h"
\r
100 #include "IntQueue.h"
\r
101 #include "EventGroupsDemo.h"
\r
103 /* Priorities for the demo application tasks. */
\r
104 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1UL )
\r
105 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2UL )
\r
106 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3UL )
\r
107 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
108 #define mainCDC_COMMAND_CONSOLE_STACK_SIZE ( configMINIMAL_STACK_SIZE * 2UL )
\r
109 #define mainCOM_TEST_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
110 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
111 #define mainQUEUE_OVERWRITE_PRIORITY ( tskIDLE_PRIORITY )
\r
113 /* The initial priority used by the UART command console task. */
\r
114 #define mainUART_COMMAND_CONSOLE_TASK_PRIORITY ( configMAX_PRIORITIES - 2 )
\r
116 /* The LED used by the check task. */
\r
117 #define mainCHECK_LED ( 0 )
\r
119 /* A block time of zero simply means "don't block". */
\r
120 #define mainDONT_BLOCK ( 0UL )
\r
122 /* The period of the check task, in ms, provided no errors have been reported by
\r
123 any of the standard demo tasks. ms are converted to the equivalent in ticks
\r
124 using the pdMS_TO_TICKS() macro constant. */
\r
125 #define mainNO_ERROR_CHECK_TASK_PERIOD pdMS_TO_TICKS( 3000UL )
\r
127 /* The period of the check task, in ms, if an error has been reported in one of
\r
128 the standard demo tasks. ms are converted to the equivalent in ticks using the
\r
129 pdMS_TO_TICKS() macro. */
\r
130 #define mainERROR_CHECK_TASK_PERIOD pdMS_TO_TICKS( 200UL )
\r
132 /* Parameters that are passed into the register check tasks solely for the
\r
133 purpose of ensuring parameters are passed into tasks correctly. */
\r
134 #define mainREG_TEST_TASK_1_PARAMETER ( ( void * ) 0x12345678 )
\r
135 #define mainREG_TEST_TASK_2_PARAMETER ( ( void * ) 0x87654321 )
\r
137 /* The base period used by the timer test tasks. */
\r
138 #define mainTIMER_TEST_PERIOD ( 50 )
\r
140 /*-----------------------------------------------------------*/
\r
143 * Called by main() to run the full demo (as opposed to the blinky demo) when
\r
144 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
\r
146 void main_full( void );
\r
149 * The check task, as described at the top of this file.
\r
151 static void prvCheckTask( void *pvParameters );
\r
154 * Register check tasks, and the tasks used to write over and check the contents
\r
155 * of the FPU registers, as described at the top of this file. The nature of
\r
156 * these files necessitates that they are written in an assembly file, but the
\r
157 * entry points are kept in the C file for the convenience of checking the task
\r
160 static void prvRegTestTaskEntry1( void *pvParameters );
\r
161 extern void vRegTest1Implementation( void );
\r
162 static void prvRegTestTaskEntry2( void *pvParameters );
\r
163 extern void vRegTest2Implementation( void );
\r
166 * Register commands that can be used with FreeRTOS+CLI. The commands are
\r
167 * defined in CLI-Commands.c and File-Related-CLI-Command.c respectively.
\r
169 extern void vRegisterSampleCLICommands( void );
\r
172 * The task that manages the FreeRTOS+CLI input and output.
\r
174 extern void vUSBCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );
\r
177 * A high priority task that does nothing other than execute at a pseudo random
\r
178 * time to ensure the other test tasks don't just execute in a repeating
\r
181 static void prvPseudoRandomiser( void *pvParameters );
\r
183 /*-----------------------------------------------------------*/
\r
185 /* The following two variables are used to communicate the status of the
\r
186 register check tasks to the check task. If the variables keep incrementing,
\r
187 then the register check tasks have not discovered any errors. If a variable
\r
188 stops incrementing, then an error has been found. */
\r
189 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
\r
191 /*-----------------------------------------------------------*/
\r
193 void main_full( void )
\r
195 /* Start all the other standard demo/test tasks. They have no particular
\r
196 functionality, but do demonstrate how to use the FreeRTOS API and test the
\r
198 vStartInterruptQueueTasks();
\r
199 vStartDynamicPriorityTasks();
\r
200 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
201 vCreateBlockTimeTasks();
\r
202 vStartCountingSemaphoreTasks();
\r
203 vStartGenericQueueTasks( tskIDLE_PRIORITY );
\r
204 vStartRecursiveMutexTasks();
\r
205 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
206 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
207 vStartTimerDemoTask( mainTIMER_TEST_PERIOD );
\r
208 vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );
\r
209 vStartEventGroupTasks();
\r
211 /* Start the tasks that implements the command console on the UART, as
\r
212 described above. */
\r
213 vUSBCommandConsoleStart( mainCDC_COMMAND_CONSOLE_STACK_SIZE, mainUART_COMMAND_CONSOLE_TASK_PRIORITY );
\r
215 /* Register the standard CLI commands. */
\r
216 vRegisterSampleCLICommands();
\r
218 /* Create the register check tasks, as described at the top of this file */
\r
219 xTaskCreate( prvRegTestTaskEntry1, "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
220 xTaskCreate( prvRegTestTaskEntry2, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
222 /* Create the task that just adds a little random behaviour. */
\r
223 xTaskCreate( prvPseudoRandomiser, "Rnd", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
225 /* Create the task that performs the 'check' functionality, as described at
\r
226 the top of this file. */
\r
227 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
229 /* The set of tasks created by the following function call have to be
\r
230 created last as they keep account of the number of tasks they expect to see
\r
232 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
234 /* Start the scheduler. */
\r
235 vTaskStartScheduler();
\r
237 /* If all is well, the scheduler will now be running, and the following
\r
238 line will never be reached. If the following line does execute, then
\r
239 there was either insufficient FreeRTOS heap memory available for the idle
\r
240 and/or timer tasks to be created, or vTaskStartScheduler() was called from
\r
241 User mode. See the memory management section on the FreeRTOS web site for
\r
242 more details on the FreeRTOS heap http://www.freertos.org/a00111.html. The
\r
243 mode from which main() is called is set in the C start up code and must be
\r
244 a privileged mode (not user mode). */
\r
247 /*-----------------------------------------------------------*/
\r
249 static void prvCheckTask( void *pvParameters )
\r
251 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;
\r
252 TickType_t xLastExecutionTime;
\r
253 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
\r
254 unsigned long ulErrorFound = pdFALSE;
\r
256 /* Just to stop compiler warnings. */
\r
257 ( void ) pvParameters;
\r
259 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
260 works correctly. */
\r
261 xLastExecutionTime = xTaskGetTickCount();
\r
263 /* Cycle for ever, delaying then checking all the other tasks are still
\r
264 operating without error. The onboard LED is toggled on each iteration.
\r
265 If an error is detected then the delay period is decreased from
\r
266 mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD. This has the
\r
267 effect of increasing the rate at which the onboard LED toggles, and in so
\r
268 doing gives visual feedback of the system status. */
\r
271 /* Delay until it is time to execute again. */
\r
272 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );
\r
274 /* Check all the demo tasks (other than the flash tasks) to ensure
\r
275 that they are all still running, and that none have detected an error. */
\r
276 if( xAreIntQueueTasksStillRunning() != pdTRUE )
\r
278 ulErrorFound = pdTRUE;
\r
281 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
283 ulErrorFound = pdTRUE;
\r
286 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
288 ulErrorFound = pdTRUE;
\r
291 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
293 ulErrorFound = pdTRUE;
\r
296 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
298 ulErrorFound = pdTRUE;
\r
301 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
303 ulErrorFound = pdTRUE;
\r
306 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
308 ulErrorFound = pdTRUE;
\r
311 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
313 ulErrorFound = pdTRUE;
\r
316 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
318 ulErrorFound = pdTRUE;
\r
321 if( xAreTimerDemoTasksStillRunning( ( TickType_t ) mainNO_ERROR_CHECK_TASK_PERIOD ) != pdPASS )
\r
323 ulErrorFound = pdTRUE;
\r
326 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
328 ulErrorFound = pdTRUE;
\r
331 if( xIsQueueOverwriteTaskStillRunning() != pdPASS )
\r
333 ulErrorFound = pdTRUE;
\r
336 if( xAreEventGroupTasksStillRunning() != pdPASS )
\r
338 ulErrorFound = pdTRUE;
\r
341 /* Check that the register test 1 task is still running. */
\r
342 if( ulLastRegTest1Value == ulRegTest1LoopCounter )
\r
344 ulErrorFound = pdTRUE;
\r
346 ulLastRegTest1Value = ulRegTest1LoopCounter;
\r
348 /* Check that the register test 2 task is still running. */
\r
349 if( ulLastRegTest2Value == ulRegTest2LoopCounter )
\r
351 ulErrorFound = pdTRUE;
\r
353 ulLastRegTest2Value = ulRegTest2LoopCounter;
\r
355 /* Toggle the check LED to give an indication of the system status. If
\r
356 the LED toggles every mainNO_ERROR_CHECK_TASK_PERIOD milliseconds then
\r
357 everything is ok. A faster toggle indicates an error. */
\r
358 vParTestToggleLED( mainCHECK_LED );
\r
360 if( ulErrorFound != pdFALSE )
\r
362 /* An error has been detected in one of the tasks - flash the LED
\r
363 at a higher frequency to give visible feedback that something has
\r
364 gone wrong (it might just be that the loop back connector required
\r
365 by the comtest tasks has not been fitted). */
\r
366 xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;
\r
370 /*-----------------------------------------------------------*/
\r
372 static void prvRegTestTaskEntry1( void *pvParameters )
\r
374 /* Although the regtest task is written in assembler, its entry point is
\r
375 written in C for convenience of checking the task parameter is being passed
\r
377 if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )
\r
379 /* The reg test task also tests the floating point registers. Tasks
\r
380 that use the floating point unit must call vPortTaskUsesFPU() before
\r
381 any floating point instructions are executed. */
\r
382 vPortTaskUsesFPU();
\r
384 /* Start the part of the test that is written in assembler. */
\r
385 vRegTest1Implementation();
\r
388 /* The following line will only execute if the task parameter is found to
\r
389 be incorrect. The check task will detect that the regtest loop counter is
\r
390 not being incremented and flag an error. */
\r
391 vTaskDelete( NULL );
\r
393 /*-----------------------------------------------------------*/
\r
395 static void prvRegTestTaskEntry2( void *pvParameters )
\r
397 /* Although the regtest task is written in assembler, its entry point is
\r
398 written in C for convenience of checking the task parameter is being passed
\r
400 if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )
\r
402 /* The reg test task also tests the floating point registers. Tasks
\r
403 that use the floating point unit must call vPortTaskUsesFPU() before
\r
404 any floating point instructions are executed. */
\r
405 vPortTaskUsesFPU();
\r
407 /* Start the part of the test that is written in assembler. */
\r
408 vRegTest2Implementation();
\r
411 /* The following line will only execute if the task parameter is found to
\r
412 be incorrect. The check task will detect that the regtest loop counter is
\r
413 not being incremented and flag an error. */
\r
414 vTaskDelete( NULL );
\r
416 /*-----------------------------------------------------------*/
\r
418 static void prvPseudoRandomiser( void *pvParameters )
\r
420 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL, ulMinDelay = ( 35 / portTICK_PERIOD_MS ), ulIBit = ( 1UL << 7UL );
\r
421 volatile uint32_t ulNextRand = ( uint32_t ) &pvParameters, ulValue;
\r
423 /* A few minor port tests before entering the randomiser loop.
\r
425 At this point interrupts should be enabled. */
\r
426 configASSERT( ( __get_CPSR() & ulIBit ) == 0 );
\r
428 /* The CPU does not have an interrupt mask register, so critical sections
\r
429 have to globally disable interrupts. Therefore entering a critical section
\r
430 should leave the I bit set. */
\r
431 taskENTER_CRITICAL();
\r
432 configASSERT( ( __get_CPSR() & ulIBit ) == ulIBit );
\r
434 /* Nest the critical sections. */
\r
435 taskENTER_CRITICAL();
\r
436 configASSERT( ( __get_CPSR() & ulIBit ) == ulIBit );
\r
438 /* After yielding the I bit should still be set. Note yielding is possible
\r
439 in a critical section as each task maintains its own critical section
\r
440 nesting count so some tasks are in critical sections and others are not -
\r
441 however this is *not* something task code should do! */
\r
443 configASSERT( ( __get_CPSR() & ulIBit ) == ulIBit );
\r
445 /* The I bit should not be cleared again until both critical sections have
\r
447 taskEXIT_CRITICAL();
\r
449 configASSERT( ( __get_CPSR() & ulIBit ) == ulIBit );
\r
450 taskEXIT_CRITICAL();
\r
451 configASSERT( ( __get_CPSR() & ulIBit ) == 0 );
\r
453 configASSERT( ( __get_CPSR() & ulIBit ) == 0 );
\r
455 /* This task does nothing other than ensure there is a little bit of
\r
456 disruption in the scheduling pattern of the other tasks. Normally this is
\r
457 done by generating interrupts at pseudo random times. */
\r
460 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
\r
461 ulValue = ( ulNextRand >> 16UL ) & 0xffUL;
\r
463 if( ulValue < ulMinDelay )
\r
465 ulValue = ulMinDelay;
\r
468 vTaskDelay( ulValue );
\r
470 while( ulValue > 0 )
\r
472 __asm volatile( "NOP" );
\r
473 __asm volatile( "NOP" );
\r
474 __asm volatile( "NOP" );
\r
475 __asm volatile( "NOP" );
\r