]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WizNET_DEMO_GCC_ARM7/main.c
Update version numbers to V7.4.1.
[freertos] / FreeRTOS / Demo / WizNET_DEMO_GCC_ARM7 / main.c
1 /*\r
2     FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /* \r
76         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
77         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
78         called.  The demo applications included in the FreeRTOS.org download switch\r
79         to supervisor mode prior to main being called.  If you are not using one of\r
80         these demo application projects then ensure Supervisor mode is used.\r
81 */\r
82 \r
83 \r
84 /*\r
85  * Program entry point.\r
86  * \r
87  * main() is responsible for setting up the microcontroller peripherals, then\r
88  * starting the demo application tasks.  Once the tasks have been created the\r
89  * scheduler is started and main() should never complete.\r
90  *\r
91  * The demo creates the three standard 'flash' tasks to provide some visual\r
92  * feedback that the system and scheduler are still operating correctly.\r
93  *\r
94  * The HTTP server task operates at the highest priority so will always preempt\r
95  * the flash or idle task on TCP/IP events.\r
96  */\r
97 \r
98 /* Standard includes. */\r
99 #include <stdlib.h>\r
100 \r
101 /* Scheduler include files. */\r
102 #include "FreeRTOS.h"\r
103 #include "semphr.h"\r
104 #include "task.h"\r
105 \r
106 /* Application includes. */\r
107 #include "i2c.h"\r
108 #include "HTTP_Serv.h"\r
109 #include "flash.h"\r
110 #include "partest.h"\r
111 #include "dynamic.h"\r
112 #include "semtest.h"\r
113 #include "PollQ.h"\r
114 #include "BlockQ.h"\r
115 #include "integer.h"\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /* Constants to setup the PLL. */\r
120 #define mainPLL_MUL_4           ( ( unsigned char ) 0x0003 )\r
121 #define mainPLL_DIV_1           ( ( unsigned char ) 0x0000 )\r
122 #define mainPLL_ENABLE          ( ( unsigned char ) 0x0001 )\r
123 #define mainPLL_CONNECT         ( ( unsigned char ) 0x0003 )\r
124 #define mainPLL_FEED_BYTE1      ( ( unsigned char ) 0xaa )\r
125 #define mainPLL_FEED_BYTE2      ( ( unsigned char ) 0x55 )\r
126 #define mainPLL_LOCK            ( ( unsigned long ) 0x0400 )\r
127 \r
128 /* Constants to setup the MAM. */\r
129 #define mainMAM_TIM_3           ( ( unsigned char ) 0x03 )\r
130 #define mainMAM_MODE_FULL       ( ( unsigned char ) 0x02 )\r
131 \r
132 /* Constants to setup the peripheral bus. */\r
133 #define mainBUS_CLK_FULL        ( ( unsigned char ) 0x01 )\r
134 \r
135 /* Constants to setup I/O and processor. */\r
136 #define mainBUS_CLK_FULL        ( ( unsigned char ) 0x01 )\r
137 #define mainLED_TO_OUTPUT       ( ( unsigned long ) 0xff0000 )\r
138 #define mainJTAG_PORT           ( ( unsigned long ) 0x3E0000UL )\r
139 \r
140 /* Priorities for the demo application tasks. */\r
141 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
142 #define mainHTTP_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
143 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
144 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
145 #define mainERROR_CHECK_PRIORITY        ( tskIDLE_PRIORITY + 1 )\r
146 \r
147 /* Flash rates of the on board LED to indicate the health of the system. */\r
148 #define mainNO_ERROR_DELAY                      ( 3000 )\r
149 #define mainERROR_DELAY                         ( 500 )\r
150 #define mainON_BOARD_LED_BIT            ( ( unsigned long ) 0x80 )\r
151 \r
152 /*-----------------------------------------------------------*/\r
153 \r
154 /*\r
155  * The Olimex demo board has a single built in LED.  This function simply\r
156  * toggles its state. \r
157  */\r
158 void prvToggleOnBoardLED( void );\r
159 \r
160 /*\r
161  * Configure the processor for use with the Olimex demo board.\r
162  */\r
163 static void prvSetupHardware( void );\r
164 \r
165 /*\r
166  * Simply check for errors and toggle the onboard LED.\r
167  */\r
168 static void prvErrorChecks( void *pvParameters );\r
169 \r
170 /*\r
171  * Return true if the demo tasks are executing without error - otherwise \r
172  * return false.\r
173  */\r
174 static void prvMainCheckOtherTasksAreStillRunning( void );\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 /* Flag set by prvMainCheckOtherTasksAreStillExecuting(). */\r
178 long lErrorInTask = pdFALSE;\r
179 \r
180 /*\r
181  * Application entry point:\r
182  * Starts all the other tasks, then starts the scheduler. \r
183  */\r
184 int main( void )\r
185 {\r
186         /* Setup the hardware for use with the Olimex demo board. */\r
187         prvSetupHardware();\r
188 \r
189         /* Start the standard flash tasks so the WEB server is not the only thing \r
190         running. */\r
191         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
192         vStartSemaphoreTasks( tskIDLE_PRIORITY );\r
193         vStartDynamicPriorityTasks();\r
194         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
195         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
196         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
197 \r
198         /* Start the WEB server task and the error check task. */\r
199         xTaskCreate( vHTTPServerTask, ( signed char * ) "HTTP", configMINIMAL_STACK_SIZE, NULL, mainHTTP_TASK_PRIORITY, NULL );\r
200         xTaskCreate( prvErrorChecks, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainERROR_CHECK_PRIORITY, NULL );\r
201         \r
202         /* Now all the tasks have been started - start the scheduler.\r
203 \r
204         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
205         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
206         called.  The demo applications included in the FreeRTOS.org download switch\r
207         to supervisor mode prior to main being called.  If you are not using one of\r
208         these demo application projects then ensure Supervisor mode is used. */\r
209         vTaskStartScheduler();\r
210 \r
211         /* Should never reach here! */\r
212         return 0;\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 static void prvSetupHardware( void )\r
217 {\r
218         #ifdef RUN_FROM_RAM\r
219                 /* Remap the interrupt vectors to RAM if we are are running from RAM. */\r
220                 SCB_MEMMAP = 2;\r
221         #endif\r
222 \r
223         /* Set all GPIO to output other than the P0.14 (BSL), and the JTAG pins.  \r
224         The JTAG pins are left as input as I'm not sure what will happen if the \r
225         Wiggler is connected after powerup - not that it would be a good idea to\r
226         do that anyway. */\r
227         GPIO_IODIR = ~( mainJTAG_PORT );\r
228 \r
229         /* Setup the PLL to multiply the XTAL input by 4. */\r
230         SCB_PLLCFG = ( mainPLL_MUL_4 | mainPLL_DIV_1 );\r
231 \r
232         /* Activate the PLL by turning it on then feeding the correct sequence of\r
233         bytes. */\r
234         SCB_PLLCON = mainPLL_ENABLE;\r
235         SCB_PLLFEED = mainPLL_FEED_BYTE1;\r
236         SCB_PLLFEED = mainPLL_FEED_BYTE2;\r
237 \r
238         /* Wait for the PLL to lock... */\r
239         while( !( SCB_PLLSTAT & mainPLL_LOCK ) );\r
240 \r
241         /* ...before connecting it using the feed sequence again. */\r
242         SCB_PLLCON = mainPLL_CONNECT;\r
243         SCB_PLLFEED = mainPLL_FEED_BYTE1;\r
244         SCB_PLLFEED = mainPLL_FEED_BYTE2;\r
245 \r
246         /* Setup and turn on the MAM.  Three cycle access is used due to the fast\r
247         PLL used.  It is possible faster overall performance could be obtained by\r
248         tuning the MAM and PLL settings. */\r
249         MAM_TIM = mainMAM_TIM_3;\r
250         MAM_CR = mainMAM_MODE_FULL;\r
251 \r
252         /* Setup the peripheral bus to be the same as the PLL output. */\r
253         SCB_VPBDIV = mainBUS_CLK_FULL;\r
254 \r
255         /* Initialise the i2c peripheral. */\r
256         i2cInit();\r
257 \r
258         /* Initialise the LED's used by the flash tasks. */\r
259         vParTestInitialise();\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 static void prvMainCheckOtherTasksAreStillRunning( void )\r
264 {\r
265         /* Check all the demo tasks (other than the flash tasks) to ensure\r
266         that they are all still running, and that none of them have detected\r
267         an error. */\r
268 \r
269         /* This function is called from more than one task. */\r
270         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
271         {\r
272                 lErrorInTask = pdTRUE;\r
273         }\r
274 \r
275         if( xArePollingQueuesStillRunning() != pdTRUE )\r
276         {\r
277                 lErrorInTask = pdTRUE;\r
278         }\r
279 \r
280         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
281         {\r
282                 lErrorInTask = pdTRUE;\r
283         }\r
284 \r
285         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
286         {\r
287                 lErrorInTask = pdTRUE;\r
288         }\r
289 \r
290         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
291         {\r
292                 lErrorInTask = pdTRUE;\r
293         }\r
294 }\r
295 /*-----------------------------------------------------------*/\r
296 \r
297 void prvToggleOnBoardLED( void )\r
298 {\r
299 unsigned long ulState;\r
300 \r
301         ulState = GPIO0_IOPIN;\r
302         if( ulState & mainON_BOARD_LED_BIT )\r
303         {\r
304                 GPIO_IOCLR = mainON_BOARD_LED_BIT;\r
305         }\r
306         else\r
307         {\r
308                 GPIO_IOSET = mainON_BOARD_LED_BIT;\r
309         }       \r
310 }\r
311 /*-----------------------------------------------------------*/\r
312 \r
313 static void prvErrorChecks( void *pvParameters )\r
314 {\r
315 portTickType xDelay = mainNO_ERROR_DELAY;\r
316 \r
317         /* The parameters are not used. */\r
318         ( void ) pvParameters;\r
319 \r
320         for( ;; )\r
321         {\r
322                 /* How long we delay depends on whether an error has been detected\r
323                 or not.  Therefore the flash rate of the on board LED indicates \r
324                 whether or not an error has occurred. */\r
325                 vTaskDelay( xDelay );\r
326 \r
327                 /* Update the lErrorInTask flag. */\r
328                 prvMainCheckOtherTasksAreStillRunning();\r
329 \r
330                 if( lErrorInTask )\r
331                 {\r
332                         /* An error has been found so reduce the delay period and in so\r
333                         doing speed up the flash rate of the on board LED. */\r
334                         xDelay = mainERROR_DELAY;\r
335                 }\r
336 \r
337                 prvToggleOnBoardLED();\r
338         }\r
339 }\r
340 \r