]> git.sur5r.net Git - freertos/blob - Demo/AVR32_UC3A_GCC/main.c
Temporarily revert the AVR32 port back to the V6.0.5 files. Work will continue on...
[freertos] / Demo / AVR32_UC3A_GCC / main.c
1 /*\r
2     FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\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
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\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
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \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
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\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
59  *\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
67  *\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
76  *\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
79  *\r
80  */\r
81 \r
82 #include <stdio.h>\r
83 #include <stdlib.h>\r
84 #include <string.h>\r
85 \r
86 /* Environment header files. */\r
87 #include "pm.h"\r
88 \r
89 /* Scheduler header files. */\r
90 #include "FreeRTOS.h"\r
91 #include "task.h"\r
92 \r
93 /* Demo file headers. */\r
94 #include "partest.h"\r
95 #include "serial.h"\r
96 #include "integer.h"\r
97 #include "comtest.h"\r
98 #include "PollQ.h"\r
99 #include "semtest.h"\r
100 #include "dynamic.h"\r
101 #include "BlockQ.h"\r
102 #include "death.h"\r
103 #include "flop.h"\r
104 #include "flash.h"\r
105 \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
115 \r
116 /* Baud rate used by the loopback test task. */\r
117 #define mainCOM_TEST_BAUD_RATE    ( ( unsigned long ) 38400 )\r
118 \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
122 \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
127 \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
132 \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
136 \r
137 /* The LED to use by the simple flash task. */\r
138 #define mainSIMPLE_FLASH_LED      ( 0 )\r
139 \r
140 /* The frequency of the simple flashing LED. */\r
141 #define mainSIMPLE_FLASH_RATE     ( ( portTickType ) 200 / portTICK_RATE_MS )\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 /*\r
145  * The 'Check' task that executes at the highest priority and calls\r
146  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
147  * of the file.\r
148  */\r
149 static void prvErrorChecks( void *pvParameters );\r
150 \r
151 /*\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
154  */\r
155 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void );\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 int main( void )\r
159 {\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
162 \r
163         /* Setup the LED's for output. */\r
164         vParTestInitialise();\r
165 \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
175 \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
184 \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
188 \r
189         /* Start the scheduler. */\r
190         vTaskStartScheduler();\r
191 \r
192         /* Will only get here if there was insufficient memory to create the idle\r
193         task. */\r
194         for( ;; );\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 static void prvErrorChecks( void *pvParameters )\r
199 {\r
200 portTickType xDelayPeriod = mainNO_ERROR_FLASH_RATE;\r
201 \r
202         /* The parameters are not used.  Prevent compiler warnings. */\r
203         ( void ) pvParameters;\r
204 \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
208 \r
209         for( ;; )\r
210         {\r
211                 /* Delay until it is time to execute again. */\r
212                 vTaskDelay( xDelayPeriod );\r
213 \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
217                 {\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
223                 }\r
224 \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
228         }\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void )\r
233 {\r
234 portBASE_TYPE xStatus = pdPASS;\r
235 \r
236         if( xAreComTestTasksStillRunning() != pdPASS )\r
237         {\r
238                 xStatus = pdFAIL;\r
239         }\r
240 \r
241         if( xArePollingQueuesStillRunning() != pdPASS )\r
242         {\r
243                 xStatus = pdFAIL;\r
244         }\r
245 \r
246         if( xAreIntegerMathsTaskStillRunning() != pdPASS )\r
247         {\r
248                 xStatus = pdFAIL;\r
249         }\r
250 \r
251         if( xAreSemaphoreTasksStillRunning() != pdPASS )\r
252         {\r
253                 xStatus = pdFAIL;\r
254         }\r
255 \r
256         if( xAreBlockingQueuesStillRunning() != pdPASS )\r
257         {\r
258                 xStatus = pdFAIL;\r
259         }\r
260 \r
261         if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
262         {\r
263                 xStatus = pdFAIL;\r
264         }\r
265 \r
266         if( xAreMathsTaskStillRunning() != pdPASS )\r
267         {\r
268                 xStatus = pdFAIL;\r
269         }\r
270 \r
271         if( xIsCreateTaskStillRunning() != pdPASS )\r
272         {\r
273                 xStatus = pdFAIL;\r
274         }\r
275 \r
276         return xStatus;\r
277 }\r
278 /*-----------------------------------------------------------*/\r