]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_AT91SAM7X256_Eclipse/RTOSDemo/main.c
8771dff44bb2198b52ea12e198af059c2bc975d9
[freertos] / FreeRTOS / Demo / ARM7_AT91SAM7X256_Eclipse / RTOSDemo / main.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\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 modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or 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     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /*\r
68         NOTE : Tasks run in System mode and the scheduler runs in Supervisor mode.\r
69         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
70         called.  The demo applications included in the FreeRTOS.org download switch\r
71         to supervisor mode prior to main being called.  If you are not using one of\r
72         these demo application projects then ensure Supervisor mode is used.\r
73 */\r
74 \r
75 /*\r
76  * This demo includes a (basic) USB mouse driver and a WEB server.  It is\r
77  * targeted for the AT91SAM7X EK prototyping board which includes a small\r
78  * joystick to provide the mouse inputs.  The WEB interface provides some basic\r
79  * interactivity through the use of a check box to turn on and off an LED.\r
80  *\r
81  * main() creates the WEB server, USB, and a set of the standard demo tasks\r
82  * before starting the scheduler.  See the online FreeRTOS.org documentation \r
83  * for more information on the standard demo tasks.  \r
84  *\r
85  * LEDs D1 to D3 are controlled by the standard 'flash' tasks - each will \r
86  * toggle at a different fixed frequency.\r
87  *\r
88  * A tick hook function is used to monitor the standard demo tasks - with LED\r
89  * D4 being used to indicate the system status.  D4 toggling every 5 seconds\r
90  * indicates that all the standard demo tasks are executing without error.  The\r
91  * toggle rate increasing to 500ms is indicative of an error having been found\r
92  * in at least one demo task.\r
93  *\r
94  * See the online documentation page that accompanies this demo for full setup\r
95  * and usage information.\r
96  */\r
97 \r
98 /* Standard includes. */\r
99 #include <stdlib.h>\r
100 \r
101 /* Scheduler includes. */\r
102 #include "FreeRTOS.h"\r
103 #include "task.h"\r
104 \r
105 /* Demo application includes. */\r
106 #include "partest.h"\r
107 #include "USBSample.h"\r
108 #include "uIP_Task.h"\r
109 #include "BlockQ.h"\r
110 #include "blocktim.h"\r
111 #include "flash.h"\r
112 #include "QPeek.h"\r
113 #include "dynamic.h"\r
114 \r
115 /* Priorities for the demo application tasks. */\r
116 #define mainUIP_PRIORITY                                        ( tskIDLE_PRIORITY + 2 )\r
117 #define mainUSB_PRIORITY                                        ( tskIDLE_PRIORITY + 2 )\r
118 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 1 )\r
119 #define mainFLASH_PRIORITY                  ( tskIDLE_PRIORITY + 2 )\r
120 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY ) \r
121 \r
122 /* The task allocated to the uIP task is large to account for its use of the\r
123 sprintf() library function.  Use of a cut down printf() library would allow\r
124 the stack usage to be greatly reduced. */\r
125 #define mainUIP_TASK_STACK_SIZE         ( configMINIMAL_STACK_SIZE * 6 )\r
126 \r
127 /* The LED toggle by the tick hook should an error have been found in a task. */\r
128 #define mainERROR_LED                                           ( 3 )\r
129 \r
130 /*-----------------------------------------------------------*/\r
131 \r
132 /*\r
133  * Configure the processor for use with the Atmel demo board.  Setup is minimal\r
134  * as the low level init function (called from the startup asm file) takes care\r
135  * of most things.\r
136  */\r
137 static void prvSetupHardware( void );\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 /*\r
142  * Starts all the other tasks, then starts the scheduler.\r
143  */\r
144 int main( void )\r
145 {\r
146         /* Setup any hardware that has not already been configured by the low\r
147         level init routines. */\r
148         prvSetupHardware();\r
149 \r
150         /* Start the task that handles the TCP/IP and WEB server functionality. */\r
151     xTaskCreate( vuIP_Task, "uIP", mainUIP_TASK_STACK_SIZE, NULL, mainUIP_PRIORITY, NULL );\r
152         \r
153         /* Also start the USB demo which is just for the SAM7. */\r
154     vStartUSBTask( mainUSB_PRIORITY );\r
155         \r
156         /* Start the standard demo tasks. */\r
157         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
158     vCreateBlockTimeTasks();\r
159     vStartLEDFlashTasks( mainFLASH_PRIORITY );\r
160     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
161     vStartQueuePeekTasks();   \r
162     vStartDynamicPriorityTasks();\r
163 \r
164         /* Start the scheduler.\r
165 \r
166         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
167         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
168         called.  The demo applications included in the FreeRTOS.org download switch\r
169         to supervisor mode prior to main being called.  If you are not using one of\r
170         these demo application projects then ensure Supervisor mode is used here. */\r
171 \r
172         vTaskStartScheduler();\r
173 \r
174         /* We should never get here as control is now taken by the scheduler. */\r
175         return 0;\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 static void prvSetupHardware( void )\r
180 {\r
181         portDISABLE_INTERRUPTS();\r
182         \r
183         /* When using the JTAG debugger the hardware is not always initialised to\r
184         the correct default state.  This line just ensures that this does not\r
185         cause all interrupts to be masked at the start. */\r
186         AT91C_BASE_AIC->AIC_EOICR = 0;\r
187         \r
188         /* Most setup is performed by the low level init function called from the\r
189         startup asm file. */\r
190 \r
191         /* Enable the peripheral clock. */\r
192     AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA;\r
193     AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOB;\r
194         AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_EMAC;\r
195 \r
196         /* Initialise the LED outputs for use by the demo application tasks. */\r
197         vParTestInitialise();\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 void vApplicationTickHook( void )\r
202 {\r
203 static unsigned long ulCallCount = 0, ulErrorFound = pdFALSE;\r
204 \r
205 /* The rate at which LED D4 will toggle if an error has been found in one or \r
206 more of the standard demo tasks. */\r
207 const unsigned long ulErrorFlashRate = 500 / portTICK_RATE_MS;\r
208 \r
209 /* The rate at which LED D4 will toggle if no errors have been found in any\r
210 of the standard demo tasks. */\r
211 const unsigned long ulNoErrorCheckRate = 5000 / portTICK_RATE_MS;\r
212 \r
213         ulCallCount++;\r
214 \r
215         if( ulErrorFound != pdFALSE )\r
216         {\r
217                 /* We have already found an error, so flash the LED with the appropriate\r
218                 frequency. */\r
219                 if( ulCallCount > ulErrorFlashRate )\r
220                 {\r
221                         ulCallCount = 0;\r
222                         vParTestToggleLED( mainERROR_LED );\r
223                 }\r
224         }\r
225         else\r
226         {\r
227                 if( ulCallCount > ulNoErrorCheckRate )\r
228                 {\r
229                         ulCallCount = 0;\r
230                         \r
231                         /* We have not yet found an error.  Check all the demo tasks to ensure\r
232                         this is still the case. */\r
233                         \r
234                         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
235                         {\r
236                                 ulErrorFound |= 0x01;\r
237                         }\r
238                         \r
239                         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
240                         {\r
241                                 ulErrorFound |= 0x02;\r
242                         }\r
243         \r
244                         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
245                         {\r
246                                 ulErrorFound |= 0x04;\r
247                         }\r
248                         \r
249                         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
250                         {\r
251                                 ulErrorFound |= 0x08;\r
252                         }\r
253                         \r
254                         if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
255                         {\r
256                                 ulErrorFound |= 0x10;\r
257                         }\r
258                         \r
259                         vParTestToggleLED( mainERROR_LED );\r
260                 }\r
261         }\r
262 }\r
263 \r
264 \r
265 \r
266 \r