]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/uIP_Demo_Rowley_ARM7/main.c
Minor updates and change version number for V7.5.0 release.
[freertos] / FreeRTOS / Demo / uIP_Demo_Rowley_ARM7 / main.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /* \r
66         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
67         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
68         called.  The demo applications included in the FreeRTOS.org download switch\r
69         to supervisor mode prior to main being called.  If you are not using one of\r
70         these demo application projects then ensure Supervisor mode is used.\r
71 */\r
72 \r
73 \r
74 /*\r
75  * Creates all the application tasks, then starts the scheduler.  \r
76  * \r
77  * A task is created called "uIP".  This executes the uIP stack and small\r
78  * WEB server sample.  All the other tasks are from the set of standard\r
79  * demo tasks.  The WEB documentation provides more details of the standard\r
80  * demo application tasks.\r
81  *\r
82  * Main.c also creates a task called "Check".  This only executes every three \r
83  * seconds but has the highest priority so is guaranteed to get processor time.  \r
84  * Its main function is to check that all the other tasks are still operational.\r
85  * Each standard demo task maintains a unique count that is incremented each \r
86  * time the task successfully completes its function.  Should any error occur \r
87  * within such a task the count is permanently halted.  The check task inspects\r
88  * the count of each task to ensure it has changed since the last time the \r
89  * check task executed.  If all the count variables have changed all the tasks \r
90  * are still executing error free, and the check task toggles the yellow LED.  \r
91  * Should any task contain an error at any time the LED toggle rate will change \r
92  * from 3 seconds to 500ms.\r
93  *\r
94  */\r
95 \r
96 \r
97 /* Standard includes. */\r
98 #include <stdlib.h>\r
99 #include <string.h>\r
100 \r
101 /* Scheduler includes. */\r
102 #include "FreeRTOS.h"\r
103 #include "task.h"\r
104 \r
105 /* Demo application includes. */\r
106 #include "PollQ.h"\r
107 #include "dynamic.h"\r
108 #include "semtest.h"\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /* Constants to setup the PLL. */\r
113 #define mainPLL_MUL_4           ( ( unsigned char ) 0x0003 )\r
114 #define mainPLL_DIV_1           ( ( unsigned char ) 0x0000 )\r
115 #define mainPLL_ENABLE          ( ( unsigned char ) 0x0001 )\r
116 #define mainPLL_CONNECT         ( ( unsigned char ) 0x0003 )\r
117 #define mainPLL_FEED_BYTE1      ( ( unsigned char ) 0xaa )\r
118 #define mainPLL_FEED_BYTE2      ( ( unsigned char ) 0x55 )\r
119 #define mainPLL_LOCK            ( ( unsigned long ) 0x0400 )\r
120 \r
121 /* Constants to setup the MAM. */\r
122 #define mainMAM_TIM_3           ( ( unsigned char ) 0x03 )\r
123 #define mainMAM_MODE_FULL       ( ( unsigned char ) 0x02 )\r
124 \r
125 /* Constants to setup the peripheral bus. */\r
126 #define mainBUS_CLK_FULL        ( ( unsigned char ) 0x01 )\r
127 \r
128 /* Priorities/stacks for the demo application tasks. */\r
129 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
130 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
131 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
132 #define mainUIP_PRIORITY                        ( tskIDLE_PRIORITY + 3 )\r
133 #define mainUIP_TASK_STACK_SIZE         ( 150 )\r
134 \r
135 /* The rate at which the on board LED will toggle when there is/is not an \r
136 error. */\r
137 #define mainNO_ERROR_FLASH_PERIOD       ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
138 #define mainERROR_FLASH_PERIOD          ( ( portTickType ) 500 / portTICK_RATE_MS  )\r
139 #define mainON_BOARD_LED_BIT            ( ( unsigned long ) 0x80 )\r
140 #define mainYELLOW_LED                          ( 1 << 11 )\r
141 \r
142 /*-----------------------------------------------------------*/\r
143 \r
144 /*\r
145  * This is the uIP task which is defined within the uip.c file.  This has not\r
146  * been placed into a header file in order to minimise the changes to the uip\r
147  * code.\r
148  */\r
149 extern void ( vuIP_TASK ) ( void *pvParameters );\r
150 \r
151 /*\r
152  * The Yellow LED is under the control of the Check task.  All the other LED's\r
153  * are under the control of the uIP task. \r
154  */\r
155 void prvToggleOnBoardLED( void );\r
156 \r
157 /*\r
158  * Checks that all the demo application tasks are still executing without error\r
159  * - as described at the top of the file.\r
160  */\r
161 static long prvCheckOtherTasksAreStillRunning( void );\r
162 \r
163 /*\r
164  * The task that executes at the highest priority and calls \r
165  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
166  * of the file.\r
167  */\r
168 static void vErrorChecks( void *pvParameters );\r
169 \r
170 /*\r
171  * Configure the processor for use with the Olimex demo board.  This includes\r
172  * setup for the I/O, system clock, and access timings.\r
173  */\r
174 static void prvSetupHardware( void );\r
175 \r
176 /*-----------------------------------------------------------*/\r
177 \r
178 /*\r
179  * Starts all the other tasks, then starts the scheduler. \r
180  */\r
181 int main( void )\r
182 {\r
183         /* Configure the processor. */\r
184         prvSetupHardware();\r
185 \r
186         /* Start the task that handles the TCP/IP functionality. */\r
187     xTaskCreate( vuIP_TASK, "uIP", mainUIP_TASK_STACK_SIZE, NULL, mainUIP_PRIORITY, NULL );\r
188         \r
189         /* Start the demo/test application tasks.  These are created in addition \r
190         to the TCP/IP task for demonstration and test purposes. */\r
191         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
192         vStartDynamicPriorityTasks();\r
193         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
194 \r
195         /* Start the check task - which is defined in this file. */     \r
196     xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
197 \r
198         /* Now all the tasks have been started - start the scheduler.\r
199 \r
200         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
201         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
202         called.  The demo applications included in the FreeRTOS.org download switch\r
203         to supervisor mode prior to main being called.  If you are not using one of\r
204         these demo application projects then ensure Supervisor mode is used here. */\r
205         vTaskStartScheduler();\r
206 \r
207         /* Should never reach here! */\r
208         return 0;\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 static void vErrorChecks( void *pvParameters )\r
213 {\r
214 portTickType xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;\r
215 \r
216         /* Cycle for ever, delaying then checking all the other tasks are still\r
217         operating without error.  If an error is detected then the delay period\r
218         is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so\r
219         the on board LED flash rate will increase. */\r
220         for( ;; )\r
221         {\r
222                 /* Delay until it is time to execute again. */\r
223                 vTaskDelay( xDelayPeriod );\r
224         \r
225                 /* Check all the standard demo application tasks are executing without \r
226                 error.  */\r
227                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
228                 {\r
229                         /* An error has been detected in one of the tasks - flash faster. */\r
230                         xDelayPeriod = mainERROR_FLASH_PERIOD;\r
231                 }\r
232 \r
233                 prvToggleOnBoardLED();\r
234         }\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 static void prvSetupHardware( void )\r
239 {\r
240         #ifdef RUN_FROM_RAM\r
241                 /* Remap the interrupt vectors to RAM if we are are running from RAM. */\r
242                 SCB_MEMMAP = 2;\r
243         #endif\r
244 \r
245         /* Setup the PLL to multiply the XTAL input by 4. */\r
246         SCB_PLLCFG = ( mainPLL_MUL_4 | mainPLL_DIV_1 );\r
247 \r
248         /* Activate the PLL by turning it on then feeding the correct sequence of\r
249         bytes. */\r
250         SCB_PLLCON = mainPLL_ENABLE;\r
251         SCB_PLLFEED = mainPLL_FEED_BYTE1;\r
252         SCB_PLLFEED = mainPLL_FEED_BYTE2;\r
253 \r
254         /* Wait for the PLL to lock... */\r
255         while( !( SCB_PLLSTAT & mainPLL_LOCK ) );\r
256 \r
257         /* ...before connecting it using the feed sequence again. */\r
258         SCB_PLLCON = mainPLL_CONNECT;\r
259         SCB_PLLFEED = mainPLL_FEED_BYTE1;\r
260         SCB_PLLFEED = mainPLL_FEED_BYTE2;\r
261 \r
262         /* Setup and turn on the MAM.  Three cycle access is used due to the fast\r
263         PLL used.  It is possible faster overall performance could be obtained by\r
264         tuning the MAM and PLL settings. */\r
265         MAM_TIM = mainMAM_TIM_3;\r
266         MAM_CR = mainMAM_MODE_FULL;\r
267 \r
268         /* Setup the peripheral bus to be the same as the PLL output. */\r
269         SCB_VPBDIV = mainBUS_CLK_FULL;\r
270 }\r
271 /*-----------------------------------------------------------*/\r
272 \r
273 void prvToggleOnBoardLED( void )\r
274 {\r
275 unsigned long ulState;\r
276 \r
277         ulState = GPIO0_IOPIN;\r
278         if( ulState & mainYELLOW_LED )\r
279         {\r
280                 GPIO_IOCLR = mainYELLOW_LED;\r
281         }\r
282         else\r
283         {\r
284                 GPIO_IOSET = mainYELLOW_LED;\r
285         }       \r
286 }\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 static long prvCheckOtherTasksAreStillRunning( void )\r
290 {\r
291 long lReturn = ( long ) pdPASS;\r
292 \r
293         /* Check all the demo tasks (other than the flash tasks) to ensure\r
294         that they are all still running, and that none of them have detected\r
295         an error. */\r
296 \r
297         if( xArePollingQueuesStillRunning() != pdTRUE )\r
298         {\r
299                 lReturn = ( long ) pdFAIL;\r
300         }\r
301 \r
302         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
303         {\r
304                 lReturn = ( long ) pdFAIL;\r
305         }\r
306 \r
307         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
308         {\r
309                 lReturn = ( long ) pdFAIL;\r
310         }\r
311 \r
312         return lReturn;\r
313 }\r
314 \r
315 \r
316 \r