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: This project provides two demo applications. A simple blinky style
\r
77 * project, and a more comprehensive test and demo application. The
\r
78 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
\r
79 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
\r
80 * in main.c. This file implements the comprehensive test and demo version.
\r
82 * NOTE 2: This file only contains the source code that is specific to the
\r
83 * full demo. Generic functions, such FreeRTOS hook functions, and functions
\r
84 * required to configure the hardware, are defined in main.c.
\r
85 ******************************************************************************
\r
87 * main_full() creates a set of standard demo tasks, some application specific
\r
88 * tasks, and four timers. It then starts the scheduler. The web documentation
\r
89 * provides more details of the standard demo application tasks, which provide
\r
90 * no particular functionality, but do provide a good example of how to use the
\r
93 * In addition to the standard demo tasks, the following tasks and timer are
\r
94 * defined and/or created within this file:
\r
96 * "Reg test" tasks - These fill the registers with known values, then check
\r
97 * that each register maintains its expected value for the lifetime of the
\r
98 * task. Each task uses a different set of values. The reg test tasks execute
\r
99 * with a very low priority, so get preempted very frequently. A register
\r
100 * containing an unexpected value is indicative of an error in the context
\r
101 * switching mechanism.
\r
103 * "Flash timers" - A software timer callback function is defined that does
\r
104 * nothing but toggle an LED. Three software timers are created that each
\r
105 * use the same callback function, but each toggles a different LED at a
\r
106 * different frequency. One software timer uses LED1, another LED2 and the
\r
109 * "Check" software timer - The check timer period is initially set to three
\r
110 * seconds. Its callback function checks that all the standard demo tasks, and
\r
111 * the register check tasks, are not only still executing, but are executing
\r
112 * without reporting any errors. If the check timer callback discovers that a
\r
113 * task has either stalled, or reported an error, then it changes the period of
\r
114 * the check timer from the initial three seconds, to just 200ms. The callback
\r
115 * function also toggles LED 4 each time it is called. This provides a visual
\r
116 * indication of the system status: If the LED toggles every three seconds,
\r
117 * then no issues have been discovered. If the LED toggles every 200ms, then
\r
118 * an issue has been discovered with at least one task.
\r
121 /* Kernel includes. */
\r
122 #include "FreeRTOS.h"
\r
125 #include "timers.h"
\r
127 /* Common demo includes. */
\r
128 #include "blocktim.h"
\r
129 #include "countsem.h"
\r
130 #include "recmutex.h"
\r
131 #include "ParTest.h"
\r
132 #include "dynamic.h"
\r
134 /* Hardware includes. */
\r
135 #include "stm320518_eval.h"
\r
137 /* The period after which the check timer will expire provided no errors have
\r
138 been reported by any of the standard demo tasks. ms are converted to the
\r
139 equivalent in ticks using the portTICK_RATE_MS constant. */
\r
140 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_RATE_MS )
\r
142 /* The period at which the check timer will expire if an error has been
\r
143 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
144 in ticks using the portTICK_RATE_MS constant. */
\r
145 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_RATE_MS )
\r
147 /* A block time of zero simply means "don't block". */
\r
148 #define mainDONT_BLOCK ( 0UL )
\r
150 /* The base toggle rate used by the flash timers. Each toggle rate is a
\r
151 multiple of this. */
\r
152 #define mainFLASH_TIMER_BASE_RATE ( 200UL / portTICK_RATE_MS )
\r
154 /* The LED toggle by the check timer. */
\r
155 #define mainCHECK_LED ( 3 )
\r
156 /*-----------------------------------------------------------*/
\r
159 * Register check tasks, as described at the top of this file. The nature of
\r
160 * these files necessitates that they are written in an assembly.
\r
162 extern void vRegTest1Task( void *pvParameters );
\r
163 extern void vRegTest2Task( void *pvParameters );
\r
166 * The hardware only has a single LED. Simply toggle it.
\r
168 extern void vMainToggleLED( void );
\r
171 * The check timer callback function, as described at the top of this file.
\r
173 static void prvCheckTimerCallback( xTimerHandle xTimer );
\r
176 * The flash timer callback function, as described at the top of this file.
\r
177 * This callback function is assigned to three separate software timers.
\r
179 static void prvFlashTimerCallback( xTimerHandle xTimer );
\r
182 * Called by main() to create the comprehensive test/demo application if
\r
183 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.
\r
185 void main_full( void );
\r
187 /*-----------------------------------------------------------*/
\r
189 /* The following two variables are used to communicate the status of the
\r
190 register check tasks to the check software timer. If the variables keep
\r
191 incrementing, then the register check tasks has not discovered any errors. If
\r
192 a variable stops incrementing, then an error has been found. */
\r
193 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
\r
195 /*-----------------------------------------------------------*/
\r
197 void main_full( void )
\r
199 xTimerHandle xTimer = NULL;
\r
200 unsigned long ulTimer;
\r
201 const unsigned long ulTimersToCreate = 3L;
\r
202 /* The register test tasks are asm functions that don't use a stack. The
\r
203 stack allocated just has to be large enough to hold the task context, and
\r
204 for the additional required for the stack overflow checking to work (if
\r
206 const size_t xRegTestStackSize = 25U;
\r
208 /* Create the standard demo tasks */
\r
209 vCreateBlockTimeTasks();
\r
210 vStartCountingSemaphoreTasks();
\r
211 vStartRecursiveMutexTasks();
\r
212 vStartDynamicPriorityTasks();
\r
214 /* Create the register test tasks as described at the top of this file.
\r
215 These are naked functions that don't use any stack. A stack still has
\r
216 to be allocated to hold the task context. */
\r
217 xTaskCreate( vRegTest1Task, /* Function that implements the task. */
\r
218 ( signed char * ) "Reg1", /* Text name of the task. */
\r
219 xRegTestStackSize, /* Stack allocated to the task. */
\r
220 NULL, /* The task parameter is not used. */
\r
221 tskIDLE_PRIORITY, /* The priority to assign to the task. */
\r
222 NULL ); /* Don't receive a handle back, it is not needed. */
\r
224 xTaskCreate( vRegTest2Task, /* Function that implements the task. */
\r
225 ( signed char * ) "Reg2", /* Text name of the task. */
\r
226 xRegTestStackSize, /* Stack allocated to the task. */
\r
227 NULL, /* The task parameter is not used. */
\r
228 tskIDLE_PRIORITY, /* The priority to assign to the task. */
\r
229 NULL ); /* Don't receive a handle back, it is not needed. */
\r
231 /* Create the three flash timers. */
\r
232 for( ulTimer = 0UL; ulTimer < ulTimersToCreate; ulTimer++ )
\r
234 xTimer = xTimerCreate( ( const signed char * ) "FlashTimer", /* A text name, purely to help debugging. */
\r
235 ( mainFLASH_TIMER_BASE_RATE * ( ulTimer + 1UL ) ), /* The timer period, in this case 3000ms (3s). */
\r
236 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
237 ( void * ) ulTimer, /* The ID is used to hold the number of the LED that will be flashed. */
\r
238 prvFlashTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
241 if( xTimer != NULL )
\r
243 xTimerStart( xTimer, mainDONT_BLOCK );
\r
247 /* Create the software timer that performs the 'check' functionality,
\r
248 as described at the top of this file. */
\r
249 xTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */
\r
250 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
251 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
252 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
253 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
256 /* If the software timer was created successfully, start it. It won't
\r
257 actually start running until the scheduler starts. A block time of
\r
258 zero is used in this call, although any value could be used as the block
\r
259 time will be ignored because the scheduler has not started yet. */
\r
260 if( xTimer != NULL )
\r
262 xTimerStart( xTimer, mainDONT_BLOCK );
\r
265 /* Start the kernel. From here on, only tasks and interrupts will run. */
\r
266 vTaskStartScheduler();
\r
268 /* If all is well, the scheduler will now be running, and the following
\r
269 line will never be reached. If the following line does execute, then there
\r
270 was insufficient FreeRTOS heap memory available for the idle and/or timer
\r
271 tasks to be created. See the memory management section on the FreeRTOS web
\r
272 site, or the FreeRTOS tutorial books for more details. */
\r
275 /*-----------------------------------------------------------*/
\r
277 /* See the description at the top of this file. */
\r
278 static void prvCheckTimerCallback( xTimerHandle xTimer )
\r
280 static long lChangedTimerPeriodAlready = pdFALSE;
\r
281 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
\r
282 unsigned long ulErrorFound = pdFALSE;
\r
284 /* Check all the demo and test tasks to ensure that they are all still
\r
285 running, and that none have detected an error. */
\r
286 if( xAreDynamicPriorityTasksStillRunning() != pdPASS )
\r
288 ulErrorFound |= ( 0x01UL << 0UL );
\r
291 if( xAreBlockTimeTestTasksStillRunning() != pdPASS )
\r
293 ulErrorFound |= ( 0x01UL << 1UL );
\r
296 if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )
\r
298 ulErrorFound |= ( 0x01UL << 2UL );
\r
301 if( xAreRecursiveMutexTasksStillRunning() != pdPASS )
\r
303 ulErrorFound |= ( 0x01UL << 3UL );
\r
306 /* Check that the register test 1 task is still running. */
\r
307 if( ulLastRegTest1Value == ulRegTest1LoopCounter )
\r
309 ulErrorFound |= ( 0x01UL << 4UL );
\r
311 ulLastRegTest1Value = ulRegTest1LoopCounter;
\r
313 /* Check that the register test 2 task is still running. */
\r
314 if( ulLastRegTest2Value == ulRegTest2LoopCounter )
\r
316 ulErrorFound |= ( 0x01UL << 5UL );
\r
318 ulLastRegTest2Value = ulRegTest2LoopCounter;
\r
320 /* Toggle the check LED to give an indication of the system status. If
\r
321 the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then
\r
322 everything is ok. A faster toggle indicates an error. */
\r
323 vParTestToggleLED( mainCHECK_LED );
\r
325 /* Have any errors been latched in ulErrorFound? If so, shorten the
\r
326 period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.
\r
327 This will result in an increase in the rate at which mainCHECK_LED
\r
329 if( ulErrorFound != pdFALSE )
\r
331 if( lChangedTimerPeriodAlready == pdFALSE )
\r
333 lChangedTimerPeriodAlready = pdTRUE;
\r
335 /* This call to xTimerChangePeriod() uses a zero block time.
\r
336 Functions called from inside of a timer callback function must
\r
337 *never* attempt to block. */
\r
338 xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
342 /*-----------------------------------------------------------*/
\r
344 static void prvFlashTimerCallback( xTimerHandle xTimer )
\r
346 unsigned long ulLED;
\r
348 /* This callback function is assigned to three separate software timers.
\r
349 Each timer toggles a different LED. Obtain the number of the LED that
\r
350 this timer is toggling. */
\r
351 ulLED = ( unsigned long ) pvTimerGetTimerID( xTimer );
\r
353 /* Toggle the LED. */
\r
354 vParTestToggleLED( ulLED );
\r