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