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