2 FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /******************************************************************************
\r
67 * NOTE 1: This project provides two demo applications. A simple blinky style
\r
68 * project, and a more comprehensive test and demo application. The
\r
69 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
\r
70 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
\r
71 * in main.c. This file implements the comprehensive test and demo version.
\r
73 * NOTE 2: This file only contains the source code that is specific to the
\r
74 * full demo. Generic functions, such FreeRTOS hook functions, and functions
\r
75 * required to configure the hardware, along with an example of how to write an
\r
76 * interrupt service routine, are defined in main.c.
\r
77 ******************************************************************************
\r
79 * main_full() creates all the demo application tasks and two software timers,
\r
80 * then starts the scheduler. The web documentation provides more details of
\r
81 * the standard demo application tasks, which provide no particular
\r
82 * functionality, but do provide a good example of how to use the FreeRTOS API.
\r
84 * In addition to the standard demo tasks, the following tasks, tests and
\r
85 * timers are created within this file:
\r
87 * "Reg test" tasks - These fill the registers with known values, then check
\r
88 * that each register still contains its expected value. Each task uses a
\r
89 * different set of values. The reg test tasks execute with a very low priority,
\r
90 * so get preempted very frequently. A register containing an unexpected value
\r
91 * is indicative of an error in the context switching mechanism.
\r
93 * The "Demo" Timer and Callback Function:
\r
94 * The demo timer callback function does nothing more than increment a variable.
\r
95 * The period of the demo timer is set relative to the period of the check timer
\r
96 * (described below). This allows the check timer to know how many times the
\r
97 * demo timer callback function should execute between each execution of the
\r
98 * check timer callback function. The variable incremented in the demo timer
\r
99 * callback function is used to determine how many times the callback function
\r
102 * The "Check" Timer and Callback Function:
\r
103 * The check timer period is initially set to three seconds. The check timer
\r
104 * callback function checks that all the standard demo tasks, the reg test
\r
105 * tasks, and the demo timer are not only still executing, but are executing
\r
106 * without reporting any errors. If the check timer discovers that a task or
\r
107 * timer has stalled, or reported an error, then it changes its own period from
\r
108 * the initial three seconds, to just 200ms. The check timer callback function
\r
109 * also toggles an LED each time it is called. This provides a visual
\r
110 * indication of the system status: If the LED toggles every three seconds,
\r
111 * then no issues have been discovered. If the LED toggles every 200ms, then
\r
112 * an issue has been discovered with at least one task.
\r
114 * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON
\r
115 * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO
\r
116 * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!
\r
120 /* Scheduler include files. */
\r
121 #include "FreeRTOS.h"
\r
123 #include "timers.h"
\r
125 /* Standard demo includes. */
\r
126 #include "dynamic.h"
\r
128 #include "blocktim.h"
\r
130 /* Hardware includes. */
\r
131 #include "demo_specific_io.h"
\r
133 /* The period at which the check timer will expire, in ms, provided no errors
\r
134 have been reported by any of the standard demo tasks. ms are converted to the
\r
135 equivalent in ticks using the portTICK_PERIOD_MS constant. */
\r
136 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_PERIOD_MS )
\r
138 /* The period at which the check timer will expire, in ms, if an error has been
\r
139 reported in one of the standard demo tasks, the check tasks, or the demo timer.
\r
140 ms are converted to the equivalent in ticks using the portTICK_PERIOD_MS
\r
142 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_PERIOD_MS )
\r
144 /* These two definitions are used to set the period of the demo timer. The demo
\r
145 timer period is always relative to the check timer period, so the check timer
\r
146 can determine if the demo timer has expired the expected number of times between
\r
147 its own executions. */
\r
148 #define mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT ( 100UL )
\r
149 #define mainDEMO_TIMER_PERIOD_MS ( mainCHECK_TIMER_PERIOD_MS / mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT )
\r
151 /* A block time of zero simply means "don't block". */
\r
152 #define mainDONT_BLOCK ( 0U )
\r
154 /* Values that are passed as parameters into the reg test tasks (purely to
\r
155 ensure task parameters are passed correctly). */
\r
156 #define mainREG_TEST_1_PARAMETER ( ( void * ) 0x1234 )
\r
157 #define mainREG_TEST_2_PARAMETER ( ( void * ) 0x5678 )
\r
159 /*-----------------------------------------------------------*/
\r
162 * The 'check' timer callback function, as described at the top of this file.
\r
164 static void prvCheckTimerCallback( TimerHandle_t xTimer );
\r
167 * The 'demo' timer callback function, as described at the top of this file.
\r
169 static void prvDemoTimerCallback( TimerHandle_t xTimer );
\r
172 * Functions that define the RegTest tasks, as described at the top of this
\r
173 * file. The RegTest tasks are written (necessarily) in assembler. Their
\r
174 * entry points are written in C to allow for easy checking of the task
\r
175 * parameter values.
\r
177 extern void vRegTest1Task( void );
\r
178 extern void vRegTest2Task( void );
\r
179 static void prvRegTest1Entry( void *pvParameters );
\r
180 static void prvRegTest2Entry( void *pvParameters );
\r
183 * Called if a RegTest task discovers an error as a mechanism to stop the
\r
184 * tasks loop counter incrementing (so the check task can detect that an
\r
187 void vRegTestError( void );
\r
190 * Called by main() to create the more comprehensive application if
\r
191 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
\r
193 void main_full( void );
\r
195 /*-----------------------------------------------------------*/
\r
197 /* Variables that are incremented on each cycle of the two reg tests to allow
\r
198 the check timer to know that they are still executing. */
\r
199 unsigned short usRegTest1LoopCounter = 0, usRegTest2LoopCounter;
\r
201 /* The check timer. This uses prvCheckTimerCallback() as its callback
\r
203 static TimerHandle_t xCheckTimer = NULL;
\r
205 /* The demo timer. This uses prvDemoTimerCallback() as its callback function. */
\r
206 static TimerHandle_t xDemoTimer = NULL;
\r
208 /* This variable is incremented each time the demo timer expires. */
\r
209 static volatile unsigned long ulDemoSoftwareTimerCounter = 0UL;
\r
211 /*-----------------------------------------------------------*/
\r
213 void main_full( void )
\r
215 /* Creates all the tasks and timers, then starts the scheduler. */
\r
217 /* First create the 'standard demo' tasks. These are used to demonstrate
\r
218 API functions being used and also to test the kernel port. More information
\r
219 is provided on the FreeRTOS.org WEB site. */
\r
220 vStartDynamicPriorityTasks();
\r
221 vStartPolledQueueTasks( tskIDLE_PRIORITY );
\r
222 vCreateBlockTimeTasks();
\r
224 /* Create the RegTest tasks as described at the top of this file. */
\r
225 xTaskCreate( prvRegTest1Entry, /* The function that implements the task. */
\r
226 "Reg1", /* Text name for the task - to assist debugging only, not used by the kernel. */
\r
227 configMINIMAL_STACK_SIZE, /* The size of the stack allocated to the task (in words, not bytes). */
\r
228 mainREG_TEST_1_PARAMETER, /* The parameter passed into the task. */
\r
229 tskIDLE_PRIORITY, /* The priority at which the task will execute. */
\r
230 NULL ); /* Used to pass the handle of the created task out to the function caller - not used in this case. */
\r
232 xTaskCreate( prvRegTest2Entry, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
234 /* Create the software timer that performs the 'check' functionality,
\r
235 as described at the top of this file. */
\r
236 xCheckTimer = xTimerCreate( "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 /* Create the software timer that just increments a variable for demo
\r
245 xDemoTimer = xTimerCreate( "DemoTimer",/* A text name, purely to help debugging. */
\r
246 ( mainDEMO_TIMER_PERIOD_MS ), /* The timer period, in this case it is always calculated relative to the check timer period (see the definition of mainDEMO_TIMER_PERIOD_MS). */
\r
247 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
248 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
249 prvDemoTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
252 /* Start both the check timer and the demo timer. The timers won't actually
\r
253 start until the scheduler is started. */
\r
254 xTimerStart( xCheckTimer, mainDONT_BLOCK );
\r
255 xTimerStart( xDemoTimer, mainDONT_BLOCK );
\r
257 /* Finally start the scheduler running. */
\r
258 vTaskStartScheduler();
\r
260 /* If all is well execution will never reach here as the scheduler will be
\r
261 running. If this null loop is reached then it is likely there was
\r
262 insufficient FreeRTOS heap available for the idle task and/or timer task to
\r
263 be created. See http://www.freertos.org/a00111.html. */
\r
266 /*-----------------------------------------------------------*/
\r
268 static void prvDemoTimerCallback( TimerHandle_t xTimer )
\r
270 /* Remove compiler warning about unused parameter. */
\r
273 /* The demo timer has expired. All it does is increment a variable. The
\r
274 period of the demo timer is relative to that of the check timer, so the
\r
275 check timer knows how many times this variable should have been incremented
\r
276 between each execution of the check timer's own callback. */
\r
277 ulDemoSoftwareTimerCounter++;
\r
279 /*-----------------------------------------------------------*/
\r
281 static void prvCheckTimerCallback( TimerHandle_t xTimer )
\r
283 static portBASE_TYPE xChangedTimerPeriodAlready = pdFALSE, xErrorStatus = pdPASS;
\r
284 static unsigned short usLastRegTest1Counter = 0, usLastRegTest2Counter = 0;
\r
286 /* Remove compiler warning about unused parameter. */
\r
289 /* Inspect the status of the standard demo tasks. */
\r
290 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
292 xErrorStatus = pdFAIL;
\r
295 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
297 xErrorStatus = pdFAIL;
\r
300 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
302 xErrorStatus = pdFAIL;
\r
305 /* Indicate an error if either of the reg test loop counters have not
\r
306 incremented since the last time this function was called. */
\r
307 if( usLastRegTest1Counter == usRegTest1LoopCounter )
\r
309 xErrorStatus = pdFAIL;
\r
313 usLastRegTest1Counter = usRegTest1LoopCounter;
\r
316 if( usLastRegTest2Counter == usRegTest2LoopCounter )
\r
318 xErrorStatus = pdFAIL;
\r
322 usLastRegTest2Counter = usRegTest2LoopCounter;
\r
325 /* Ensure that the demo software timer has expired
\r
326 mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT times in between
\r
327 each call of this function. A critical section is not required to access
\r
328 ulDemoSoftwareTimerCounter as the variable is only accessed from another
\r
329 software timer callback, and only one software timer callback can be
\r
330 executing at any time. */
\r
331 if( ( ulDemoSoftwareTimerCounter < ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT - 1 ) ) ||
\r
332 ( ulDemoSoftwareTimerCounter > ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT + 1 ) )
\r
335 xErrorStatus = pdFAIL;
\r
339 ulDemoSoftwareTimerCounter = 0UL;
\r
342 if( ( xErrorStatus == pdFAIL ) && ( xChangedTimerPeriodAlready == pdFALSE ) )
\r
344 /* An error has occurred, but the timer's period has not yet been changed,
\r
345 change it now, and remember that it has been changed. Shortening the
\r
346 timer's period means the LED will toggle at a faster rate, giving a
\r
347 visible indication that something has gone wrong. */
\r
348 xChangedTimerPeriodAlready = pdTRUE;
\r
350 /* This call to xTimerChangePeriod() uses a zero block time. Functions
\r
351 called from inside of a timer callback function must *never* attempt to
\r
353 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
356 /* Toggle the LED. The toggle rate will depend on whether or not an error
\r
357 has been found in any tasks. */
\r
358 LED_BIT = !LED_BIT;
\r
360 /*-----------------------------------------------------------*/
\r
362 void vRegTestError( void )
\r
364 /* Called by both reg test tasks if an error is found. There is no way out
\r
365 of this function so the loop counter of the calling task will stop
\r
366 incrementing, which will result in the check timer signaling an error. */
\r
369 /*-----------------------------------------------------------*/
\r
371 static void prvRegTest1Entry( void *pvParameters )
\r
373 /* If the parameter has its expected value then start the first reg test
\r
374 task (this is only done to test that the RTOS port is correctly handling
\r
375 task parameters. */
\r
376 if( pvParameters == mainREG_TEST_1_PARAMETER )
\r
385 /* It is not possible to get here as neither of the two functions called
\r
386 above will ever return. */
\r
388 /*-----------------------------------------------------------*/
\r
390 static void prvRegTest2Entry( void *pvParameters )
\r
392 /* If the parameter has its expected value then start the first reg test
\r
393 task (this is only done to test that the RTOS port is correctly handling
\r
394 task parameters. */
\r
395 if( pvParameters == mainREG_TEST_2_PARAMETER )
\r
404 /* It is not possible to get here as neither of the two functions called
\r
405 above will ever return. */
\r
407 /*-----------------------------------------------------------*/
\r