2 FreeRTOS V8.1.0 - 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 !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\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, are defined in main.c.
\r
76 ******************************************************************************
\r
78 * main_full() creates all the demo application tasks and software timers, then
\r
79 * starts the scheduler. The WEB documentation provides more details of the
\r
80 * standard demo application tasks. In addition to the standard demo tasks, the
\r
81 * following tasks and tests are also defined:
\r
83 * "Register test" tasks - These tasks are used in part to test the kernel port.
\r
84 * They set each processor register to a known value, then check that the
\r
85 * register still contains that value. Each of the tasks sets the registers
\r
86 * to different values, and will get swapping in and out between setting and
\r
87 * then subsequently checking the register values. Discovery of an incorrect
\r
88 * value would be indicative of an error in the task switching mechanism.
\r
90 * "ISR triggered task" - This is provided as an example of how to write a
\r
91 * FreeRTOS compatible interrupt service routine. See the comments in
\r
92 * ISRTriggeredTask.c.
\r
94 * "High Frequency Timer Test" - The high frequency timer is created to test
\r
95 * the interrupt nesting method. The standard demo interrupt nesting test tasks
\r
96 * are created with priorities at or below configMAX_SYSCALL_INTERRUPT_PRIORITY
\r
97 * because they use interrupt safe FreeRTOS API functions. The high frequency
\r
98 * time is created with a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY,
\r
99 * so cannot us the same API functions.
\r
101 * By way of demonstration, the demo application defines
\r
102 * configMAX_SYSCALL_INTERRUPT_PRIORITY to be 3, configKERNEL_INTERRUPT_PRIORITY
\r
103 * to be 1, and all other interrupts as follows:
\r
105 * See the online documentation for this demo for more information on interrupt
\r
108 * "Check" timer - The check software timer period is initially set to three
\r
109 * seconds. The callback function associated with the check software timer
\r
110 * checks that all the standard demo tasks, and the register check tasks, are
\r
111 * not only still executing, but are executing without reporting any errors. If
\r
112 * the check software timer discovers that a task has either stalled, or
\r
113 * reported an error, then it changes its own execution period from the initial
\r
114 * three seconds, to just 200ms. The check software timer also toggle LED
\r
115 * mainCHECK_LED; If mainCHECK_LED toggles every 3 seconds, no errors have
\r
116 * been detected. If mainCHECK_LED toggles every 200ms then an error has been
\r
117 * detected in at least one task.
\r
121 /* Scheduler includes. */
\r
122 #include "FreeRTOS.h"
\r
125 #include "semphr.h"
\r
126 #include "timers.h"
\r
128 /* Demo application includes. */
\r
129 #include "partest.h"
\r
130 #include "blocktim.h"
\r
131 #include "flash_timer.h"
\r
132 #include "semtest.h"
\r
133 #include "GenQTest.h"
\r
135 #include "IntQueue.h"
\r
136 #include "countsem.h"
\r
137 #include "dynamic.h"
\r
138 #include "QueueOverwrite.h"
\r
139 #include "QueueSet.h"
\r
140 #include "recmutex.h"
\r
141 #include "EventGroupsDemo.h"
\r
143 /*-----------------------------------------------------------*/
\r
145 /* The period after which the check timer will expire, in ms, provided no errors
\r
146 have been reported by any of the standard demo tasks. ms are converted to the
\r
147 equivalent in ticks using the portTICK_PERIOD_MS constant. */
\r
148 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_PERIOD_MS )
\r
150 /* The period at which the check timer will expire, in ms, if an error has been
\r
151 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
152 in ticks using the portTICK_PERIOD_MS constant. */
\r
153 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_PERIOD_MS )
\r
155 /* The priorities of the various demo application tasks. */
\r
156 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
157 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
158 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
159 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
160 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
161 #define mainQUEUE_OVERWRITE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
163 /* The LED controlled by the 'check' software timer. */
\r
164 #define mainCHECK_LED ( 2 )
\r
166 /* The number of LEDs that should be controlled by the flash software timer
\r
167 standard demo. In this case it is only 1 as the starter kit has three LEDs, one
\r
168 of which is controlled by the check timer and one of which is controlled by the
\r
169 ISR triggered task. */
\r
170 #define mainNUM_FLASH_TIMER_LEDS ( 1 )
\r
173 #define mainDONT_BLOCK ( 0 )
\r
175 /* The frequency at which the "high frequency interrupt" interrupt will
\r
177 #define mainTEST_INTERRUPT_FREQUENCY ( 20000 )
\r
179 /*-----------------------------------------------------------*/
\r
182 * The check timer callback function, as described at the top of this file.
\r
184 static void prvCheckTimerCallback( TimerHandle_t xTimer );
\r
187 * It is important to ensure the high frequency timer test does not start before
\r
188 * the kernel. It is therefore started from inside a software timer callback
\r
189 * function, which will not execute until the timer service/daemon task is
\r
190 * executing. A one-shot timer is used, so the callback function will only
\r
191 * execute once (unless it is manually reset/restarted).
\r
193 static void prvSetupHighFrequencyTimerTest( TimerHandle_t xTimer );
\r
196 * Tasks that test the context switch mechanism by filling the processor
\r
197 * registers with known values, then checking that the values contained
\r
198 * within the registers is as expected. The tasks are likely to get swapped
\r
199 * in and out between setting the register values and checking the register
\r
202 static void prvRegTestTask1( void *pvParameters );
\r
203 static void prvRegTestTask2( void *pvParameters );
\r
206 * The task that is periodically triggered by an interrupt, as described at the
\r
207 * top of this file.
\r
209 extern void vStartISRTriggeredTask( void );
\r
211 /*-----------------------------------------------------------*/
\r
213 /* Variables incremented by prvRegTestTask1() and prvRegTestTask2() respectively
\r
214 on each iteration of their function. These are used to detect errors in the
\r
216 volatile unsigned long ulRegTest1Cycles = 0, ulRegTest2Cycles = 0;
\r
218 /*-----------------------------------------------------------*/
\r
221 * Create the demo tasks then start the scheduler.
\r
223 int main_full( void )
\r
225 TimerHandle_t xTimer = NULL;
\r
227 /* Create all the other standard demo tasks. */
\r
228 vStartLEDFlashTimers( mainNUM_FLASH_TIMER_LEDS );
\r
229 vCreateBlockTimeTasks();
\r
230 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
231 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
232 vStartQueuePeekTasks();
\r
233 vStartInterruptQueueTasks();
\r
234 vStartISRTriggeredTask();
\r
235 vStartCountingSemaphoreTasks();
\r
236 vStartDynamicPriorityTasks();
\r
237 vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_TASK_PRIORITY );
\r
238 vStartQueueSetTasks();
\r
239 vStartRecursiveMutexTasks();
\r
240 vStartEventGroupTasks();
\r
242 /* Create the tasks defined within this file. */
\r
243 xTaskCreate( prvRegTestTask1, /* The function that implements the task. */
\r
244 "Reg1", /* Text name for the task to assist debugger - not used by FreeRTOS itself. */
\r
245 configMINIMAL_STACK_SIZE, /* The stack size to allocate for the task - specified in words not bytes. */
\r
246 NULL, /* The parameter to pass into the task - not used in this case so set to NULL. */
\r
247 tskIDLE_PRIORITY, /* The priority to assign to the task. */
\r
248 NULL ); /* Used to obtain a handle to the task being created - not used in this case so set to NULL. */
\r
250 xTaskCreate( prvRegTestTask2, "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
252 /* Create the software timer that performs the 'check' functionality, as
\r
253 described at the top of this file. */
\r
254 xTimer = xTimerCreate( "CheckTimer",/* A text name, purely to help debugging. */
\r
255 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
256 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
257 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
258 prvCheckTimerCallback ); /* The callback function that inspects the status of all the other tasks. */
\r
260 if( xTimer != NULL )
\r
262 xTimerStart( xTimer, mainDONT_BLOCK );
\r
265 /* A software timer is also used to start the high frequency timer test.
\r
266 This is to ensure the test does not start before the kernel. This time a
\r
267 one shot software timer is used. */
\r
268 xTimer = xTimerCreate( "HighHzTimerSetup", 1, pdFALSE, ( void * ) 0, prvSetupHighFrequencyTimerTest );
\r
269 if( xTimer != NULL )
\r
271 xTimerStart( xTimer, mainDONT_BLOCK );
\r
274 /* Finally start the scheduler. */
\r
275 vTaskStartScheduler();
\r
277 /* If all is well, the scheduler will now be running, and the following line
\r
278 will never be reached. If the following line does execute, then there was
\r
279 insufficient FreeRTOS heap memory available for the idle and/or timer tasks
\r
280 to be created. See the memory management section on the FreeRTOS web site
\r
281 for more details. http://www.freertos.org/a00111.html */
\r
284 /*-----------------------------------------------------------*/
\r
286 static void prvRegTestTask1( void *pvParameters )
\r
288 extern void vRegTest1( volatile unsigned long * );
\r
290 /* Avoid compiler warnings. */
\r
291 ( void ) pvParameters;
\r
293 /* Pass the address of the RegTest1 loop counter into the test function,
\r
294 which is necessarily implemented in assembler. */
\r
295 vRegTest1( &ulRegTest1Cycles );
\r
297 /* vRegTest1 should never exit! */
\r
298 vTaskDelete( NULL );
\r
300 /*-----------------------------------------------------------*/
\r
302 static void prvRegTestTask2( void *pvParameters )
\r
304 extern void vRegTest2( volatile unsigned long * );
\r
306 /* Avoid compiler warnings. */
\r
307 ( void ) pvParameters;
\r
309 /* Pass the address of the RegTest2 loop counter into the test function,
\r
310 which is necessarily implemented in assembler. */
\r
311 vRegTest2( &ulRegTest2Cycles );
\r
313 /* vRegTest1 should never exit! */
\r
314 vTaskDelete( NULL );
\r
316 /*-----------------------------------------------------------*/
\r
318 static void prvCheckTimerCallback( TimerHandle_t xTimer )
\r
320 static long lChangedTimerPeriodAlready = pdFALSE;
\r
321 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0, ulLastHighFrequencyTimerInterrupts = 0;
\r
322 static const unsigned long ulExpectedHighFrequencyInterrupts = ( ( mainTEST_INTERRUPT_FREQUENCY / 1000UL ) * mainCHECK_TIMER_PERIOD_MS ) - 10; /* 10 allows for a margin of error. */
\r
323 unsigned long ulErrorOccurred = pdFALSE;
\r
325 /* The count of the high frequency timer interrupts. */
\r
326 extern unsigned long ulHighFrequencyTimerInterrupts;
\r
328 /* Avoid compiler warnings. */
\r
331 /* Check that the register test 1 task is still running. */
\r
332 if( ulLastRegTest1Value == ulRegTest1Cycles )
\r
334 ulErrorOccurred |= ( 0x01UL << 1UL );
\r
336 ulLastRegTest1Value = ulRegTest1Cycles;
\r
339 /* Check that the register test 2 task is still running. */
\r
340 if( ulLastRegTest2Value == ulRegTest2Cycles )
\r
342 ulErrorOccurred |= ( 0x01UL << 2UL );
\r
344 ulLastRegTest2Value = ulRegTest2Cycles;
\r
346 /* Have any of the standard demo tasks detected an error in their
\r
348 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
350 ulErrorOccurred |= ( 0x01UL << 3UL );
\r
352 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
354 ulErrorOccurred |= ( 0x01UL << 4UL );
\r
356 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
358 ulErrorOccurred |= ( 0x01UL << 5UL );
\r
360 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
362 ulErrorOccurred |= ( 0x01UL << 6UL );
\r
364 else if( xAreIntQueueTasksStillRunning() != pdTRUE )
\r
366 ulErrorOccurred |= ( 0x01UL << 7UL );
\r
368 else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
370 ulErrorOccurred |= ( 0x01UL << 8UL );
\r
372 else if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
374 ulErrorOccurred |= ( 0x01UL << 9UL );
\r
376 else if( xIsQueueOverwriteTaskStillRunning() != pdTRUE )
\r
378 ulErrorOccurred |= ( 0x01UL << 10UL );
\r
380 else if( xAreQueueSetTasksStillRunning() != pdTRUE )
\r
382 ulErrorOccurred |= ( 0x01UL << 11UL );
\r
384 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
386 ulErrorOccurred |= ( 0x01UL << 12UL );
\r
388 else if( xAreEventGroupTasksStillRunning() != pdTRUE )
\r
390 ulErrorOccurred |= ( 0x01UL << 13UL );
\r
393 /* Ensure the expected number of high frequency interrupts have occurred. */
\r
394 if( ulLastHighFrequencyTimerInterrupts != 0 )
\r
396 if( ( ulHighFrequencyTimerInterrupts - ulLastHighFrequencyTimerInterrupts ) < ulExpectedHighFrequencyInterrupts )
\r
398 ulErrorOccurred |= ( 0x01UL << 14UL );
\r
401 ulLastHighFrequencyTimerInterrupts = ulHighFrequencyTimerInterrupts;
\r
403 if( ulErrorOccurred != pdFALSE )
\r
405 /* An error occurred. Increase the frequency at which the check timer
\r
406 toggles its LED to give visual feedback of the potential error
\r
408 if( lChangedTimerPeriodAlready == pdFALSE )
\r
410 lChangedTimerPeriodAlready = pdTRUE;
\r
412 /* This call to xTimerChangePeriod() uses a zero block time.
\r
413 Functions called from inside of a timer callback function must
\r
414 *never* attempt to block as to do so could impact other software
\r
416 xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
420 vParTestToggleLED( mainCHECK_LED );
\r
422 /*-----------------------------------------------------------*/
\r
424 static void prvSetupHighFrequencyTimerTest( TimerHandle_t xTimer )
\r
426 void vSetupTimerTest( unsigned short usFrequencyHz );
\r
428 /* Avoid compiler warnings. */
\r
431 /* Setup the high frequency, high priority, timer test. It is setup in this
\r
432 software timer callback to ensure it does not start before the kernel does.
\r
433 This is a one shot timer - so the setup routine will only be executed once. */
\r
434 vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );
\r
436 /*-----------------------------------------------------------*/
\r