]> git.sur5r.net Git - freertos/blob - Demo/uIP_Demo_Rowley_ARM7/main.c
Remove unnecessary use of portLONG, portCHAR and portSHORT.
[freertos] / Demo / uIP_Demo_Rowley_ARM7 / main.c
1 /*\r
2     FreeRTOS V6.0.0 - 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     * The FreeRTOS eBook and reference manual are available to purchase for a *\r
29     * small fee. Help yourself get started quickly while also helping the     *\r
30     * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *\r
31     *                                                                         *\r
32     ***************************************************************************\r
33 \r
34     1 tab == 4 spaces!\r
35 \r
36     Please ensure to read the configuration and relevant port sections of the\r
37     online documentation.\r
38 \r
39     http://www.FreeRTOS.org - Documentation, latest information, license and\r
40     contact details.\r
41 \r
42     http://www.SafeRTOS.com - A version that is certified for use in safety\r
43     critical systems.\r
44 \r
45     http://www.OpenRTOS.com - Commercial support, development, porting,\r
46     licensing and training services.\r
47 */\r
48 \r
49 /* \r
50         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
51         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
52         called.  The demo applications included in the FreeRTOS.org download switch\r
53         to supervisor mode prior to main being called.  If you are not using one of\r
54         these demo application projects then ensure Supervisor mode is used.\r
55 */\r
56 \r
57 \r
58 /*\r
59  * Creates all the application tasks, then starts the scheduler.  \r
60  * \r
61  * A task is created called "uIP".  This executes the uIP stack and small\r
62  * WEB server sample.  All the other tasks are from the set of standard\r
63  * demo tasks.  The WEB documentation provides more details of the standard\r
64  * demo application tasks.\r
65  *\r
66  * Main.c also creates a task called "Check".  This only executes every three \r
67  * seconds but has the highest priority so is guaranteed to get processor time.  \r
68  * Its main function is to check that all the other tasks are still operational.\r
69  * Each standard demo task maintains a unique count that is incremented each \r
70  * time the task successfully completes its function.  Should any error occur \r
71  * within such a task the count is permanently halted.  The check task inspects\r
72  * the count of each task to ensure it has changed since the last time the \r
73  * check task executed.  If all the count variables have changed all the tasks \r
74  * are still executing error free, and the check task toggles the yellow LED.  \r
75  * Should any task contain an error at any time the LED toggle rate will change \r
76  * from 3 seconds to 500ms.\r
77  *\r
78  */\r
79 \r
80 \r
81 /* Standard includes. */\r
82 #include <stdlib.h>\r
83 #include <string.h>\r
84 \r
85 /* Scheduler includes. */\r
86 #include "FreeRTOS.h"\r
87 #include "task.h"\r
88 \r
89 /* Demo application includes. */\r
90 #include "PollQ.h"\r
91 #include "dynamic.h"\r
92 #include "semtest.h"\r
93 \r
94 /*-----------------------------------------------------------*/\r
95 \r
96 /* Constants to setup the PLL. */\r
97 #define mainPLL_MUL_4           ( ( unsigned char ) 0x0003 )\r
98 #define mainPLL_DIV_1           ( ( unsigned char ) 0x0000 )\r
99 #define mainPLL_ENABLE          ( ( unsigned char ) 0x0001 )\r
100 #define mainPLL_CONNECT         ( ( unsigned char ) 0x0003 )\r
101 #define mainPLL_FEED_BYTE1      ( ( unsigned char ) 0xaa )\r
102 #define mainPLL_FEED_BYTE2      ( ( unsigned char ) 0x55 )\r
103 #define mainPLL_LOCK            ( ( unsigned long ) 0x0400 )\r
104 \r
105 /* Constants to setup the MAM. */\r
106 #define mainMAM_TIM_3           ( ( unsigned char ) 0x03 )\r
107 #define mainMAM_MODE_FULL       ( ( unsigned char ) 0x02 )\r
108 \r
109 /* Constants to setup the peripheral bus. */\r
110 #define mainBUS_CLK_FULL        ( ( unsigned char ) 0x01 )\r
111 \r
112 /* Priorities/stacks for the demo application tasks. */\r
113 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
114 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
115 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
116 #define mainUIP_PRIORITY                        ( tskIDLE_PRIORITY + 3 )\r
117 #define mainUIP_TASK_STACK_SIZE         ( 150 )\r
118 \r
119 /* The rate at which the on board LED will toggle when there is/is not an \r
120 error. */\r
121 #define mainNO_ERROR_FLASH_PERIOD       ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
122 #define mainERROR_FLASH_PERIOD          ( ( portTickType ) 500 / portTICK_RATE_MS  )\r
123 #define mainON_BOARD_LED_BIT            ( ( unsigned long ) 0x80 )\r
124 #define mainYELLOW_LED                          ( 1 << 11 )\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128 /*\r
129  * This is the uIP task which is defined within the uip.c file.  This has not\r
130  * been placed into a header file in order to minimise the changes to the uip\r
131  * code.\r
132  */\r
133 extern void ( vuIP_TASK ) ( void *pvParameters );\r
134 \r
135 /*\r
136  * The Yellow LED is under the control of the Check task.  All the other LED's\r
137  * are under the control of the uIP task. \r
138  */\r
139 void prvToggleOnBoardLED( void );\r
140 \r
141 /*\r
142  * Checks that all the demo application tasks are still executing without error\r
143  * - as described at the top of the file.\r
144  */\r
145 static long prvCheckOtherTasksAreStillRunning( void );\r
146 \r
147 /*\r
148  * The task that executes at the highest priority and calls \r
149  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
150  * of the file.\r
151  */\r
152 static void vErrorChecks( void *pvParameters );\r
153 \r
154 /*\r
155  * Configure the processor for use with the Olimex demo board.  This includes\r
156  * setup for the I/O, system clock, and access timings.\r
157  */\r
158 static void prvSetupHardware( void );\r
159 \r
160 /*-----------------------------------------------------------*/\r
161 \r
162 /*\r
163  * Starts all the other tasks, then starts the scheduler. \r
164  */\r
165 int main( void )\r
166 {\r
167         /* Configure the processor. */\r
168         prvSetupHardware();\r
169 \r
170         /* Start the task that handles the TCP/IP functionality. */\r
171     xTaskCreate( vuIP_TASK, "uIP", mainUIP_TASK_STACK_SIZE, NULL, mainUIP_PRIORITY, NULL );\r
172         \r
173         /* Start the demo/test application tasks.  These are created in addition \r
174         to the TCP/IP task for demonstration and test purposes. */\r
175         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
176         vStartDynamicPriorityTasks();\r
177         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
178 \r
179         /* Start the check task - which is defined in this file. */     \r
180     xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
181 \r
182         /* Now all the tasks have been started - start the scheduler.\r
183 \r
184         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
185         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
186         called.  The demo applications included in the FreeRTOS.org download switch\r
187         to supervisor mode prior to main being called.  If you are not using one of\r
188         these demo application projects then ensure Supervisor mode is used here. */\r
189         vTaskStartScheduler();\r
190 \r
191         /* Should never reach here! */\r
192         return 0;\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static void vErrorChecks( void *pvParameters )\r
197 {\r
198 portTickType xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;\r
199 \r
200         /* Cycle for ever, delaying then checking all the other tasks are still\r
201         operating without error.  If an error is detected then the delay period\r
202         is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so\r
203         the on board LED flash rate will increase. */\r
204         for( ;; )\r
205         {\r
206                 /* Delay until it is time to execute again. */\r
207                 vTaskDelay( xDelayPeriod );\r
208         \r
209                 /* Check all the standard demo application tasks are executing without \r
210                 error.  */\r
211                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
212                 {\r
213                         /* An error has been detected in one of the tasks - flash faster. */\r
214                         xDelayPeriod = mainERROR_FLASH_PERIOD;\r
215                 }\r
216 \r
217                 prvToggleOnBoardLED();\r
218         }\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 static void prvSetupHardware( void )\r
223 {\r
224         #ifdef RUN_FROM_RAM\r
225                 /* Remap the interrupt vectors to RAM if we are are running from RAM. */\r
226                 SCB_MEMMAP = 2;\r
227         #endif\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 /*-----------------------------------------------------------*/\r
256 \r
257 void prvToggleOnBoardLED( void )\r
258 {\r
259 unsigned long ulState;\r
260 \r
261         ulState = GPIO0_IOPIN;\r
262         if( ulState & mainYELLOW_LED )\r
263         {\r
264                 GPIO_IOCLR = mainYELLOW_LED;\r
265         }\r
266         else\r
267         {\r
268                 GPIO_IOSET = mainYELLOW_LED;\r
269         }       \r
270 }\r
271 /*-----------------------------------------------------------*/\r
272 \r
273 static long prvCheckOtherTasksAreStillRunning( void )\r
274 {\r
275 long lReturn = ( long ) pdPASS;\r
276 \r
277         /* Check all the demo tasks (other than the flash tasks) to ensure\r
278         that they are all still running, and that none of them have detected\r
279         an error. */\r
280 \r
281         if( xArePollingQueuesStillRunning() != pdTRUE )\r
282         {\r
283                 lReturn = ( long ) pdFAIL;\r
284         }\r
285 \r
286         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
287         {\r
288                 lReturn = ( long ) pdFAIL;\r
289         }\r
290 \r
291         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
292         {\r
293                 lReturn = ( long ) pdFAIL;\r
294         }\r
295 \r
296         return lReturn;\r
297 }\r
298 \r
299 \r
300 \r