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