]> git.sur5r.net Git - freertos/blob - Demo/WizNET_DEMO_GCC_ARM7/main.c
Update to V5.0.0.
[freertos] / Demo / WizNET_DEMO_GCC_ARM7 / main.c
1 /*\r
2         FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26     ***************************************************************************\r
27     ***************************************************************************\r
28     *                                                                         *\r
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and \r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety \r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting, \r
47         licensing and training services.\r
48 */\r
49 \r
50 /* \r
51         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
52         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
53         called.  The demo applications included in the FreeRTOS.org download switch\r
54         to supervisor mode prior to main being called.  If you are not using one of\r
55         these demo application projects then ensure Supervisor mode is used.\r
56 */\r
57 \r
58 \r
59 /*\r
60  * Program entry point.\r
61  * \r
62  * main() is responsible for setting up the microcontroller peripherals, then\r
63  * starting the demo application tasks.  Once the tasks have been created the\r
64  * scheduler is started and main() should never complete.\r
65  *\r
66  * The demo creates the three standard 'flash' tasks to provide some visual\r
67  * feedback that the system and scheduler are still operating correctly.\r
68  *\r
69  * The HTTP server task operates at the highest priority so will always preempt\r
70  * the flash or idle task on TCP/IP events.\r
71  */\r
72 \r
73 /* Standard includes. */\r
74 #include <stdlib.h>\r
75 \r
76 /* Scheduler include files. */\r
77 #include "FreeRTOS.h"\r
78 #include "semphr.h"\r
79 #include "task.h"\r
80 \r
81 /* Application includes. */\r
82 #include "i2c.h"\r
83 #include "HTTP_Serv.h"\r
84 #include "flash.h"\r
85 #include "partest.h"\r
86 #include "dynamic.h"\r
87 #include "semtest.h"\r
88 #include "PollQ.h"\r
89 #include "BlockQ.h"\r
90 #include "integer.h"\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /* Constants to setup the PLL. */\r
95 #define mainPLL_MUL_4           ( ( unsigned portCHAR ) 0x0003 )\r
96 #define mainPLL_DIV_1           ( ( unsigned portCHAR ) 0x0000 )\r
97 #define mainPLL_ENABLE          ( ( unsigned portCHAR ) 0x0001 )\r
98 #define mainPLL_CONNECT         ( ( unsigned portCHAR ) 0x0003 )\r
99 #define mainPLL_FEED_BYTE1      ( ( unsigned portCHAR ) 0xaa )\r
100 #define mainPLL_FEED_BYTE2      ( ( unsigned portCHAR ) 0x55 )\r
101 #define mainPLL_LOCK            ( ( unsigned portLONG ) 0x0400 )\r
102 \r
103 /* Constants to setup the MAM. */\r
104 #define mainMAM_TIM_3           ( ( unsigned portCHAR ) 0x03 )\r
105 #define mainMAM_MODE_FULL       ( ( unsigned portCHAR ) 0x02 )\r
106 \r
107 /* Constants to setup the peripheral bus. */\r
108 #define mainBUS_CLK_FULL        ( ( unsigned portCHAR ) 0x01 )\r
109 \r
110 /* Constants to setup I/O and processor. */\r
111 #define mainBUS_CLK_FULL        ( ( unsigned portCHAR ) 0x01 )\r
112 #define mainLED_TO_OUTPUT       ( ( unsigned portLONG ) 0xff0000 )\r
113 #define mainJTAG_PORT           ( ( unsigned portLONG ) 0x3E0000UL )\r
114 \r
115 /* Priorities for the demo application tasks. */\r
116 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
117 #define mainHTTP_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
118 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
119 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
120 #define mainERROR_CHECK_PRIORITY        ( tskIDLE_PRIORITY + 1 )\r
121 \r
122 /* Flash rates of the on board LED to indicate the health of the system. */\r
123 #define mainNO_ERROR_DELAY                      ( 3000 )\r
124 #define mainERROR_DELAY                         ( 500 )\r
125 #define mainON_BOARD_LED_BIT            ( ( unsigned portLONG ) 0x80 )\r
126 \r
127 /*-----------------------------------------------------------*/\r
128 \r
129 /*\r
130  * The Olimex demo board has a single built in LED.  This function simply\r
131  * toggles its state. \r
132  */\r
133 void prvToggleOnBoardLED( void );\r
134 \r
135 /*\r
136  * Configure the processor for use with the Olimex demo board.\r
137  */\r
138 static void prvSetupHardware( void );\r
139 \r
140 /*\r
141  * Simply check for errors and toggle the onboard LED.\r
142  */\r
143 static void prvErrorChecks( void *pvParameters );\r
144 \r
145 /*\r
146  * Return true if the demo tasks are executing without error - otherwise \r
147  * return false.\r
148  */\r
149 static void prvMainCheckOtherTasksAreStillRunning( void );\r
150 /*-----------------------------------------------------------*/\r
151 \r
152 /* Flag set by prvMainCheckOtherTasksAreStillExecuting(). */\r
153 portLONG lErrorInTask = pdFALSE;\r
154 \r
155 /*\r
156  * Application entry point:\r
157  * Starts all the other tasks, then starts the scheduler. \r
158  */\r
159 int main( void )\r
160 {\r
161         /* Setup the hardware for use with the Olimex demo board. */\r
162         prvSetupHardware();\r
163 \r
164         /* Start the standard flash tasks so the WEB server is not the only thing \r
165         running. */\r
166         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
167         vStartSemaphoreTasks( tskIDLE_PRIORITY );\r
168         vStartDynamicPriorityTasks();\r
169         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
170         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
171         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
172 \r
173         /* Start the WEB server task and the error check task. */\r
174         xTaskCreate( vHTTPServerTask, ( signed portCHAR * ) "HTTP", configMINIMAL_STACK_SIZE, NULL, mainHTTP_TASK_PRIORITY, NULL );\r
175         xTaskCreate( prvErrorChecks, ( signed portCHAR * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainERROR_CHECK_PRIORITY, NULL );\r
176         \r
177         /* Now all the tasks have been started - start the scheduler.\r
178 \r
179         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
180         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
181         called.  The demo applications included in the FreeRTOS.org download switch\r
182         to supervisor mode prior to main being called.  If you are not using one of\r
183         these demo application projects then ensure Supervisor mode is used. */\r
184         vTaskStartScheduler();\r
185 \r
186         /* Should never reach here! */\r
187         return 0;\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 static void prvSetupHardware( void )\r
192 {\r
193         #ifdef RUN_FROM_RAM\r
194                 /* Remap the interrupt vectors to RAM if we are are running from RAM. */\r
195                 SCB_MEMMAP = 2;\r
196         #endif\r
197 \r
198         /* Set all GPIO to output other than the P0.14 (BSL), and the JTAG pins.  \r
199         The JTAG pins are left as input as I'm not sure what will happen if the \r
200         Wiggler is connected after powerup - not that it would be a good idea to\r
201         do that anyway. */\r
202         GPIO_IODIR = ~( mainJTAG_PORT );\r
203 \r
204         /* Setup the PLL to multiply the XTAL input by 4. */\r
205         SCB_PLLCFG = ( mainPLL_MUL_4 | mainPLL_DIV_1 );\r
206 \r
207         /* Activate the PLL by turning it on then feeding the correct sequence of\r
208         bytes. */\r
209         SCB_PLLCON = mainPLL_ENABLE;\r
210         SCB_PLLFEED = mainPLL_FEED_BYTE1;\r
211         SCB_PLLFEED = mainPLL_FEED_BYTE2;\r
212 \r
213         /* Wait for the PLL to lock... */\r
214         while( !( SCB_PLLSTAT & mainPLL_LOCK ) );\r
215 \r
216         /* ...before connecting it using the feed sequence again. */\r
217         SCB_PLLCON = mainPLL_CONNECT;\r
218         SCB_PLLFEED = mainPLL_FEED_BYTE1;\r
219         SCB_PLLFEED = mainPLL_FEED_BYTE2;\r
220 \r
221         /* Setup and turn on the MAM.  Three cycle access is used due to the fast\r
222         PLL used.  It is possible faster overall performance could be obtained by\r
223         tuning the MAM and PLL settings. */\r
224         MAM_TIM = mainMAM_TIM_3;\r
225         MAM_CR = mainMAM_MODE_FULL;\r
226 \r
227         /* Setup the peripheral bus to be the same as the PLL output. */\r
228         SCB_VPBDIV = mainBUS_CLK_FULL;\r
229 \r
230         /* Initialise the i2c peripheral. */\r
231         i2cInit();\r
232 \r
233         /* Initialise the LED's used by the flash tasks. */\r
234         vParTestInitialise();\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 static void prvMainCheckOtherTasksAreStillRunning( void )\r
239 {\r
240         /* Check all the demo tasks (other than the flash tasks) to ensure\r
241         that they are all still running, and that none of them have detected\r
242         an error. */\r
243 \r
244         /* This function is called from more than one task. */\r
245         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
246         {\r
247                 lErrorInTask = pdTRUE;\r
248         }\r
249 \r
250         if( xArePollingQueuesStillRunning() != pdTRUE )\r
251         {\r
252                 lErrorInTask = pdTRUE;\r
253         }\r
254 \r
255         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
256         {\r
257                 lErrorInTask = pdTRUE;\r
258         }\r
259 \r
260         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
261         {\r
262                 lErrorInTask = pdTRUE;\r
263         }\r
264 \r
265         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
266         {\r
267                 lErrorInTask = pdTRUE;\r
268         }\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 void prvToggleOnBoardLED( void )\r
273 {\r
274 unsigned portLONG ulState;\r
275 \r
276         ulState = GPIO0_IOPIN;\r
277         if( ulState & mainON_BOARD_LED_BIT )\r
278         {\r
279                 GPIO_IOCLR = mainON_BOARD_LED_BIT;\r
280         }\r
281         else\r
282         {\r
283                 GPIO_IOSET = mainON_BOARD_LED_BIT;\r
284         }       \r
285 }\r
286 /*-----------------------------------------------------------*/\r
287 \r
288 static void prvErrorChecks( void *pvParameters )\r
289 {\r
290 portTickType xDelay = mainNO_ERROR_DELAY;\r
291 \r
292         /* The parameters are not used. */\r
293         ( void ) pvParameters;\r
294 \r
295         for( ;; )\r
296         {\r
297                 /* How long we delay depends on whether an error has been detected\r
298                 or not.  Therefore the flash rate of the on board LED indicates \r
299                 whether or not an error has occurred. */\r
300                 vTaskDelay( xDelay );\r
301 \r
302                 /* Update the lErrorInTask flag. */\r
303                 prvMainCheckOtherTasksAreStillRunning();\r
304 \r
305                 if( lErrorInTask )\r
306                 {\r
307                         /* An error has been found so reduce the delay period and in so\r
308                         doing speed up the flash rate of the on board LED. */\r
309                         xDelay = mainERROR_DELAY;\r
310                 }\r
311 \r
312                 prvToggleOnBoardLED();\r
313         }\r
314 }\r
315 \r