2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * This is a simple demo that creates a number of tasks from a pool of
\r
56 * 'standard demo tasks' which are used by all the FreeRTOS port demos. The
\r
57 * standard demo tasks don't provide any useful functionality other than to
\r
58 * demonstrate the FreeRTOS API being used and show how the scheduler behaves.
\r
60 * A COM test is included whereby one task transmits characters on the UART
\r
61 * which are then received by another task. A loopback connector is required
\r
62 * on the UART1 connector for this test to pass (pins 2 and 3 need to be
\r
63 * connected together - a paper clip is usually all that is required). LED
\r
64 * 5 red and green are under the control of the COM test tasks. Red will toggle
\r
65 * each time a character is successfully transmitted, and the green LED toggles
\r
66 * for each received character.
\r
68 * In addition this file creates a 'Check' task. This periodically inspects
\r
69 * the standard demo tasks and makes a few other simple tests to see if the
\r
70 * system is performing as expected. The check task toggles LED 6 green every
\r
71 * 3 seconds provided no errors exist and sets it to red if an error has
\r
72 * occurred. The toggle rate will increase to 500ms if an error is detected
\r
73 * at any time. This mechanism can be tested by removing the loopback
\r
74 * connector from UART1, and in so doing deliberately generating an error in
\r
75 * the COM test task.
\r
77 * LED 1 through 4 are controlled by simple LED flashing tasks. Each should
\r
78 * toggle at a fixed but different frequency.
\r
86 /* Environment header files. */
\r
89 /* Scheduler header files. */
\r
90 #include "FreeRTOS.h"
\r
93 /* Demo file headers. */
\r
94 #include "partest.h"
\r
96 #include "integer.h"
\r
97 #include "comtest.h"
\r
99 #include "semtest.h"
\r
100 #include "dynamic.h"
\r
101 #include "BlockQ.h"
\r
106 /* Task priorities. */
\r
107 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
108 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
109 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
110 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
111 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
112 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
113 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
114 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
116 /* Baud rate used by the loopback test task. */
\r
117 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 38400 )
\r
119 /* LED used by the serial port tasks. This is toggled on each character Tx,
\r
120 and mainCOM_TEST_LED + 1 is toggled on each character Rx. */
\r
121 #define mainCOM_TEST_LED ( 4 )
\r
123 /* LED that is toggled by the check task. The check task periodically checks
\r
124 that all the other tasks are operating without error. If an error is found at
\r
125 any time the LED toggle frequency increases. */
\r
126 #define mainCHECK_TASK_LED ( 6 )
\r
128 /* The frequency at which the check task executes assuming no errors have been
\r
129 found. portTICK_RATE_MS is used to convert milliseconds to ticks, depending on
\r
130 the tick frequency. */
\r
131 #define mainNO_ERROR_FLASH_RATE ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
133 /* The frequency at which the check task executes if an error has been found
\r
134 in any of the demo tasks. */
\r
135 #define mainERROR_FLASH_RATE ( (portTickType) 500 / portTICK_RATE_MS )
\r
137 /* The LED to use by the simple flash task. */
\r
138 #define mainSIMPLE_FLASH_LED ( 0 )
\r
140 /* The frequency of the simple flashing LED. */
\r
141 #define mainSIMPLE_FLASH_RATE ( ( portTickType ) 200 / portTICK_RATE_MS )
\r
142 /*-----------------------------------------------------------*/
\r
145 * The 'Check' task that executes at the highest priority and calls
\r
146 * prvCheckOtherTasksAreStillRunning(). See the description at the top
\r
149 static void prvErrorChecks( void *pvParameters );
\r
152 * Checks that all the demo application tasks are still executing without error
\r
153 * - as described at the top of the file.
\r
155 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void );
\r
156 /*-----------------------------------------------------------*/
\r
160 /* Start the crystal oscillator 0 and switch the main clock to it. */
\r
161 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
\r
163 /* Setup the LED's for output. */
\r
164 vParTestInitialise();
\r
166 /* Start the standard demo tasks. */
\r
167 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
168 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
169 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
170 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
171 vStartDynamicPriorityTasks();
\r
172 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
173 vStartMathTasks( tskIDLE_PRIORITY );
\r
174 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
176 /* Start the demo tasks defined within this file, first the check
\r
177 task as described at the top of this file. */
\r
178 xTaskCreate( prvErrorChecks, /* The function that implements the task. */
\r
179 ( const signed char * ) "ErrCheck", /* The name of the task. The kernel does not use this, its just to facilitate debugging. */
\r
180 configMINIMAL_STACK_SIZE, /* The size of the stack (in words) that should be allocated to the task. */
\r
181 NULL, /* No task parameter is being used. */
\r
182 mainCHECK_TASK_PRIORITY, /* The priority to assign to the task, 0 being the lowest priority, configMAX_PRIORITIES - 1 being the highest priority. */
\r
183 NULL ); /* Not interested in receiving a handle to the task being created, so just passing in NULL. */
\r
185 /* This task has to be created last as it keeps account of the number of
\r
186 tasks it expects to see running. */
\r
187 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
189 /* Start the scheduler. */
\r
190 vTaskStartScheduler();
\r
192 /* Will only get here if there was insufficient memory to create the idle
\r
196 /*-----------------------------------------------------------*/
\r
198 static void prvErrorChecks( void *pvParameters )
\r
200 portTickType xDelayPeriod = mainNO_ERROR_FLASH_RATE;
\r
202 /* The parameters are not used. Prevent compiler warnings. */
\r
203 ( void ) pvParameters;
\r
205 /* Cycle for ever, delaying then checking all the other tasks are still
\r
206 operating without error. */
\r
207 vParTestSetLED( mainCHECK_TASK_LED, pdFALSE );
\r
211 /* Delay until it is time to execute again. */
\r
212 vTaskDelay( xDelayPeriod );
\r
214 /* Check all other tasks are still operating without error.
\r
215 Check that vMemCheckTask did increment the counter. */
\r
216 if( prvCheckOtherTasksAreStillRunning() != pdPASS )
\r
218 /* An error has occurred in one of the tasks. Increase the
\r
219 frequency at which this task executes and in so doing increase
\r
220 the rate at which the mainCHECK_TASK_LED toggles. */
\r
221 xDelayPeriod = mainERROR_FLASH_RATE;
\r
222 vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );
\r
225 /* Toggle the LED - the frequency of the LED toggle indicates the
\r
226 health of the system. */
\r
227 vParTestToggleLED( mainCHECK_TASK_LED + 1 );
\r
230 /*-----------------------------------------------------------*/
\r
232 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void )
\r
234 portBASE_TYPE xStatus = pdPASS;
\r
236 if( xAreComTestTasksStillRunning() != pdPASS )
\r
241 if( xArePollingQueuesStillRunning() != pdPASS )
\r
246 if( xAreIntegerMathsTaskStillRunning() != pdPASS )
\r
251 if( xAreSemaphoreTasksStillRunning() != pdPASS )
\r
256 if( xAreBlockingQueuesStillRunning() != pdPASS )
\r
261 if( xAreDynamicPriorityTasksStillRunning() != pdPASS )
\r
266 if( xAreMathsTaskStillRunning() != pdPASS )
\r
271 if( xIsCreateTaskStillRunning() != pdPASS )
\r
278 /*-----------------------------------------------------------*/
\r