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