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