2 FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /******************************************************************************
\r
71 * >>>>>> NOTE 1: <<<<<<
\r
73 * main() can be configured to create either a very simple LED flasher demo, or
\r
74 * a more comprehensive test/demo application.
\r
76 * To create a very simple LED flasher example, set the
\r
77 * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY constant (defined below) to 1. When
\r
78 * this is done, only the standard demo flash tasks are created. The standard
\r
79 * demo flash example creates three tasks, each of which toggle an LED at a
\r
80 * fixed but different frequency.
\r
82 * To create a more comprehensive test and demo application, set
\r
83 * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 0.
\r
84 ******************************************************************************
\r
86 * main() creates all the demo application tasks and software timers, then starts
\r
87 * the scheduler. The web documentation provides more details of the standard
\r
88 * demo application tasks, which provide no particular functionality, but do
\r
89 * provide a good example of how to use the FreeRTOS API.
\r
91 * In addition to the standard demo tasks, the following tasks and tests are
\r
92 * defined and/or created within this file:
\r
94 * "Reg test" tasks - These fill both the core and floating point registers with
\r
95 * known values, then check that each register maintains its expected value for
\r
96 * the lifetime of the task. Each task uses a different set of values. The reg
\r
97 * test tasks execute with a very low priority, so get preempted very
\r
98 * frequently. A register containing an unexpected value is indicative of an
\r
99 * error in the context switching mechanism.
\r
101 * "Check" timer - The check software timer period is initially set to three
\r
102 * seconds. The callback function associated with the check software timer
\r
103 * checks that all the standard demo tasks, and the register check tasks, are
\r
104 * not only still executing, but are executing without reporting any errors. If
\r
105 * the check software timer discovers that a task has either stalled, or
\r
106 * reported an error, then it changes its own execution period from the initial
\r
107 * three seconds, to just 200ms. The check software timer callback function
\r
108 * also toggles an LED each time it is called. This provides a visual
\r
109 * indication of the system status: If the LED toggles every three seconds,
\r
110 * then no issues have been discovered. If the LED toggles every 200ms, then
\r
111 * an issue has been discovered with at least one task.
\r
114 /* Standard includes. */
\r
117 /* Kernel includes. */
\r
118 #include "FreeRTOS.h"
\r
120 #include "timers.h"
\r
121 #include "semphr.h"
\r
123 /* Demo application includes. */
\r
124 #include "partest.h"
\r
127 #include "integer.h"
\r
129 #include "semtest.h"
\r
130 #include "dynamic.h"
\r
131 #include "BlockQ.h"
\r
132 #include "blocktim.h"
\r
133 #include "countsem.h"
\r
134 #include "GenQTest.h"
\r
135 #include "recmutex.h"
\r
138 /* Hardware includes. */
\r
139 #include "platform_config.h"
\r
141 /* Priorities for the demo application tasks. */
\r
142 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1UL )
\r
143 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2UL )
\r
144 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1UL )
\r
145 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2UL )
\r
146 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3UL )
\r
147 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
149 /* The LED used by the check timer. */
\r
150 #define mainCHECK_LED ( 3UL )
\r
152 /* A block time of zero simply means "don't block". */
\r
153 #define mainDONT_BLOCK ( 0UL )
\r
155 /* The period after which the check timer will expire, in ms, provided no errors
\r
156 have been reported by any of the standard demo tasks. ms are converted to the
\r
157 equivalent in ticks using the portTICK_PERIOD_MS constant. */
\r
158 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_PERIOD_MS )
\r
160 /* The period at which the check timer will expire, in ms, if an error has been
\r
161 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
162 in ticks using the portTICK_PERIOD_MS constant. */
\r
163 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_PERIOD_MS )
\r
165 /* Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 1 to create a simple demo.
\r
166 Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 0 to create a much more
\r
167 comprehensive test application. See the comments at the top of this file, and
\r
168 the documentation page on the http://www.FreeRTOS.org web site for more
\r
170 #define mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY 0
\r
172 /*-----------------------------------------------------------*/
\r
175 * Set up the hardware ready to run this demo.
\r
177 static void prvSetupHardware( void );
\r
180 * The check timer callback function, as described at the top of this file.
\r
182 static void prvCheckTimerCallback( TimerHandle_t xTimer );
\r
185 * Register check tasks, and the tasks used to write over and check the contents
\r
186 * of the FPU registers, as described at the top of this file. The nature of
\r
187 * these files necessitates that they are written in an assembly file.
\r
189 extern void vRegTest1Task( void *pvParameters );
\r
190 extern void vRegTest2Task( void *pvParameters );
\r
191 extern void vRegTestClearFlopRegistersToParameterValue( unsigned long ulValue );
\r
192 extern unsigned long ulRegTestCheckFlopRegistersContainParameterValue( unsigned long ulValue );
\r
195 * This file can be used to create either a simple LED flasher example, or a
\r
196 * comprehensive test/demo application - depending on the setting of the
\r
197 * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY constant defined above. If
\r
198 * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 1, then the following
\r
199 * function will create a lot of additional tasks and a software timer. If
\r
200 * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 0, then the following
\r
201 * function will do nothing.
\r
203 static void prvOptionallyCreateComprehensveTestApplication( void );
\r
205 /*-----------------------------------------------------------*/
\r
207 /* The following two variables are used to communicate the status of the
\r
208 register check tasks to the check software timer. If the variables keep
\r
209 incrementing, then the register check tasks have not discovered any errors. If
\r
210 a variable stops incrementing, then an error has been found. */
\r
211 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
\r
213 /*-----------------------------------------------------------*/
\r
217 /* Configure the hardware ready to run the test. */
\r
218 prvSetupHardware();
\r
220 /* Start standard demo/test application flash tasks. See the comments at
\r
221 the top of this file. The LED flash tasks are always created. The other
\r
222 tasks are only created if mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to
\r
223 0 (at the top of this file). See the comments at the top of this file for
\r
224 more information. */
\r
225 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
227 /* The following function will only create more tasks and timers if
\r
228 mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 0 (at the top of this
\r
229 file). See the comments at the top of this file for more information. */
\r
230 prvOptionallyCreateComprehensveTestApplication();
\r
232 /* Start the scheduler. */
\r
233 vTaskStartScheduler();
\r
235 /* Infinite loop */
\r
238 /*-----------------------------------------------------------*/
\r
240 static void prvCheckTimerCallback( TimerHandle_t xTimer )
\r
242 static long lChangedTimerPeriodAlready = pdFALSE;
\r
243 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
\r
244 unsigned long ulErrorFound = pdFALSE;
\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
249 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
251 ulErrorFound |= 0x01UL << 0UL;
\r
254 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
256 ulErrorFound |= 0x01UL << 1UL;
\r
259 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
261 ulErrorFound |= 0x01UL << 2UL;
\r
264 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
266 ulErrorFound |= 0x01UL << 3UL;
\r
269 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
271 ulErrorFound |= 0x01UL << 4UL;
\r
274 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
276 ulErrorFound |= 0x01UL << 5UL;
\r
279 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
281 ulErrorFound |= 0x01UL << 6UL;
\r
284 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
286 ulErrorFound |= 0x01UL << 7UL;
\r
289 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
291 ulErrorFound |= 0x01UL << 8UL;
\r
294 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
296 ulErrorFound |= 0x01UL << 9UL;
\r
299 /* Check that the register test 1 task is still running. */
\r
300 if( ulLastRegTest1Value == ulRegTest1LoopCounter )
\r
302 ulErrorFound |= 0x01UL << 10UL;
\r
304 ulLastRegTest1Value = ulRegTest1LoopCounter;
\r
306 /* Check that the register test 2 task is still running. */
\r
307 if( ulLastRegTest2Value == ulRegTest2LoopCounter )
\r
309 ulErrorFound |= 0x01UL << 11UL;
\r
311 ulLastRegTest2Value = ulRegTest2LoopCounter;
\r
313 /* Toggle the check LED to give an indication of the system status. If
\r
314 the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then
\r
315 everything is ok. A faster toggle indicates an error. */
\r
316 vParTestToggleLED( mainCHECK_LED );
\r
318 /* Have any errors been latch in ulErrorFound? If so, shorten the
\r
319 period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.
\r
320 This will result in an increase in the rate at which mainCHECK_LED
\r
322 if( ulErrorFound != pdFALSE )
\r
324 if( lChangedTimerPeriodAlready == pdFALSE )
\r
326 lChangedTimerPeriodAlready = pdTRUE;
\r
328 /* This call to xTimerChangePeriod() uses a zero block time.
\r
329 Functions called from inside of a timer callback function must
\r
330 *never* attempt to block. */
\r
331 xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
335 /*-----------------------------------------------------------*/
\r
337 static void prvSetupHardware( void )
\r
339 extern void Hitex_CGU_Init( void );
\r
341 /* Setup system (clock, PLL and Flash configuration) */
\r
344 /* Wind the clock speed up in steps to its maximum. */
\r
347 /* Ensure all priority bits are assigned as preemption priority bits. */
\r
348 NVIC_SetPriorityGrouping( 0 );
\r
350 /* Setup the LED outputs. */
\r
351 vParTestInitialise();
\r
353 /*-----------------------------------------------------------*/
\r
355 static void prvOptionallyCreateComprehensveTestApplication( void )
\r
357 #if ( mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY == 0 )
\r
359 TimerHandle_t xCheckTimer = NULL;
\r
361 /* Start all the other standard demo/test tasks. */
\r
362 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
363 vStartDynamicPriorityTasks();
\r
364 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
365 vCreateBlockTimeTasks();
\r
366 vStartCountingSemaphoreTasks();
\r
367 vStartGenericQueueTasks( tskIDLE_PRIORITY );
\r
368 vStartRecursiveMutexTasks();
\r
369 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
370 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
372 /* Most importantly, start the tasks that use the FPU. */
\r
373 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
375 /* Create the register check tasks, as described at the top of this
\r
377 xTaskCreate( vRegTest1Task, "Reg1", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );
\r
378 xTaskCreate( vRegTest2Task, "Reg2", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );
\r
380 /* Create the software timer that performs the 'check' functionality,
\r
381 as described at the top of this file. */
\r
382 xCheckTimer = xTimerCreate( "CheckTimer", /* A text name, purely to help debugging. */
\r
383 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
384 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
385 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
386 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
389 if( xCheckTimer != NULL )
\r
391 xTimerStart( xCheckTimer, mainDONT_BLOCK );
\r
394 /* This task has to be created last as it keeps account of the number of
\r
395 tasks it expects to see running. */
\r
396 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
398 #else /* mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY */
\r
400 /* Just to prevent compiler warnings when the configuration options are
\r
401 set such that these static functions are not used. */
\r
402 ( void ) vRegTest1Task;
\r
403 ( void ) vRegTest2Task;
\r
404 ( void ) prvCheckTimerCallback;
\r
405 ( void ) prvSetupNestedFPUInterruptsTest;
\r
407 #endif /* mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY */
\r
409 /*-----------------------------------------------------------*/
\r
411 void vApplicationMallocFailedHook( void )
\r
413 /* vApplicationMallocFailedHook() will only be called if
\r
414 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
415 function that will get called if a call to pvPortMalloc() fails.
\r
416 pvPortMalloc() is called internally by the kernel whenever a task, queue,
\r
417 timer or semaphore is created. It is also called by various parts of the
\r
418 demo application. If heap_1.c or heap_2.c are used, then the size of the
\r
419 heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
\r
420 FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
\r
421 to query the size of free heap space that remains (although it does not
\r
422 provide information on how the remaining heap might be fragmented). */
\r
423 taskDISABLE_INTERRUPTS();
\r
426 /*-----------------------------------------------------------*/
\r
428 void vApplicationIdleHook( void )
\r
430 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
431 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
432 task. It is essential that code added to this hook function never attempts
\r
433 to block in any way (for example, call xQueueReceive() with a block time
\r
434 specified, or call vTaskDelay()). If the application makes use of the
\r
435 vTaskDelete() API function (as this demo application does) then it is also
\r
436 important that vApplicationIdleHook() is permitted to return to its calling
\r
437 function, because it is the responsibility of the idle task to clean up
\r
438 memory allocated by the kernel to any task that has since been deleted. */
\r
440 /*-----------------------------------------------------------*/
\r
442 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
444 ( void ) pcTaskName;
\r
447 /* Run time stack overflow checking is performed if
\r
448 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
449 function is called if a stack overflow is detected. */
\r
450 taskDISABLE_INTERRUPTS();
\r
453 /*-----------------------------------------------------------*/
\r
455 void vApplicationTickHook( void )
\r
457 /* This function will be called by each tick interrupt if
\r
458 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
\r
459 added here, but the tick hook is called from an interrupt context, so
\r
460 code must not attempt to block, and only the interrupt safe FreeRTOS API
\r
461 functions can be used (those that end in FromISR()). */
\r
463 /*-----------------------------------------------------------*/
\r