2 FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
75 /******************************************************************************
\r
76 * >>>>>> NOTE 1: <<<<<<
\r
78 * main() can be configured to create either a very simple LED flasher demo, or
\r
79 * a more comprehensive test/demo application.
\r
81 * To create a very simple LED flasher example, set the
\r
82 * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY constant (defined below) to 1. When
\r
83 * this is done, only the standard demo flash tasks are created. The standard
\r
84 * demo flash example creates three tasks, each of which toggle an LED at a
\r
85 * fixed but different frequency.
\r
87 * To create a more comprehensive test and demo application, set
\r
88 * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 0.
\r
89 ******************************************************************************
\r
91 * main() creates all the demo application tasks and software timers, then starts
\r
92 * the scheduler. The web documentation provides more details of the standard
\r
93 * demo application tasks, which provide no particular functionality, but do
\r
94 * provide a good example of how to use the FreeRTOS API.
\r
96 * In addition to the standard demo tasks, the following tasks and tests are
\r
97 * defined and/or created within this file:
\r
99 * "Reg test" tasks - These fill both the core and floating point registers with
\r
100 * known values, then check that each register maintains its expected value for
\r
101 * the lifetime of the task. Each task uses a different set of values. The reg
\r
102 * test tasks execute with a very low priority, so get preempted very
\r
103 * frequently. A register containing an unexpected value is indicative of an
\r
104 * error in the context switching mechanism.
\r
106 * "Check" timer - The check software timer period is initially set to three
\r
107 * seconds. The callback function associated with the check software timer
\r
108 * checks that all the standard demo tasks, and the register check tasks, are
\r
109 * not only still executing, but are executing without reporting any errors. If
\r
110 * the check software timer discovers that a task has either stalled, or
\r
111 * reported an error, then it changes its own execution period from the initial
\r
112 * three seconds, to just 200ms. The check software timer callback function
\r
113 * also toggles an LED each time it is called. This provides a visual
\r
114 * indication of the system status: If the LED toggles every three seconds,
\r
115 * then no issues have been discovered. If the LED toggles every 200ms, then
\r
116 * an issue has been discovered with at least one task.
\r
119 /* Standard includes. */
\r
122 /* Kernel includes. */
\r
123 #include "FreeRTOS.h"
\r
125 #include "timers.h"
\r
126 #include "semphr.h"
\r
128 /* Demo application includes. */
\r
129 #include "partest.h"
\r
132 #include "integer.h"
\r
134 #include "semtest.h"
\r
135 #include "dynamic.h"
\r
136 #include "BlockQ.h"
\r
137 #include "blocktim.h"
\r
138 #include "countsem.h"
\r
139 #include "GenQTest.h"
\r
140 #include "recmutex.h"
\r
143 /* Hardware includes. */
\r
144 #include "platform_config.h"
\r
146 /* Priorities for the demo application tasks. */
\r
147 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1UL )
\r
148 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2UL )
\r
149 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1UL )
\r
150 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2UL )
\r
151 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3UL )
\r
152 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
154 /* The LED used by the check timer. */
\r
155 #define mainCHECK_LED ( 3UL )
\r
157 /* A block time of zero simply means "don't block". */
\r
158 #define mainDONT_BLOCK ( 0UL )
\r
160 /* The period after which the check timer will expire, in ms, provided no errors
\r
161 have been reported by any of the standard demo tasks. ms are converted to the
\r
162 equivalent in ticks using the portTICK_RATE_MS constant. */
\r
163 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_RATE_MS )
\r
165 /* The period at which the check timer will expire, in ms, if an error has been
\r
166 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
167 in ticks using the portTICK_RATE_MS constant. */
\r
168 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_RATE_MS )
\r
170 /* Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 1 to create a simple demo.
\r
171 Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 0 to create a much more
\r
172 comprehensive test application. See the comments at the top of this file, and
\r
173 the documentation page on the http://www.FreeRTOS.org web site for more
\r
175 #define mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY 0
\r
177 /*-----------------------------------------------------------*/
\r
180 * Set up the hardware ready to run this demo.
\r
182 static void prvSetupHardware( void );
\r
185 * The check timer callback function, as described at the top of this file.
\r
187 static void prvCheckTimerCallback( xTimerHandle xTimer );
\r
190 * Register check tasks, and the tasks used to write over and check the contents
\r
191 * of the FPU registers, as described at the top of this file. The nature of
\r
192 * these files necessitates that they are written in an assembly file.
\r
194 extern void vRegTest1Task( void *pvParameters );
\r
195 extern void vRegTest2Task( void *pvParameters );
\r
196 extern void vRegTestClearFlopRegistersToParameterValue( unsigned long ulValue );
\r
197 extern unsigned long ulRegTestCheckFlopRegistersContainParameterValue( unsigned long ulValue );
\r
200 * This file can be used to create either a simple LED flasher example, or a
\r
201 * comprehensive test/demo application - depending on the setting of the
\r
202 * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY constant defined above. If
\r
203 * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 1, then the following
\r
204 * function will create a lot of additional tasks and a software timer. If
\r
205 * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 0, then the following
\r
206 * function will do nothing.
\r
208 static void prvOptionallyCreateComprehensveTestApplication( void );
\r
210 /*-----------------------------------------------------------*/
\r
212 /* The following two variables are used to communicate the status of the
\r
213 register check tasks to the check software timer. If the variables keep
\r
214 incrementing, then the register check tasks has not discovered any errors. If
\r
215 a variable stops incrementing, then an error has been found. */
\r
216 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
\r
218 /*-----------------------------------------------------------*/
\r
222 /* Configure the hardware ready to run the test. */
\r
223 prvSetupHardware();
\r
225 /* Start standard demo/test application flash tasks. See the comments at
\r
226 the top of this file. The LED flash tasks are always created. The other
\r
227 tasks are only created if mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to
\r
228 0 (at the top of this file). See the comments at the top of this file for
\r
229 more information. */
\r
230 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
232 /* The following function will only create more tasks and timers if
\r
233 mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 0 (at the top of this
\r
234 file). See the comments at the top of this file for more information. */
\r
235 prvOptionallyCreateComprehensveTestApplication();
\r
237 /* Start the scheduler. */
\r
238 vTaskStartScheduler();
\r
240 /* Infinite loop */
\r
243 /*-----------------------------------------------------------*/
\r
245 static void prvCheckTimerCallback( xTimerHandle xTimer )
\r
247 static long lChangedTimerPeriodAlready = pdFALSE;
\r
248 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
\r
249 unsigned long ulErrorFound = pdFALSE;
\r
251 /* Check all the demo tasks (other than the flash tasks) to ensure
\r
252 that they are all still running, and that none have detected an error. */
\r
254 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
256 ulErrorFound |= 0x01UL << 0UL;
\r
259 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
261 ulErrorFound |= 0x01UL << 1UL;
\r
264 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
266 ulErrorFound |= 0x01UL << 2UL;
\r
269 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
271 ulErrorFound |= 0x01UL << 3UL;
\r
274 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
276 ulErrorFound |= 0x01UL << 4UL;
\r
279 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
281 ulErrorFound |= 0x01UL << 5UL;
\r
284 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
286 ulErrorFound |= 0x01UL << 6UL;
\r
289 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
291 ulErrorFound |= 0x01UL << 7UL;
\r
294 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
296 ulErrorFound |= 0x01UL << 8UL;
\r
299 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
301 ulErrorFound |= 0x01UL << 9UL;
\r
304 /* Check that the register test 1 task is still running. */
\r
305 if( ulLastRegTest1Value == ulRegTest1LoopCounter )
\r
307 ulErrorFound |= 0x01UL << 10UL;
\r
309 ulLastRegTest1Value = ulRegTest1LoopCounter;
\r
311 /* Check that the register test 2 task is still running. */
\r
312 if( ulLastRegTest2Value == ulRegTest2LoopCounter )
\r
314 ulErrorFound |= 0x01UL << 11UL;
\r
316 ulLastRegTest2Value = ulRegTest2LoopCounter;
\r
318 /* Toggle the check LED to give an indication of the system status. If
\r
319 the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then
\r
320 everything is ok. A faster toggle indicates an error. */
\r
321 vParTestToggleLED( mainCHECK_LED );
\r
323 /* Have any errors been latch in ulErrorFound? If so, shorten the
\r
324 period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.
\r
325 This will result in an increase in the rate at which mainCHECK_LED
\r
327 if( ulErrorFound != pdFALSE )
\r
329 if( lChangedTimerPeriodAlready == pdFALSE )
\r
331 lChangedTimerPeriodAlready = pdTRUE;
\r
333 /* This call to xTimerChangePeriod() uses a zero block time.
\r
334 Functions called from inside of a timer callback function must
\r
335 *never* attempt to block. */
\r
336 xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
340 /*-----------------------------------------------------------*/
\r
342 static void prvSetupHardware( void )
\r
344 extern void Hitex_CGU_Init( void );
\r
346 /* Setup system (clock, PLL and Flash configuration) */
\r
349 /* Wind the clock speed up in steps to its maximum. */
\r
352 /* Ensure all priority bits are assigned as preemption priority bits. */
\r
353 NVIC_SetPriorityGrouping( 0 );
\r
355 /* Setup the LED outputs. */
\r
356 vParTestInitialise();
\r
358 /*-----------------------------------------------------------*/
\r
360 static void prvOptionallyCreateComprehensveTestApplication( void )
\r
362 #if ( mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY == 0 )
\r
364 xTimerHandle xCheckTimer = NULL;
\r
366 /* Start all the other standard demo/test tasks. */
\r
367 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
368 vStartDynamicPriorityTasks();
\r
369 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
370 vCreateBlockTimeTasks();
\r
371 vStartCountingSemaphoreTasks();
\r
372 vStartGenericQueueTasks( tskIDLE_PRIORITY );
\r
373 vStartRecursiveMutexTasks();
\r
374 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
375 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
377 /* Most importantly, start the tasks that use the FPU. */
\r
378 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
380 /* Create the register check tasks, as described at the top of this
\r
382 xTaskCreate( vRegTest1Task, ( signed char * ) "Reg1", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );
\r
383 xTaskCreate( vRegTest2Task, ( signed char * ) "Reg2", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );
\r
385 /* Create the software timer that performs the 'check' functionality,
\r
386 as described at the top of this file. */
\r
387 xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */
\r
388 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
389 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
390 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
391 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
394 if( xCheckTimer != NULL )
\r
396 xTimerStart( xCheckTimer, mainDONT_BLOCK );
\r
399 /* This task has to be created last as it keeps account of the number of
\r
400 tasks it expects to see running. */
\r
401 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
403 #else /* mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY */
\r
405 /* Just to prevent compiler warnings when the configuration options are
\r
406 set such that these static functions are not used. */
\r
407 ( void ) vRegTest1Task;
\r
408 ( void ) vRegTest2Task;
\r
409 ( void ) prvCheckTimerCallback;
\r
410 ( void ) prvSetupNestedFPUInterruptsTest;
\r
412 #endif /* mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY */
\r
414 /*-----------------------------------------------------------*/
\r
416 void vApplicationMallocFailedHook( void )
\r
418 /* vApplicationMallocFailedHook() will only be called if
\r
419 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
420 function that will get called if a call to pvPortMalloc() fails.
\r
421 pvPortMalloc() is called internally by the kernel whenever a task, queue,
\r
422 timer or semaphore is created. It is also called by various parts of the
\r
423 demo application. If heap_1.c or heap_2.c are used, then the size of the
\r
424 heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
\r
425 FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
\r
426 to query the size of free heap space that remains (although it does not
\r
427 provide information on how the remaining heap might be fragmented). */
\r
428 taskDISABLE_INTERRUPTS();
\r
431 /*-----------------------------------------------------------*/
\r
433 void vApplicationIdleHook( void )
\r
435 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
436 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
437 task. It is essential that code added to this hook function never attempts
\r
438 to block in any way (for example, call xQueueReceive() with a block time
\r
439 specified, or call vTaskDelay()). If the application makes use of the
\r
440 vTaskDelete() API function (as this demo application does) then it is also
\r
441 important that vApplicationIdleHook() is permitted to return to its calling
\r
442 function, because it is the responsibility of the idle task to clean up
\r
443 memory allocated by the kernel to any task that has since been deleted. */
\r
445 /*-----------------------------------------------------------*/
\r
447 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
449 ( void ) pcTaskName;
\r
452 /* Run time stack overflow checking is performed if
\r
453 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
454 function is called if a stack overflow is detected. */
\r
455 taskDISABLE_INTERRUPTS();
\r
458 /*-----------------------------------------------------------*/
\r
460 void vApplicationTickHook( void )
\r
462 /* This function will be called by each tick interrupt if
\r
463 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
\r
464 added here, but the tick hook is called from an interrupt context, so
\r
465 code must not attempt to block, and only the interrupt safe FreeRTOS API
\r
466 functions can be used (those that end in FromISR()). */
\r
468 /*-----------------------------------------------------------*/
\r