2 FreeRTOS V7.3.0 - Copyright (C) 2012 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
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 /******************************************************************************
\r
70 * NOTE 1: This project provides two demo applications. A simple blinky style
\r
71 * project, and a more comprehensive test and demo application. The
\r
72 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
\r
73 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
\r
74 * in main.c. This file implements the comprehensive test and demo version.
\r
76 * NOTE 2: This file only contains the source code that is specific to the
\r
77 * full demo. Generic functions, such FreeRTOS hook functions, and functions
\r
78 * required to configure the hardware, are defined in main.c.
\r
79 ******************************************************************************
\r
81 * main_full() creates all the demo application tasks and software timers,
\r
82 * then starts the scheduler. The web documentation provides more details of
\r
83 * the standard demo application tasks, which provide no particular
\r
84 * functionality, but do provide a good example of how to use the FreeRTOS API.
\r
86 * In addition to the standard demo tasks, the following tasks and tests are
\r
87 * defined and/or created within this file:
\r
89 * "Check" timer - The check software timer period is set to three seconds.
\r
90 * The callback function associated with the check software timer checks that
\r
91 * all the standard demo tasks are not only still executing, but are executing
\r
92 * without reporting any errors. If the check software timer discovers that a
\r
93 * task has either stalled, or reported an error, then the error is logged and
\r
94 * the check software timer toggles the red LEDs. If an error has never been
\r
95 * latched, the check software timer toggles the green LEDs. Therefore, if the
\r
96 * system is executing correctly, the green LEDs will toggle every three
\r
97 * seconds, and if an error has ever been detected, the red LEDs will toggle
\r
98 * every three seconds.
\r
100 * "Reg test" tasks - These fill both the core and floating point registers
\r
101 * with known values, then check that each register maintains its expected
\r
102 * value for the lifetime of the tasks. Each task uses a different set of
\r
103 * values. The reg test tasks execute with a very low priority, so get
\r
104 * preempted very frequently. A register containing an unexpected value is
\r
105 * indicative of an error in the context switching mechanism.
\r
107 * "LED" software timer - The callback function associated with the LED
\r
108 * software time maintains a pattern of spinning white LEDs.
\r
110 * See the documentation page for this demo on the FreeRTOS.org web site for
\r
111 * full information, including hardware setup requirements.
\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 /* Standard demo application includes. */
\r
124 #include "integer.h"
\r
126 #include "semtest.h"
\r
127 #include "dynamic.h"
\r
128 #include "BlockQ.h"
\r
129 #include "blocktim.h"
\r
130 #include "countsem.h"
\r
131 #include "GenQTest.h"
\r
132 #include "recmutex.h"
\r
134 #include "partest.h"
\r
136 #include "serial.h"
\r
137 #include "comtest.h"
\r
139 /* Priorities for the demo application tasks. */
\r
140 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2UL )
\r
141 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1UL )
\r
142 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2UL )
\r
143 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3UL )
\r
144 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
145 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
146 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
148 /* A block time of zero simply means "don't block". */
\r
149 #define mainDONT_BLOCK ( 0UL )
\r
151 /* The period after which the check timer will expire, converted to ticks. */
\r
152 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_RATE_MS )
\r
154 /* The period after which the LED timer will expire, converted to ticks. */
\r
155 #define mainLED_TIMER_PERIOD_MS ( 75UL / portTICK_RATE_MS )
\r
157 /* Constants for the ComTest tasks. */
\r
158 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 19200 )
\r
159 #define mainCOM_TEST_LED ( 100 )
\r
161 /*-----------------------------------------------------------*/
\r
164 * The check timer callback function, as described at the top of this file.
\r
166 static void prvCheckTimerCallback( xTimerHandle xTimer );
\r
169 * The LED timer callback function, as described at the top of this file.
\r
171 static void prvLEDTimerCallback( xTimerHandle xTimer );
\r
174 * The reg test tasks, as described at the top of this file.
\r
176 extern void vRegTestTask1( void *pvParameters );
\r
177 extern void vRegTestTask2( void *pvParameters );
\r
179 /*-----------------------------------------------------------*/
\r
181 /* Variables that are incremented on each iteration of the reg test tasks -
\r
182 provided the tasks have not reported any errors. The check task inspects these
\r
183 variables to ensure they are still incrementing as expected. If a variable
\r
184 stops incrementing then it is likely that its associate task has stalled. */
\r
185 volatile unsigned long ulRegTest1Counter = 0, ulRegTest2Counter = 0;
\r
187 /*-----------------------------------------------------------*/
\r
189 void main_full( void )
\r
191 xTimerHandle xTimer = NULL;
\r
193 /* Start all the standard demo/test tasks. These have not particular
\r
194 functionality, but do demonstrate how to use the FreeRTOS API, and test the
\r
196 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
197 vStartDynamicPriorityTasks();
\r
198 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
199 vCreateBlockTimeTasks();
\r
200 vStartCountingSemaphoreTasks();
\r
201 vStartGenericQueueTasks( tskIDLE_PRIORITY );
\r
202 vStartRecursiveMutexTasks();
\r
203 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
204 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
205 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
206 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
208 /* Create the register test tasks, as described at the top of this file. */
\r
209 xTaskCreate( vRegTestTask1, ( const signed char * ) "Reg1...", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
210 xTaskCreate( vRegTestTask2, ( const signed char * ) "Reg2...", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
213 /* Create the software timer that performs the 'check' functionality,
\r
214 as described at the top of this file. */
\r
215 xTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */
\r
216 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
217 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
218 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
219 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
222 if( xTimer != NULL )
\r
224 xTimerStart( xTimer, mainDONT_BLOCK );
\r
227 /* Create the software timer that performs the 'LED spin' functionality,
\r
228 as described at the top of this file. */
\r
229 xTimer = xTimerCreate( ( const signed char * ) "LEDTimer", /* A text name, purely to help debugging. */
\r
230 ( mainLED_TIMER_PERIOD_MS ), /* The timer period, in this case 75ms. */
\r
231 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
232 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
233 prvLEDTimerCallback /* The callback function that toggles the white LEDs. */
\r
236 if( xTimer != NULL )
\r
238 xTimerStart( xTimer, mainDONT_BLOCK );
\r
241 /* The set of tasks created by the following function call have to be
\r
242 created last as they keep account of the number of tasks they expect to see
\r
244 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
246 /* Start the scheduler. */
\r
247 vTaskStartScheduler();
\r
249 /* If all is well, the scheduler will now be running, and the following line
\r
250 will never be reached. If the following line does execute, then there was
\r
251 insufficient FreeRTOS heap memory available for the idle and/or timer tasks
\r
252 to be created. See the memory management section on the FreeRTOS web site
\r
253 for more details. */
\r
256 /*-----------------------------------------------------------*/
\r
258 static void prvCheckTimerCallback( xTimerHandle xTimer )
\r
260 static long lChangeToRedLEDsAlready = pdFALSE;
\r
261 static unsigned long ulLastRegTest1Counter = 0, ulLastRegTest2Counter = 0;
\r
262 unsigned long ulErrorFound = pdFALSE;
\r
263 /* LEDs are defaulted to use the Green LEDs. The Red LEDs are used if an error
\r
265 static unsigned long ulLED1 = 8, ulLED2 = 11;
\r
266 const unsigned long ulRedLED1 = 6, ulRedLED2 = 9;
\r
268 /* Check all the demo tasks (other than the flash tasks) to ensure
\r
269 they are all still running, and that none have detected an error. */
\r
271 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
273 ulErrorFound = pdTRUE;
\r
276 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
278 ulErrorFound = pdTRUE;
\r
281 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
283 ulErrorFound = pdTRUE;
\r
286 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
288 ulErrorFound = pdTRUE;
\r
291 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
293 ulErrorFound = pdTRUE;
\r
296 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
298 ulErrorFound = pdTRUE;
\r
301 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
303 ulErrorFound = pdTRUE;
\r
306 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
308 ulErrorFound = pdTRUE;
\r
311 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
313 ulErrorFound = pdTRUE;
\r
316 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
318 ulErrorFound = pdTRUE;
\r
321 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
323 ulErrorFound = pdTRUE;
\r
326 /* Check the reg test tasks are still cycling. They will stop
\r
327 incrementing their loop counters if they encounter an error. */
\r
328 if( ulRegTest1Counter == ulLastRegTest1Counter )
\r
330 ulErrorFound = pdTRUE;
\r
333 if( ulRegTest2Counter == ulLastRegTest2Counter )
\r
335 ulErrorFound = pdTRUE;
\r
338 ulLastRegTest1Counter = ulRegTest1Counter;
\r
339 ulLastRegTest2Counter = ulRegTest2Counter;
\r
341 /* Toggle the check LEDs to give an indication of the system status. If
\r
342 the green LEDs are toggling, then no errors have been detected. If the red
\r
343 LEDs are toggling, then an error has been reported in at least one task. */
\r
344 vParTestToggleLED( ulLED1 );
\r
345 vParTestToggleLED( ulLED2 );
\r
347 /* Have any errors been latch in ulErrorFound? If so, ensure the gree LEDs
\r
348 are off, then switch to using the red LEDs. */
\r
349 if( ulErrorFound != pdFALSE )
\r
351 if( lChangeToRedLEDsAlready == pdFALSE )
\r
353 lChangeToRedLEDsAlready = pdTRUE;
\r
355 /* An error has been found. Switch to use the red LEDs. */
\r
356 vParTestSetLED( ulLED1, pdFALSE );
\r
357 vParTestSetLED( ulLED2, pdFALSE );
\r
358 ulLED1 = ulRedLED1;
\r
359 ulLED2 = ulRedLED2;
\r
363 /*-----------------------------------------------------------*/
\r
365 static void prvLEDTimerCallback( xTimerHandle xTimer )
\r
367 const unsigned long ulNumWhiteLEDs = 6;
\r
368 static unsigned long ulLit1 = 2, ulLit2 = 1;
\r
370 vParTestSetLED( ulLit2, pdFALSE );
\r
375 if( ulLit1 >= ulNumWhiteLEDs )
\r
380 vParTestSetLED( ulLit1, pdTRUE );
\r
382 /*-----------------------------------------------------------*/
\r