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