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