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