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