]> git.sur5r.net Git - freertos/blob - Demo/uIP_Demo_Rowley_ARM7/main.c
Update the V6.0.4. The primary difference being that the unsupported demos have...
[freertos] / Demo / uIP_Demo_Rowley_ARM7 / main.c
1 /*\r
2     FreeRTOS V6.0.4 - Copyright (C) 2010 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\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 exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* \r
55         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
56         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
57         called.  The demo applications included in the FreeRTOS.org download switch\r
58         to supervisor mode prior to main being called.  If you are not using one of\r
59         these demo application projects then ensure Supervisor mode is used.\r
60 */\r
61 \r
62 \r
63 /*\r
64  * Creates all the application tasks, then starts the scheduler.  \r
65  * \r
66  * A task is created called "uIP".  This executes the uIP stack and small\r
67  * WEB server sample.  All the other tasks are from the set of standard\r
68  * demo tasks.  The WEB documentation provides more details of the standard\r
69  * demo application tasks.\r
70  *\r
71  * Main.c also creates a task called "Check".  This only executes every three \r
72  * seconds but has the highest priority so is guaranteed to get processor time.  \r
73  * Its main function is to check that all the other tasks are still operational.\r
74  * Each standard demo task maintains a unique count that is incremented each \r
75  * time the task successfully completes its function.  Should any error occur \r
76  * within such a task the count is permanently halted.  The check task inspects\r
77  * the count of each task to ensure it has changed since the last time the \r
78  * check task executed.  If all the count variables have changed all the tasks \r
79  * are still executing error free, and the check task toggles the yellow LED.  \r
80  * Should any task contain an error at any time the LED toggle rate will change \r
81  * from 3 seconds to 500ms.\r
82  *\r
83  */\r
84 \r
85 \r
86 /* Standard includes. */\r
87 #include <stdlib.h>\r
88 #include <string.h>\r
89 \r
90 /* Scheduler includes. */\r
91 #include "FreeRTOS.h"\r
92 #include "task.h"\r
93 \r
94 /* Demo application includes. */\r
95 #include "PollQ.h"\r
96 #include "dynamic.h"\r
97 #include "semtest.h"\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /* Constants to setup the PLL. */\r
102 #define mainPLL_MUL_4           ( ( unsigned char ) 0x0003 )\r
103 #define mainPLL_DIV_1           ( ( unsigned char ) 0x0000 )\r
104 #define mainPLL_ENABLE          ( ( unsigned char ) 0x0001 )\r
105 #define mainPLL_CONNECT         ( ( unsigned char ) 0x0003 )\r
106 #define mainPLL_FEED_BYTE1      ( ( unsigned char ) 0xaa )\r
107 #define mainPLL_FEED_BYTE2      ( ( unsigned char ) 0x55 )\r
108 #define mainPLL_LOCK            ( ( unsigned long ) 0x0400 )\r
109 \r
110 /* Constants to setup the MAM. */\r
111 #define mainMAM_TIM_3           ( ( unsigned char ) 0x03 )\r
112 #define mainMAM_MODE_FULL       ( ( unsigned char ) 0x02 )\r
113 \r
114 /* Constants to setup the peripheral bus. */\r
115 #define mainBUS_CLK_FULL        ( ( unsigned char ) 0x01 )\r
116 \r
117 /* Priorities/stacks for the demo application tasks. */\r
118 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
119 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
120 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
121 #define mainUIP_PRIORITY                        ( tskIDLE_PRIORITY + 3 )\r
122 #define mainUIP_TASK_STACK_SIZE         ( 150 )\r
123 \r
124 /* The rate at which the on board LED will toggle when there is/is not an \r
125 error. */\r
126 #define mainNO_ERROR_FLASH_PERIOD       ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
127 #define mainERROR_FLASH_PERIOD          ( ( portTickType ) 500 / portTICK_RATE_MS  )\r
128 #define mainON_BOARD_LED_BIT            ( ( unsigned long ) 0x80 )\r
129 #define mainYELLOW_LED                          ( 1 << 11 )\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /*\r
134  * This is the uIP task which is defined within the uip.c file.  This has not\r
135  * been placed into a header file in order to minimise the changes to the uip\r
136  * code.\r
137  */\r
138 extern void ( vuIP_TASK ) ( void *pvParameters );\r
139 \r
140 /*\r
141  * The Yellow LED is under the control of the Check task.  All the other LED's\r
142  * are under the control of the uIP task. \r
143  */\r
144 void prvToggleOnBoardLED( void );\r
145 \r
146 /*\r
147  * Checks that all the demo application tasks are still executing without error\r
148  * - as described at the top of the file.\r
149  */\r
150 static long prvCheckOtherTasksAreStillRunning( void );\r
151 \r
152 /*\r
153  * The task that executes at the highest priority and calls \r
154  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
155  * of the file.\r
156  */\r
157 static void vErrorChecks( void *pvParameters );\r
158 \r
159 /*\r
160  * Configure the processor for use with the Olimex demo board.  This includes\r
161  * setup for the I/O, system clock, and access timings.\r
162  */\r
163 static void prvSetupHardware( void );\r
164 \r
165 /*-----------------------------------------------------------*/\r
166 \r
167 /*\r
168  * Starts all the other tasks, then starts the scheduler. \r
169  */\r
170 int main( void )\r
171 {\r
172         /* Configure the processor. */\r
173         prvSetupHardware();\r
174 \r
175         /* Start the task that handles the TCP/IP functionality. */\r
176     xTaskCreate( vuIP_TASK, "uIP", mainUIP_TASK_STACK_SIZE, NULL, mainUIP_PRIORITY, NULL );\r
177         \r
178         /* Start the demo/test application tasks.  These are created in addition \r
179         to the TCP/IP task for demonstration and test purposes. */\r
180         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
181         vStartDynamicPriorityTasks();\r
182         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
183 \r
184         /* Start the check task - which is defined in this file. */     \r
185     xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
186 \r
187         /* Now all the tasks have been started - start the scheduler.\r
188 \r
189         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
190         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
191         called.  The demo applications included in the FreeRTOS.org download switch\r
192         to supervisor mode prior to main being called.  If you are not using one of\r
193         these demo application projects then ensure Supervisor mode is used here. */\r
194         vTaskStartScheduler();\r
195 \r
196         /* Should never reach here! */\r
197         return 0;\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 static void vErrorChecks( void *pvParameters )\r
202 {\r
203 portTickType xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;\r
204 \r
205         /* Cycle for ever, delaying then checking all the other tasks are still\r
206         operating without error.  If an error is detected then the delay period\r
207         is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so\r
208         the on board LED flash rate will increase. */\r
209         for( ;; )\r
210         {\r
211                 /* Delay until it is time to execute again. */\r
212                 vTaskDelay( xDelayPeriod );\r
213         \r
214                 /* Check all the standard demo application tasks are executing without \r
215                 error.  */\r
216                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
217                 {\r
218                         /* An error has been detected in one of the tasks - flash faster. */\r
219                         xDelayPeriod = mainERROR_FLASH_PERIOD;\r
220                 }\r
221 \r
222                 prvToggleOnBoardLED();\r
223         }\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 static void prvSetupHardware( void )\r
228 {\r
229         #ifdef RUN_FROM_RAM\r
230                 /* Remap the interrupt vectors to RAM if we are are running from RAM. */\r
231                 SCB_MEMMAP = 2;\r
232         #endif\r
233 \r
234         /* Setup the PLL to multiply the XTAL input by 4. */\r
235         SCB_PLLCFG = ( mainPLL_MUL_4 | mainPLL_DIV_1 );\r
236 \r
237         /* Activate the PLL by turning it on then feeding the correct sequence of\r
238         bytes. */\r
239         SCB_PLLCON = mainPLL_ENABLE;\r
240         SCB_PLLFEED = mainPLL_FEED_BYTE1;\r
241         SCB_PLLFEED = mainPLL_FEED_BYTE2;\r
242 \r
243         /* Wait for the PLL to lock... */\r
244         while( !( SCB_PLLSTAT & mainPLL_LOCK ) );\r
245 \r
246         /* ...before connecting it using the feed sequence again. */\r
247         SCB_PLLCON = mainPLL_CONNECT;\r
248         SCB_PLLFEED = mainPLL_FEED_BYTE1;\r
249         SCB_PLLFEED = mainPLL_FEED_BYTE2;\r
250 \r
251         /* Setup and turn on the MAM.  Three cycle access is used due to the fast\r
252         PLL used.  It is possible faster overall performance could be obtained by\r
253         tuning the MAM and PLL settings. */\r
254         MAM_TIM = mainMAM_TIM_3;\r
255         MAM_CR = mainMAM_MODE_FULL;\r
256 \r
257         /* Setup the peripheral bus to be the same as the PLL output. */\r
258         SCB_VPBDIV = mainBUS_CLK_FULL;\r
259 }\r
260 /*-----------------------------------------------------------*/\r
261 \r
262 void prvToggleOnBoardLED( void )\r
263 {\r
264 unsigned long ulState;\r
265 \r
266         ulState = GPIO0_IOPIN;\r
267         if( ulState & mainYELLOW_LED )\r
268         {\r
269                 GPIO_IOCLR = mainYELLOW_LED;\r
270         }\r
271         else\r
272         {\r
273                 GPIO_IOSET = mainYELLOW_LED;\r
274         }       \r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 static long prvCheckOtherTasksAreStillRunning( void )\r
279 {\r
280 long lReturn = ( long ) pdPASS;\r
281 \r
282         /* Check all the demo tasks (other than the flash tasks) to ensure\r
283         that they are all still running, and that none of them have detected\r
284         an error. */\r
285 \r
286         if( xArePollingQueuesStillRunning() != pdTRUE )\r
287         {\r
288                 lReturn = ( long ) pdFAIL;\r
289         }\r
290 \r
291         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
292         {\r
293                 lReturn = ( long ) pdFAIL;\r
294         }\r
295 \r
296         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
297         {\r
298                 lReturn = ( long ) pdFAIL;\r
299         }\r
300 \r
301         return lReturn;\r
302 }\r
303 \r
304 \r
305 \r