2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
65 /******************************************************************************
\r
66 * NOTE 1: This project provides two demo applications. A simple blinky style
\r
67 * project, and a more comprehensive test and demo application. The
\r
68 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
\r
69 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
\r
70 * in main.c. This file implements the comprehensive test and demo version.
\r
72 * NOTE 2: This file only contains the source code that is specific to the
\r
73 * full demo. Generic functions, such FreeRTOS hook functions, and functions
\r
74 * required to configure the hardware, are defined in main.c.
\r
76 * NOTE 3: If mainINCLUDE_FAT_SL_DEMO is set to 1 then the UART is used to
\r
77 * interface to the FreeRTOS+CLI command line interface. If
\r
78 * mainINCLUDE_FAT_SL_DEMO is set to 0 then the UART is used to run the standard
\r
79 * COM test tasks and a loopback connector must be fitted to the UART port
\r
80 * because the test expects to receive every character that is transmitted. A
\r
81 * simple loopback connector can be created by linking pins 2 and 3 of the 9 way
\r
83 ******************************************************************************
\r
85 * main_full() creates all the demo application tasks and software timers, then
\r
86 * starts the scheduler. The web documentation provides more details of the
\r
87 * standard demo application tasks, which provide no particular functionality,
\r
88 * but do provide a good example of how to use the FreeRTOS API.
\r
90 * In addition to the standard demo tasks, the following tasks and tests are
\r
91 * defined and/or created within this file:
\r
93 * FreeRTOS+CLI command console. The command console is access through UART2
\r
94 * using 115200 baud if mainINCLUDE_FAT_SL_DEMO is set to 1. For reasons of
\r
95 * robustness testing the UART driver is deliberately written to be inefficient
\r
96 * and should not be used as a template for a production driver. Type "help" to
\r
97 * see a list of registered commands. The FreeRTOS+CLI license is different to
\r
98 * the FreeRTOS license, see http://www.FreeRTOS.org/cli for license and usage
\r
101 * FreeRTOS+FAT SL. FreeRTOS+FAT SL is demonstrated using a RAM disk if
\r
102 * mainINCLUDE_FAT_SL_DEMO is set to 1. [At the time of writing] The
\r
103 * functionality of the file system demo is identical to the functionality of
\r
104 * the FreeRTOS Win32 simulator file system demo, with the command console being
\r
105 * accessed via the UART (as described above) instead of a network terminal.
\r
106 * The FreeRTOS+FAT SL license is different to the FreeRTOS license, see
\r
107 * http://www.FreeRTOS.org/fat_sl for license and usage details, and a
\r
108 * description of the file system demo functionality.
\r
110 * "Reg test" tasks - These fill both the core and floating point registers with
\r
111 * known values, then check that each register maintains its expected value for
\r
112 * the lifetime of the task. Each task uses a different set of values. The reg
\r
113 * test tasks execute with a very low priority, so get preempted very
\r
114 * frequently. A register containing an unexpected value is indicative of an
\r
115 * error in the context switching mechanism.
\r
117 * "Check" task - The check task period is initially set to three seconds. The
\r
118 * task checks that all the standard demo tasks, and the register check tasks,
\r
119 * are not only still executing, but are executing without reporting any errors.
\r
120 * If the check task discovers that a task has either stalled, or reported an
\r
121 * error, then it changes its own execution period from the initial three
\r
122 * seconds, to just 200ms. The check task also toggles an LED each time it is
\r
123 * called. This provides a visual indication of the system status: If the LED
\r
124 * toggles every three seconds, then no issues have been discovered. If the LED
\r
125 * toggles every 200ms, then an issue has been discovered with at least one
\r
129 /* Standard includes. */
\r
132 /* Kernel includes. */
\r
133 #include "FreeRTOS.h"
\r
135 #include "timers.h"
\r
136 #include "semphr.h"
\r
138 /* Standard demo application includes. */
\r
140 #include "semtest.h"
\r
141 #include "dynamic.h"
\r
142 #include "BlockQ.h"
\r
143 #include "blocktim.h"
\r
144 #include "countsem.h"
\r
145 #include "GenQTest.h"
\r
146 #include "recmutex.h"
\r
148 #include "partest.h"
\r
149 #include "comtest2.h"
\r
150 #include "serial.h"
\r
151 #include "TimerDemo.h"
\r
152 #include "QueueOverwrite.h"
\r
154 /* FreeRTOS+CLI and FreeRTOS+FAT SL includes. */
\r
155 #include "UARTCommandConsole.h"
\r
157 /* Either the FreeRTOS+FAT SL demo or the COM test demo can be build into the
\r
158 project, not both (because they use the same UART). Set
\r
159 configINCLUDE_FAT_SL_DEMO to 1 to include the FreeRTOS+FAT SL (and therefore
\r
160 also FreeRTOS+CLI) demo in the build. Set configINCLUDE_FAT_SL_DEMO to 0 to
\r
161 include the COM test tasks. The COM test tasks require a loop back connector
\r
162 to be fitted to the UART port. */
\r
163 #define mainINCLUDE_FAT_SL_DEMO 1
\r
165 /* Priorities for the demo application tasks. */
\r
166 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1UL )
\r
167 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2UL )
\r
168 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3UL )
\r
169 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
170 #define mainUART_COMMAND_CONSOLE_STACK_SIZE ( configMINIMAL_STACK_SIZE * 3UL )
\r
171 #define mainCOM_TEST_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
172 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
173 #define mainQUEUE_OVERWRITE_PRIORITY ( tskIDLE_PRIORITY )
\r
175 /* The priority used by the UART command console task. */
\r
176 #define mainUART_COMMAND_CONSOLE_TASK_PRIORITY ( configMAX_PRIORITIES - 2 )
\r
178 /* The LED used by the check timer. */
\r
179 #define mainCHECK_LED ( 0 )
\r
181 /* A block time of zero simply means "don't block". */
\r
182 #define mainDONT_BLOCK ( 0UL )
\r
184 /* In this example the baud rate is hard coded and there is no LED for use by
\r
185 the COM test tasks, so just set both to invalid values. */
\r
186 #define mainCOM_TEST_LED ( 100 )
\r
187 #define mainBAUD_RATE ( 0 )
\r
189 /* The period after which the check timer will expire, in ms, provided no errors
\r
190 have been reported by any of the standard demo tasks. ms are converted to the
\r
191 equivalent in ticks using the portTICK_RATE_MS constant. */
\r
192 #define mainNO_ERROR_CHECK_TASK_PERIOD ( 3000UL / portTICK_RATE_MS )
\r
194 /* The period at which the check timer will expire, in ms, if an error has been
\r
195 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
196 in ticks using the portTICK_RATE_MS constant. */
\r
197 #define mainERROR_CHECK_TASK_PERIOD ( 200UL / portTICK_RATE_MS )
\r
199 /* Parameters that are passed into the register check tasks solely for the
\r
200 purpose of ensuring parameters are passed into tasks correctly. */
\r
201 #define mainREG_TEST_TASK_1_PARAMETER ( ( void * ) 0x12345678 )
\r
202 #define mainREG_TEST_TASK_2_PARAMETER ( ( void * ) 0x87654321 )
\r
204 /* The base period used by the timer test tasks. */
\r
205 #define mainTIMER_TEST_PERIOD ( 50 )
\r
207 /* The length of queues used to pass characters into and out of the UART
\r
208 interrupt. Note the comments above about the UART driver being implemented in
\r
209 this way to test the kernel robustness rather than to provide a template for an
\r
210 efficient production driver. */
\r
211 #define mainUART_QUEUE_LENGTHS 10
\r
213 /*-----------------------------------------------------------*/
\r
216 * Called by main() to run the full demo (as opposed to the blinky demo) when
\r
217 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
\r
219 void main_full( void );
\r
222 * The check task, as described at the top of this file.
\r
224 static void prvCheckTask( void *pvParameters );
\r
227 * Register check tasks, and the tasks used to write over and check the contents
\r
228 * of the FPU registers, as described at the top of this file. The nature of
\r
229 * these files necessitates that they are written in an assembly file, but the
\r
230 * entry points are kept in the C file for the convenience of checking the task
\r
233 static void prvRegTestTaskEntry1( void *pvParameters );
\r
234 extern void vRegTest1Implementation( void );
\r
235 static void prvRegTestTaskEntry2( void *pvParameters );
\r
236 extern void vRegTest2Implementation( void );
\r
239 * Register commands that can be used with FreeRTOS+CLI. The commands are
\r
240 * defined in CLI-Commands.c and File-Related-CLI-Command.c respectively.
\r
242 extern void vRegisterSampleCLICommands( void );
\r
243 extern void vRegisterFileSystemCLICommands( void );
\r
246 * Creates and verifies different files on the volume, demonstrating the use of
\r
247 * various different API functions.
\r
249 extern void vCreateAndVerifySampleFiles( void );
\r
251 /*-----------------------------------------------------------*/
\r
253 /* The following two variables are used to communicate the status of the
\r
254 register check tasks to the check software timer. If the variables keep
\r
255 incrementing, then the register check tasks has not discovered any errors. If
\r
256 a variable stops incrementing, then an error has been found. */
\r
257 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
\r
259 /*-----------------------------------------------------------*/
\r
261 void main_full( void )
\r
263 /* The baud rate setting here has no effect, hence it is set to 0 to
\r
264 make that obvious. */
\r
265 xSerialPortInitMinimal( 0, mainUART_QUEUE_LENGTHS );
\r
267 /* If the file system is only going to be accessed from one task then
\r
268 F_FS_THREAD_AWARE can be set to 0 and the set of example files are created
\r
269 before the RTOS scheduler is started. If the file system is going to be
\r
270 access from more than one task then F_FS_THREAD_AWARE must be set to 1 and
\r
271 the set of sample files are created from the idle task hook function
\r
272 vApplicationIdleHook() - which is defined in this file. */
\r
273 #if ( mainINCLUDE_FAT_SL_DEMO == 1 )&& ( F_FS_THREAD_AWARE == 0 )
\r
275 /* Initialise the drive and file system, then create a few example
\r
276 files. The output from this function just goes to the stdout window,
\r
277 allowing the output to be viewed when the UDP command console is not
\r
279 vCreateAndVerifySampleFiles();
\r
283 /* Start all the other standard demo/test tasks. The have not particular
\r
284 functionality, but do demonstrate how to use the FreeRTOS API and test the
\r
286 vStartDynamicPriorityTasks();
\r
287 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
288 vCreateBlockTimeTasks();
\r
289 vStartCountingSemaphoreTasks();
\r
290 vStartGenericQueueTasks( tskIDLE_PRIORITY );
\r
291 vStartRecursiveMutexTasks();
\r
292 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
293 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
294 vStartTimerDemoTask( mainTIMER_TEST_PERIOD );
\r
295 vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );
\r
297 #if mainINCLUDE_FAT_SL_DEMO == 1
\r
299 /* Start the tasks that implements the command console on the UART, as
\r
300 described above. */
\r
301 vUARTCommandConsoleStart( mainUART_COMMAND_CONSOLE_STACK_SIZE, mainUART_COMMAND_CONSOLE_TASK_PRIORITY );
\r
303 /* Register both the standard and file system related CLI commands. */
\r
304 vRegisterSampleCLICommands();
\r
305 vRegisterFileSystemCLICommands();
\r
309 /* The COM test tasks can use the UART if the CLI is not used by the
\r
310 FAT SL demo. The COM test tasks require a UART connector to be fitted
\r
311 to the UART port. */
\r
312 vAltStartComTestTasks( mainCOM_TEST_TASK_PRIORITY, mainBAUD_RATE, mainCOM_TEST_LED );
\r
317 /* Create the register check tasks, as described at the top of this
\r
319 xTaskCreate( prvRegTestTaskEntry1, ( signed char * ) "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
320 xTaskCreate( prvRegTestTaskEntry2, ( signed char * ) "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
322 /* Create the task that performs the 'check' functionality, as described at
\r
323 the top of this file. */
\r
324 xTaskCreate( prvCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
326 /* The set of tasks created by the following function call have to be
\r
327 created last as they keep account of the number of tasks they expect to see
\r
329 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
331 /* Start the scheduler. */
\r
332 vTaskStartScheduler();
\r
334 /* If all is well, the scheduler will now be running, and the following
\r
335 line will never be reached. If the following line does execute, then
\r
336 there was either insufficient FreeRTOS heap memory available for the idle
\r
337 and/or timer tasks to be created, or vTaskStartScheduler() was called from
\r
338 User mode. See the memory management section on the FreeRTOS web site for
\r
339 more details on the FreeRTOS heap http://www.freertos.org/a00111.html. The
\r
340 mode from which main() is called is set in the C start up code and must be
\r
341 a privileged mode (not user mode). */
\r
344 /*-----------------------------------------------------------*/
\r
346 static void prvCheckTask( void *pvParameters )
\r
348 portTickType xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;
\r
349 portTickType xLastExecutionTime;
\r
350 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
\r
351 unsigned long ulErrorFound = pdFALSE;
\r
353 /* Just to stop compiler warnings. */
\r
354 ( void ) pvParameters;
\r
356 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
357 works correctly. */
\r
358 xLastExecutionTime = xTaskGetTickCount();
\r
360 /* Cycle for ever, delaying then checking all the other tasks are still
\r
361 operating without error. The onboard LED is toggled on each iteration.
\r
362 If an error is detected then the delay period is decreased from
\r
363 mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD. This has the
\r
364 effect of increasing the rate at which the onboard LED toggles, and in so
\r
365 doing gives visual feedback of the system status. */
\r
368 /* Delay until it is time to execute again. */
\r
369 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );
\r
371 /* Check all the demo tasks (other than the flash tasks) to ensure
\r
372 that they are all still running, and that none have detected an error. */
\r
373 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
375 ulErrorFound = pdTRUE;
\r
378 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
380 ulErrorFound = pdTRUE;
\r
383 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
385 ulErrorFound = pdTRUE;
\r
388 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
390 ulErrorFound = pdTRUE;
\r
393 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
395 ulErrorFound = pdTRUE;
\r
398 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
400 ulErrorFound = pdTRUE;
\r
403 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
405 ulErrorFound = pdTRUE;
\r
408 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
410 ulErrorFound = pdTRUE;
\r
413 if( xAreTimerDemoTasksStillRunning( ( portTickType ) mainNO_ERROR_CHECK_TASK_PERIOD ) != pdPASS )
\r
415 ulErrorFound = pdTRUE;
\r
418 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
420 ulErrorFound = pdTRUE;
\r
423 if( xIsQueueOverwriteTaskStillRunning() != pdPASS )
\r
425 ulErrorFound = pdTRUE;
\r
428 #if mainINCLUDE_FAT_SL_DEMO == 0
\r
430 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
432 ulErrorFound = pdTRUE;
\r
437 /* Check that the register test 1 task is still running. */
\r
438 if( ulLastRegTest1Value == ulRegTest1LoopCounter )
\r
440 ulErrorFound = pdTRUE;
\r
442 ulLastRegTest1Value = ulRegTest1LoopCounter;
\r
444 /* Check that the register test 2 task is still running. */
\r
445 if( ulLastRegTest2Value == ulRegTest2LoopCounter )
\r
447 ulErrorFound = pdTRUE;
\r
449 ulLastRegTest2Value = ulRegTest2LoopCounter;
\r
451 /* Toggle the check LED to give an indication of the system status. If
\r
452 the LED toggles every mainNO_ERROR_CHECK_TASK_PERIOD milliseconds then
\r
453 everything is ok. A faster toggle indicates an error. */
\r
454 vParTestToggleLED( mainCHECK_LED );
\r
456 if( ulErrorFound != pdFALSE )
\r
458 /* An error has been detected in one of the tasks - flash the LED
\r
459 at a higher frequency to give visible feedback that something has
\r
460 gone wrong (it might just be that the loop back connector required
\r
461 by the comtest tasks has not been fitted). */
\r
462 xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;
\r
466 /*-----------------------------------------------------------*/
\r
468 static void prvRegTestTaskEntry1( void *pvParameters )
\r
470 /* Although the regtest task is written in assembler, its entry point is
\r
471 written in C for convenience of checking the task parameter is being passed
\r
473 if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )
\r
475 /* The reg test task also tests the floating point registers. Tasks
\r
476 that use the floating point unit must call vPortTaskUsesFPU() before
\r
477 any floating point instructions are executed. */
\r
478 vPortTaskUsesFPU();
\r
480 /* Start the part of the test that is written in assembler. */
\r
481 vRegTest1Implementation();
\r
484 /* The following line will only execute if the task parameter is found to
\r
485 be incorrect. The check timer will detect that the regtest loop counter is
\r
486 not being incremented and flag an error. */
\r
487 vTaskDelete( NULL );
\r
489 /*-----------------------------------------------------------*/
\r
491 static void prvRegTestTaskEntry2( void *pvParameters )
\r
493 /* Although the regtest task is written in assembler, its entry point is
\r
494 written in C for convenience of checking the task parameter is being passed
\r
496 if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )
\r
498 /* The reg test task also tests the floating point registers. Tasks
\r
499 that use the floating point unit must call vPortTaskUsesFPU() before
\r
500 any floating point instructions are executed. */
\r
501 vPortTaskUsesFPU();
\r
503 /* Start the part of the test that is written in assembler. */
\r
504 vRegTest2Implementation();
\r
507 /* The following line will only execute if the task parameter is found to
\r
508 be incorrect. The check timer will detect that the regtest loop counter is
\r
509 not being incremented and flag an error. */
\r
510 vTaskDelete( NULL );
\r
512 /*-----------------------------------------------------------*/
\r