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