2 FreeRTOS V7.5.2 - 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
75 ******************************************************************************
\r
77 * main_full() creates a set of standard demo tasks, some application specific
\r
78 * tasks, and four timers. It then starts the scheduler. The web documentation
\r
79 * provides more details of the standard demo application tasks, which provide
\r
80 * no particular functionality, but do provide a good example of how to use the
\r
83 * In addition to the standard demo tasks, the following tasks and timer are
\r
84 * defined and/or created within this file:
\r
86 * "Reg test" tasks - These fill the registers with known values, then check
\r
87 * that each register maintains its expected value for the lifetime of the
\r
88 * task. Each task uses a different set of values. The reg test tasks execute
\r
89 * with a very low priority, so get preempted very frequently. A register
\r
90 * containing an unexpected value is indicative of an error in the context
\r
91 * switching mechanism.
\r
93 * "Flash timers" - A software timer callback function is defined that does
\r
94 * nothing but toggle an LED. Three software timers are created that each
\r
95 * use the same callback function, but each toggles a different LED at a
\r
96 * different frequency. One software timer uses LED1, another LED2 and the
\r
99 * "Check" software timer - The check timer period is initially set to three
\r
100 * seconds. Its callback function checks that all the standard demo tasks, and
\r
101 * the register check tasks, are not only still executing, but are executing
\r
102 * without reporting any errors. If the check timer callback discovers that a
\r
103 * task has either stalled, or reported an error, then it changes the period of
\r
104 * the check timer from the initial three seconds, to just 200ms. The callback
\r
105 * function also toggles LED 4 each time it is called. This provides a visual
\r
106 * indication of the system status: If the LED toggles every three seconds,
\r
107 * then no issues have been discovered. If the LED toggles every 200ms, then
\r
108 * an issue has been discovered with at least one task.
\r
111 /* Kernel includes. */
\r
112 #include "FreeRTOS.h"
\r
115 #include "timers.h"
\r
117 /* Common demo includes. */
\r
118 #include "blocktim.h"
\r
119 #include "countsem.h"
\r
120 #include "recmutex.h"
\r
121 #include "ParTest.h"
\r
122 #include "dynamic.h"
\r
124 /* The period after which the check timer will expire provided no errors have
\r
125 been reported by any of the standard demo tasks. ms are converted to the
\r
126 equivalent in ticks using the portTICK_RATE_MS constant. */
\r
127 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_RATE_MS )
\r
129 /* The period at which the check timer will expire if an error has been
\r
130 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
131 in ticks using the portTICK_RATE_MS constant. */
\r
132 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_RATE_MS )
\r
134 /* A block time of zero simply means "don't block". */
\r
135 #define mainDONT_BLOCK ( 0UL )
\r
137 /* The base toggle rate used by the flash timers. Each toggle rate is a
\r
138 multiple of this. */
\r
139 #define mainFLASH_TIMER_BASE_RATE ( 200UL / portTICK_RATE_MS )
\r
141 /* The LED toggle by the check timer. */
\r
142 #define mainCHECK_LED ( 3 )
\r
143 /*-----------------------------------------------------------*/
\r
146 * Register check tasks, as described at the top of this file. The nature of
\r
147 * these files necessitates that they are written in an assembly.
\r
149 extern void vRegTest1Task( void *pvParameters );
\r
150 extern void vRegTest2Task( void *pvParameters );
\r
153 * The hardware only has a single LED. Simply toggle it.
\r
155 extern void vMainToggleLED( void );
\r
158 * The check timer callback function, as described at the top of this file.
\r
160 static void prvCheckTimerCallback( xTimerHandle xTimer );
\r
163 * The flash timer callback function, as described at the top of this file.
\r
164 * This callback function is assigned to three separate software timers.
\r
166 static void prvFlashTimerCallback( xTimerHandle xTimer );
\r
169 * Called by main() to create the comprehensive test/demo application if
\r
170 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.
\r
172 void main_full( void );
\r
174 /*-----------------------------------------------------------*/
\r
176 /* The following two variables are used to communicate the status of the
\r
177 register check tasks to the check software timer. If the variables keep
\r
178 incrementing, then the register check tasks has not discovered any errors. If
\r
179 a variable stops incrementing, then an error has been found. */
\r
180 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
\r
182 /*-----------------------------------------------------------*/
\r
184 void main_full( void )
\r
186 xTimerHandle xTimer = NULL;
\r
187 unsigned long ulTimer;
\r
188 const unsigned long ulTimersToCreate = 3L;
\r
189 /* The register test tasks are asm functions that don't use a stack. The
\r
190 stack allocated just has to be large enough to hold the task context, and
\r
191 for the additional required for the stack overflow checking to work (if
\r
193 const size_t xRegTestStackSize = 25U;
\r
195 /* Create the standard demo tasks */
\r
196 vCreateBlockTimeTasks();
\r
197 vStartCountingSemaphoreTasks();
\r
198 vStartRecursiveMutexTasks();
\r
199 vStartDynamicPriorityTasks();
\r
201 /* Create the register test tasks as described at the top of this file.
\r
202 These are naked functions that don't use any stack. A stack still has
\r
203 to be allocated to hold the task context. */
\r
204 xTaskCreate( vRegTest1Task, /* Function that implements the task. */
\r
205 ( signed char * ) "Reg1", /* Text name of the task. */
\r
206 xRegTestStackSize, /* Stack allocated to the task. */
\r
207 NULL, /* The task parameter is not used. */
\r
208 tskIDLE_PRIORITY, /* The priority to assign to the task. */
\r
209 NULL ); /* Don't receive a handle back, it is not needed. */
\r
211 xTaskCreate( vRegTest2Task, /* Function that implements the task. */
\r
212 ( signed char * ) "Reg2", /* Text name of the task. */
\r
213 xRegTestStackSize, /* Stack allocated to the task. */
\r
214 NULL, /* The task parameter is not used. */
\r
215 tskIDLE_PRIORITY, /* The priority to assign to the task. */
\r
216 NULL ); /* Don't receive a handle back, it is not needed. */
\r
218 /* Create the three flash timers. */
\r
219 for( ulTimer = 0UL; ulTimer < ulTimersToCreate; ulTimer++ )
\r
221 xTimer = xTimerCreate( ( const signed char * ) "FlashTimer", /* A text name, purely to help debugging. */
\r
222 ( mainFLASH_TIMER_BASE_RATE * ( ulTimer + 1UL ) ), /* The timer period, in this case 3000ms (3s). */
\r
223 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
224 ( void * ) ulTimer, /* The ID is used to hold the number of the LED that will be flashed. */
\r
225 prvFlashTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
228 if( xTimer != NULL )
\r
230 xTimerStart( xTimer, mainDONT_BLOCK );
\r
234 /* Create the software timer that performs the 'check' functionality,
\r
235 as described at the top of this file. */
\r
236 xTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */
\r
237 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
238 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
239 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
240 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
243 /* If the software timer was created successfully, start it. It won't
\r
244 actually start running until the scheduler starts. A block time of
\r
245 zero is used in this call, although any value could be used as the block
\r
246 time will be ignored because the scheduler has not started yet. */
\r
247 if( xTimer != NULL )
\r
249 xTimerStart( xTimer, mainDONT_BLOCK );
\r
252 /* Start the kernel. From here on, only tasks and interrupts will run. */
\r
253 vTaskStartScheduler();
\r
255 /* If all is well, the scheduler will now be running, and the following
\r
256 line will never be reached. If the following line does execute, then there
\r
257 was insufficient FreeRTOS heap memory available for the idle and/or timer
\r
258 tasks to be created. See the memory management section on the FreeRTOS web
\r
259 site, or the FreeRTOS tutorial books for more details. */
\r
262 /*-----------------------------------------------------------*/
\r
264 /* See the description at the top of this file. */
\r
265 static void prvCheckTimerCallback( xTimerHandle xTimer )
\r
267 static long lChangedTimerPeriodAlready = pdFALSE;
\r
268 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
\r
269 unsigned long ulErrorFound = pdFALSE;
\r
271 /* Check all the demo and test tasks to ensure that they are all still
\r
272 running, and that none have detected an error. */
\r
273 if( xAreDynamicPriorityTasksStillRunning() != pdPASS )
\r
275 ulErrorFound |= ( 0x01UL << 0UL );
\r
278 if( xAreBlockTimeTestTasksStillRunning() != pdPASS )
\r
280 ulErrorFound |= ( 0x01UL << 1UL );
\r
283 if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )
\r
285 ulErrorFound |= ( 0x01UL << 2UL );
\r
288 if( xAreRecursiveMutexTasksStillRunning() != pdPASS )
\r
290 ulErrorFound |= ( 0x01UL << 3UL );
\r
293 /* Check that the register test 1 task is still running. */
\r
294 if( ulLastRegTest1Value == ulRegTest1LoopCounter )
\r
296 ulErrorFound |= ( 0x01UL << 4UL );
\r
298 ulLastRegTest1Value = ulRegTest1LoopCounter;
\r
300 /* Check that the register test 2 task is still running. */
\r
301 if( ulLastRegTest2Value == ulRegTest2LoopCounter )
\r
303 ulErrorFound |= ( 0x01UL << 5UL );
\r
305 ulLastRegTest2Value = ulRegTest2LoopCounter;
\r
307 /* Toggle the check LED to give an indication of the system status. If
\r
308 the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then
\r
309 everything is ok. A faster toggle indicates an error. */
\r
310 vParTestToggleLED( mainCHECK_LED );
\r
312 /* Have any errors been latched in ulErrorFound? If so, shorten the
\r
313 period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.
\r
314 This will result in an increase in the rate at which mainCHECK_LED
\r
316 if( ulErrorFound != pdFALSE )
\r
318 if( lChangedTimerPeriodAlready == pdFALSE )
\r
320 lChangedTimerPeriodAlready = pdTRUE;
\r
322 /* This call to xTimerChangePeriod() uses a zero block time.
\r
323 Functions called from inside of a timer callback function must
\r
324 *never* attempt to block. */
\r
325 xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
329 /*-----------------------------------------------------------*/
\r
331 static void prvFlashTimerCallback( xTimerHandle xTimer )
\r
333 unsigned long ulLED;
\r
335 /* This callback function is assigned to three separate software timers.
\r
336 Each timer toggles a different LED. Obtain the number of the LED that
\r
337 this timer is toggling. */
\r
338 ulLED = ( unsigned long ) pvTimerGetTimerID( xTimer );
\r
340 /* Toggle the LED. */
\r
341 vParTestToggleLED( ulLED );
\r