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